Skip to content

Commit c7b527f

Browse files
committed
Roll forward "Use package:stack_trace in unittest."
This reverts r24669 and rolls forward r24652, r24659, and r24661. BUG=11649 [email protected], [email protected] Review URL: https://codereview.chromium.org//18892003 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@25131 260f80e4-7a28-3924-810f-c04153c831b5
1 parent a1ca4b8 commit c7b527f

File tree

11 files changed

+103
-127
lines changed

11 files changed

+103
-127
lines changed

pkg/unittest/lib/compact_vm_config.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'dart:io';
1515
import 'dart:isolate';
1616

1717
import 'unittest.dart';
18+
import 'src/utils.dart';
1819
import 'vm_config.dart';
1920

2021
const String _GREEN = '\u001b[32m';
@@ -57,11 +58,11 @@ class CompactVMConfiguration extends VMConfiguration {
5758
_progressLine(_start, _pass, _fail, test.description);
5859
print('');
5960
if (test.message != '') {
60-
print(_indent(test.message));
61+
print(indent(test.message));
6162
}
6263

63-
if (test.stackTrace != null && test.stackTrace != '') {
64-
print(_indent(test.stackTrace));
64+
if (test.stackTrace != null) {
65+
print(indent(test.stackTrace.toString()));
6566
}
6667
}
6768
}
@@ -75,10 +76,6 @@ class CompactVMConfiguration extends VMConfiguration {
7576
});
7677
}
7778

