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 // A custom failure handler for [expect] that routes expect failures | 7 // A custom failure handler for [expect] that routes expect failures |
8 // to the config. | 8 // to the config. |
9 class _ExpectFailureHandler extends DefaultFailureHandler { | 9 class _ExpectFailureHandler extends DefaultFailureHandler { |
10 Configuration _config; | 10 Configuration _config; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 /** | 49 /** |
50 * 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 |
51 * [expect]. If false, failed [expect]s will not cause the test | 51 * [expect]. If false, failed [expect]s will not cause the test |
52 * to stop (other exceptions will still terminate the test). | 52 * to stop (other exceptions will still terminate the test). |
53 */ | 53 */ |
54 bool stopTestOnExpectFailure = true; | 54 bool stopTestOnExpectFailure = true; |
55 | 55 |
56 // If stopTestOnExpectFailure is false, we need to capture failures, which | 56 // If stopTestOnExpectFailure is false, we need to capture failures, which |
57 // we do with this List. | 57 // we do with this List. |
58 final _testLogBuffer = <Pair<String, Trace>>[]; | 58 List _testLogBuffer = new List(); |
59 | 59 |
60 /** | 60 /** |
61 * The constructor sets up a failure handler for [expect] that redirects | 61 * The constructor sets up a failure handler for [expect] that redirects |
62 * [expect] failures to [onExpectFailure]. | 62 * [expect] failures to [onExpectFailure]. |
63 */ | 63 */ |
64 Configuration() { | 64 Configuration() { |
65 configureExpectFailureHandler(new _ExpectFailureHandler(this)); | 65 configureExpectFailureHandler(new _ExpectFailureHandler(this)); |
66 } | 66 } |
67 /** | 67 /** |
68 * Called as soon as the unittest framework becomes initialized. This is done | 68 * Called as soon as the unittest framework becomes initialized. This is done |
69 * even before tests are added to the test framework. It might be used to | 69 * even before tests are added to the test framework. It might be used to |
(...skipping 22 matching lines...) Expand all Loading... |
92 /** | 92 /** |
93 * Called when each test is first completed. Useful to show intermediate | 93 * Called when each test is first completed. Useful to show intermediate |
94 * progress on a test suite. Derived classes should call this first | 94 * progress on a test suite. Derived classes should call this first |
95 * before their own override code. | 95 * before their own override code. |
96 */ | 96 */ |
97 void onTestResult(TestCase testCase) { | 97 void onTestResult(TestCase testCase) { |
98 assert(testCase != null); | 98 assert(testCase != null); |
99 if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) { | 99 if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) { |
100 // Write the message/stack pairs up to the last pairs. | 100 // Write the message/stack pairs up to the last pairs. |
101 var reason = new StringBuffer(); | 101 var reason = new StringBuffer(); |
102 for (var reasonAndTrace in | 102 for (var i = 0; i < _testLogBuffer.length - 2; i += 2) { |
103 _testLogBuffer.take(_testLogBuffer.length - 1)) { | 103 reason.write(_testLogBuffer[i]); |
104 reason.write(reasonAndTrace.first); | |
105 reason.write('\n'); | 104 reason.write('\n'); |
106 reason.write(reasonAndTrace.last); | 105 reason.write(_formatStack(_testLogBuffer[i+1])); |
107 reason.write('\n'); | 106 reason.write('\n'); |
108 } | 107 } |
109 var lastReasonAndTrace = _testLogBuffer.last; | |
110 // Write the last message. | 108 // Write the last message. |
111 reason.write(lastReasonAndTrace.first); | 109 reason.write(_testLogBuffer[_testLogBuffer.length - 2]); |
112 if (testCase.result == PASS) { | 110 if (testCase.result == PASS) { |
113 testCase._result = FAIL; | 111 testCase._result = FAIL; |
114 testCase._message = reason.toString(); | 112 testCase._message = reason.toString(); |
115 // Use the last stack as the overall failure stack. | 113 // Use the last stack as the overall failure stack. |
116 testCase._stackTrace = lastReasonAndTrace.last; | 114 testCase._stackTrace = |
| 115 _formatStack(_testLogBuffer[_testLogBuffer.length - 1]); |
117 } else { | 116 } else { |
118 // Add the last stack to the message; we have a further stack | 117 // Add the last stack to the message; we have a further stack |
119 // caused by some other failure. | 118 // caused by some other failure. |
120 reason.write(lastReasonAndTrace.last); | 119 reason.write(_formatStack(_testLogBuffer[_testLogBuffer.length - 1])); |
121 reason.write('\n'); | 120 reason.write('\n'); |
122 // Add the existing reason to the end of the expect log to | 121 // Add the existing reason to the end of the expect log to |
123 // create the final message. | 122 // create the final message. |
124 testCase._message = '${reason.toString()}\n${testCase._message}'; | 123 testCase._message = '${reason.toString()}\n${testCase._message}'; |
125 } | 124 } |
126 } | 125 } |
127 } | 126 } |
128 | 127 |
129 /** | 128 /** |
130 * Called when an already completed test changes state; for example a test | 129 * Called when an already completed test changes state; for example a test |
(...skipping 13 matching lines...) Expand all Loading... |
144 } | 143 } |
145 | 144 |
146 /** | 145 /** |
147 * Handles failures from expect(). The default in | 146 * Handles failures from expect(). The default in |
148 * this base configuration is to throw an exception; | 147 * this base configuration is to throw an exception; |
149 */ | 148 */ |
150 void onExpectFailure(String reason) { | 149 void onExpectFailure(String reason) { |
151 if (stopTestOnExpectFailure) { | 150 if (stopTestOnExpectFailure) { |
152 throw new TestFailure(reason); | 151 throw new TestFailure(reason); |
153 } else { | 152 } else { |
| 153 _testLogBuffer.add(reason); |
154 try { | 154 try { |
155 throw ''; | 155 throw ''; |
156 } catch (_, stack) { | 156 } catch (_, stack) { |
157 _testLogBuffer.add( | 157 _testLogBuffer.add(stack); |
158 new Pair<String, Trace>(reason, new Trace.from(stack))); | |
159 } | 158 } |
160 } | 159 } |
161 } | 160 } |
162 | 161 |
163 /** | 162 /** |
164 * Format a test result. | 163 * Format a test result. |
165 */ | 164 */ |
166 String formatResult(TestCase testCase) { | 165 String formatResult(TestCase testCase) { |
167 var result = new StringBuffer(); | 166 var result = new StringBuffer(); |
168 result.write(testCase.result.toUpperCase()); | 167 result.write(testCase.result.toUpperCase()); |
169 result.write(": "); | 168 result.write(": "); |
170 result.write(testCase.description); | 169 result.write(testCase.description); |
171 result.write("\n"); | 170 result.write("\n"); |
172 | 171 |
173 if (testCase.message != '') { | 172 if (testCase.message != '') { |
174 result.write(indent(testCase.message)); | 173 result.write(_indent(testCase.message)); |
175 result.write("\n"); | 174 result.write("\n"); |
176 } | 175 } |
177 | 176 |
178 if (testCase.stackTrace != null) { | 177 if (testCase.stackTrace != null && testCase.stackTrace != '') { |
179 result.write(indent(testCase.stackTrace.toString())); | 178 result.write(_indent(testCase.stackTrace)); |
180 result.write("\n"); | 179 result.write("\n"); |
181 } | 180 } |
182 return result.toString(); | 181 return result.toString(); |
183 } | 182 } |
184 | 183 |
185 /** | 184 /** |
186 * Called with the result of all test cases. The default implementation prints | 185 * Called with the result of all test cases. The default implementation prints |
187 * the result summary using the built-in [print] command. Browser tests | 186 * the result summary using the built-in [print] command. Browser tests |
188 * commonly override this to reformat the output. | 187 * commonly override this to reformat the output. |
189 * | 188 * |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 _postMessage('unittest-suite-success'); | 221 _postMessage('unittest-suite-success'); |
223 _receivePort.close(); | 222 _receivePort.close(); |
224 } else { | 223 } else { |
225 _receivePort.close(); | 224 _receivePort.close(); |
226 if (throwOnTestFailures) { | 225 if (throwOnTestFailures) { |
227 throw new Exception('Some tests failed.'); | 226 throw new Exception('Some tests failed.'); |
228 } | 227 } |
229 } | 228 } |
230 } | 229 } |
231 | 230 |
| 231 String _indent(String str) { |
| 232 // TODO(nweiz): Use this simpler code once issue 2980 is fixed. |
| 233 // return str.replaceAll(new RegExp("^", multiLine: true), " "); |
| 234 |
| 235 return str.split("\n").map((line) => " $line").join("\n"); |
| 236 } |
| 237 |
232 /** Handle errors that happen outside the tests. */ | 238 /** Handle errors that happen outside the tests. */ |
233 // TODO(vsm): figure out how to expose the stack trace here | 239 // TODO(vsm): figure out how to expose the stack trace here |
234 // Currently e.message works in dartium, but not in dartc. | 240 // Currently e.message works in dartium, but not in dartc. |
235 void handleExternalError(e, String message, [stack]) => | 241 void handleExternalError(e, String message, [String stack = '']) => |
236 _reportTestError('$message\nCaught $e', stack); | 242 _reportTestError('$message\nCaught $e', stack); |
237 | 243 |
238 _postMessage(String message) { | 244 _postMessage(String message) { |
239 // In dart2js browser tests, the JavaScript-based test controller | 245 // In dart2js browser tests, the JavaScript-based test controller |
240 // intercepts calls to print and listens for "secret" messages. | 246 // intercepts calls to print and listens for "secret" messages. |
241 print(message); | 247 print(message); |
242 } | 248 } |
243 } | 249 } |
OLD | NEW |