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