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