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