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