| 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 part of unittest; | 5 part of unittest; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Hooks to configure the unittest library for different platforms. This class | 8 * Hooks to configure the unittest library for different platforms. This class |
| 9 * implements the API in a platform-independent way. Tests that want to take | 9 * implements the API in a platform-independent way. Tests that want to take |
| 10 * advantage of the platform can create a subclass and override methods from | 10 * advantage of the platform can create a subclass and override methods from |
| 11 * this class. | 11 * this class. |
| 12 */ | 12 */ |
| 13 | 13 |
| 14 class Configuration { | 14 class Configuration { |
| 15 // The VM won't shut down if a receive port is open. Use this to make sure | 15 // The VM won't shut down if a receive port is open. Use this to make sure |
| 16 // we correctly wait for asynchronous tests. | 16 // we correctly wait for asynchronous tests. |
| 17 ReceivePort _receivePort; | 17 ReceivePort _receivePort; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Subclasses can override this with something useful for diagnostics. | 20 * Subclasses can override this with something useful for diagnostics. |
| 21 * Particularly useful in cases where we have parent/child configurations | 21 * Particularly useful in cases where we have parent/child configurations |
| 22 * such as layout tests. | 22 * such as layout tests. |
| 23 */ | 23 */ |
| 24 String get name => 'Configuration'; | 24 final String name = 'Configuration'; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * If true, then tests are started automatically (otherwise [runTests] | 27 * If true, then tests are started automatically (otherwise [runTests] |
| 28 * must be called explicitly after the tests are set up. | 28 * must be called explicitly after the tests are set up. |
| 29 */ | 29 */ |
| 30 bool get autoStart => true; | 30 final bool autoStart = true; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Called as soon as the unittest framework becomes initialized. This is done | 33 * Called as soon as the unittest framework becomes initialized. This is done |
| 34 * even before tests are added to the test framework. It might be used to | 34 * even before tests are added to the test framework. It might be used to |
| 35 * determine/debug errors that occur before the test harness starts executing. | 35 * determine/debug errors that occur before the test harness starts executing. |
| 36 * It is also used to tell the vm or browser that tests are going to be run | 36 * It is also used to tell the vm or browser that tests are going to be run |
| 37 * asynchronously and that the process should wait until they are done. | 37 * asynchronously and that the process should wait until they are done. |
| 38 */ | 38 */ |
| 39 void onInit() { | 39 void onInit() { |
| 40 _receivePort = new ReceivePort(); | 40 _receivePort = new ReceivePort(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 63 /** | 63 /** |
| 64 * Called when an already completed test changes state; for example a test | 64 * Called when an already completed test changes state; for example a test |
| 65 * that was marked as passing may later be marked as being in error because | 65 * that was marked as passing may later be marked as being in error because |
| 66 * it still had callbacks being invoked. | 66 * it still had callbacks being invoked. |
| 67 */ | 67 */ |
| 68 void onTestResultChanged(TestCase testCase) { | 68 void onTestResultChanged(TestCase testCase) { |
| 69 assert(testCase != null); | 69 assert(testCase != null); |
| 70 } | 70 } |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * Can be called by tests to log status. Tests should use this | |
| 74 * instead of print. Subclasses should not override this; they | |
| 75 * should instead override logMessage which is passed the test case. | |
| 76 */ | |
| 77 void logMessage(String message) { | |
| 78 if (currentTestCase == null) { | |
| 79 // Before or after tests run. In this case we pass null for the test | |
| 80 // case reference and let the config decide what to do with this. | |
| 81 logTestCaseMessage(null, message); | |
| 82 } else { | |
| 83 logTestCaseMessage(currentTestCase, message); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Handles the logging of messages by a test case. The default in | 73 * Handles the logging of messages by a test case. The default in |
| 89 * this base configuration is to call print(); | 74 * this base configuration is to call print(); |
| 90 */ | 75 */ |
| 91 void logTestCaseMessage(TestCase testCase, String message) { | 76 void onLogMessage(TestCase testCase, String message) { |
| 92 print(message); | 77 print(message); |
| 93 } | 78 } |
| 94 | 79 |
| 95 /** | 80 /** |
| 96 * Called with the result of all test cases. The default implementation prints | 81 * Called with the result of all test cases. The default implementation prints |
| 97 * the result summary using the built-in [print] command. Browser tests | 82 * the result summary using the built-in [print] command. Browser tests |
| 98 * commonly override this to reformat the output. | 83 * commonly override this to reformat the output. |
| 99 * | 84 * |
| 100 * When [uncaughtError] is not null, it contains an error that occured outside | 85 * When [uncaughtError] is not null, it contains an error that occured outside |
| 101 * of tests (e.g. setting up the test). | 86 * of tests (e.g. setting up the test). |
| 102 */ | 87 */ |
| 103 void onSummary(int passed, int failed, int errors, List<TestCase> results, | 88 void onSummary(int passed, int failed, int errors, List<TestCase> results, |
| 104 String uncaughtError) { | 89 String uncaughtError) { |
| 105 // Print each test's result. | 90 // Print each test's result. |
| 106 for (final t in testCases) { | 91 for (final t in results) { |
| 107 var resultString = "${t.result}".toUpperCase(); | 92 var resultString = "${t.result}".toUpperCase(); |
| 108 print('$resultString: ${t.description}'); | 93 print('$resultString: ${t.description}'); |
| 109 | 94 |
| 110 if (t.message != '') { | 95 if (t.message != '') { |
| 111 print(_indent(t.message)); | 96 print(_indent(t.message)); |
| 112 } | 97 } |
| 113 | 98 |
| 114 if (t.stackTrace != null && t.stackTrace != '') { | 99 if (t.stackTrace != null && t.stackTrace != '') { |
| 115 print(_indent(t.stackTrace)); | 100 print(_indent(t.stackTrace)); |
| 116 } | 101 } |
| 117 } | 102 } |
| 118 | 103 |
| 119 // Show the summary. | 104 // Show the summary. |
| 120 print(''); | 105 print(''); |
| 121 | 106 |
| 122 var success = false; | |
| 123 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { | 107 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { |
| 124 print('No tests found.'); | 108 print('No tests found.'); |
| 125 // This is considered a failure too. | 109 // This is considered a failure too. |
| 126 } else if (failed == 0 && errors == 0 && uncaughtError == null) { | 110 } else if (failed == 0 && errors == 0 && uncaughtError == null) { |
| 127 print('All $passed tests passed.'); | 111 print('All $passed tests passed.'); |
| 128 success = true; | |
| 129 } else { | 112 } else { |
| 130 if (uncaughtError != null) { | 113 if (uncaughtError != null) { |
| 131 print('Top-level uncaught error: $uncaughtError'); | 114 print('Top-level uncaught error: $uncaughtError'); |
| 132 } | 115 } |
| 133 print('$passed PASSED, $failed FAILED, $errors ERRORS'); | 116 print('$passed PASSED, $failed FAILED, $errors ERRORS'); |
| 134 } | 117 } |
| 135 } | 118 } |
| 136 | 119 |
| 137 /** | 120 /** |
| 138 * Called when the unittest framework is done running. [success] indicates | 121 * Called when the unittest framework is done running. [success] indicates |
| (...skipping 21 matching lines...) Expand all Loading... |
| 160 // Currently e.message works in dartium, but not in dartc. | 143 // Currently e.message works in dartium, but not in dartc. |
| 161 void handleExternalError(e, String message, [String stack = '']) => | 144 void handleExternalError(e, String message, [String stack = '']) => |
| 162 _reportTestError('$message\nCaught $e', stack); | 145 _reportTestError('$message\nCaught $e', stack); |
| 163 | 146 |
| 164 _postMessage(String message) { | 147 _postMessage(String message) { |
| 165 // In dart2js browser tests, the JavaScript-based test controller | 148 // In dart2js browser tests, the JavaScript-based test controller |
| 166 // intercepts calls to print and listens for "secret" messages. | 149 // intercepts calls to print and listens for "secret" messages. |
| 167 print(message); | 150 print(message); |
| 168 } | 151 } |
| 169 } | 152 } |
| OLD | NEW |