| 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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 _SpreadArgsHelper.variableCallCount(callback, isDone) { | 370 _SpreadArgsHelper.variableCallCount(callback, isDone) { |
| 371 _init(callback, _always, isDone, 1); | 371 _init(callback, _always, isDone, 1); |
| 372 } | 372 } |
| 373 | 373 |
| 374 _SpreadArgsHelper.optionalCalls(callback) { | 374 _SpreadArgsHelper.optionalCalls(callback) { |
| 375 _init(callback, _always, () => false, 0); | 375 _init(callback, _always, () => false, 0); |
| 376 } | 376 } |
| 377 | 377 |
| 378 _after() { | 378 _after() { |
| 379 if (_isDone()) { | 379 if (_isDone()) { |
| 380 _handleCallbackFunctionComplete(); | 380 _handleCallbackFunctionComplete(_testNum); |
| 381 } | 381 } |
| 382 } | 382 } |
| 383 | 383 |
| 384 _allCallsDone() => _actualCalls == _expectedCalls; | 384 _allCallsDone() => _actualCalls == _expectedCalls; |
| 385 | 385 |
| 386 _always() { | 386 _always() { |
| 387 // Always run except if the test is done. | 387 // Always run except if the test is done. |
| 388 if (_testCase.isComplete) { | 388 if (_testCase.isComplete) { |
| 389 _testCase.error( | 389 _testCase.error( |
| 390 'Callback called after already being marked as done ($_actualCalls).', | 390 'Callback called after already being marked as done ($_actualCalls).', |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 * calls to [test]. | 639 * calls to [test]. |
| 640 */ | 640 */ |
| 641 void tearDown(Function teardownTest) { | 641 void tearDown(Function teardownTest) { |
| 642 _testTeardown = teardownTest; | 642 _testTeardown = teardownTest; |
| 643 } | 643 } |
| 644 | 644 |
| 645 /** | 645 /** |
| 646 * Called when one of the callback functions is done with all expected | 646 * Called when one of the callback functions is done with all expected |
| 647 * calls. | 647 * calls. |
| 648 */ | 648 */ |
| 649 void _handleCallbackFunctionComplete() { | 649 void _handleCallbackFunctionComplete(testNum) { |
| 650 // TODO (gram): we defer this to give the nextBatch recursive | 650 // TODO (gram): we defer this to give the nextBatch recursive |
| 651 // stack a chance to unwind. This is a temporary hack but | 651 // stack a chance to unwind. This is a temporary hack but |
| 652 // really a bunch of code here needs to be fixed. We have a | 652 // really a bunch of code here needs to be fixed. We have a |
| 653 // single array that is being iterated through by nextBatch(), | 653 // single array that is being iterated through by nextBatch(), |
| 654 // which is recursively invoked in the case of async tests that | 654 // which is recursively invoked in the case of async tests that |
| 655 // run synchronously. Bad things can then happen. | 655 // run synchronously. Bad things can then happen. |
| 656 _defer(() { | 656 _defer(() { |
| 657 if (_currentTest != testNum) { |
| 658 if (_tests[testNum].result == PASS) { |
| 659 _tests[testNum].error("Unexpected extra callbacks"); |
| 660 } |
| 661 return; // Extraneous callback. |
| 662 } |
| 657 if (_currentTest < _tests.length) { | 663 if (_currentTest < _tests.length) { |
| 658 final testCase = _tests[_currentTest]; | 664 final testCase = _tests[_currentTest]; |
| 659 --testCase.callbackFunctionsOutstanding; | 665 --testCase.callbackFunctionsOutstanding; |
| 660 if (testCase.callbackFunctionsOutstanding < 0) { | 666 if (testCase.callbackFunctionsOutstanding < 0) { |
| 661 // TODO(gram): Check: Can this even happen? | 667 // TODO(gram): Check: Can this even happen? |
| 662 testCase.error( | 668 testCase.error( |
| 663 'More calls to _handleCallbackFunctionComplete() than expected.', | 669 'More calls to _handleCallbackFunctionComplete() than expected.', |
| 664 ''); | 670 ''); |
| 665 } else if (testCase.callbackFunctionsOutstanding == 0) { | 671 } else if (testCase.callbackFunctionsOutstanding == 0) { |
| 666 if (!testCase.isComplete) { | 672 if (!testCase.isComplete) { |
| 667 testCase.pass(); | 673 testCase.pass(); |
| 668 } | 674 } |
| 669 _nextTestCase(); | 675 _nextTestCase(); |
| 670 } | 676 } |
| 671 } | 677 } |
| 672 }); | 678 }); |
| 673 } | 679 } |
| 674 | 680 |
| 675 /** Advance to the next test case. */ | 681 /** Advance to the next test case. */ |
| 676 void _nextTestCase() { | 682 void _nextTestCase() { |
| 677 _currentTest++; | 683 _currentTest++; |
| 678 _testRunner(); | 684 _testRunner(); |
| 679 } | 685 } |
| 680 | 686 |
| 681 /** | 687 /** |
| 682 * Temporary hack: expose old API. | 688 * Temporary hack: expose old API. |
| 683 * TODO(gram) remove this when WebKit tests are working with new framework | 689 * TODO(gram) remove this when WebKit tests are working with new framework |
| 684 */ | 690 */ |
| 685 void callbackDone() { | 691 void callbackDone() { |
| 686 _handleCallbackFunctionComplete(); | 692 _handleCallbackFunctionComplete(_currentTest); |
| 687 } | 693 } |
| 688 | 694 |
| 689 /** | 695 /** |
| 690 * Utility function that can be used to notify the test framework that an | 696 * Utility function that can be used to notify the test framework that an |
| 691 * error was caught outside of this library. | 697 * error was caught outside of this library. |
| 692 */ | 698 */ |
| 693 void _reportTestError(String msg, String trace) { | 699 void _reportTestError(String msg, String trace) { |
| 694 if (_currentTest < _tests.length) { | 700 if (_currentTest < _tests.length) { |
| 695 final testCase = _tests[_currentTest]; | 701 final testCase = _tests[_currentTest]; |
| 696 testCase.error(msg, trace); | 702 testCase.error(msg, trace); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 } | 903 } |
| 898 | 904 |
| 899 /** Enable a test by ID. */ | 905 /** Enable a test by ID. */ |
| 900 void enableTest(int testId) => _setTestEnabledState(testId, true); | 906 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 901 | 907 |
| 902 /** Disable a test by ID. */ | 908 /** Disable a test by ID. */ |
| 903 void disableTest(int testId) => _setTestEnabledState(testId, false); | 909 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 904 | 910 |
| 905 /** Signature for a test function. */ | 911 /** Signature for a test function. */ |
| 906 typedef void TestFunction(); | 912 typedef void TestFunction(); |
| OLD | NEW |