78-
String _indent(String str) {
79-
return str.split("\n").map((line) => " $line").join("\n");
80-
}
81-
8279
void onSummary(int passed, int failed, int errors, List<TestCase> results,
8380
String uncaughtError) {
8481
var success = false;

pkg/unittest/lib/html_config.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ String _toHtml(TestCase test_) {
8080
</tr>''';
8181

8282
if (test_.stackTrace != null) {
83-
html = '$html<tr><td></td><td colspan="2"><pre>${_htmlEscape(test_.stackTrace)}</pre></td></tr>';
83+
html = '$html<tr><td></td><td colspan="2"><pre>' +
84+
_htmlEscape(test_.stackTrace.toString()) +
85+
'</pre></td></tr>';
8486
}
8587

8688
return html;

pkg/unittest/lib/html_enhanced_config.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ class HtmlEnhancedConfiguration extends Configuration {
270270
'${test_.description}. ${_htmlEscape(test_.message)}');
271271

272272
if (test_.stackTrace != null) {
273-
addRowElement('', '', '<pre>${_htmlEscape(test_.stackTrace)}</pre>');
273+
addRowElement('', '',
274+
'<pre>${_htmlEscape(test_.stackTrace.toString())}</pre>');
274275
}
275276
}
276277

pkg/unittest/lib/interactive_html_config.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ library unittest_interactive_html_config;
2020
import 'dart:html';
2121
import 'dart:async';
2222
import 'dart:math';
23+
24+
import 'package:stack_trace/stack_trace.dart';
25+
2326
import 'unittest.dart';
2427

2528
/** The messages exchanged between parent and child. */
@@ -152,17 +155,25 @@ class ChildInteractiveHtmlConfiguration extends HtmlConfiguration {
152155
}
153156

154157
/**
155-
* Get the elapsed time for the test, anbd post the test result
156-
* back to the parent window. If the test failed due to an exception
157-
* the stack is posted back too (before the test result).
158+
* Get the elapsed time for the test, and post the test result back to the
159+
* parent window. If the test failed due to an exception the stack is posted
160+
* back too (before the test result).
158161
*/
159162
void onTestResult(TestCase testCase) {
160163
super.onTestResult(testCase);
161164
DateTime end = new DateTime.now();
162165
int elapsed = end.difference(_testStarts[testCase.id]).inMilliseconds;
163166
if (testCase.stackTrace != null) {
167+
var message = json.stringify(testCase.stackTrace.frames.map((frame) {
168+
return <String>{
169+
"uri": frame.uri.toString(),
170+
"line": frame.line,
171+
"column": frame.column,
172+
"member": frame.member
173+
};
174+
}).toList());
164175
_parentWindow.postMessage(
165-
_Message.text(_Message.STACK, elapsed, testCase.stackTrace), '*');
176+
_Message.text(_Message.STACK, elapsed, message), '*');
166177
}
167178
_parentWindow.postMessage(
168179
_Message.text(testCase.result, elapsed, testCase.message), '*');
@@ -185,7 +196,7 @@ class ParentInteractiveHtmlConfiguration extends HtmlConfiguration {
185196

186197

187198
/** The stack that was posted back from the child, if any. */
188-
String _stack;
199+
Trace _stack;
189200

190201
int _testTime;
191202
/**
@@ -233,7 +244,13 @@ class ParentInteractiveHtmlConfiguration extends HtmlConfiguration {
233244
if (msg.messageType == _Message.LOG) {
234245
logMessage(e.data);
235246
} else if (msg.messageType == _Message.STACK) {
236-
_stack = msg.body;
247+
_stack = new Trace(json.parse(msg.body).map((frame) {
248+
return new Frame(
249+
Uri.parse(frame['uri']),
250+
frame['line'],
251+
frame['column'],
252+
frame['member']);
253+
}));
237254
} else {
238255
_testTime = msg.elapsed;
239256
logMessage(_Message.text(_Message.LOG, _testTime, 'Complete'));

pkg/unittest/lib/src/config.dart

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class Configuration {
5555

5656
// If stopTestOnExpectFailure is false, we need to capture failures, which
5757
// we do with this List.
58-
List _testLogBuffer = new List();
59-
58+
final _testLogBuffer = <Pair<String, Trace>>[];
59+
6060
/**
6161
* The constructor sets up a failure handler for [expect] that redirects
6262
* [expect] failures to [onExpectFailure].
@@ -99,24 +99,25 @@ class Configuration {
9999
if (!stopTestOnExpectFailure && _testLogBuffer.length > 0) {
100100
// Write the message/stack pairs up to the last pairs.
101101
var reason = new StringBuffer();
102-
for (var i = 0; i < _testLogBuffer.length - 2; i += 2) {
103-
reason.write(_testLogBuffer[i]);
102+
for (var reasonAndTrace in
103+
_testLogBuffer.take(_testLogBuffer.length - 1)) {
104+
reason.write(reasonAndTrace.first);
104105
reason.write('\n');
105-
reason.write(_formatStack(_testLogBuffer[i+1]));
106+
reason.write(reasonAndTrace.last);
106107
reason.write('\n');
107108
}
109+
var lastReasonAndTrace = _testLogBuffer.last;
108110
// Write the last message.
109-
reason.write(_testLogBuffer[_testLogBuffer.length - 2]);
111+
reason.write(lastReasonAndTrace.first);
110112
if (testCase.result == PASS) {
111113
testCase._result = FAIL;
112114
testCase._message = reason.toString();
113115
// Use the last stack as the overall failure stack.
114-
testCase._stackTrace =
115-
_formatStack(_testLogBuffer[_testLogBuffer.length - 1]);
116+
testCase._stackTrace = lastReasonAndTrace.last;
116117
} else {
117118
// Add the last stack to the message; we have a further stack
118119
// caused by some other failure.
119-
reason.write(_formatStack(_testLogBuffer[_testLogBuffer.length - 1]));
120+
reason.write(lastReasonAndTrace.last);
120121
reason.write('\n');
121122
// Add the existing reason to the end of the expect log to
122123
// create the final message.
@@ -150,11 +151,11 @@ class Configuration {
150151
if (stopTestOnExpectFailure) {
151152
throw new TestFailure(reason);
152153
} else {
153-
_testLogBuffer.add(reason);
154154
try {
155155
throw '';
156156
} catch (_, stack) {
157-
_testLogBuffer.add(stack);
157+
_testLogBuffer.add(
158+
new Pair<String, Trace>(reason, new Trace.from(stack)));
158159
}
159160
}
160161
}
@@ -170,12 +171,12 @@ class Configuration {
170171
result.write("\n");
171172

172173
if (testCase.message != '') {
173-
result.write(_indent(testCase.message));
174+
result.write(indent(testCase.message));
174175
result.write("\n");
175176
}
176177

177-
if (testCase.stackTrace != null && testCase.stackTrace != '') {
178-
result.write(_indent(testCase.stackTrace));
178+
if (testCase.stackTrace != null) {
179+
result.write(indent(testCase.stackTrace.toString()));
179180
result.write("\n");
180181
}
181182
return result.toString();
@@ -228,17 +229,10 @@ class Configuration {
228229
}
229230
}
230231

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-
238232
/** Handle errors that happen outside the tests. */
239233
// TODO(vsm): figure out how to expose the stack trace here
240234
// 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]) =>
242236
_reportTestError('$message\nCaught $e', stack);
243237

244238
_postMessage(String message) {

pkg/unittest/lib/src/test_case.dart

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class TestCase {
4141
*/
4242
String get result => _result;
4343

44-
String _stackTrace;
44+
Trace _stackTrace;
4545
/** Stack trace associated with this test, or [null] if it succeeded. */
46-
String get stackTrace => _stackTrace;
46+
Trace get stackTrace => _stackTrace;
4747

4848
/** The group (or groups) under which this test is running. */
4949
final String currentGroup;
@@ -69,7 +69,6 @@ class TestCase {
6969

7070
Function _errorHandler(String stage) => (e) {
7171
var stack = getAttachedStackTrace(e);
72-
stack = (stack == null) ? '' : '$stack';
7372
if (result == null || result == PASS) {
7473
if (e is TestFailure) {
7574
fail("$e", stack);
@@ -122,9 +121,9 @@ class TestCase {
122121

123122
// Set the results, notify the config, and return true if this
124123
// is the first time the result is being set.
125-
void _setResult(String testResult, String messageText, String stack) {
124+
void _setResult(String testResult, String messageText, stack) {
126125
_message = messageText;
127-
_stackTrace = _formatStack(stack);
126+
_stackTrace = _getTrace(stack);
128127
if (result == null) {
129128
_result = testResult;
130129
_config.onTestResult(this);
@@ -134,9 +133,7 @@ class TestCase {
134133
}
135134
}
136135

137-
void _complete(String testResult,
138-
[String messageText = '',
139-
String stack = '']) {
136+
void _complete(String testResult, [String messageText = '', stack]) {
140137
if (runningTime == null) {
141138
// The startTime can be `null` if an error happened during setup. In this
142139
// case we simply report a running time of 0.
@@ -158,8 +155,7 @@ class TestCase {
158155
_complete(PASS);
159156
}
160157

161-
void fail(String messageText, [String stack = '']) {
162-
assert(stack != null);
158+
void fail(String messageText, [stack]) {
163159
if (result != null) {
164160
String newMessage = (result == PASS)
165161
? 'Test failed after initially passing: $messageText'
@@ -171,8 +167,7 @@ class TestCase {
171167
}
172168
}
173169

174-
void error(String messageText, [String stack = '']) {
175-
assert(stack != null);
170+
void error(String messageText, [stack]) {
176171
_complete(ERROR, messageText, stack);
177172
}
178173

pkg/unittest/lib/src/utils.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,24 @@ String _escapeChar(String ch) {
4646
return ch;
4747
}
4848

49+
/** Indent each line in [str] by two spaces. */
50+
String indent(String str) =>
51+
str.replaceAll(new RegExp("^", multiLine: true), " ");
52+
53+
/** A pair of values. */
54+
class Pair<E, F> {
55+
E first;
56+
F last;
57+
58+
Pair(this.first, this.last);
59+
60+
String toString() => '($first, $last)';
61+
62+
bool operator==(other) {
63+
if (other is! Pair) return false;
64+
return other.first == first && other.last == last;
65+
}
66+
67+
int get hashCode => first.hashCode ^ last.hashCode;
68+
}
69+

0 commit comments

Comments
 (0)