| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 * A library for writing dart unit tests. | 6 * A library for writing dart unit tests. |
| 7 * | 7 * |
| 8 * To import this library, use the pub package manager. | 8 * To import this library, use the pub package manager. |
| 9 * Create a pubspec.yaml file in your project and add | 9 * Create a pubspec.yaml file in your project and add |
| 10 * a dependency on unittest with the following lines: | 10 * a dependency on unittest with the following lines: |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 complete = true; | 383 complete = true; |
| 384 if (--testCase.callbackFunctionsOutstanding == 0 && | 384 if (--testCase.callbackFunctionsOutstanding == 0 && |
| 385 !testCase.isComplete) { | 385 !testCase.isComplete) { |
| 386 testCase.pass(); | 386 testCase.pass(); |
| 387 } | 387 } |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 | 390 |
| 391 invoke([arg0 = sentinel, arg1 = sentinel, arg2 = sentinel, | 391 invoke([arg0 = sentinel, arg1 = sentinel, arg2 = sentinel, |
| 392 arg3 = sentinel, arg4 = sentinel]) { | 392 arg3 = sentinel, arg4 = sentinel]) { |
| 393 return guardAsync(() { | 393 return _guardAsync(() { |
| 394 if (!shouldCallBack()) { | 394 if (!shouldCallBack()) { |
| 395 return; | 395 return; |
| 396 } else if (arg0 == sentinel) { | 396 } else if (arg0 == sentinel) { |
| 397 return callback(); | 397 return callback(); |
| 398 } else if (arg1 == sentinel) { | 398 } else if (arg1 == sentinel) { |
| 399 return callback(arg0); | 399 return callback(arg0); |
| 400 } else if (arg2 == sentinel) { | 400 } else if (arg2 == sentinel) { |
| 401 return callback(arg0, arg1); | 401 return callback(arg0, arg1); |
| 402 } else if (arg3 == sentinel) { | 402 } else if (arg3 == sentinel) { |
| 403 return callback(arg0, arg1, arg2); | 403 return callback(arg0, arg1, arg2); |
| 404 } else if (arg4 == sentinel) { | 404 } else if (arg4 == sentinel) { |
| 405 return callback(arg0, arg1, arg2, arg3); | 405 return callback(arg0, arg1, arg2, arg3); |
| 406 } else { | 406 } else { |
| 407 testCase.error( | 407 testCase.error( |
| 408 'unittest lib does not support callbacks with more than' | 408 'unittest lib does not support callbacks with more than' |
| 409 ' 4 arguments.', | 409 ' 4 arguments.', |
| 410 ''); | 410 ''); |
| 411 } | 411 } |
| 412 }, | 412 }, |
| 413 after, testNum); | 413 after, testNum); |
| 414 } | 414 } |
| 415 | 415 |
| 416 invoke0() { | 416 invoke0() { |
| 417 return guardAsync( | 417 return _guardAsync( |
| 418 () { | 418 () { |
| 419 if (shouldCallBack()) { | 419 if (shouldCallBack()) { |
| 420 return callback(); | 420 return callback(); |
| 421 } | 421 } |
| 422 }, | 422 }, |
| 423 after, testNum); | 423 after, testNum); |
| 424 } | 424 } |
| 425 | 425 |
| 426 invoke1(arg1) { | 426 invoke1(arg1) { |
| 427 return guardAsync( | 427 return _guardAsync( |
| 428 () { | 428 () { |
| 429 if (shouldCallBack()) { | 429 if (shouldCallBack()) { |
| 430 return callback(arg1); | 430 return callback(arg1); |
| 431 } | 431 } |
| 432 }, | 432 }, |
| 433 after, testNum); | 433 after, testNum); |
| 434 } | 434 } |
| 435 | 435 |
| 436 invoke2(arg1, arg2) { | 436 invoke2(arg1, arg2) { |
| 437 return guardAsync( | 437 return _guardAsync( |
| 438 () { | 438 () { |
| 439 if (shouldCallBack()) { | 439 if (shouldCallBack()) { |
| 440 return callback(arg1, arg2); | 440 return callback(arg1, arg2); |
| 441 } | 441 } |
| 442 }, | 442 }, |
| 443 after, testNum); | 443 after, testNum); |
| 444 } | 444 } |
| 445 } | 445 } |
| 446 | 446 |
| 447 /** | 447 /** |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 _testRunner(); | 716 _testRunner(); |
| 717 }); | 717 }); |
| 718 } | 718 } |
| 719 | 719 |
| 720 /** | 720 /** |
| 721 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is | 721 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is |
| 722 * passed to the corresponding test. | 722 * passed to the corresponding test. |
| 723 * | 723 * |
| 724 * The value returned by [tryBody] (if any) is returned by [guardAsync]. | 724 * The value returned by [tryBody] (if any) is returned by [guardAsync]. |
| 725 */ | 725 */ |
| 726 guardAsync(Function tryBody, [Function finallyBody, int testNum = -1]) { | 726 guardAsync(Function tryBody) { |
| 727 if (testNum < 0) testNum = _currentTest; | 727 return _guardAsync(tryBody, null, _currentTest); |
| 728 } |
| 729 |
| 730 _guardAsync(Function tryBody, Function finallyBody, int testNum) { |
| 731 assert(testNum >= 0); |
| 728 try { | 732 try { |
| 729 return tryBody(); | 733 return tryBody(); |
| 730 } catch (e, trace) { | 734 } catch (e, trace) { |
| 731 _registerException(testNum, e, trace); | 735 _registerException(testNum, e, trace); |
| 732 } finally { | 736 } finally { |
| 733 if (finallyBody != null) finallyBody(); | 737 if (finallyBody != null) finallyBody(); |
| 734 } | 738 } |
| 735 } | 739 } |
| 736 | 740 |
| 737 /** | 741 /** |
| (...skipping 21 matching lines...) Expand all Loading... |
| 759 * running. Tests will resume executing when such asynchronous test calls | 763 * running. Tests will resume executing when such asynchronous test calls |
| 760 * [done] or if it fails with an exception. | 764 * [done] or if it fails with an exception. |
| 761 */ | 765 */ |
| 762 _nextBatch() { | 766 _nextBatch() { |
| 763 while (true) { | 767 while (true) { |
| 764 if (_currentTest >= _tests.length) { | 768 if (_currentTest >= _tests.length) { |
| 765 _completeTests(); | 769 _completeTests(); |
| 766 break; | 770 break; |
| 767 } | 771 } |
| 768 final testCase = _tests[_currentTest]; | 772 final testCase = _tests[_currentTest]; |
| 769 var f = guardAsync(testCase.run, null, _currentTest); | 773 var f = _guardAsync(testCase.run, null, _currentTest); |
| 770 if (f != null) { | 774 if (f != null) { |
| 771 f.whenComplete(() { | 775 f.whenComplete(() { |
| 772 _nextTestCase(); // Schedule the next test. | 776 _nextTestCase(); // Schedule the next test. |
| 773 }); | 777 }); |
| 774 break; | 778 break; |
| 775 } | 779 } |
| 776 _currentTest++; | 780 _currentTest++; |
| 777 } | 781 } |
| 778 } | 782 } |
| 779 | 783 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 } | 859 } |
| 856 | 860 |
| 857 /** Enable a test by ID. */ | 861 /** Enable a test by ID. */ |
| 858 void enableTest(int testId) => _setTestEnabledState(testId, true); | 862 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 859 | 863 |
| 860 /** Disable a test by ID. */ | 864 /** Disable a test by ID. */ |
| 861 void disableTest(int testId) => _setTestEnabledState(testId, false); | 865 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 862 | 866 |
| 863 /** Signature for a test function. */ | 867 /** Signature for a test function. */ |
| 864 typedef dynamic TestFunction(); | 868 typedef dynamic TestFunction(); |
| OLD | NEW |