Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: pkg/unittest/lib/src/config.dart

Issue 19540015: Roll forward "Use package:stack_trace in unittest." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/unittest/lib/interactive_html_config.dart ('k') | pkg/unittest/lib/src/test_case.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 List _testLogBuffer = new List(); 58 final _testLogBuffer = <Pair<String, Trace>>[];
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
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 i = 0; i < _testLogBuffer.length - 2; i += 2) { 102 for (var reasonAndTrace in
103 reason.write(_testLogBuffer[i]); 103 _testLogBuffer.take(_testLogBuffer.length - 1)) {
104 reason.write(reasonAndTrace.first);
104 reason.write('\n'); 105 reason.write('\n');
105 reason.write(_formatStack(_testLogBuffer[i+1])); 106 reason.write(reasonAndTrace.last);
106 reason.write('\n'); 107 reason.write('\n');
107 } 108 }
109 var lastReasonAndTrace = _testLogBuffer.last;
108 // Write the last message. 110 // Write the last message.
109 reason.write(_testLogBuffer[_testLogBuffer.length - 2]); 111 reason.write(lastReasonAndTrace.first);
110 if (testCase.result == PASS) { 112 if (testCase.result == PASS) {
111 testCase._result = FAIL; 113 testCase._result = FAIL;
112 testCase._message = reason.toString(); 114 testCase._message = reason.toString();
113 // Use the last stack as the overall failure stack. 115 // Use the last stack as the overall failure stack.
114 testCase._stackTrace = 116 testCase._stackTrace = lastReasonAndTrace.last;
115 _formatStack(_testLogBuffer[_testLogBuffer.length - 1]);
116 } else { 117 } else {
117 // Add the last stack to the message; we have a further stack 118 // Add the last stack to the message; we have a further stack
118 // caused by some other failure. 119 // caused by some other failure.
119 reason.write(_formatStack(_testLogBuffer[_testLogBuffer.length - 1])); 120 reason.write(lastReasonAndTrace.last);
120 reason.write('\n'); 121 reason.write('\n');
121 // Add the existing reason to the end of the expect log to 122 // Add the existing reason to the end of the expect log to
122 // create the final message. 123 // create the final message.
123 testCase._message = '${reason.toString()}\n${testCase._message}'; 124 testCase._message = '${reason.toString()}\n${testCase._message}';
124 } 125 }
125 } 126 }
126 } 127 }
127 128
128 /** 129 /**
129 * Called when an already completed test changes state; for example a test 130 * Called when an already completed test changes state; for example a test
(...skipping 13 matching lines...) Expand all
143 } 144 }
144 145
145 /** 146 /**
146 * Handles failures from expect(). The default in 147 * Handles failures from expect(). The default in
147 * this base configuration is to throw an exception; 148 * this base configuration is to throw an exception;
148 */ 149 */
149 void onExpectFailure(String reason) { 150 void onExpectFailure(String reason) {
150 if (stopTestOnExpectFailure) { 151 if (stopTestOnExpectFailure) {
151 throw new TestFailure(reason); 152 throw new TestFailure(reason);
152 } else { 153 } else {
153 _testLogBuffer.add(reason);
154 try { 154 try {
155 throw ''; 155 throw '';
156 } catch (_, stack) { 156 } catch (_, stack) {
157 _testLogBuffer.add(stack); 157 _testLogBuffer.add(
158 new Pair<String, Trace>(reason, new Trace.from(stack)));
158 } 159 }
159 } 160 }
160 } 161 }
161 162
162 /** 163 /**
163 * Format a test result. 164 * Format a test result.
164 */ 165 */
165 String formatResult(TestCase testCase) { 166 String formatResult(TestCase testCase) {
166 var result = new StringBuffer(); 167 var result = new StringBuffer();
167 result.write(testCase.result.toUpperCase()); 168 result.write(testCase.result.toUpperCase());
168 result.write(": "); 169 result.write(": ");
169 result.write(testCase.description); 170 result.write(testCase.description);
170 result.write("\n"); 171 result.write("\n");
171 172
172 if (testCase.message != '') { 173 if (testCase.message != '') {
173 result.write(_indent(testCase.message)); 174 result.write(indent(testCase.message));
174 result.write("\n"); 175 result.write("\n");
175 } 176 }
176 177
177 if (testCase.stackTrace != null && testCase.stackTrace != '') { 178 if (testCase.stackTrace != null) {
178 result.write(_indent(testCase.stackTrace)); 179 result.write(indent(testCase.stackTrace.toString()));
179 result.write("\n"); 180 result.write("\n");
180 } 181 }
181 return result.toString(); 182 return result.toString();
182 } 183 }
183 184
184 /** 185 /**
185 * Called with the result of all test cases. The default implementation prints 186 * Called with the result of all test cases. The default implementation prints
186 * the result summary using the built-in [print] command. Browser tests 187 * the result summary using the built-in [print] command. Browser tests
187 * commonly override this to reformat the output. 188 * commonly override this to reformat the output.
188 * 189 *
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 _postMessage('unittest-suite-success'); 222 _postMessage('unittest-suite-success');
222 _receivePort.close(); 223 _receivePort.close();
223 } else { 224 } else {
224 _receivePort.close(); 225 _receivePort.close();
225 if (throwOnTestFailures) { 226 if (throwOnTestFailures) {
226 throw new Exception('Some tests failed.'); 227 throw new Exception('Some tests failed.');
227 } 228 }
228 } 229 }
229 } 230 }
230 231
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
238 /** Handle errors that happen outside the tests. */ 232 /** Handle errors that happen outside the tests. */
239 // TODO(vsm): figure out how to expose the stack trace here 233 // TODO(vsm): figure out how to expose the stack trace here
240 // Currently e.message works in dartium, but not in dartc. 234 // Currently e.message works in dartium, but not in dartc.
241 void handleExternalError(e, String message, [String stack = '']) => 235 void handleExternalError(e, String message, [stack]) =>
242 _reportTestError('$message\nCaught $e', stack); 236 _reportTestError('$message\nCaught $e', stack);
243 237
244 _postMessage(String message) { 238 _postMessage(String message) {
245 // In dart2js browser tests, the JavaScript-based test controller 239 // In dart2js browser tests, the JavaScript-based test controller
246 // intercepts calls to print and listens for "secret" messages. 240 // intercepts calls to print and listens for "secret" messages.
247 print(message); 241 print(message);
248 } 242 }
249 } 243 }
OLDNEW
« no previous file with comments | « pkg/unittest/lib/interactive_html_config.dart ('k') | pkg/unittest/lib/src/test_case.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698