Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: tests/lib/logging/logging_test.dart

Issue 10693042: first version of a logging library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: addressing cl comments Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« lib/logging/logging.dart ('K') | « lib/logging/logging.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5
6 #library('logging_test');
7
8 #import('../../../lib/logging/logging.dart');
9 #import('../../../lib/unittest/unittest.dart');
10
11 main() {
12 test('level comparison is a valid comparator', () {
13 var level1 = const Level('NOT_REAL1', 253);
14 expect(level1 == level1);
15 expect(level1 <= level1);
16 expect(level1 >= level1);
17 expect(level1 < level1, isFalse);
18 expect(level1 > level1, isFalse);
19
20 var level2 = const Level('NOT_REAL2', 455);
21 expect(level1 <= level2);
22 expect(level1 < level2);
23 expect(level2 >= level1);
24 expect(level2 > level1);
25
26 var level3 = const Level('NOT_REAL3', 253);
27 expect(level1 !== level3); // different instances
28 expect(level1 == level3); // same value.
29 });
30
31 test('default levels are in order', () {
32 final levels = const [
33 Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG,
34 Level.INFO, Level.WARNING, Level.SEVERE, Level.SHOUT, Level.OFF
35 ];
36
37 for (int i = 0; i < levels.length; i++) {
38 for (int j = i + 1; j < levels.length; j++) {
39 expect(levels[i] < levels[j]);
40 }
41 }
42 });
43
44 test('levels are comparable', () {
45 final unsorted = [
46 Level.INFO, Level.CONFIG, Level.FINE, Level.SHOUT, Level.OFF,
47 Level.FINER, Level.ALL, Level.WARNING, Level.FINEST, Level.SEVERE,
48 ];
49 final sorted = const [
50 Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG,
51 Level.INFO, Level.WARNING, Level.SEVERE, Level.SHOUT, Level.OFF
52 ];
53 expect(unsorted, isNot(orderedEquals(sorted)));
54
55 unsorted.sort((a, b) => a.compareTo(b));
56 expect(unsorted, orderedEquals(sorted));
57 });
58
59 test('levels are hashable', () {
60 var map = new Map<Level, String>();
61 map[Level.INFO] = 'info';
62 map[Level.SHOUT] = 'shout';
63 expect(map[Level.INFO], equals('info'));
64 expect(map[Level.SHOUT], equals('shout'));
65 });
66
67 test('logger naming is hierarchical', () {
68 Logger c = new Logger('a.b.c');
69 expect(c.name, equals('c'));
70 expect(c.parent.name, equals('b'));
71 expect(c.parent.parent.name, equals('a'));
72 expect(c.parent.parent.parent.name, equals(''));
73 expect(c.parent.parent.parent.parent, isNull);
74 });
75
76 test('logger full name', () {
77 Logger c = new Logger('a.b.c');
78 expect(c.fullName, equals('a.b.c'));
79 expect(c.parent.fullName, equals('a.b'));
80 expect(c.parent.parent.fullName, equals('a'));
81 expect(c.parent.parent.parent.fullName, equals(''));
82 expect(c.parent.parent.parent.parent, isNull);
83 });
84
85 test('logger parent-child links are correct', () {
86 Logger a = new Logger('a');
87 Logger b = new Logger('a.b');
88 Logger c = new Logger('a.c');
89 expect(a == b.parent);
90 expect(a == c.parent);
91 expect(a.children['b'] == b);
92 expect(a.children['c'] == c);
93 });
94
95 test('loggers are singletons', () {
96 Logger a1 = new Logger('a');
97 Logger a2 = new Logger('a');
98 Logger b = new Logger('a.b');
99 Logger root = Logger.root;
100 expect(a1 === a2);
101 expect(a1 === b.parent);
102 expect(root === a1.parent);
103 expect(root === new Logger(''));
104 });
105
106 group('mutating levels', () {
107 Logger root = Logger.root;
108 Logger a = new Logger('a');
109 Logger b = new Logger('a.b');
110 Logger c = new Logger('a.b.c');
111 Logger d = new Logger('a.b.c.d');
112 Logger e = new Logger('a.b.c.d.e');
113
114 setUp(() {
115 hierarchicalLoggingEnabled = true;
116 root.level = Level.INFO;
117 a.level = null;
118 b.level = null;
119 c.level = null;
120 d.level = null;
121 e.level = null;
122 root.on.record.clear();
123 a.on.record.clear();
124 b.on.record.clear();
125 c.on.record.clear();
126 d.on.record.clear();
127 e.on.record.clear();
128 hierarchicalLoggingEnabled = false;
129 root.level = Level.INFO;
130 });
131
132 test('cannot set level if hierarchy is disabled', () {
133 expectThrow(() {a.level = Level.FINE;});
134 });
135
136 test('loggers effective level - no hierarchy', () {
137 expect(root.level, equals(Level.INFO));
138 expect(a.level, equals(Level.INFO));
139 expect(b.level, equals(Level.INFO));
140
141 root.level = Level.SHOUT;
142
143 expect(root.level, equals(Level.SHOUT));
144 expect(a.level, equals(Level.SHOUT));
145 expect(b.level, equals(Level.SHOUT));
146 });
147
148 test('loggers effective level - with hierarchy', () {
149 hierarchicalLoggingEnabled = true;
150 expect(root.level, equals(Level.INFO));
151 expect(a.level, equals(Level.INFO));
152 expect(b.level, equals(Level.INFO));
153 expect(c.level, equals(Level.INFO));
154
155 root.level = Level.SHOUT;
156 b.level = Level.FINE;
157
158 expect(root.level, equals(Level.SHOUT));
159 expect(a.level, equals(Level.SHOUT));
160 expect(b.level, equals(Level.FINE));
161 expect(c.level, equals(Level.FINE));
162 });
163
164 test('isLoggable is appropriate', () {
165 hierarchicalLoggingEnabled = true;
166 root.level = Level.SEVERE;
167 c.level = Level.ALL;
168 e.level = Level.OFF;
169
170 expect(root.isLoggable(Level.SHOUT));
171 expect(root.isLoggable(Level.SEVERE));
172 expect(!root.isLoggable(Level.WARNING));
173 expect(c.isLoggable(Level.FINEST));
174 expect(c.isLoggable(Level.FINE));
175 expect(!e.isLoggable(Level.SHOUT));
176 });
177
178 test('add/remove handlers - no hierarchy', () {
179 int calls = 0;
180 var handler = (_) { calls++; };
181 c.on.record.add(handler);
182 root.info("foo");
183 root.info("foo");
184 expect(calls, equals(2));
185 c.on.record.remove(handler);
186 root.info("foo");
187 expect(calls, equals(2));
188 });
189
190 test('add/remove handlers - with hierarchy', () {
191 hierarchicalLoggingEnabled = true;
192 int calls = 0;
193 var handler = (_) { calls++; };
194 c.on.record.add(handler);
195 root.info("foo");
196 root.info("foo");
197 expect(calls, equals(0));
198 });
199
200 test('logging methods store appropriate level', () {
201 root.level = Level.ALL;
202 var rootMessages = [];
203 root.on.record.add((record) {
204 rootMessages.add('${record.level}: ${record.message}');
205 });
206
207 root.finest('1');
208 root.finer('2');
209 root.fine('3');
210 root.config('4');
211 root.info('5');
212 root.warning('6');
213 root.severe('7');
214 root.shout('8');
215
216 expect(rootMessages, equals([
217 'FINEST: 1',
218 'FINER: 2',
219 'FINE: 3',
220 'CONFIG: 4',
221 'INFO: 5',
222 'WARNING: 6',
223 'SEVERE: 7',
224 'SHOUT: 8']));
225 });
226
227 test('message logging - no hierarchy', () {
228 root.level = Level.WARNING;
229 var rootMessages = [];
230 var aMessages = [];
231 var cMessages = [];
232 c.on.record.add((record) {
233 cMessages.add('${record.level}: ${record.message}');
234 });
235 a.on.record.add((record) {
236 aMessages.add('${record.level}: ${record.message}');
237 });
238 root.on.record.add((record) {
239 rootMessages.add('${record.level}: ${record.message}');
240 });
241
242 root.info('1');
243 root.fine('2');
244 root.shout('3');
245
246 b.info('4');
247 b.severe('5');
248 b.warning('6');
249 b.fine('7');
250
251 c.fine('8');
252 c.warning('9');
253 c.shout('10');
254
255 expect(rootMessages, equals([
256 // 'INFO: 1' is not loggable
257 // 'FINE: 2' is not loggable
258 'SHOUT: 3',
259 // 'INFO: 4' is not loggable
260 'SEVERE: 5',
261 'WARNING: 6',
262 // 'FINE: 7' is not loggable
263 // 'FINE: 8' is not loggable
264 'WARNING: 9',
265 'SHOUT: 10']));
266
267 // no hierarchy means we all hear the same thing.
268 expect(aMessages, equals(rootMessages));
269 expect(cMessages, equals(rootMessages));
270 });
271
272 test('message logging - with hierarchy', () {
273 hierarchicalLoggingEnabled = true;
274
275 b.level = Level.WARNING;
276
277 var rootMessages = [];
278 var aMessages = [];
279 var cMessages = [];
280 c.on.record.add((record) {
281 cMessages.add('${record.level}: ${record.message}');
282 });
283 a.on.record.add((record) {
284 aMessages.add('${record.level}: ${record.message}');
285 });
286 root.on.record.add((record) {
287 rootMessages.add('${record.level}: ${record.message}');
288 });
289
290 root.info('1');
291 root.fine('2');
292 root.shout('3');
293
294 b.info('4');
295 b.severe('5');
296 b.warning('6');
297 b.fine('7');
298
299 c.fine('8');
300 c.warning('9');
301 c.shout('10');
302
303 expect(rootMessages, equals([
304 'INFO: 1',
305 // 'FINE: 2' is not loggable
306 'SHOUT: 3',
307 // 'INFO: 4' is not loggable
308 'SEVERE: 5',
309 'WARNING: 6',
310 // 'FINE: 7' is not loggable
311 // 'FINE: 8' is not loggable
312 'WARNING: 9',
313 'SHOUT: 10']));
314
315 expect(aMessages, equals([
316 // 1,2 and 3 are lower in the hierarchy
317 // 'INFO: 4' is not loggable
318 'SEVERE: 5',
319 'WARNING: 6',
320 // 'FINE: 7' is not loggable
321 // 'FINE: 8' is not loggable
322 'WARNING: 9',
323 'SHOUT: 10']));
324
325 expect(cMessages, equals([
326 // 1 - 7 are lower in the hierarchy
327 // 'FINE: 8' is not loggable
328 'WARNING: 9',
329 'SHOUT: 10']));
330 });
331 });
332 }
OLDNEW
« lib/logging/logging.dart ('K') | « lib/logging/logging.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698