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 #library('unittestTest'); | |
6 #import('../../../pkg/unittest/unittest.dart'); | |
7 | |
8 class MockList extends Mock implements List { | |
9 } | |
10 | |
11 class Foo { | |
12 sum(a, b, c) => a + b + c; | |
13 } | |
14 | |
15 class FooSpy extends Mock implements Foo { | |
16 Foo real; | |
17 FooSpy() { | |
18 real = new Foo(); | |
19 this.when(callsTo('sum')).alwaysCall(real.sum); | |
20 } | |
21 } | |
22 | |
23 makeTestLogEntry(String methodName, List args, int time, | |
24 [String mockName]) { | |
25 LogEntry e = new LogEntry(mockName, methodName, args, Action.IGNORE); | |
26 e.time = new Date.fromMillisecondsSinceEpoch(time, true); | |
27 return e; | |
28 } | |
29 | |
30 makeTestLog() { | |
31 LogEntryList logList = new LogEntryList('test'); | |
32 List args = new List(); | |
33 logList.add(makeTestLogEntry('a', args, 1000)); | |
34 logList.add(makeTestLogEntry('b', args, 2000)); | |
35 logList.add(makeTestLogEntry('c', args, 3000)); | |
36 return logList; | |
37 } | |
38 | |
39 main() { | |
40 test('Mocking: Basics', () { | |
41 var m = new Mock(); | |
42 print(m.length); | |
43 m.getLogs(callsTo('get length')).verify(happenedOnce); | |
44 | |
45 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); | |
46 m.when(callsTo('foo', 1, 1)).thenReturn('C'); | |
47 m.when(callsTo('foo', 9, anything)).thenReturn('D'); | |
48 m.when(callsTo('bar', anything, anything)).thenReturn('E'); | |
49 m.when(callsTo('foobar')).thenReturn('F'); | |
50 | |
51 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' | |
52 '${m.bar(1,1)}${m.foo(1,2)}'; | |
53 m.getLogs(callsTo('foo', anything, anything)). | |
54 verify(happenedExactly(4)); | |
55 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); | |
56 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); | |
57 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); | |
58 m.getLogs(callsTo('foobar')).verify(neverHappened); | |
59 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); | |
60 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). | |
61 verify(happenedExactly(2)); | |
62 expect(s, 'ACDEB'); | |
63 }); | |
64 | |
65 test('Mocking: Mock List', () { | |
66 var l = new MockList(); | |
67 l.when(callsTo('get length')).thenReturn(1); | |
68 l.when(callsTo('add', anything)).alwaysReturn(0); | |
69 l.add('foo'); | |
70 expect(l.length, 1); | |
71 | |
72 var m = new MockList(); | |
73 m.when(callsTo('add', anything)).alwaysReturn(0); | |
74 | |
75 m.add('foo'); | |
76 m.add('bar'); | |
77 | |
78 m.getLogs(callsTo('add')).verify(happenedExactly(2)); | |
79 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce); | |
80 }); | |
81 | |
82 test('Mocking: Spy', () { | |
83 var p = new FooSpy(); | |
84 p.sum(1, 2, 3); | |
85 p.getLogs(callsTo('sum')).verify(happenedOnce); | |
86 p.sum(2, 2, 2); | |
87 p.getLogs(callsTo('sum')).verify(happenedExactly(2)); | |
88 p.getLogs(callsTo('sum')).verify(sometimeReturned(6)); | |
89 p.getLogs(callsTo('sum')).verify(alwaysReturned(6)); | |
90 p.getLogs(callsTo('sum')).verify(neverReturned(5)); | |
91 p.sum(2, 2, 1); | |
92 p.getLogs(callsTo('sum')).verify(sometimeReturned(5)); | |
93 }); | |
94 | |
95 test('Mocking: Excess Calls', () { | |
96 var m = new Mock(); | |
97 m.when(callsTo('foo')).alwaysReturn(null); | |
98 expect(() { m.foo(); }, returnsNormally); | |
99 expect(() { m.foo(); }, returnsNormally); | |
100 expect( | |
101 () { | |
102 m.getLogs(callsTo('foo')).verify(happenedOnce); | |
103 }, | |
104 throwsA( | |
105 (e) => | |
106 collapseWhitespace(e.toString()) == | |
107 "Expected foo() to be called 1 times but:" | |
108 " was called 2 times.") | |
109 ); | |
110 }); | |
111 | |
112 test('Mocking: No action', () { | |
113 var m = new Mock(); | |
114 m.when(callsTo('foo')).thenReturn(null); | |
115 expect(() => m.foo(), returnsNormally); | |
116 expect(() => m.foo(), throwsA((e) => | |
117 e.toString() == 'Exception: No more actions for method foo.')); | |
118 }); | |
119 | |
120 test('Mocking: No matching return', () { | |
121 var p = new FooSpy(); | |
122 p.sum(1, 2, 3); | |
123 expect(() => p.getLogs(callsTo('sum')).verify(sometimeReturned(0)), | |
124 throwsA((e) => collapseWhitespace(e.toString()) == | |
125 "Expected sum() to sometimes return <0> but: never did.") | |
126 ); | |
127 }); | |
128 | |
129 test('Mocking: No behavior', () { | |
130 var m = new Mock.custom(throwIfNoBehavior:true); | |
131 m.when(callsTo('foo')).thenReturn(null); | |
132 expect(() => m.foo(), returnsNormally); | |
133 expect(() => m.bar(), throwsA((e) => e.toString() == | |
134 'Exception: No behavior specified for method bar.')); | |
135 }); | |
136 | |
137 test('Mocking: Shared logList', () { | |
138 var logList = new LogEntryList(); | |
139 var m1 = new Mock.custom(name:'m1', log:logList); | |
140 var m2 = new Mock.custom(name:'m2', log:logList); | |
141 m1.foo(); | |
142 m2.foo(); | |
143 m1.bar(); | |
144 m2.bar(); | |
145 expect(logList.logs.length, 4); | |
146 logList.getMatches(anything, callsTo('foo')).verify(happenedExactly(2)); | |
147 logList.getMatches('m1', callsTo('foo')).verify(happenedOnce); | |
148 logList.getMatches('m1', callsTo('bar')).verify(happenedOnce); | |
149 m2.getLogs(callsTo('foo')).verify(happenedOnce); | |
150 m2.getLogs(callsTo('bar')).verify(happenedOnce); | |
151 }); | |
152 | |
153 test('Mocking: Null CallMatcher', () { | |
154 var m = new Mock(); | |
155 m.when(callsTo(null, 1)).alwaysReturn(2); | |
156 m.when(callsTo(null, 2)).alwaysReturn(4); | |
157 expect(m.foo(1), 2); | |
158 expect(m.foo(2), 4); | |
159 expect(m.bar(1), 2); | |
160 expect(m.bar(2), 4); | |
161 m.getLogs(callsTo()).verify(happenedExactly(4)); | |
162 m.getLogs(callsTo(null, 1)).verify(happenedExactly(2)); | |
163 m.getLogs(callsTo(null, 2)).verify(happenedExactly(2)); | |
164 m.getLogs(null, returning(1)).verify(neverHappened); | |
165 m.getLogs(null, returning(2)).verify(happenedExactly(2)); | |
166 m.getLogs(null, returning(4)).verify(happenedExactly(2)); | |
167 }); | |
168 | |
169 test('Mocking: RegExp CallMatcher good', () { | |
170 var m = new Mock(); | |
171 m.when(callsTo(matches('^[A-Z]'))). | |
172 alwaysThrow('Method names must start with lower case.'); | |
173 m.test(); | |
174 }); | |
175 | |
176 test('Mocking: No logging', () { | |
177 var m = new Mock.custom(enableLogging:false); | |
178 m.Test(); | |
179 expect(() => m.getLogs(callsTo('Test')), throwsA((e) => e.toString() == | |
180 "Exception: Can't retrieve logs when logging was never enabled.")); | |
181 }); | |
182 | |
183 test('Mocking: Find logList entry', () { | |
184 LogEntryList logList = makeTestLog(); | |
185 // Basic behavior, with call matcher. | |
186 expect(logList.findLogEntry(callsTo('a')), 0); | |
187 expect(logList.findLogEntry(callsTo('b')), 1); | |
188 expect(logList.findLogEntry(callsTo('c')), 2); | |
189 expect(logList.findLogEntry(callsTo('d')), -1); | |
190 // Find using predicate. | |
191 expect(logList.findLogEntry((le) => le.methodName == 'a'), 0); | |
192 expect(logList.findLogEntry((le) => le.methodName == 'b'), 1); | |
193 expect(logList.findLogEntry((le) => le.methodName == 'c'), 2); | |
194 // Test explicit return value. | |
195 expect(logList.findLogEntry((le) => le.methodName == 'd', 0, 3), 3); | |
196 // Find from start of logList. | |
197 expect(logList.findLogEntry(callsTo('a'), 0), 0); | |
198 expect(logList.findLogEntry(callsTo('b'), 0), 1); | |
199 expect(logList.findLogEntry(callsTo('c'), 0), 2); | |
200 // Find from second entry in logList. | |
201 expect(logList.findLogEntry(callsTo('a'), 1), -1); | |
202 expect(logList.findLogEntry(callsTo('b'), 1), 1); | |
203 expect(logList.findLogEntry(callsTo('c'), 1), 2); | |
204 // Find from last entry in logList. | |
205 expect(logList.findLogEntry(callsTo('a'), 2), -1); | |
206 expect(logList.findLogEntry(callsTo('b'), 2), -1); | |
207 expect(logList.findLogEntry(callsTo('c'), 2), 2); | |
208 // Find from start position passed end of logList. | |
209 expect(logList.findLogEntry(callsTo('a'), 3), -1); | |
210 expect(logList.findLogEntry(callsTo('b'), 3), -1); | |
211 expect(logList.findLogEntry(callsTo('c'), 3), -1); | |
212 // No restriction on entry. | |
213 expect(logList.findLogEntry(null, 0), 0); | |
214 expect(logList.findLogEntry(null, 1), 1); | |
215 expect(logList.findLogEntry(null, 2), 2); | |
216 expect(logList.findLogEntry(null, 3), -1); | |
217 }); | |
218 | |
219 test('Mocking: from,after,before,until', () { | |
220 LogEntryList logList = makeTestLog(); | |
221 LogEntryList log2; | |
222 Date t0 = new Date.fromMillisecondsSinceEpoch(0, true); | |
223 Date t1000 = new Date.fromMillisecondsSinceEpoch(1000, true); | |
224 Date t2000 = new Date.fromMillisecondsSinceEpoch(2000, true); | |
225 Date t3000 = new Date.fromMillisecondsSinceEpoch(3000, true); | |
226 Date t4000 = new Date.fromMillisecondsSinceEpoch(4000, true); | |
227 | |
228 log2 = logList.before(t0); | |
229 expect(log2.logs, hasLength(0)); | |
230 expect(log2.filter, 'test before 1970-01-01 00:00:00.000Z'); | |
231 log2 = logList.until(t0); | |
232 expect(log2.logs, hasLength(0)); | |
233 expect(log2.filter, 'test until 1970-01-01 00:00:00.000Z'); | |
234 log2 = logList.from(t0); | |
235 expect(log2.logs, hasLength(3)); | |
236 expect(log2.first.methodName, 'a'); | |
237 expect(log2.last.methodName, 'c'); | |
238 expect(log2.filter, 'test from 1970-01-01 00:00:00.000Z'); | |
239 log2 = logList.after(t0); | |
240 expect(log2.logs, hasLength(3)); | |
241 expect(log2.first.methodName, 'a'); | |
242 expect(log2.last.methodName, 'c'); | |
243 expect(log2.filter, 'test after 1970-01-01 00:00:00.000Z'); | |
244 | |
245 log2 = logList.before(t1000); | |
246 expect(log2.logs, hasLength(0)); | |
247 log2 = logList.until(t1000); | |
248 expect(log2.logs, hasLength(1)); | |
249 expect(log2.first.methodName, 'a'); | |
250 expect(log2.last.methodName, 'a'); | |
251 log2 = logList.from(t1000); | |
252 expect(log2.logs, hasLength(3)); | |
253 expect(log2.first.methodName, 'a'); | |
254 expect(log2.last.methodName, 'c'); | |
255 log2 = logList.after(t1000); | |
256 expect(log2.logs, hasLength(2)); | |
257 expect(log2.first.methodName, 'b'); | |
258 expect(log2.last.methodName, 'c'); | |
259 | |
260 log2 = logList.before(t2000); | |
261 expect(log2.logs, hasLength(1)); | |
262 expect(log2.first.methodName, 'a'); | |
263 expect(log2.last.methodName, 'a'); | |
264 log2 = logList.until(t2000); | |
265 expect(log2.logs, hasLength(2)); | |
266 expect(log2.first.methodName, 'a'); | |
267 expect(log2.last.methodName, 'b'); | |
268 log2 = logList.from(t2000); | |
269 expect(log2.logs, hasLength(2)); | |
270 expect(log2.first.methodName, 'b'); | |
271 expect(log2.last.methodName, 'c'); | |
272 log2 = logList.after(t2000); | |
273 expect(log2.logs, hasLength(1)); | |
274 expect(log2.first.methodName, 'c'); | |
275 expect(log2.last.methodName, 'c'); | |
276 | |
277 log2 = logList.before(t3000); | |
278 expect(log2.logs, hasLength(2)); | |
279 expect(log2.first.methodName, 'a'); | |
280 expect(log2.last.methodName, 'b'); | |
281 log2 = logList.until(t3000); | |
282 expect(log2.logs, hasLength(3)); | |
283 expect(log2.first.methodName, 'a'); | |
284 expect(log2.last.methodName, 'c'); | |
285 | |
286 log2 = logList.from(t3000); | |
287 expect(log2.logs, hasLength(1)); | |
288 expect(log2.first.methodName, 'c'); | |
289 expect(log2.last.methodName, 'c'); | |
290 log2 = logList.after(t3000); | |
291 expect(log2.logs, hasLength(0)); | |
292 | |
293 log2 = logList.before(t4000); | |
294 expect(log2.logs, hasLength(3)); | |
295 expect(log2.first.methodName, 'a'); | |
296 expect(log2.last.methodName, 'c'); | |
297 log2 = logList.until(t4000); | |
298 expect(log2.logs, hasLength(3)); | |
299 expect(log2.first.methodName, 'a'); | |
300 expect(log2.last.methodName, 'c'); | |
301 log2 = logList.from(t4000); | |
302 expect(log2.logs, hasLength(0)); | |
303 log2 = logList.after(t4000); | |
304 expect(log2.logs, hasLength(0)); | |
305 }); | |
306 | |
307 test('Mocking: inplace from,after,before,until', () { | |
308 Date t0 = new Date.fromMillisecondsSinceEpoch(0, true); | |
309 Date t1000 = new Date.fromMillisecondsSinceEpoch(1000, true); | |
310 Date t2000 = new Date.fromMillisecondsSinceEpoch(2000, true); | |
311 Date t3000 = new Date.fromMillisecondsSinceEpoch(3000, true); | |
312 Date t4000 = new Date.fromMillisecondsSinceEpoch(4000, true); | |
313 | |
314 LogEntryList logList = makeTestLog().before(t0, true); | |
315 expect(logList.logs, hasLength(0)); | |
316 expect(logList.filter, 'test before 1970-01-01 00:00:00.000Z'); | |
317 logList = makeTestLog().until(t0, true); | |
318 expect(logList.logs, hasLength(0)); | |
319 expect(logList.filter, 'test until 1970-01-01 00:00:00.000Z'); | |
320 logList = makeTestLog().from(t0, true); | |
321 expect(logList.logs, hasLength(3)); | |
322 expect(logList.first.methodName, 'a'); | |
323 expect(logList.last.methodName, 'c'); | |
324 expect(logList.filter, 'test from 1970-01-01 00:00:00.000Z'); | |
325 logList = makeTestLog().after(t0, true); | |
326 expect(logList.logs, hasLength(3)); | |
327 expect(logList.first.methodName, 'a'); | |
328 expect(logList.last.methodName, 'c'); | |
329 expect(logList.filter, 'test after 1970-01-01 00:00:00.000Z'); | |
330 | |
331 logList = makeTestLog().before(t1000, true); | |
332 expect(logList.logs, hasLength(0)); | |
333 logList = makeTestLog().until(t1000, true); | |
334 expect(logList.logs, hasLength(1)); | |
335 expect(logList.first.methodName, 'a'); | |
336 expect(logList.last.methodName, 'a'); | |
337 logList = makeTestLog().from(t1000, true); | |
338 expect(logList.logs, hasLength(3)); | |
339 expect(logList.first.methodName, 'a'); | |
340 expect(logList.last.methodName, 'c'); | |
341 logList = makeTestLog().after(t1000, true); | |
342 expect(logList.logs, hasLength(2)); | |
343 expect(logList.first.methodName, 'b'); | |
344 expect(logList.last.methodName, 'c'); | |
345 | |
346 logList = makeTestLog().before(t2000, true); | |
347 expect(logList.logs, hasLength(1)); | |
348 expect(logList.first.methodName, 'a'); | |
349 expect(logList.last.methodName, 'a'); | |
350 logList = makeTestLog().until(t2000, true); | |
351 expect(logList.logs, hasLength(2)); | |
352 expect(logList.first.methodName, 'a'); | |
353 expect(logList.last.methodName, 'b'); | |
354 logList = makeTestLog().from(t2000, true); | |
355 expect(logList.logs, hasLength(2)); | |
356 expect(logList.first.methodName, 'b'); | |
357 expect(logList.last.methodName, 'c'); | |
358 logList = makeTestLog().after(t2000, true); | |
359 expect(logList.logs, hasLength(1)); | |
360 expect(logList.first.methodName, 'c'); | |
361 expect(logList.last.methodName, 'c'); | |
362 | |
363 logList = makeTestLog().before(t3000, true); | |
364 expect(logList.logs, hasLength(2)); | |
365 expect(logList.first.methodName, 'a'); | |
366 expect(logList.last.methodName, 'b'); | |
367 logList = makeTestLog().until(t3000, true); | |
368 expect(logList.logs, hasLength(3)); | |
369 expect(logList.first.methodName, 'a'); | |
370 expect(logList.last.methodName, 'c'); | |
371 logList = makeTestLog().from(t3000, true); | |
372 expect(logList.logs, hasLength(1)); | |
373 expect(logList.first.methodName, 'c'); | |
374 expect(logList.last.methodName, 'c'); | |
375 logList = makeTestLog().after(t3000); | |
376 expect(logList.logs, hasLength(0)); | |
377 | |
378 logList = makeTestLog().before(t4000, true); | |
379 expect(logList.logs, hasLength(3)); | |
380 expect(logList.first.methodName, 'a'); | |
381 expect(logList.last.methodName, 'c'); | |
382 logList = makeTestLog().until(t4000, true); | |
383 expect(logList.logs, hasLength(3)); | |
384 expect(logList.first.methodName, 'a'); | |
385 expect(logList.last.methodName, 'c'); | |
386 logList = makeTestLog().from(t4000, true); | |
387 expect(logList.logs, hasLength(0)); | |
388 logList = makeTestLog().after(t4000, true); | |
389 expect(logList.logs, hasLength(0)); | |
390 }); | |
391 | |
392 test('Mocking: Neighbors', () { | |
393 LogEntryList logList = new LogEntryList('test'); | |
394 List args0 = new List(); | |
395 List args1 = new List(); | |
396 args1.add('test'); | |
397 LogEntry e0 = makeTestLogEntry('foo', args0, 1000); | |
398 logList.add(e0); | |
399 LogEntry e1 = makeTestLogEntry('bar1', args0, 2000, 'a'); | |
400 logList.add(e1); | |
401 LogEntry e2 = makeTestLogEntry('bar1', args1, 3000, 'b'); | |
402 logList.add(e2); | |
403 LogEntry e3 = makeTestLogEntry('foo', args0, 4000); | |
404 logList.add(e3); | |
405 LogEntry e4 = makeTestLogEntry('hello', args0, 4500); | |
406 logList.add(e4); | |
407 LogEntry e5 = makeTestLogEntry('bar2', args0, 5000, 'a'); | |
408 logList.add(e5); | |
409 LogEntry e6 = makeTestLogEntry('bar2', args1, 6000, 'b'); | |
410 logList.add(e6); | |
411 LogEntry e7 = makeTestLogEntry('foo', args0, 7000); | |
412 logList.add(e7); | |
413 LogEntry e8 = makeTestLogEntry('bar3', args0, 8000, 'a'); | |
414 logList.add(e8); | |
415 LogEntry e9 = makeTestLogEntry('bar3', args1, 9000, 'b'); | |
416 logList.add(e9); | |
417 LogEntry e10 = makeTestLogEntry('foo', args0, 10000); | |
418 logList.add(e10); | |
419 | |
420 LogEntryList keyList = new LogEntryList('keys'); | |
421 | |
422 // Test with empty key list. | |
423 | |
424 LogEntryList result; | |
425 result = logList.preceding(keyList); | |
426 expect(result.logs, hasLength(0)); | |
427 | |
428 result = logList.preceding(keyList, includeKeys:true); | |
429 expect(result.logs, hasLength(0)); | |
430 | |
431 // Single key, distance 1, no restrictions. | |
432 | |
433 keyList.add(e3); | |
434 result = logList.preceding(keyList); | |
435 expect(result.logs, orderedEquals([e2])); | |
436 | |
437 result = logList.following(keyList); | |
438 expect(result.logs, orderedEquals([e4])); | |
439 | |
440 // Single key, distance 2, no restrictions. | |
441 | |
442 result = logList.preceding(keyList, distance:2); | |
443 expect(result.logs, orderedEquals([e1, e2])); | |
444 | |
445 result = logList.following(keyList, distance:2); | |
446 expect(result.logs, orderedEquals([e4, e5])); | |
447 | |
448 // Single key, distance 3, no restrictions. | |
449 | |
450 result = logList.preceding(keyList, distance:3); | |
451 expect(result.logs, orderedEquals([e0, e1, e2])); | |
452 | |
453 result = logList.following(keyList, distance:3); | |
454 expect(result.logs, orderedEquals([e4, e5, e6])); | |
455 | |
456 // Include keys in result | |
457 | |
458 result = logList.preceding(keyList, distance:3, includeKeys:true); | |
459 expect(result.logs, orderedEquals([e0, e1, e2, e3])); | |
460 | |
461 result = logList.following(keyList, distance:3, includeKeys:true); | |
462 expect(result.logs, orderedEquals([e3, e4, e5, e6])); | |
463 | |
464 // Restrict the matches | |
465 | |
466 result = logList.preceding(keyList, logFilter:callsTo(startsWith('bar')), | |
467 distance:3); | |
468 expect(result.logs, orderedEquals([e1, e2])); | |
469 | |
470 result = logList.preceding(keyList, logFilter:callsTo(startsWith('bar')), | |
471 distance:3, includeKeys:true); | |
472 expect(result.logs, orderedEquals([e1, e2, e3])); | |
473 | |
474 result = logList.preceding(keyList, equals('a'), callsTo(startsWith('bar')), | |
475 distance:3); | |
476 expect(result.logs, orderedEquals([e1])); | |
477 | |
478 result = logList.preceding(keyList, equals('a'), callsTo(startsWith('bar')), | |
479 distance:3, includeKeys:true); | |
480 expect(result.logs, orderedEquals([e1, e3])); | |
481 | |
482 keyList.logs.clear(); | |
483 keyList.add(e0); | |
484 keyList.add(e3); | |
485 keyList.add(e7); | |
486 | |
487 result = logList.preceding(keyList); | |
488 expect(result.logs, orderedEquals([e2, e6])); | |
489 | |
490 result = logList.following(keyList); | |
491 expect(result.logs, orderedEquals([e1, e4, e8])); | |
492 | |
493 result = logList.preceding(keyList, includeKeys:true); | |
494 expect(result.logs, orderedEquals([e0, e2, e3, e6, e7])); | |
495 | |
496 result = logList.following(keyList, includeKeys:true); | |
497 expect(result.logs, orderedEquals([e0, e1, e3, e4, e7, e8])); | |
498 | |
499 keyList.logs.clear(); | |
500 keyList.add(e3); | |
501 keyList.add(e7); | |
502 keyList.add(e10); | |
503 | |
504 result = logList.preceding(keyList); | |
505 expect(result.logs, orderedEquals([e2, e6, e9])); | |
506 | |
507 result = logList.following(keyList); | |
508 expect(result.logs, orderedEquals([e4, e8])); | |
509 | |
510 result = logList.preceding(keyList, includeKeys:true); | |
511 expect(result.logs, orderedEquals([e2, e3, e6, e7, e9, e10])); | |
512 | |
513 result = logList.following(keyList, includeKeys:true); | |
514 expect(result.logs, orderedEquals([e3, e4, e7, e8, e10])); | |
515 | |
516 keyList.logs.clear(); | |
517 keyList.add(e0); | |
518 keyList.add(e3); | |
519 keyList.add(e7); | |
520 keyList.add(e10); | |
521 | |
522 result = logList.preceding(keyList); | |
523 expect(result.logs, orderedEquals([e2, e6, e9])); | |
524 | |
525 result = logList.following(keyList); | |
526 expect(result.logs, orderedEquals([e1, e4, e8])); | |
527 | |
528 result = logList.preceding(keyList, includeKeys:true); | |
529 expect(result.logs, orderedEquals([e0, e2, e3, e6, e7, e9, e10])); | |
530 | |
531 result = logList.following(keyList, includeKeys:true); | |
532 expect(result.logs, orderedEquals([e0, e1, e3, e4, e7, e8, e10])); | |
533 | |
534 keyList.logs.clear(); | |
535 keyList.add(e0); | |
536 keyList.add(e3); | |
537 keyList.add(e7); | |
538 | |
539 result = logList.preceding(keyList, distance:3); | |
540 expect(result.logs, orderedEquals([e1, e2, e4, e5, e6])); | |
541 | |
542 result = logList.following(keyList, distance:3); | |
543 expect(result.logs, orderedEquals([e1, e2, e4, e5, e6, e8, e9, e10])); | |
544 | |
545 result = logList.preceding(keyList, distance:3, includeKeys:true); | |
546 expect(result.logs, orderedEquals([e0, e1, e2, e3, e4, e5, e6, e7])); | |
547 | |
548 result = logList.following(keyList, distance:3, includeKeys:true); | |
549 expect(result.logs, orderedEquals([e0, e1, e2, e3, e4, e5, e6, | |
550 e7, e8, e9, e10])); | |
551 | |
552 keyList.logs.clear(); | |
553 keyList.add(e3); | |
554 keyList.add(e7); | |
555 keyList.add(e10); | |
556 | |
557 result = logList.preceding(keyList, distance:3); | |
558 expect(result.logs, orderedEquals([e0, e1, e2, e4, e5, e6, e8, e9])); | |
559 | |
560 result = logList.following(keyList, distance:3); | |
561 expect(result.logs, orderedEquals([e4, e5, e6, e8, e9])); | |
562 | |
563 result = logList.preceding(keyList, distance:3, includeKeys:true); | |
564 expect(result.logs, orderedEquals([e0, e1, e2, e3, e4, e5, e6, | |
565 e7, e8, e9, e10])); | |
566 | |
567 result = logList.following(keyList, distance:3, includeKeys:true); | |
568 expect(result.logs, orderedEquals([e3, e4, e5, e6, e7, e8, e9, e10])); | |
569 | |
570 keyList.logs.clear(); | |
571 keyList.add(e0); | |
572 keyList.add(e3); | |
573 keyList.add(e7); | |
574 keyList.add(e10); | |
575 | |
576 result = logList.preceding(keyList, distance:3); | |
577 expect(result.logs, orderedEquals([e1, e2, e4, e5, e6, e8, e9])); | |
578 | |
579 result = logList.following(keyList, distance:3); | |
580 expect(result.logs, orderedEquals([e1, e2, e4, e5, e6, e8, e9])); | |
581 | |
582 result = logList.preceding(keyList, distance:3, includeKeys:true); | |
583 expect(result.logs, orderedEquals([e0, e1, e2, e3, e4, e5, e6, | |
584 e7, e8, e9, e10])); | |
585 | |
586 result = logList.following(keyList, distance:3, includeKeys:true); | |
587 expect(result.logs, orderedEquals([e0, e1, e2, e3, e4, e5, e6, | |
588 e7, e8, e9, e10])); | |
589 }); | |
590 | |
591 test('Mocking: stepwiseValidate', () { | |
592 LogEntryList logList = new LogEntryList('test'); | |
593 for (var i = 0; i < 10; i++) { | |
594 LogEntry e = new LogEntry(null, 'foo', [i], Action.IGNORE); | |
595 logList.add(e); | |
596 } | |
597 int total = 0; | |
598 logList.stepwiseValidate((log, pos) { | |
599 total += log[pos].args[0] * log[pos + 1].args[0]; | |
600 expect(log[pos + 1].args[0] - log[pos].args[0], equals(1)); | |
601 return 2; | |
602 }); | |
603 expect(total, equals((0 * 1) + (2 * 3) + (4 * 5) + (6 * 7) + (8 * 9))); | |
604 }); | |
605 | |
606 test('Mocking: clearLogs', () { | |
607 var m = new Mock(); | |
608 m.foo(); | |
609 m.foo(); | |
610 m.foo(); | |
611 expect(m.log.logs, hasLength(3)); | |
612 m.clearLogs(); | |
613 expect(m.log.logs, hasLength(0)); | |
614 LogEntryList log = new LogEntryList(); | |
615 var m1 = new Mock.custom(name: 'm1', log: log); | |
616 var m2 = new Mock.custom(name: 'm2', log: log); | |
617 var m3 = new Mock.custom(name: 'm3', log: log); | |
618 for (var i = 0; i < 3; i++) { | |
619 m1.foo(); | |
620 m2.bar(); | |
621 m3.pow(); | |
622 } | |
623 expect(log.logs, hasLength(9)); | |
624 m1.clearLogs(); | |
625 expect(log.logs, hasLength(6)); | |
626 m1.clearLogs(); | |
627 expect(log.logs, hasLength(6)); | |
628 expect(log.logs.every((e) => e.mockName == 'm2' || e.mockName == 'm3'), | |
629 isTrue); | |
630 m2.clearLogs(); | |
631 expect(log.logs, hasLength(3)); | |
632 expect(log.logs.every((e) => e.mockName =='m3'), isTrue); | |
633 m3.clearLogs(); | |
634 expect(log.logs, hasLength(0)); | |
635 }); | |
636 } | |
OLD | NEW |