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