OLD | NEW |
| (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 '../../../pkg/logging/lib/logging.dart'; | |
9 import 'package: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, isTrue); | |
15 expect(level1 <= level1, isTrue); | |
16 expect(level1 >= level1, isTrue); | |
17 expect(level1 < level1, isFalse); | |
18 expect(level1 > level1, isFalse); | |
19 | |
20 var level2 = const Level('NOT_REAL2', 455); | |
21 expect(level1 <= level2, isTrue); | |
22 expect(level1 < level2, isTrue); | |
23 expect(level2 >= level1, isTrue); | |
24 expect(level2 > level1, isTrue); | |
25 | |
26 var level3 = const Level('NOT_REAL3', 253); | |
27 expect(!identical(level1, level3), isTrue); // different instances | |
28 expect(level1 == level3, isTrue); // 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], isTrue); | |
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 name cannot start with a "." ', () { | |
68 expect(() => new Logger('.c'), throws); | |
69 }); | |
70 | |
71 test('logger naming is hierarchical', () { | |
72 Logger c = new Logger('a.b.c'); | |
73 expect(c.name, equals('c')); | |
74 expect(c.parent.name, equals('b')); | |
75 expect(c.parent.parent.name, equals('a')); | |
76 expect(c.parent.parent.parent.name, equals('')); | |
77 expect(c.parent.parent.parent.parent, isNull); | |
78 }); | |
79 | |
80 test('logger full name', () { | |
81 Logger c = new Logger('a.b.c'); | |
82 expect(c.fullName, equals('a.b.c')); | |
83 expect(c.parent.fullName, equals('a.b')); | |
84 expect(c.parent.parent.fullName, equals('a')); | |
85 expect(c.parent.parent.parent.fullName, equals('')); | |
86 expect(c.parent.parent.parent.parent, isNull); | |
87 }); | |
88 | |
89 test('logger parent-child links are correct', () { | |
90 Logger a = new Logger('a'); | |
91 Logger b = new Logger('a.b'); | |
92 Logger c = new Logger('a.c'); | |
93 expect(a == b.parent, isTrue); | |
94 expect(a == c.parent, isTrue); | |
95 expect(a.children['b'] == b, isTrue); | |
96 expect(a.children['c'] == c, isTrue); | |
97 }); | |
98 | |
99 test('loggers are singletons', () { | |
100 Logger a1 = new Logger('a'); | |
101 Logger a2 = new Logger('a'); | |
102 Logger b = new Logger('a.b'); | |
103 Logger root = Logger.root; | |
104 expect(identical(a1, a2), isTrue); | |
105 expect(identical(a1, b.parent), isTrue); | |
106 expect(identical(root, a1.parent), isTrue); | |
107 expect(identical(root, new Logger('')), isTrue); | |
108 }); | |
109 | |
110 group('mutating levels', () { | |
111 Logger root = Logger.root; | |
112 Logger a = new Logger('a'); | |
113 Logger b = new Logger('a.b'); | |
114 Logger c = new Logger('a.b.c'); | |
115 Logger d = new Logger('a.b.c.d'); | |
116 Logger e = new Logger('a.b.c.d.e'); | |
117 | |
118 setUp(() { | |
119 hierarchicalLoggingEnabled = true; | |
120 root.level = Level.INFO; | |
121 a.level = null; | |
122 b.level = null; | |
123 c.level = null; | |
124 d.level = null; | |
125 e.level = null; | |
126 root.on.record.clear(); | |
127 a.on.record.clear(); | |
128 b.on.record.clear(); | |
129 c.on.record.clear(); | |
130 d.on.record.clear(); | |
131 e.on.record.clear(); | |
132 hierarchicalLoggingEnabled = false; | |
133 root.level = Level.INFO; | |
134 }); | |
135 | |
136 test('cannot set level if hierarchy is disabled', () { | |
137 expect(() {a.level = Level.FINE;}, throws); | |
138 }); | |
139 | |
140 test('loggers effective level - no hierarchy', () { | |
141 expect(root.level, equals(Level.INFO)); | |
142 expect(a.level, equals(Level.INFO)); | |
143 expect(b.level, equals(Level.INFO)); | |
144 | |
145 root.level = Level.SHOUT; | |
146 | |
147 expect(root.level, equals(Level.SHOUT)); | |
148 expect(a.level, equals(Level.SHOUT)); | |
149 expect(b.level, equals(Level.SHOUT)); | |
150 }); | |
151 | |
152 test('loggers effective level - with hierarchy', () { | |
153 hierarchicalLoggingEnabled = true; | |
154 expect(root.level, equals(Level.INFO)); | |
155 expect(a.level, equals(Level.INFO)); | |
156 expect(b.level, equals(Level.INFO)); | |
157 expect(c.level, equals(Level.INFO)); | |
158 | |
159 root.level = Level.SHOUT; | |
160 b.level = Level.FINE; | |
161 | |
162 expect(root.level, equals(Level.SHOUT)); | |
163 expect(a.level, equals(Level.SHOUT)); | |
164 expect(b.level, equals(Level.FINE)); | |
165 expect(c.level, equals(Level.FINE)); | |
166 }); | |
167 | |
168 test('isLoggable is appropriate', () { | |
169 hierarchicalLoggingEnabled = true; | |
170 root.level = Level.SEVERE; | |
171 c.level = Level.ALL; | |
172 e.level = Level.OFF; | |
173 | |
174 expect(root.isLoggable(Level.SHOUT), isTrue); | |
175 expect(root.isLoggable(Level.SEVERE), isTrue); | |
176 expect(root.isLoggable(Level.WARNING), isFalse); | |
177 expect(c.isLoggable(Level.FINEST), isTrue); | |
178 expect(c.isLoggable(Level.FINE), isTrue); | |
179 expect(!e.isLoggable(Level.SHOUT), isTrue); | |
180 }); | |
181 | |
182 test('add/remove handlers - no hierarchy', () { | |
183 int calls = 0; | |
184 var handler = (_) { calls++; }; | |
185 c.on.record.add(handler); | |
186 root.info("foo"); | |
187 root.info("foo"); | |
188 expect(calls, equals(2)); | |
189 c.on.record.remove(handler); | |
190 root.info("foo"); | |
191 expect(calls, equals(2)); | |
192 }); | |
193 | |
194 test('add/remove handlers - with hierarchy', () { | |
195 hierarchicalLoggingEnabled = true; | |
196 int calls = 0; | |
197 var handler = (_) { calls++; }; | |
198 c.on.record.add(handler); | |
199 root.info("foo"); | |
200 root.info("foo"); | |
201 expect(calls, equals(0)); | |
202 }); | |
203 | |
204 test('logging methods store appropriate level', () { | |
205 root.level = Level.ALL; | |
206 var rootMessages = []; | |
207 root.on.record.add((record) { | |
208 rootMessages.add('${record.level}: ${record.message}'); | |
209 }); | |
210 | |
211 root.finest('1'); | |
212 root.finer('2'); | |
213 root.fine('3'); | |
214 root.config('4'); | |
215 root.info('5'); | |
216 root.warning('6'); | |
217 root.severe('7'); | |
218 root.shout('8'); | |
219 | |
220 expect(rootMessages, equals([ | |
221 'FINEST: 1', | |
222 'FINER: 2', | |
223 'FINE: 3', | |
224 'CONFIG: 4', | |
225 'INFO: 5', | |
226 'WARNING: 6', | |
227 'SEVERE: 7', | |
228 'SHOUT: 8'])); | |
229 }); | |
230 | |
231 test('message logging - no hierarchy', () { | |
232 root.level = Level.WARNING; | |
233 var rootMessages = []; | |
234 var aMessages = []; | |
235 var cMessages = []; | |
236 c.on.record.add((record) { | |
237 cMessages.add('${record.level}: ${record.message}'); | |
238 }); | |
239 a.on.record.add((record) { | |
240 aMessages.add('${record.level}: ${record.message}'); | |
241 }); | |
242 root.on.record.add((record) { | |
243 rootMessages.add('${record.level}: ${record.message}'); | |
244 }); | |
245 | |
246 root.info('1'); | |
247 root.fine('2'); | |
248 root.shout('3'); | |
249 | |
250 b.info('4'); | |
251 b.severe('5'); | |
252 b.warning('6'); | |
253 b.fine('7'); | |
254 | |
255 c.fine('8'); | |
256 c.warning('9'); | |
257 c.shout('10'); | |
258 | |
259 expect(rootMessages, equals([ | |
260 // 'INFO: 1' is not loggable | |
261 // 'FINE: 2' is not loggable | |
262 'SHOUT: 3', | |
263 // 'INFO: 4' is not loggable | |
264 'SEVERE: 5', | |
265 'WARNING: 6', | |
266 // 'FINE: 7' is not loggable | |
267 // 'FINE: 8' is not loggable | |
268 'WARNING: 9', | |
269 'SHOUT: 10'])); | |
270 | |
271 // no hierarchy means we all hear the same thing. | |
272 expect(aMessages, equals(rootMessages)); | |
273 expect(cMessages, equals(rootMessages)); | |
274 }); | |
275 | |
276 test('message logging - with hierarchy', () { | |
277 hierarchicalLoggingEnabled = true; | |
278 | |
279 b.level = Level.WARNING; | |
280 | |
281 var rootMessages = []; | |
282 var aMessages = []; | |
283 var cMessages = []; | |
284 c.on.record.add((record) { | |
285 cMessages.add('${record.level}: ${record.message}'); | |
286 }); | |
287 a.on.record.add((record) { | |
288 aMessages.add('${record.level}: ${record.message}'); | |
289 }); | |
290 root.on.record.add((record) { | |
291 rootMessages.add('${record.level}: ${record.message}'); | |
292 }); | |
293 | |
294 root.info('1'); | |
295 root.fine('2'); | |
296 root.shout('3'); | |
297 | |
298 b.info('4'); | |
299 b.severe('5'); | |
300 b.warning('6'); | |
301 b.fine('7'); | |
302 | |
303 c.fine('8'); | |
304 c.warning('9'); | |
305 c.shout('10'); | |
306 | |
307 expect(rootMessages, equals([ | |
308 'INFO: 1', | |
309 // 'FINE: 2' is not loggable | |
310 'SHOUT: 3', | |
311 // 'INFO: 4' is not loggable | |
312 'SEVERE: 5', | |
313 'WARNING: 6', | |
314 // 'FINE: 7' is not loggable | |
315 // 'FINE: 8' is not loggable | |
316 'WARNING: 9', | |
317 'SHOUT: 10'])); | |
318 | |
319 expect(aMessages, equals([ | |
320 // 1,2 and 3 are lower in the hierarchy | |
321 // 'INFO: 4' is not loggable | |
322 'SEVERE: 5', | |
323 'WARNING: 6', | |
324 // 'FINE: 7' is not loggable | |
325 // 'FINE: 8' is not loggable | |
326 'WARNING: 9', | |
327 'SHOUT: 10'])); | |
328 | |
329 expect(cMessages, equals([ | |
330 // 1 - 7 are lower in the hierarchy | |
331 // 'FINE: 8' is not loggable | |
332 'WARNING: 9', | |
333 'SHOUT: 10'])); | |
334 }); | |
335 }); | |
336 } | |
OLD | NEW |