| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of unittest; | |
| 6 | |
| 7 /** | |
| 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 | |
| 10 * advantage of the platform can create a subclass and override methods from | |
| 11 * this class. | |
| 12 */ | |
| 13 | |
| 14 class Configuration { | |
| 15 TestCase currentTestCase = null; | |
| 16 | |
| 17 /** | |
| 18 * Subclasses can override this with something useful for diagnostics. | |
| 19 * Particularly useful in cases where we have parent/child configurations | |
| 20 * such as layout tests. | |
| 21 */ | |
| 22 get name => 'Configuration'; | |
| 23 /** | |
| 24 * If true, then tests are started automatically (otherwise [runTests] | |
| 25 * must be called explicitly after the tests are set up. | |
| 26 */ | |
| 27 get autoStart => true; | |
| 28 | |
| 29 /** | |
| 30 * Called as soon as the unittest framework becomes initialized. This is done | |
| 31 * even before tests are added to the test framework. It might be used to | |
| 32 * determine/debug errors that occur before the test harness starts executing. | |
| 33 */ | |
| 34 void onInit() {} | |
| 35 | |
| 36 /** | |
| 37 * Called as soon as the unittest framework starts running. Used commonly to | |
| 38 * tell the vm or browser that tests are still running and the process should | |
| 39 * wait until they are done. | |
| 40 */ | |
| 41 void onStart() { | |
| 42 _postMessage('unittest-suite-wait-for-done'); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Called when each test starts. Useful to show intermediate progress on | |
| 47 * a test suite. | |
| 48 */ | |
| 49 void onTestStart(TestCase testCase) { | |
| 50 currentTestCase = testCase; | |
| 51 } | |
| 52 | |
| 53 /** | |
| 54 * Called when each test is completed. Useful to show intermediate progress on | |
| 55 * a test suite. | |
| 56 */ | |
| 57 void onTestResult(TestCase testCase) { | |
| 58 currentTestCase = null; | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Can be called by tests to log status. Tests should use this | |
| 63 * instead of print. Subclasses should not override this; they | |
| 64 * should instead override logMessage which is passed the test case. | |
| 65 */ | |
| 66 void logMessage(String message) { | |
| 67 if (currentTestCase == null || _currentTest >= _tests.length || | |
| 68 currentTestCase.id != _tests[_currentTest].id) { | |
| 69 // Before or after tests run, or with a mismatch between what the | |
| 70 // config and the test harness think is the current test. In this | |
| 71 // case we pass null for the test case reference and let the config | |
| 72 // decide what to do with this. | |
| 73 logTestCaseMessage(null, message); | |
| 74 } else { | |
| 75 logTestCaseMessage(currentTestCase, message); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Handles the logging of messages by a test case. The default in | |
| 81 * this base configuration is to call print(); | |
| 82 */ | |
| 83 void logTestCaseMessage(TestCase testCase, String message) { | |
| 84 print(message); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Called with the result of all test cases. The default implementation prints | |
| 89 * the result summary using the built-in [print] command. Browser tests | |
| 90 * commonly override this to reformat the output. | |
| 91 * | |
| 92 * When [uncaughtError] is not null, it contains an error that occured outside | |
| 93 * of tests (e.g. setting up the test). | |
| 94 */ | |
| 95 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
| 96 String uncaughtError) { | |
| 97 // Print each test's result. | |
| 98 for (final t in _tests) { | |
| 99 print('${t.result.toUpperCase()}: ${t.description}'); | |
| 100 | |
| 101 if (t.message != '') { | |
| 102 print(_indent(t.message)); | |
| 103 } | |
| 104 | |
| 105 if (t.stackTrace != null && t.stackTrace != '') { | |
| 106 print(_indent(t.stackTrace)); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 // Show the summary. | |
| 111 print(''); | |
| 112 | |
| 113 var success = false; | |
| 114 if (passed == 0 && failed == 0 && errors == 0) { | |
| 115 print('No tests found.'); | |
| 116 // This is considered a failure too. | |
| 117 } else if (failed == 0 && errors == 0 && uncaughtError == null) { | |
| 118 print('All $passed tests passed.'); | |
| 119 success = true; | |
| 120 } else { | |
| 121 if (uncaughtError != null) { | |
| 122 print('Top-level uncaught error: $uncaughtError'); | |
| 123 } | |
| 124 print('$passed PASSED, $failed FAILED, $errors ERRORS'); | |
| 125 } | |
| 126 | |
| 127 if (success) { | |
| 128 _postMessage('unittest-suite-success'); | |
| 129 } else { | |
| 130 throw new Exception('Some tests failed.'); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 String _indent(String str) { | |
| 135 // TODO(nweiz): Use this simpler code once issue 2980 is fixed. | |
| 136 // return str.replaceAll(const RegExp("^", multiLine: true), " "); | |
| 137 | |
| 138 return Strings.join(str.split("\n").map((line) => " $line"), "\n"); | |
| 139 } | |
| 140 | |
| 141 /** Handle errors that happen outside the tests. */ | |
| 142 // TODO(vsm): figure out how to expose the stack trace here | |
| 143 // Currently e.message works in dartium, but not in dartc. | |
| 144 handleExternalError(e, String message) => | |
| 145 _reportTestError('$message\nCaught $e', ''); | |
| 146 | |
| 147 _postMessage(String message) { | |
| 148 // In dart2js browser tests, the JavaScript-based test controller | |
| 149 // intercepts calls to print and listens for "secret" messages. | |
| 150 print(message); | |
| 151 } | |
| 152 } | |
| OLD | NEW |