| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library unittest.simple_configuration; |
| 6 | 6 |
| 7 // A custom failure handler for [expect] that routes expect failures | 7 import 'dart:isolate'; |
| 8 // to the config. | 8 |
| 9 import 'package:matcher/matcher.dart' |
| 10 show DefaultFailureHandler, configureExpectFailureHandler, TestFailure; |
| 11 |
| 12 import '../unittest.dart'; |
| 13 import 'internal_test_case.dart'; |
| 14 import 'utils.dart'; |
| 15 |
| 16 // A custom failure handler for [expect] that routes failures to the |
| 17 // [SimpleConfiguration]. |
| 9 class _ExpectFailureHandler extends DefaultFailureHandler { | 18 class _ExpectFailureHandler extends DefaultFailureHandler { |
| 10 final SimpleConfiguration _config; | 19 final SimpleConfiguration _config; |
| 11 | 20 |
| 12 _ExpectFailureHandler(this._config); | 21 _ExpectFailureHandler(this._config); |
| 13 | 22 |
| 14 void fail(String reason) { | 23 void fail(String reason) { |
| 15 _config.onExpectFailure(reason); | 24 _config.onExpectFailure(reason); |
| 16 } | 25 } |
| 17 } | 26 } |
| 18 | 27 |
| 19 /// Hooks to configure the unittest library for different platforms. This class | 28 /// A configuration that provides hooks to configure the unittest library for |
| 20 /// implements the API in a platform-independent way. Tests that want to take | 29 /// different platforms. |
| 21 /// advantage of the platform can create a subclass and override methods from | 30 /// |
| 22 /// this class. | 31 /// This class implements the [Configuration] API in a platform-independent way. |
| 32 /// Tests that want to take advantage of the platform can create a subclass and |
| 33 /// override methods from this class. |
| 23 class SimpleConfiguration extends Configuration { | 34 class SimpleConfiguration extends Configuration { |
| 24 // The VM won't shut down if a receive port is open. Use this to make sure | 35 /// A port that keeps the VM alive while we wait for asynchronous tests to |
| 25 // we correctly wait for asynchronous tests. | 36 /// finish. |
| 37 /// |
| 38 /// The VM won't shut down as long as there's an open receive port. |
| 26 ReceivePort _receivePort; | 39 ReceivePort _receivePort; |
| 27 | 40 |
| 28 /// Subclasses can override this with something useful for diagnostics. | 41 /// The name of the configuration. |
| 29 /// Particularly useful in cases where we have parent/child configurations | 42 /// |
| 30 /// such as layout tests. | 43 /// Subclasses can override this with something useful for diagnostics. It's |
| 31 String get name => 'Configuration'; | 44 /// particularly useful for parent/child configurations such as layout tests. |
| 45 final name = 'Configuration'; |
| 32 | 46 |
| 33 bool get autoStart => true; | 47 /// If true (the default), throw an exception once all tests have run if any f
ailed. |
| 34 | |
| 35 /// If true (the default), throw an exception at the end if any tests failed. | |
| 36 bool throwOnTestFailures = true; | 48 bool throwOnTestFailures = true; |
| 37 | 49 |
| 38 /// If true (the default), then tests will stop after the first failed | 50 /// If true (the default), then tests will stop after the first failed |
| 39 /// [expect]. If false, failed [expect]s will not cause the test | 51 /// [expect]. |
| 40 /// to stop (other exceptions will still terminate the test). | 52 /// |
| 53 /// If false, failed [expect]s will not cause the test to stop. Other |
| 54 /// exceptions will still terminate the test. |
| 41 bool stopTestOnExpectFailure = true; | 55 bool stopTestOnExpectFailure = true; |
| 42 | 56 |
| 43 // If stopTestOnExpectFailure is false, we need to capture failures, which | 57 // If [stopTestOnExpectFailure] is false, the list of failed [expect]s. |
| 44 // we do with this List. | |
| 45 final _testLogBuffer = <Pair<String, StackTrace>>[]; | 58 final _testLogBuffer = <Pair<String, StackTrace>>[]; |
| 46 | 59 |
| 47 /// The constructor sets up a failure handler for [expect] that redirects | 60 /// The constructor sets up a failure handler for [expect] that redirects |
| 48 /// [expect] failures to [onExpectFailure]. | 61 /// [expect] failures to [onExpectFailure]. |
| 49 SimpleConfiguration(): super.blank() { | 62 SimpleConfiguration(): super.blank() { |
| 50 configureExpectFailureHandler(new _ExpectFailureHandler(this)); | 63 configureExpectFailureHandler(new _ExpectFailureHandler(this)); |
| 51 } | 64 } |
| 52 | 65 |
| 53 void onInit() { | 66 void onInit() { |
| 54 // For Dart internal tests, we don't want stack frame filtering. | 67 // For Dart internal tests, we don't want stack frame filtering. |
| 55 // We turn it off here in the default config, but by default turn | 68 // We turn it off here in the default config, but by default turn |
| 56 // it back on in the vm and html configs. | 69 // it back on in the vm and html configs. |
| 57 filterStacks = false; | 70 filterStacks = false; |
| 58 _receivePort = new ReceivePort(); | 71 _receivePort = new ReceivePort(); |
| 59 _postMessage('unittest-suite-wait-for-done'); | 72 _postMessage('unittest-suite-wait-for-done'); |
| 60 } | 73 } |
| 61 | 74 |
| 62 /// Called when each test starts. Useful to show intermediate progress on | 75 /// Called when a test starts. |
| 63 /// a test suite. Derived classes should call this first before their own | 76 /// |
| 64 /// override code. | 77 /// Derived classes should call this first before their own override code. |
| 65 void onTestStart(TestCase testCase) { | 78 void onTestStart(TestCase testCase) => _testLogBuffer.clear(); |
| 66 assert(testCase != null); | |
| 67 _testLogBuffer.clear(); | |
| 68 } | |
| 69 | 79 |
| 70 /// Called when each test is first completed. Useful to show intermediate | 80 /// Called when a test completes. |
| 71 /// progress on a test suite. Derived classes should call this first | 81 /// |
| 72 /// before their own override code. | 82 /// Derived classes should call this first before their own override code. |
| 73 void onTestResult(TestCase testCase) { | 83 void onTestResult(TestCase externalTestCase) { |
| 74 assert(testCase != null); | 84 if (stopTestOnExpectFailure || _testLogBuffer.isEmpty) return; |
| 75 if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) { | 85 |
| 76 // Write the message/stack pairs up to the last pairs. | 86 var testCase = externalTestCase as InternalTestCase; |
| 77 var reason = new StringBuffer(); | 87 |
| 78 for (var reasonAndTrace in | 88 // Write the message/stack pairs up to the last pairs. |
| 79 _testLogBuffer.take(_testLogBuffer.length - 1)) { | 89 var reason = new StringBuffer(); |
| 80 reason.write(reasonAndTrace.first); | 90 for (var reasonAndTrace in |
| 81 reason.write('\n'); | 91 _testLogBuffer.take(_testLogBuffer.length - 1)) { |
| 82 reason.write(reasonAndTrace.last); | 92 reason.write(reasonAndTrace.first); |
| 83 reason.write('\n'); | 93 reason.write('\n'); |
| 84 } | 94 reason.write(reasonAndTrace.last); |
| 85 var lastReasonAndTrace = _testLogBuffer.last; | 95 reason.write('\n'); |
| 86 // Write the last message. | 96 } |
| 87 reason.write(lastReasonAndTrace.first); | 97 |
| 88 if (testCase.result == PASS) { | 98 var lastReasonAndTrace = _testLogBuffer.last; |
| 89 testCase._result = FAIL; | 99 // Write the last message. |
| 90 testCase._message = reason.toString(); | 100 reason.write(lastReasonAndTrace.first); |
| 91 // Use the last stack as the overall failure stack. | 101 if (testCase.result == PASS) { |
| 92 testCase._stackTrace = lastReasonAndTrace.last; | 102 testCase.result = FAIL; |
| 93 } else { | 103 testCase.message = reason.toString(); |
| 94 // Add the last stack to the message; we have a further stack | 104 // Use the last stack as the overall failure stack. |
| 95 // caused by some other failure. | 105 testCase.stackTrace = lastReasonAndTrace.last; |
| 96 reason.write(lastReasonAndTrace.last); | 106 } else { |
| 97 reason.write('\n'); | 107 // Add the last stack to the message; we have a further stack |
| 98 // Add the existing reason to the end of the expect log to | 108 // caused by some other failure. |
| 99 // create the final message. | 109 reason.write(lastReasonAndTrace.last); |
| 100 testCase._message = '${reason.toString()}\n${testCase._message}'; | 110 reason.write('\n'); |
| 101 } | 111 // Add the existing reason to the end of the expect log to |
| 112 // create the final message. |
| 113 testCase.message = '${reason.toString()}\n${testCase.message}'; |
| 102 } | 114 } |
| 103 } | 115 } |
| 104 | 116 |
| 105 void onTestResultChanged(TestCase testCase) { | 117 /// Handles the logging of messages by a test case. |
| 106 assert(testCase != null); | 118 /// |
| 107 } | 119 /// The default in this base configuration is to call [print]. |
| 108 | |
| 109 /// Handles the logging of messages by a test case. The default in | |
| 110 /// this base configuration is to call print(); | |
| 111 void onLogMessage(TestCase testCase, String message) { | 120 void onLogMessage(TestCase testCase, String message) { |
| 112 print(message); | 121 print(message); |
| 113 } | 122 } |
| 114 | 123 |
| 115 /// Handles failures from expect(). The default in | 124 /// Handles failures from [expect]. |
| 116 /// this base configuration is to throw an exception; | 125 /// |
| 126 /// If [stopTestOnExpectFailure] is true, this throws a [TestFailure]. |
| 127 /// Otherwise, this stores the error. |
| 117 void onExpectFailure(String reason) { | 128 void onExpectFailure(String reason) { |
| 118 if (stopTestOnExpectFailure) { | 129 if (stopTestOnExpectFailure) throw new TestFailure(reason); |
| 119 throw new TestFailure(reason); | 130 |
| 120 } else { | 131 try { |
| 121 try { | 132 throw ''; |
| 122 throw ''; | 133 } catch (_, stack) { |
| 123 } catch (_, stack) { | 134 var trace = getTrace(stack, formatStacks, filterStacks); |
| 124 var trace = getTrace(stack, formatStacks, filterStacks); | 135 if (trace == null) trace = stack; |
| 125 if (trace == null) trace = stack; | 136 _testLogBuffer.add(new Pair<String, StackTrace>(reason, trace)); |
| 126 _testLogBuffer.add(new Pair<String, StackTrace>(reason, trace)); | |
| 127 } | |
| 128 } | 137 } |
| 129 } | 138 } |
| 130 | 139 |
| 131 /// Format a test result. | 140 /// Returns a formatted string description of a test result. |
| 132 String formatResult(TestCase testCase) { | 141 String formatResult(TestCase testCase) { |
| 133 var result = new StringBuffer(); | 142 var result = new StringBuffer(); |
| 134 result.write(testCase.result.toUpperCase()); | 143 result.write(testCase.result.toUpperCase()); |
| 135 result.write(": "); | 144 result.write(": "); |
| 136 result.write(testCase.description); | 145 result.write(testCase.description); |
| 137 result.write("\n"); | 146 result.write("\n"); |
| 138 | 147 |
| 139 if (testCase.message != '') { | 148 if (testCase.message != '') { |
| 140 result.write(indent(testCase.message)); | 149 result.write(indent(testCase.message)); |
| 141 result.write("\n"); | 150 result.write("\n"); |
| 142 } | 151 } |
| 143 | 152 |
| 144 if (testCase.stackTrace != null) { | 153 if (testCase.stackTrace != null) { |
| 145 result.write(indent(testCase.stackTrace.toString())); | 154 result.write(indent(testCase.stackTrace.toString())); |
| 146 result.write("\n"); | 155 result.write("\n"); |
| 147 } | 156 } |
| 148 return result.toString(); | 157 return result.toString(); |
| 149 } | 158 } |
| 150 | 159 |
| 151 /// Called with the result of all test cases. | 160 /// Called with the result of all test cases. |
| 152 /// | 161 /// |
| 153 /// The default implementation prints the result summary using the built-in | 162 /// The default implementation prints the result summary using [print], |
| 154 /// [print] command. Browser tests commonly override this to reformat the | 163 /// formatted with [formatResult]. Browser tests commonly override this to |
| 155 /// output. | 164 /// reformat the output. |
| 156 /// | 165 /// |
| 157 /// When [uncaughtError] is not null, it contains an error that occured | 166 /// When [uncaughtError] is not null, it contains an error that occured |
| 158 /// outside of tests (e.g. setting up the test). | 167 /// outside of tests (e.g. setting up the test). |
| 159 void onSummary(int passed, int failed, int errors, List<TestCase> results, | 168 void onSummary(int passed, int failed, int errors, List<TestCase> results, |
| 160 String uncaughtError) { | 169 String uncaughtError) { |
| 161 // Print each test's result. | 170 // Print each test's result. |
| 162 for (final t in results) { | 171 for (var test in results) { |
| 163 print(formatResult(t).trim()); | 172 print(formatResult(test).trim()); |
| 164 } | 173 } |
| 165 | 174 |
| 166 // Show the summary. | 175 // Show the summary. |
| 167 print(''); | 176 print(''); |
| 168 | 177 |
| 169 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { | 178 if (passed == 0 && failed == 0 && errors == 0 && uncaughtError == null) { |
| 170 print('No tests found.'); | 179 print('No tests found.'); |
| 171 // This is considered a failure too. | 180 // This is considered a failure too. |
| 172 } else if (failed == 0 && errors == 0 && uncaughtError == null) { | 181 } else if (failed == 0 && errors == 0 && uncaughtError == null) { |
| 173 print('All $passed tests passed.'); | 182 print('All $passed tests passed.'); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 190 } | 199 } |
| 191 } | 200 } |
| 192 } | 201 } |
| 193 | 202 |
| 194 void _postMessage(String message) { | 203 void _postMessage(String message) { |
| 195 // In dart2js browser tests, the JavaScript-based test controller | 204 // In dart2js browser tests, the JavaScript-based test controller |
| 196 // intercepts calls to print and listens for "secret" messages. | 205 // intercepts calls to print and listens for "secret" messages. |
| 197 print(message); | 206 print(message); |
| 198 } | 207 } |
| 199 } | 208 } |
| OLD | NEW |