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

Side by Side Diff: tests/lib/unittest/unittest_test.dart

Issue 10689148: CallMatchers can now be null, or take null for a method name. This might be (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: 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
« no previous file with comments | « lib/unittest/mock.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
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // TODO(gram): 5 // TODO(gram):
6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g.
7 // insufficient callbacks, because the timeout is controlled externally 7 // insufficient callbacks, because the timeout is controlled externally
8 // (test.dart?), and we would need to use a shorter timeout for the inner tests 8 // (test.dart?), and we would need to use a shorter timeout for the inner tests
9 // so the outer timeout doesn't fire. So I removed all such tests. 9 // so the outer timeout doesn't fire. So I removed all such tests.
10 // I'd like to revisit this at some point. 10 // I'd like to revisit this at some point.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 test(testName, () { 132 test(testName, () {
133 var _callback; 133 var _callback;
134 _callback = expectAsyncUntil0(() { 134 _callback = expectAsyncUntil0(() {
135 if (++_testconfig.count < 10) { 135 if (++_testconfig.count < 10) {
136 _defer(_callback); 136 _defer(_callback);
137 } 137 }
138 }, 138 },
139 () => (_testconfig.count == 10)); 139 () => (_testconfig.count == 10));
140 _defer(_callback); 140 _defer(_callback);
141 }); 141 });
142 } else if (testName == 'mock test 1 (Mock)') { 142 } else if (testName.startsWith('mock test 1 ')) {
143 test(testName, () { 143 test(testName, () {
144 var m = new Mock(); 144 var m = new Mock();
145 print(m.length); 145 print(m.length);
146 m.getLogs(callsTo('get length')).verify(happenedOnce); 146 m.getLogs(callsTo('get length')).verify(happenedOnce);
147 147
148 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); 148 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B');
149 m.when(callsTo('foo', 1, 1)).thenReturn('C'); 149 m.when(callsTo('foo', 1, 1)).thenReturn('C');
150 m.when(callsTo('foo', 9, anything)).thenReturn('D'); 150 m.when(callsTo('foo', 9, anything)).thenReturn('D');
151 m.when(callsTo('bar', anything, anything)).thenReturn('E'); 151 m.when(callsTo('bar', anything, anything)).thenReturn('E');
152 m.when(callsTo('foobar')).thenReturn('F'); 152 m.when(callsTo('foobar')).thenReturn('F');
153 153
154 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' 154 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}'
155 '${m.bar(1,1)}${m.foo(1,2)}'; 155 '${m.bar(1,1)}${m.foo(1,2)}';
156 m.getLogs(callsTo('foo', anything, anything)). 156 m.getLogs(callsTo('foo', anything, anything)).
157 verify(happenedExactly(4)); 157 verify(happenedExactly(4));
158 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); 158 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3));
159 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); 159 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce);
160 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); 160 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2));
161 m.getLogs(callsTo('foobar')).verify(neverHappened); 161 m.getLogs(callsTo('foobar')).verify(neverHappened);
162 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); 162 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened);
163 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). 163 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))).
164 verify(happenedExactly(2)); 164 verify(happenedExactly(2));
165 expect(s, 'ACDEB'); 165 expect(s, 'ACDEB');
166 }); 166 });
167 } else if (testName == 'mock test 2 (MockList)') { 167 } else if (testName.startsWith('mock test 2 ')) {
168 test(testName, () { 168 test(testName, () {
169 var l = new MockList(); 169 var l = new MockList();
170 l.when(callsTo('length')).thenReturn(1); 170 l.when(callsTo('get length')).thenReturn(1);
171 l.when(callsTo('add', anything)).alwaysReturn(0); 171 l.when(callsTo('add', anything)).alwaysReturn(0);
172 l.add('foo'); 172 l.add('foo');
173 expect(l.length(), 1); 173 expect(l.length, 1);
174 174
175 var m = new MockList(); 175 var m = new MockList();
176 m.when(callsTo('add', anything)).alwaysReturn(0); 176 m.when(callsTo('add', anything)).alwaysReturn(0);
177 177
178 m.add('foo'); 178 m.add('foo');
179 m.add('bar'); 179 m.add('bar');
180 180
181 m.getLogs(callsTo('add')).verify(happenedExactly(2)); 181 m.getLogs(callsTo('add')).verify(happenedExactly(2));
182 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce); 182 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce);
183 }); 183 });
184 } else if (testName == 'mock test 3 (Spy)') { 184 } else if (testName.startsWith('mock test 3 ')) {
185 test(testName, () { 185 test(testName, () {
186 var p = new FooSpy(); 186 var p = new FooSpy();
187 p.sum(1, 2, 3); 187 p.sum(1, 2, 3);
188 p.getLogs(callsTo('sum')).verify(happenedOnce); 188 p.getLogs(callsTo('sum')).verify(happenedOnce);
189 p.sum(2, 2, 2); 189 p.sum(2, 2, 2);
190 p.getLogs(callsTo('sum')).verify(happenedExactly(2)); 190 p.getLogs(callsTo('sum')).verify(happenedExactly(2));
191 p.getLogs(callsTo('sum')).verify(sometimeReturned(6)); 191 p.getLogs(callsTo('sum')).verify(sometimeReturned(6));
192 p.getLogs(callsTo('sum')).verify(alwaysReturned(6)); 192 p.getLogs(callsTo('sum')).verify(alwaysReturned(6));
193 p.getLogs(callsTo('sum')).verify(neverReturned(5)); 193 p.getLogs(callsTo('sum')).verify(neverReturned(5));
194 p.sum(2, 2, 1); 194 p.sum(2, 2, 1);
195 p.getLogs(callsTo('sum')).verify(sometimeReturned(5)); 195 p.getLogs(callsTo('sum')).verify(sometimeReturned(5));
196 }); 196 });
197 } else if (testName == 'mock test 4 (Excess calls)') { 197 } else if (testName.startsWith('mock test 4 ')) {
198 test(testName, () { 198 test(testName, () {
199 var m = new Mock(); 199 var m = new Mock();
200 m.when(callsTo('foo')).alwaysReturn(null); 200 m.when(callsTo('foo')).alwaysReturn(null);
201 m.foo(); 201 m.foo();
202 m.foo(); 202 m.foo();
203 m.getLogs(callsTo('foo')).verify(happenedOnce); 203 m.getLogs(callsTo('foo')).verify(happenedOnce);
204 }); 204 });
205 } else if (testName == 'mock test 5 (No action)') { 205 } else if (testName.startsWith('mock test 5 ')) {
206 test(testName, () { 206 test(testName, () {
207 var m = new Mock(); 207 var m = new Mock();
208 m.when(callsTo('foo')).thenReturn(null); 208 m.when(callsTo('foo')).thenReturn(null);
209 m.foo(); 209 m.foo();
210 m.foo(); 210 m.foo();
211 }); 211 });
212 } else if (testName == 'mock test 6 (No matching return)') { 212 } else if (testName.startsWith('mock test 6 ')) {
213 test(testName, () { 213 test(testName, () {
214 var p = new FooSpy(); 214 var p = new FooSpy();
215 p.sum(1, 2, 3); 215 p.sum(1, 2, 3);
216 p.getLogs(callsTo('sum')).verify(sometimeReturned(0)); 216 p.getLogs(callsTo('sum')).verify(sometimeReturned(0));
217 }); 217 });
218 } else if (testName == 'mock test 7 (No behavior)') { 218 } else if (testName.startsWith('mock test 7 ')) {
219 test(testName, () { 219 test(testName, () {
220 var m = new Mock.custom(throwIfNoBehavior:true); 220 var m = new Mock.custom(throwIfNoBehavior:true);
221 m.when(callsTo('foo')).thenReturn(null); 221 m.when(callsTo('foo')).thenReturn(null);
222 m.foo(); 222 m.foo();
223 m.bar(); 223 m.bar();
224 }); 224 });
225 } else if (testName == 'mock test 8 (Shared log)') { 225 } else if (testName.startsWith('mock test 8 ')) {
226 test(testName, () { 226 test(testName, () {
227 var log = new LogEntryList(); 227 var log = new LogEntryList();
228 var m1 = new Mock.custom(name:'m1', log:log); 228 var m1 = new Mock.custom(name:'m1', log:log);
229 var m2 = new Mock.custom(name:'m2', log:log); 229 var m2 = new Mock.custom(name:'m2', log:log);
230 m1.foo(); 230 m1.foo();
231 m2.foo(); 231 m2.foo();
232 m1.bar(); 232 m1.bar();
233 m2.bar(); 233 m2.bar();
234 expect(log.logs.length, 4); 234 expect(log.logs.length, 4);
235 log.getMatches(null, callsTo('foo')).verify(happenedExactly(2)); 235 log.getMatches(null, callsTo('foo')).verify(happenedExactly(2));
236 log.getMatches('m1', callsTo('foo')).verify(happenedOnce); 236 log.getMatches('m1', callsTo('foo')).verify(happenedOnce);
237 log.getMatches('m1', callsTo('bar')).verify(happenedOnce); 237 log.getMatches('m1', callsTo('bar')).verify(happenedOnce);
238 m2.getLogs(callsTo('foo')).verify(happenedOnce); 238 m2.getLogs(callsTo('foo')).verify(happenedOnce);
239 m2.getLogs(callsTo('bar')).verify(happenedOnce); 239 m2.getLogs(callsTo('bar')).verify(happenedOnce);
240 }); 240 });
241 } else if (testName.startsWith('mock test 9 ')) {
242 test(testName, () {
243 var m = new Mock();
244 m.when(callsTo(null, 1)).alwaysReturn(2);
Siggi Cherem (dart-lang) 2012/07/10 23:38:28 another option is to pass a regex, rather than a n
245 m.when(callsTo(null, 2)).alwaysReturn(4);
246 expect(m.foo(1), 2);
247 expect(m.foo(2), 4);
248 expect(m.bar(1), 2);
249 expect(m.bar(2), 4);
250 m.getLogs(callsTo()).verify(happenedExactly(4));
251 m.getLogs(callsTo(null, 1)).verify(happenedExactly(2));
252 m.getLogs(callsTo(null, 2)).verify(happenedExactly(2));
253 m.getLogs(null, returning(1)).verify(neverHappened);
254 m.getLogs(null, returning(2)).verify(happenedExactly(2));
255 m.getLogs(null, returning(4)).verify(happenedExactly(2));
256 });
241 } 257 }
242 }); 258 });
243 } 259 }
244 260
245 void nextTest(int testNum) { 261 void nextTest(int testNum) {
246 SendPort sport = spawnFunction(runTest); 262 SendPort sport = spawnFunction(runTest);
247 sport.call(tests[testNum]).then((msg) { 263 sport.call(tests[testNum]).then((msg) {
248 actual.add(msg); 264 actual.add(msg);
249 if (actual.length == expected.length) { 265 if (actual.length == expected.length) {
250 for (var i = 0; i < tests.length; i++) { 266 for (var i = 0; i < tests.length; i++) {
(...skipping 17 matching lines...) Expand all
268 'correct callback test', 284 'correct callback test',
269 'excess callback test', 285 'excess callback test',
270 'completion test', 286 'completion test',
271 'mock test 1 (Mock)', 287 'mock test 1 (Mock)',
272 'mock test 2 (MockList)', 288 'mock test 2 (MockList)',
273 'mock test 3 (Spy)', 289 'mock test 3 (Spy)',
274 'mock test 4 (Excess calls)', 290 'mock test 4 (Excess calls)',
275 'mock test 5 (No action)', 291 'mock test 5 (No action)',
276 'mock test 6 (No matching return)', 292 'mock test 6 (No matching return)',
277 'mock test 7 (No behavior)', 293 'mock test 7 (No behavior)',
278 'mock test 8 (Shared log)' 294 'mock test 8 (Shared log)',
295 'mock test 9 (Null CallMatcher)'
279 ]; 296 ];
280 297
281 expected = [ 298 expected = [
282 buildStatusString(1, 0, 0, tests[0]), 299 buildStatusString(1, 0, 0, tests[0]),
283 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), 300 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'),
284 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), 301 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'),
285 buildStatusString(2, 0, 0, 'a a::a b b'), 302 buildStatusString(2, 0, 0, 'a a::a b b'),
286 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), 303 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'),
287 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), 304 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'),
288 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, 305 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0,
289 'setup', 'teardown'), 306 'setup', 'teardown'),
290 buildStatusString(1, 0, 0, tests[7], 1), 307 buildStatusString(1, 0, 0, tests[7], 1),
291 buildStatusString(0, 0, 1, tests[8], 1, 308 buildStatusString(0, 0, 1, tests[8], 1,
292 message: 'Callback called more times than expected (2 > 1)'), 309 message: 'Callback called more times than expected (2 > 1)'),
293 buildStatusString(1, 0, 0, tests[9], 10), 310 buildStatusString(1, 0, 0, tests[9], 10),
294 buildStatusString(1, 0, 0, tests[10]), 311 buildStatusString(1, 0, 0, tests[10]),
295 buildStatusString(1, 0, 0, tests[11]), 312 buildStatusString(1, 0, 0, tests[11]),
296 buildStatusString(1, 0, 0, tests[12]), 313 buildStatusString(1, 0, 0, tests[12]),
297 buildStatusString(0, 1, 0, tests[13], 314 buildStatusString(0, 1, 0, tests[13],
298 message: 'Expected foo() to be called 1 times but:' 315 message: 'Expected foo() to be called 1 times but:'
299 ' was called 2 times'), 316 ' was called 2 times'),
300 buildStatusString(0, 1, 0, tests[14], 317 buildStatusString(0, 1, 0, tests[14],
301 message: 'Caught Exception: No more actions for method foo'), 318 message: 'Caught Exception: No more actions for method foo'),
302 buildStatusString(0, 1, 0, tests[15], 319 buildStatusString(0, 1, 0, tests[15],
303 message: 'Expected sum() to sometimes return <0> but: never did'), 320 message: 'Expected sum() to sometimes return <0> but: never did'),
304 buildStatusString(0, 1, 0, tests[16], 321 buildStatusString(0, 1, 0, tests[16],
305 message: 'Caught Exception: No behavior specified for method bar'), 322 message: 'Caught Exception: No behavior specified for method bar'),
306 buildStatusString(1, 0, 0, tests[17]) 323 buildStatusString(1, 0, 0, tests[17]),
324 buildStatusString(1, 0, 0, tests[18])
307 ]; 325 ];
308 326
309 actual = []; 327 actual = [];
310 328
311 nextTest(0); 329 nextTest(0);
312 } 330 }
313 331
OLDNEW
« no previous file with comments | « lib/unittest/mock.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698