| 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 // _minArgs throws an argument exception if the arg count is > 2. |
| 340 // this is just for paranoia |
| 341 throw new StateError('Should never get here'); |
| 342 } |
| 331 } | 343 } |
| 332 | 344 |
| 333 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ | 345 /** |
| 334 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 346 * *Deprecated* |
| 347 * |
| 348 * Use [expectAsync] instead. |
| 349 */ |
| 350 @deprecated |
| 351 Function expectAsync0(Function callback, |
| 352 {int count: 1, int max: 0, String id}) => |
| 353 expectAsync(callback, count: count, max: max, id: id); |
| 354 |
| 355 /** |
| 356 * *Deprecated* |
| 357 * |
| 358 * Use [expectAsync] instead. |
| 359 */ |
| 360 @deprecated |
| 335 Function expectAsync1(Function callback, | 361 Function expectAsync1(Function callback, |
| 336 {int count: 1, int max: 0, String id}) { | 362 {int count: 1, int max: 0, String id}) => |
| 337 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; | 363 expectAsync(callback, count: count, max: max, id: id); |
| 338 } | |
| 339 | 364 |
| 340 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ | 365 /** |
| 341 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 366 * *Deprecated* |
| 367 * |
| 368 * Use [expectAsync] instead. |
| 369 */ |
| 370 @deprecated |
| 342 Function expectAsync2(Function callback, | 371 Function expectAsync2(Function callback, |
| 343 {int count: 1, int max: 0, String id}) { | 372 {int count: 1, int max: 0, String id}) => |
| 344 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; | 373 expectAsync(callback, count: count, max: max, id: id); |
| 345 } | |
| 346 | 374 |
| 347 /** | 375 /** |
| 348 * Indicate that [callback] is expected to be called until [isDone] returns | 376 * Indicate that [callback] is expected to be called until [isDone] returns |
| 349 * true. The unittest framework check [isDone] after each callback and only | 377 * true. The unittest framework check [isDone] after each callback and only |
| 350 * when it returns true will it continue with the following test. Using | 378 * when it returns true will it continue with the following test. Using |
| 351 * [expectAsyncUntil0] will also ensure that errors that occur within | 379 * [expectAsyncUntil] will also ensure that errors that occur within |
| 352 * [callback] are tracked and reported. [callback] should take 0 positional | 380 * [callback] are tracked and reported. [callback] should take 0 positional |
| 353 * arguments (named arguments are not supported). [id] can be used to | 381 * arguments (named arguments are not supported). [id] can be used to |
| 354 * identify the callback in error messages (for example if it is called | 382 * identify the callback in error messages (for example if it is called |
| 355 * after the test case is complete). | 383 * after the test case is complete). |
| 356 */ | 384 */ |
| 357 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 385 Function expectAsyncUntil(Function callback, Function isDone, {String id}) { |
| 358 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) { | 386 var minArgs = _minArgs(callback); |
| 359 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; | |
| 360 } | |
| 361 | 387 |
| 362 /** | 388 switch(minArgs) { |
| 363 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. | 389 case 0: |
| 364 */ | 390 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; |
| 365 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 391 case 1: |
| 366 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) { | 392 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; |
| 367 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; | 393 case 2: |
| 368 } | 394 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
| 369 | 395 default: |
| 370 /** | 396 // _minArgs throws an argument exception if the arg count is > 2. |
| 371 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | 397 // this is just for paranoia |
| 372 */ | 398 throw new StateError('Should never get here'); |
| 373 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 399 } |
| 374 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { | |
| 375 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; | |
| 376 } | 400 } |
| 377 | 401 |
| 378 /** | 402 /** |
| 379 * *Deprecated* | 403 * *Deprecated* |
| 404 * |
| 405 * Use [expectAsyncUntil] instead. |
| 406 */ |
| 407 @deprecated |
| 408 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => |
| 409 expectAsyncUntil(callback, isDone, id: id); |
| 410 |
| 411 /** |
| 412 * *Deprecated* |
| 413 * |
| 414 * Use [expectAsyncUntil] instead. |
| 415 */ |
| 416 @deprecated |
| 417 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) => |
| 418 expectAsyncUntil(callback, isDone, id: id); |
| 419 |
| 420 /** |
| 421 * *Deprecated* |
| 422 * |
| 423 * Use [expectAsyncUntil] instead. |
| 424 */ |
| 425 @deprecated |
| 426 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) => |
| 427 expectAsyncUntil(callback, isDone, id: id); |
| 428 |
| 429 /** |
| 430 * *Deprecated* |
| 380 * | 431 * |
| 381 * All tests are now run an isolated [Zone]. | 432 * All tests are now run an isolated [Zone]. |
| 382 * | 433 * |
| 383 * You can safely remove calls to this method. | 434 * You can safely remove calls to this method. |
| 384 */ | 435 */ |
| 385 @deprecated | 436 @deprecated |
| 386 Function protectAsync0(Function callback, {String id}) { | 437 Function protectAsync0(Function callback, {String id}) { |
| 387 return callback; | 438 return callback; |
| 388 } | 439 } |
| 389 | 440 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 void ensureInitialized() { | 678 void ensureInitialized() { |
| 628 _ensureInitialized(true); | 679 _ensureInitialized(true); |
| 629 } | 680 } |
| 630 | 681 |
| 631 void _ensureInitialized(bool configAutoStart) { | 682 void _ensureInitialized(bool configAutoStart) { |
| 632 if (_initialized) { | 683 if (_initialized) { |
| 633 return; | 684 return; |
| 634 } | 685 } |
| 635 _initialized = true; | 686 _initialized = true; |
| 636 // Hook our async guard into the matcher library. | 687 // Hook our async guard into the matcher library. |
| 637 wrapAsync = (f, [id]) => expectAsync1(f, id: id); | 688 wrapAsync = (f, [id]) => expectAsync(f, id: id); |
| 638 | 689 |
| 639 _uncaughtErrorMessage = null; | 690 _uncaughtErrorMessage = null; |
| 640 | 691 |
| 641 unittestConfiguration.onInit(); | 692 unittestConfiguration.onInit(); |
| 642 | 693 |
| 643 if (configAutoStart && _config.autoStart) { | 694 if (configAutoStart && _config.autoStart) { |
| 644 // Immediately queue the suite up. It will run after a timeout (i.e. after | 695 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 645 // main() has returned). | 696 // main() has returned). |
| 646 scheduleMicrotask(runTests); | 697 scheduleMicrotask(runTests); |
| 647 } | 698 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 | 763 |
| 713 if (!filterStacks) return trace; | 764 if (!filterStacks) return trace; |
| 714 | 765 |
| 715 // Format the stack trace by removing everything above TestCase._runTest, | 766 // Format the stack trace by removing everything above TestCase._runTest, |
| 716 // which is usually going to be irrelevant. Also fold together unittest and | 767 // 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. | 768 // core library calls so only the function the user called is visible. |
| 718 return new Trace(trace.frames.takeWhile((frame) { | 769 return new Trace(trace.frames.takeWhile((frame) { |
| 719 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 770 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
| 720 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 771 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
| 721 } | 772 } |
| 773 |
| 774 typedef _Func0(); |
| 775 typedef _Func1(a); |
| 776 typedef _Func2(a, b); |
| 777 |
| 778 /** |
| 779 * Throws an [ArgumentError] if the callback has more than 2 required arguments. |
| 780 */ |
| 781 int _minArgs(Function callback) { |
| 782 if (callback is _Func0) return 0; |
| 783 if (callback is _Func1) return 1; |
| 784 if (callback is _Func2) return 2; |
| 785 throw new ArgumentError( |
| 786 'The callback argument has more than 2 required arguments.'); |
| 787 } |
| OLD | NEW |