Chromium Code Reviews| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 * } | 106 * } |
| 107 * | 107 * |
| 108 * expectAsyncX() will wrap the callback code in a try/catch handler to handle | 108 * expectAsyncX() will wrap the callback code in a try/catch handler to handle |
| 109 * exceptions (treated as test failures). There may be times when the number of | 109 * exceptions (treated as test failures). There may be times when the number of |
| 110 * times a callback should be called is non-deterministic. In this case a dummy | 110 * times a callback should be called is non-deterministic. In this case a dummy |
| 111 * callback can be created with expectAsync0((){}) and this can be called from | 111 * callback can be created with expectAsync0((){}) and this can be called from |
| 112 * the real callback when it is finally complete. In this case the body of the | 112 * the real callback when it is finally complete. In this case the body of the |
| 113 * callback should be protected within a call to guardAsync(); this will ensure | 113 * callback should be protected within a call to guardAsync(); this will ensure |
| 114 * that exceptions are properly handled. | 114 * that exceptions are properly handled. |
| 115 * | 115 * |
| 116 * A variation on this is expectAsyncUntilX(); this takes a callback as the firs t | |
|
Siggi Cherem (dart-lang)
2013/02/26 00:25:29
80 col
gram
2013/03/04 19:51:52
Done.
| |
| 117 * parameter and a predicate function as the second parameter; after each time | |
| 118 * the callback is called, the predicate function will be called; if it returns | |
| 119 * false the test will still be considered incomplete. | |
| 120 * | |
| 121 * Test functions can return [Future]s, which provide another way of doing | |
| 122 * asynchronous tests. The test framework will handle exceptions thrown by | |
| 123 * the Future, and will advance to the next test when the Future is complete. | |
| 124 * It is still important to use expectAsync/guardAsync with any parts of the | |
| 125 * test that may be invoked via an [Isolate] (for example, with Timer.run()], | |
|
Siggi Cherem (dart-lang)
2013/02/26 00:25:29
rephrase: 'via an [Isolate]' sounds strange.
In t
gram
2013/03/04 19:51:52
Done.
| |
| 126 * as the Future exception handler will not capture exceptions in such code. | |
| 127 * | |
| 116 * Note: due to some language limitations we have to use different functions | 128 * Note: due to some language limitations we have to use different functions |
| 117 * depending on the number of positional arguments of the callback. In the | 129 * depending on the number of positional arguments of the callback. In the |
| 118 * future, we plan to expose a single `expectAsync` function that can be used | 130 * future, we plan to expose a single `expectAsync` function that can be used |
| 119 * regardless of the number of positional arguments. This requires new langauge | 131 * regardless of the number of positional arguments. This requires new langauge |
| 120 * features or fixes to the current spec (e.g. see | 132 * features or fixes to the current spec (e.g. see |
| 121 * [Issue 2706](http://dartbug.com/2706)). | 133 * [Issue 2706](http://dartbug.com/2706)). |
| 122 * | 134 * |
| 123 * Meanwhile, we plan to add this alternative API for callbacks of more than 2 | 135 * Meanwhile, we plan to add this alternative API for callbacks of more than 2 |
| 124 * arguments or that take named parameters. (this is not implemented yet, | 136 * arguments or that take named parameters. (this is not implemented yet, |
| 125 * but will be coming here soon). | 137 * but will be coming here soon). |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 */ | 683 */ |
| 672 void _reportTestError(String msg, String trace) { | 684 void _reportTestError(String msg, String trace) { |
| 673 if (_currentTest < _tests.length) { | 685 if (_currentTest < _tests.length) { |
| 674 final testCase = _tests[_currentTest]; | 686 final testCase = _tests[_currentTest]; |
| 675 testCase.error(msg, trace); | 687 testCase.error(msg, trace); |
| 676 } else { | 688 } else { |
| 677 _uncaughtErrorMessage = "$msg: $trace"; | 689 _uncaughtErrorMessage = "$msg: $trace"; |
| 678 } | 690 } |
| 679 } | 691 } |
| 680 | 692 |
| 681 /** Runs [callback] at the end of the event loop. */ | 693 /** |
| 694 * Runs [callback] at the end of the event loop. Note that we don't wrap | |
| 695 * the callback in guardAsync; this is for test framework functions which | |
| 696 * should not be throwing unexpected exceptions that end up failing test | |
| 697 * cases! Furthermore, we need the final exception to be thrown but not | |
| 698 * caught by the test framework if any test cases failed. However, tests | |
| 699 * that make use of a similar defer function *should* wrap the callback | |
| 700 * (as we do in unitttest_test.dart). | |
| 701 */ | |
| 682 _defer(void callback()) { | 702 _defer(void callback()) { |
| 683 // Exploit isolate ports as a platform-independent mechanism to queue a | 703 (new Future.immediate(null)).then((_) => callback()); |
|
Siggi Cherem (dart-lang)
2013/02/26 00:25:29
I wonder if anything depends on the old behavior h
gram
2013/03/04 19:51:52
I've run all tests in vm (checked/unchecked) and a
| |
| 684 // message at the end of the event loop. | |
| 685 // TODO(sigmund): expose this functionality somewhere in our libraries. | |
| 686 final port = new ReceivePort(); | |
| 687 port.receive((msg, reply) { | |
| 688 callback(); | |
| 689 port.close(); | |
| 690 }); | |
| 691 port.toSendPort().send(null, null); | |
| 692 } | 704 } |
| 693 | 705 |
| 694 rerunTests() { | 706 rerunTests() { |
| 695 _uncaughtErrorMessage = null; | 707 _uncaughtErrorMessage = null; |
| 696 _initialized = true; // We don't want to reset the test array. | 708 _initialized = true; // We don't want to reset the test array. |
| 697 runTests(); | 709 runTests(); |
| 698 } | 710 } |
| 699 | 711 |
| 700 /** | 712 /** |
| 701 * Filter the tests. [testFilter] can be a [RegExp], a [String] or a | 713 * Filter the tests. [testFilter] can be a [RegExp], a [String] or a |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 774 */ | 786 */ |
| 775 _nextBatch() { | 787 _nextBatch() { |
| 776 while (true) { | 788 while (true) { |
| 777 if (_currentTest >= _tests.length) { | 789 if (_currentTest >= _tests.length) { |
| 778 _completeTests(); | 790 _completeTests(); |
| 779 break; | 791 break; |
| 780 } | 792 } |
| 781 final testCase = _tests[_currentTest]; | 793 final testCase = _tests[_currentTest]; |
| 782 var f = guardAsync(testCase.run, null, _currentTest); | 794 var f = guardAsync(testCase.run, null, _currentTest); |
| 783 if (f != null) { | 795 if (f != null) { |
| 784 f.then((_){}) | 796 f.whenComplete(() { |
| 785 .catchError((e) { | |
| 786 testCase.error(e.toString(), e.stackTrace); | |
| 787 }) | |
| 788 .whenComplete(() { | |
| 789 _nextTestCase(); // Schedule the next test. | 797 _nextTestCase(); // Schedule the next test. |
| 790 }); | 798 }); |
| 791 break; | 799 break; |
| 792 } | 800 } |
| 793 _currentTest++; | 801 _currentTest++; |
| 794 } | 802 } |
| 795 } | 803 } |
| 796 | 804 |
| 797 /** Publish results on the page and notify controller. */ | 805 /** Publish results on the page and notify controller. */ |
| 798 _completeTests() { | 806 _completeTests() { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 876 } | 884 } |
| 877 | 885 |
| 878 /** Enable a test by ID. */ | 886 /** Enable a test by ID. */ |
| 879 void enableTest(int testId) => _setTestEnabledState(testId, true); | 887 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 880 | 888 |
| 881 /** Disable a test by ID. */ | 889 /** Disable a test by ID. */ |
| 882 void disableTest(int testId) => _setTestEnabledState(testId, false); | 890 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 883 | 891 |
| 884 /** Signature for a test function. */ | 892 /** Signature for a test function. */ |
| 885 typedef void TestFunction(); | 893 typedef void TestFunction(); |
| OLD | NEW |