| 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 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return; | 441 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return; |
| 442 if (isDone != null && !isDone()) return; | 442 if (isDone != null && !isDone()) return; |
| 443 | 443 |
| 444 // Mark this callback as complete and remove it from the testcase | 444 // Mark this callback as complete and remove it from the testcase |
| 445 // oustanding callback count; if that hits zero the testcase is done. | 445 // oustanding callback count; if that hits zero the testcase is done. |
| 446 complete = true; | 446 complete = true; |
| 447 testCase._markCallbackComplete(); | 447 testCase._markCallbackComplete(); |
| 448 } | 448 } |
| 449 } | 449 } |
| 450 | 450 |
| 451 /** | 451 invoke0() { |
| 452 * Using [noSuchMethod] to handle the invocation of [call]. | |
| 453 * | |
| 454 * This allows direct access to the arguments via [Invocation], which are | |
| 455 * passed to the original callback. | |
| 456 */ | |
| 457 dynamic noSuchMethod(Invocation invocation) { | |
| 458 if (invocation.memberName != #call) return super.noSuchMethod(invocation); | |
| 459 | |
| 460 return _guardAsync( | 452 return _guardAsync( |
| 461 () { | 453 () { |
| 462 if (shouldCallBack()) { | 454 if (shouldCallBack()) { |
| 463 return Function.apply(callback, invocation.positionalArguments, | 455 return callback(); |
| 464 invocation.namedArguments); | |
| 465 } | 456 } |
| 466 }, | 457 }, |
| 467 after, testCase); | 458 after, testCase); |
| 468 } | 459 } |
| 469 | 460 |
| 470 /** | 461 invoke1(arg1) { |
| 471 * Eliminates type warnings since this class does not directly expose the | 462 return _guardAsync( |
| 472 * [call] method -- causing compliants that it is not a valid [Function]. | 463 () { |
| 473 */ | 464 if (shouldCallBack()) { |
| 474 // TODO(kevmoo): consider implementing Function and flagging class with @proxy | 465 return callback(arg1); |
| 475 Function get asFunction => (this as dynamic); | 466 } |
| 467 }, |
| 468 after, testCase); |
| 469 } |
| 470 |
| 471 invoke2(arg1, arg2) { |
| 472 return _guardAsync( |
| 473 () { |
| 474 if (shouldCallBack()) { |
| 475 return callback(arg1, arg2); |
| 476 } |
| 477 }, |
| 478 after, testCase); |
| 479 } |
| 476 } | 480 } |
| 477 | 481 |
| 478 /** | 482 /** |
| 479 * Indicate that [callback] is expected to be called a [count] number of times | 483 * Indicate that [callback] is expected to be called a [count] number of times |
| 480 * (by default 1). | 484 * (by default 1). The unittest framework will wait for the callback to run the |
| 481 * | |
| 482 * The unittest framework will wait for the callback to run the | |
| 483 * specified [count] times before it continues with the following test. Using | 485 * specified [count] times before it continues with the following test. Using |
| 484 * [expectAsync] will also ensure that errors that occur within [callback] are | 486 * [expectAsync0] will also ensure that errors that occur within [callback] are |
| 485 * tracked and reported. | 487 * tracked and reported. [callback] should take 0 positional arguments (named |
| 486 * | 488 * arguments are not supported). [id] can be used to provide more |
| 487 * [id] can be used to provide more descriptive error messages if the callback | 489 * descriptive error messages if the callback is called more often than |
| 488 * is called more often than expected. | 490 * expected. [max] can be used to specify an upper bound on the number of |
| 489 * | 491 * calls; if this is exceeded the test will fail (or be marked as in error if |
| 490 * [max] can be used to specify an upper bound on the number of calls; if this | 492 * it was already complete). A value of 0 for [max] (the default) will set |
| 491 * is exceeded the test will fail (or be marked as in error if it was already | 493 * the upper bound to the same value as [count]; i.e. the callback should be |
| 492 * complete). A value of 0 for [max] (the default) will set the upper bound to | 494 * called exactly [count] times. A value of -1 for [max] will mean no upper |
| 493 * the same value as [count]; i.e. the callback should be called exactly [count] | 495 * bound. |
| 494 * times. A value of -1 for [max] will mean no upper bound. | |
| 495 */ | 496 */ |
| 496 Function expectAsync(Function callback, | 497 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 497 {int count: 1, int max: 0, String id}) => | 498 Function expectAsync0(Function callback, |
| 498 new _SpreadArgsHelper(callback, count, max, null, id).asFunction; | 499 {int count: 1, int max: 0, String id}) { |
| 500 return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; |
| 501 } |
| 499 | 502 |
| 500 /** | 503 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ |
| 501 * *DEPRECATED*: use [expectAsync] instead. | 504 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 502 **/ | 505 Function expectAsync1(Function callback, |
| 503 @deprecated | 506 {int count: 1, int max: 0, String id}) { |
| 504 Function expectAsync0(Function callback, | 507 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; |
| 505 {int count: 1, int max: 0, String id}) => | 508 } |
| 506 expectAsync(callback, count: count, max: max, id: id); | |
| 507 | 509 |
| 508 /** | 510 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ |
| 509 * *DEPRECATED*: use [expectAsync] instead. | 511 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 510 **/ | |
| 511 @deprecated | |
| 512 Function expectAsync1(Function callback, | |
| 513 {int count: 1, int max: 0, String id}) => | |
| 514 expectAsync(callback, count: count, max: max, id: id); | |
| 515 | |
| 516 /** | |
| 517 * *DEPRECATED*: use [expectAsync] instead. | |
| 518 **/ | |
| 519 @deprecated | |
| 520 Function expectAsync2(Function callback, | 512 Function expectAsync2(Function callback, |
| 521 {int count: 1, int max: 0, String id}) => | 513 {int count: 1, int max: 0, String id}) { |
| 522 expectAsync(callback, count: count, max: max, id: id); | 514 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; |
| 515 } |
| 523 | 516 |
| 524 /** | 517 /** |
| 525 * Indicate that [callback] is expected to be called until [isDone] returns | 518 * Indicate that [callback] is expected to be called until [isDone] returns |
| 526 * true. The unittest framework check [isDone] after each callback and only | 519 * true. The unittest framework check [isDone] after each callback and only |
| 527 * when it returns true will it continue with the following test. | 520 * when it returns true will it continue with the following test. Using |
| 528 * | 521 * [expectAsyncUntil0] will also ensure that errors that occur within |
| 529 * Using [expectAsyncUntil] will also ensure that errors that occur within | 522 * [callback] are tracked and reported. [callback] should take 0 positional |
| 530 * [callback] are tracked and reported. | 523 * arguments (named arguments are not supported). [id] can be used to |
| 531 * | 524 * identify the callback in error messages (for example if it is called |
| 532 * [id] can be used to identify the callback in error messages (for example if | 525 * after the test case is complete). |
| 533 * it is called after the test case is complete). | |
| 534 */ | 526 */ |
| 535 Function expectAsyncUntil(Function callback, Function isDone, {String id}) => | 527 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 536 new _SpreadArgsHelper(callback, 0, -1, isDone, id).asFunction; | 528 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) { |
| 529 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; |
| 530 } |
| 537 | 531 |
| 538 /** | 532 /** |
| 539 * *DEPRECATED*: Use [expectAsyncUntil] instead. | 533 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. |
| 540 */ | 534 */ |
| 541 @deprecated | 535 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 542 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) => | 536 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) { |
| 543 expectAsyncUntil(callback, isDone, id: id); | 537 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; |
| 538 } |
| 544 | 539 |
| 545 /** | 540 /** |
| 546 * *DEPRECATED*: Use [expectAsyncUntil] instead. | 541 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. |
| 547 */ | 542 */ |
| 548 @deprecated | 543 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 549 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) => | 544 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { |
| 550 expectAsyncUntil(callback, isDone, id: id); | 545 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
| 551 | 546 } |
| 552 /** | |
| 553 * *DEPRECATED*: Use [expectAsyncUntil] instead. | |
| 554 */ | |
| 555 @deprecated | |
| 556 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) => | |
| 557 expectAsyncUntil(callback, isDone, id: id); | |
| 558 | 547 |
| 559 /** | 548 /** |
| 560 * Wraps the [callback] in a new function and returns that function. The new | 549 * Wraps the [callback] in a new function and returns that function. The new |
| 561 * function will be able to handle exceptions by directing them to the correct | 550 * function will be able to handle exceptions by directing them to the correct |
| 562 * test. This is thus similar to [expectAsync]. Use it to wrap any callbacks | 551 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that |
| 563 * that might optionally be called but may never be called during the test. | 552 * might optionally be called but may never be called during the test. |
| 564 * | 553 * [callback] should take 0 positional arguments (named arguments are not |
| 565 * [id] can be used to identify the callback in error messages (for example if | 554 * supported). [id] can be used to identify the callback in error |
| 566 * it is called after the test case is complete). | 555 * messages (for example if it is called after the test case is complete). |
| 567 */ | 556 */ |
| 568 Function protectAsync(Function callback, {String id}) => | 557 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 569 new _SpreadArgsHelper(callback, 0, -1, null, id).asFunction; | 558 Function protectAsync0(Function callback, {String id}) { |
| 559 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0; |
| 560 } |
| 570 | 561 |
| 571 /** | 562 /** |
| 572 * *DEPRECATED*: use [protectAsync] instead. | 563 * Like [protectAsync0] but [callback] should take 1 positional argument. |
| 573 **/ | 564 */ |
| 574 @deprecated | 565 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 575 Function protectAsync0(Function callback, {String id}) => | 566 Function protectAsync1(Function callback, {String id}) { |
| 576 protectAsync(callback, id: id); | 567 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke1; |
| 568 } |
| 577 | 569 |
| 578 /** | 570 /** |
| 579 * *DEPRECATED*: use [protectAsync] instead. | 571 * Like [protectAsync0] but [callback] should take 2 positional arguments. |
| 580 **/ | 572 */ |
| 581 @deprecated | 573 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 582 Function protectAsync1(Function callback, {String id}) => | 574 Function protectAsync2(Function callback, {String id}) { |
| 583 protectAsync(callback, id: id); | 575 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke2; |
| 584 | 576 } |
| 585 /** | |
| 586 * *DEPRECATED*: use [protectAsync] instead. | |
| 587 **/ | |
| 588 @deprecated | |
| 589 Function protectAsync2(Function callback, {String id}) => | |
| 590 protectAsync(callback, id: id); | |
| 591 | 577 |
| 592 /** | 578 /** |
| 593 * Creates a new named group of tests. Calls to group() or test() within the | 579 * Creates a new named group of tests. Calls to group() or test() within the |
| 594 * body of the function passed to this will inherit this group's description. | 580 * body of the function passed to this will inherit this group's description. |
| 595 */ | 581 */ |
| 596 void group(String description, void body()) { | 582 void group(String description, void body()) { |
| 597 ensureInitialized(); | 583 ensureInitialized(); |
| 598 _currentContext = new _GroupContext(_currentContext, description); | 584 _currentContext = new _GroupContext(_currentContext, description); |
| 599 try { | 585 try { |
| 600 body(); | 586 body(); |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 | 872 |
| 887 if (!filterStacks) return trace; | 873 if (!filterStacks) return trace; |
| 888 | 874 |
| 889 // Format the stack trace by removing everything above TestCase._runTest, | 875 // Format the stack trace by removing everything above TestCase._runTest, |
| 890 // which is usually going to be irrelevant. Also fold together unittest and | 876 // which is usually going to be irrelevant. Also fold together unittest and |
| 891 // core library calls so only the function the user called is visible. | 877 // core library calls so only the function the user called is visible. |
| 892 return new Trace(trace.frames.takeWhile((frame) { | 878 return new Trace(trace.frames.takeWhile((frame) { |
| 893 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 879 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
| 894 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 880 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
| 895 } | 881 } |
| OLD | NEW |