OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
6 * Support for writing Dart unit tests. | 6 * Support for writing Dart unit tests. |
7 * | 7 * |
8 * For information on installing and importing this library, see the | 8 * For information on installing and importing this library, see the |
9 * [unittest package on pub.dartlang.org] | 9 * [unittest package on pub.dartlang.org] |
10 * (http://pub.dartlang.org/packages/unittest). | 10 * (http://pub.dartlang.org/packages/unittest). |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 test(spec, body); | 306 test(spec, body); |
307 } finally { | 307 } finally { |
308 --_soloNestingLevel; | 308 --_soloNestingLevel; |
309 } | 309 } |
310 } | 310 } |
311 | 311 |
312 /** | 312 /** |
313 * Indicate that [callback] is expected to be called a [count] number of times | 313 * Indicate that [callback] is expected to be called a [count] number of times |
314 * (by default 1). The unittest framework will wait for the callback to run the | 314 * (by default 1). The unittest framework will wait for the callback to run the |
315 * specified [count] times before it continues with the following test. Using | 315 * specified [count] times before it continues with the following test. Using |
316 * [expectAsync0] will also ensure that errors that occur within [callback] are | 316 * [expectAsync] will also ensure that errors that occur within [callback] are |
317 * tracked and reported. [callback] should take 0 positional arguments (named | 317 * tracked and reported. [callback] should take 0 positional arguments (named |
318 * arguments are not supported). [id] can be used to provide more | 318 * arguments are not supported). [id] can be used to provide more |
319 * descriptive error messages if the callback is called more often than | 319 * descriptive error messages if the callback is called more often than |
320 * expected. [max] can be used to specify an upper bound on the number of | 320 * expected. [max] can be used to specify an upper bound on the number of |
321 * calls; if this is exceeded the test will fail (or be marked as in error if | 321 * calls; if this is exceeded the test will fail (or be marked as in error if |
322 * it was already complete). A value of 0 for [max] (the default) will set | 322 * it was already complete). A value of 0 for [max] (the default) will set |
323 * the upper bound to the same value as [count]; i.e. the callback should be | 323 * the upper bound to the same value as [count]; i.e. the callback should be |
324 * called exactly [count] times. A value of -1 for [max] will mean no upper | 324 * called exactly [count] times. A value of -1 for [max] will mean no upper |
325 * bound. | 325 * bound. |
326 */ | 326 */ |
327 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 327 Function expectAsync(Function callback, |
328 Function expectAsync0(Function callback, | |
329 {int count: 1, int max: 0, String id}) { | 328 {int count: 1, int max: 0, String id}) { |
330 return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; | 329 var minArgs = _minArgs(callback); |
330 | |
331 switch(minArgs) { | |
332 case 0: | |
333 return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; | |
334 case 1: | |
335 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; | |
336 case 2: | |
337 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; | |
338 default: | |
339 throw new StateError('Should never get here'); | |
Siggi Cherem (dart-lang)
2014/02/07 20:34:08
let's change the message to help users know why :)
kevmoo
2014/02/07 20:45:27
Done.
| |
340 } | |
331 } | 341 } |
332 | 342 |
333 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ | 343 /** |
334 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 344 * *Deprecated* |
345 * | |
346 * Use [expectAsync] instead. | |
347 */ | |
348 @deprecated | |
349 Function expectAsync0(Function callback, | |
350 {int count: 1, int max: 0, String id}) => | |
351 expectAsync(callback, count: count, max: max, id: id); | |
352 | |
353 /** | |
354 * *Deprecated* | |
355 * | |
356 * Use [expectAsync] instead. | |
357 */ | |
358 @deprecated | |
335 Function expectAsync1(Function callback, | 359 Function expectAsync1(Function callback, |
336 {int count: 1, int max: 0, String id}) { | 360 {int count: 1, int max: 0, String id}) => |
337 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; | 361 expectAsync(callback, count: count, max: max, id: id); |
338 } | |
339 | 362 |
340 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ | 363 /** |
341 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 364 * *Deprecated* |
365 * | |
366 * Use [expectAsync] instead. | |
367 */ | |
368 @deprecated | |
342 Function expectAsync2(Function callback, | 369 Function expectAsync2(Function callback, |
343 {int count: 1, int max: 0, String id}) { | 370 {int count: 1, int max: 0, String id}) => |
344 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; | 371 expectAsync(callback, count: count, max: max, id: id); |
345 } | |
346 | 372 |
347 /** | 373 /** |
348 * Indicate that [callback] is expected to be called until [isDone] returns | 374 * Indicate that [callback] is expected to be called until [isDone] returns |
349 * true. The unittest framework check [isDone] after each callback and only | 375 * true. The unittest framework check [isDone] after each callback and only |
350 * when it returns true will it continue with the following test. Using | 376 * when it returns true will it continue with the following test. Using |
351 * [expectAsyncUntil0] will also ensure that errors that occur within | 377 * [expectAsyncUntil] will also ensure that errors that occur within |
352 * [callback] are tracked and reported. [callback] should take 0 positional | 378 * [callback] are tracked and reported. [callback] should take 0 positional |
353 * arguments (named arguments are not supported). [id] can be used to | 379 * arguments (named arguments are not supported). [id] can be used to |
354 * identify the callback in error messages (for example if it is called | 380 * identify the callback in error messages (for example if it is called |
355 * after the test case is complete). | 381 * after the test case is complete). |
356 */ | 382 */ |
357 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 383 Function expectAsyncUntil(Function callback, Function isDone, {String id}) { |
358 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) { | 384 var minArgs = _minArgs(callback); |
359 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; | |
360 } | |
361 | 385 |
362 /** | 386 switch(minArgs) { |
363 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. | 387 case 0: |
364 */ | 388 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; |
365 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 389 case 1: |
366 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) { | 390 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; |
367 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; | 391 case 2: |
368 } | 392 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
369 | 393 default: |
370 /** | 394 throw new StateError('Should never get here'); |
Siggi Cherem (dart-lang)
2014/02/07 20:34:08
same here
kevmoo
2014/02/07 20:45:27
Done.
| |
371 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | 395 } |
372 */ | |
373 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | |
374 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { | |
375 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; | |
376 } | 396 } |
377 | 397 |
378 /** | 398 /** |
379 * *Deprecated* | 399 * *Deprecated* |
400 * | |
401 * Use [expectAsyncUntil] instead. | |
402 */ | |
403 @deprecated | |
404 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => | |
405 expectAsyncUntil(callback, isDone, id: id); | |
406 | |
407 /** | |
408 * *Deprecated* | |
409 * | |
410 * Use [expectAsyncUntil] instead. | |
411 */ | |
412 @deprecated | |
413 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) => | |
414 expectAsyncUntil(callback, isDone, id: id); | |
415 | |
416 /** | |
417 * *Deprecated* | |
418 * | |
419 * Use [expectAsyncUntil] instead. | |
420 */ | |
421 @deprecated | |
422 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) => | |
423 expectAsyncUntil(callback, isDone, id: id); | |
424 | |
425 /** | |
426 * *Deprecated* | |
380 * | 427 * |
381 * All tests are now run an isolated [Zone]. | 428 * All tests are now run an isolated [Zone]. |
382 * | 429 * |
383 * You can safely remove calls to this method. | 430 * You can safely remove calls to this method. |
384 */ | 431 */ |
385 @deprecated | 432 @deprecated |
386 Function protectAsync0(Function callback, {String id}) { | 433 Function protectAsync0(Function callback, {String id}) { |
387 return callback; | 434 return callback; |
388 } | 435 } |
389 | 436 |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 void ensureInitialized() { | 674 void ensureInitialized() { |
628 _ensureInitialized(true); | 675 _ensureInitialized(true); |
629 } | 676 } |
630 | 677 |
631 void _ensureInitialized(bool configAutoStart) { | 678 void _ensureInitialized(bool configAutoStart) { |
632 if (_initialized) { | 679 if (_initialized) { |
633 return; | 680 return; |
634 } | 681 } |
635 _initialized = true; | 682 _initialized = true; |
636 // Hook our async guard into the matcher library. | 683 // Hook our async guard into the matcher library. |
637 wrapAsync = (f, [id]) => expectAsync1(f, id: id); | 684 wrapAsync = (f, [id]) => expectAsync(f, id: id); |
638 | 685 |
639 _uncaughtErrorMessage = null; | 686 _uncaughtErrorMessage = null; |
640 | 687 |
641 unittestConfiguration.onInit(); | 688 unittestConfiguration.onInit(); |
642 | 689 |
643 if (configAutoStart && _config.autoStart) { | 690 if (configAutoStart && _config.autoStart) { |
644 // Immediately queue the suite up. It will run after a timeout (i.e. after | 691 // Immediately queue the suite up. It will run after a timeout (i.e. after |
645 // main() has returned). | 692 // main() has returned). |
646 scheduleMicrotask(runTests); | 693 scheduleMicrotask(runTests); |
647 } | 694 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 | 759 |
713 if (!filterStacks) return trace; | 760 if (!filterStacks) return trace; |
714 | 761 |
715 // Format the stack trace by removing everything above TestCase._runTest, | 762 // Format the stack trace by removing everything above TestCase._runTest, |
716 // which is usually going to be irrelevant. Also fold together unittest and | 763 // which is usually going to be irrelevant. Also fold together unittest and |
717 // core library calls so only the function the user called is visible. | 764 // core library calls so only the function the user called is visible. |
718 return new Trace(trace.frames.takeWhile((frame) { | 765 return new Trace(trace.frames.takeWhile((frame) { |
719 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 766 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
720 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 767 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
721 } | 768 } |
769 | |
770 typedef _Func0(); | |
771 typedef _Func1(a); | |
772 typedef _Func2(a, b); | |
773 | |
774 /** | |
775 * Throws an [ArgumentError] if the callback has more than 2 required arguments. | |
776 */ | |
777 int _minArgs(Function callback) { | |
778 if (callback is _Func0) return 0; | |
779 if (callback is _Func1) return 1; | |
780 if (callback is _Func2) return 2; | |
781 throw new ArgumentError( | |
782 'The callback argument has more than 2 required arguments.'); | |
Siggi Cherem (dart-lang)
2014/02/07 20:34:08
ah - I see, then you can keep the other state erro
kevmoo
2014/02/07 20:45:27
Done.
| |
783 } | |
OLD | NEW |