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

Side by Side Diff: lib/unittest/unittest.dart

Issue 10153005: unittest step 3 and 4: remove TestFramework.dart, make test.dart use (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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
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 /** 5 /**
6 * A library for writing dart unit tests. 6 * A library for writing dart unit tests.
7 * 7 *
8 * ##Concepts## 8 * ##Concepts##
9 * 9 *
10 * * Tests: Tests are specified via the top-level function [test], they can be 10 * * Tests: Tests are specified via the top-level function [test], they can be
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 final _RUNNING_TEST = 2; 136 final _RUNNING_TEST = 2;
137 137
138 /** 138 /**
139 * Whether an undetected error occurred while running the last test. These 139 * Whether an undetected error occurred while running the last test. These
140 * errors are commonly caused by DOM callbacks that were not guarded in a 140 * errors are commonly caused by DOM callbacks that were not guarded in a
141 * try-catch block. 141 * try-catch block.
142 */ 142 */
143 final _UNCAUGHT_ERROR = 3; 143 final _UNCAUGHT_ERROR = 3;
144 144
145 int _state = _UNINITIALIZED; 145 int _state = _UNINITIALIZED;
146 String _uncaughtErrorMessage = null;
146 147
147 final _PASS = 'pass'; 148 final _PASS = 'pass';
148 final _FAIL = 'fail'; 149 final _FAIL = 'fail';
149 final _ERROR = 'error'; 150 final _ERROR = 'error';
150 151
151 /** Creates an expectation for the given value. */ 152 /** Creates an expectation for the given value. */
152 Expectation expect(value) => new Expectation(value); 153 Expectation expect(value) => new Expectation(value);
153 154
154 /** Evaluates the given function and validates that it throws an exception. */ 155 /** Evaluates the given function and validates that it throws an exception. */
155 void expectThrow(function) { 156 void expectThrow(function) {
156 bool threw = false; 157 bool threw = false;
157 try { 158 try {
158 function(); 159 function();
159 } catch (var e) { 160 } catch (var e) {
160 threw = true; 161 threw = true;
161 } 162 }
162 Expect.equals(true, threw, 'Expected exception but none was thrown.'); 163 Expect.equals(true, threw, 'Expected exception but none was thrown.');
163 } 164 }
164 165
165 /** 166 /**
166 * Creates a new test case with the given description and body. The 167 * Creates a new test case with the given description and body. The
167 * description will include the descriptions of any surrounding group() 168 * description will include the descriptions of any surrounding group()
168 * calls. 169 * calls.
169 */ 170 */
170 void test(String spec, TestFunction body) { 171 void test(String spec, TestFunction body) {
171 _ensureInitialized(); 172 ensureInitialized();
172 173
173 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); 174 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0));
174 } 175 }
175 176
176 /** 177 /**
177 * Creates a new async test case with the given description and body. The 178 * Creates a new async test case with the given description and body. The
178 * description will include the descriptions of any surrounding group() 179 * description will include the descriptions of any surrounding group()
179 * calls. 180 * calls.
180 */ 181 */
181 // TODO(sigmund): deprecate this API 182 // TODO(sigmund): deprecate this API
182 void asyncTest(String spec, int callbacks, TestFunction body) { 183 void asyncTest(String spec, int callbacks, TestFunction body) {
183 _ensureInitialized(); 184 ensureInitialized();
184 185
185 final testCase = new TestCase( 186 final testCase = new TestCase(
186 _tests.length + 1, _fullSpec(spec), body, callbacks); 187 _tests.length + 1, _fullSpec(spec), body, callbacks);
187 _tests.add(testCase); 188 _tests.add(testCase);
188 189
189 if (callbacks < 1) { 190 if (callbacks < 1) {
190 testCase.error( 191 testCase.error(
191 'Async tests must wait for at least one callback ', ''); 192 'Async tests must wait for at least one callback ', '');
192 } 193 }
193 } 194 }
194 195
195 /** 196 /**
196 * Indicate to the unittest framework that a 0-argument callback is expected. 197 * Indicate to the unittest framework that a 0-argument callback is expected.
197 * 198 *
198 * The framework will wait for the callback to run before it continues with the 199 * The framework will wait for the callback to run before it continues with the
199 * following test. The callback must excute once and only once. Using [later] 200 * following test. The callback must excute once and only once. Using [later]
200 * will also ensure that errors that occur within the callback are tracked and 201 * will also ensure that errors that occur within the callback are tracked and
201 * reported by the unittest framework. 202 * reported by the unittest framework.
202 */ 203 */
203 // TODO(sigmund): expose this functionality 204 // TODO(sigmund): expose this functionality
204 Function _later0(Function callback) { 205 Function later0(Function callback) {
Bob Nystrom 2012/04/20 22:31:36 I remember discussing it, but I don't remember the
Siggi Cherem (dart-lang) 2012/04/21 00:03:43 I tried the approach that conflates them in a sing
205 Expect.isTrue(_currentTest < _tests.length); 206 Expect.isTrue(_currentTest < _tests.length);
206 var testCase = _tests[_currentTest]; 207 var testCase = _tests[_currentTest];
207 testCase.callbacks++; 208 testCase.callbacks++;
208 return () { 209 return () {
209 _guard(() => callback(), callbackDone); 210 _guard(() => callback(), callbackDone);
210 }; 211 };
211 } 212 }
212 213
213 // TODO(sigmund): expose this functionality 214 // TODO(sigmund): expose this functionality
Emily Fortuna 2012/04/20 20:06:19 Comment obsolete now?
Siggi Cherem (dart-lang) 2012/04/21 00:03:43 Done.
214 /** Like [_later0] but expecting a callback with 1 argument. */ 215 /** Like [later0] but expecting a callback with 1 argument. */
215 Function _later1(Function callback) { 216 Function later1(Function callback) {
Emily Fortuna 2012/04/20 20:06:19 I'm not thrilled with the names of these functions
Siggi Cherem (dart-lang) 2012/04/21 00:03:43 I'm not thrilled either. There is a way to elimina
216 Expect.isTrue(_currentTest < _tests.length); 217 Expect.isTrue(_currentTest < _tests.length);
217 var testCase = _tests[_currentTest]; 218 var testCase = _tests[_currentTest];
218 testCase.callbacks++; 219 testCase.callbacks++;
219 return (arg0) { 220 return (arg0) {
220 _guard(() => callback(arg0), callbackDone); 221 _guard(() => callback(arg0), callbackDone);
221 }; 222 };
222 } 223 }
223 224
224 // TODO(sigmund): expose this functionality 225 // TODO(sigmund): expose this functionality
225 /** Like [_later0] but expecting a callback with 2 arguments. */ 226 /** Like [later0] but expecting a callback with 2 arguments. */
226 Function _later2(Function callback) { 227 Function later2(Function callback) {
227 Expect.isTrue(_currentTest < _tests.length); 228 Expect.isTrue(_currentTest < _tests.length);
228 var testCase = _tests[_currentTest]; 229 var testCase = _tests[_currentTest];
229 testCase.callbacks++; 230 testCase.callbacks++;
230 return (arg0, arg1) { 231 return (arg0, arg1) {
231 _guard(() => callback(arg0, arg1), callbackDone); 232 _guard(() => callback(arg0, arg1), callbackDone);
232 }; 233 };
233 } 234 }
234 235
235 /** 236 /**
236 * Creates a new named group of tests. Calls to group() or test() within the 237 * Creates a new named group of tests. Calls to group() or test() within the
237 * body of the function passed to this will inherit this group's description. 238 * body of the function passed to this will inherit this group's description.
238 */ 239 */
239 void group(String description, void body()) { 240 void group(String description, void body()) {
240 _ensureInitialized(); 241 ensureInitialized();
241 242
242 // Concatenate the new group. 243 // Concatenate the new group.
243 final oldGroup = _currentGroup; 244 final oldGroup = _currentGroup;
244 if (_currentGroup != '') { 245 if (_currentGroup != '') {
245 // Add a space. 246 // Add a space.
246 _currentGroup = '$_currentGroup $description'; 247 _currentGroup = '$_currentGroup $description';
247 } else { 248 } else {
248 // The first group. 249 // The first group.
249 _currentGroup = description; 250 _currentGroup = description;
250 } 251 }
(...skipping 17 matching lines...) Expand all
268 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); 269 'Actual: ${_callbacksCalled}, expected: ${expected}', '');
269 _state = _UNCAUGHT_ERROR; 270 _state = _UNCAUGHT_ERROR;
270 } else if ((_callbacksCalled == testCase.callbacks) && 271 } else if ((_callbacksCalled == testCase.callbacks) &&
271 (_state != _RUNNING_TEST)) { 272 (_state != _RUNNING_TEST)) {
272 if (testCase.result == null) testCase.pass(); 273 if (testCase.result == null) testCase.pass();
273 _currentTest++; 274 _currentTest++;
274 _testRunner(); 275 _testRunner();
275 } 276 }
276 } 277 }
277 278
279 /** Menchanism to notify that an error was caught outside of this library. */
Bob Nystrom 2012/04/20 22:31:36 Should this even be public?
Siggi Cherem (dart-lang) 2012/04/21 00:03:43 Yes - we need it to be public so that unittest can
Bob Nystrom 2012/04/23 17:34:07 In that case, can we rename it something a little
278 void notifyError(String msg, String trace) { 280 void notifyError(String msg, String trace) {
279 if (_currentTest < _tests.length) { 281 if (_currentTest < _tests.length) {
280 final testCase = _tests[_currentTest]; 282 final testCase = _tests[_currentTest];
281 testCase.error(msg, trace); 283 testCase.error(msg, trace);
282 _state = _UNCAUGHT_ERROR; 284 _state = _UNCAUGHT_ERROR;
283 if (testCase.callbacks > 0) { 285 if (testCase.callbacks > 0) {
284 _currentTest++; 286 _currentTest++;
285 _testRunner(); 287 _testRunner();
286 } 288 }
289 } else {
290 _uncaughtErrorMessage = "$msg: $trace";
287 } 291 }
288 } 292 }
289 293
290 /** Runs [callback] at the end of the event loop. */ 294 /** Runs [callback] at the end of the event loop. */
291 _defer(void callback()) { 295 _defer(void callback()) {
292 // Exploit isolate ports as a platform-independent mechanism to queue a 296 // Exploit isolate ports as a platform-independent mechanism to queue a
293 // message at the end of the event loop. 297 // message at the end of the event loop.
294 // TODO(sigmund): expose this functionality somewhere in our libraries. 298 // TODO(sigmund): expose this functionality somewhere in our libraries.
295 final port = new ReceivePort(); 299 final port = new ReceivePort();
296 port.receive((msg, reply) { 300 port.receive((msg, reply) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 int testsErrors_ = 0; 377 int testsErrors_ = 0;
374 378
375 for (TestCase t in _tests) { 379 for (TestCase t in _tests) {
376 switch (t.result) { 380 switch (t.result) {
377 case _PASS: testsPassed_++; break; 381 case _PASS: testsPassed_++; break;
378 case _FAIL: testsFailed_++; break; 382 case _FAIL: testsFailed_++; break;
379 case _ERROR: testsErrors_++; break; 383 case _ERROR: testsErrors_++; break;
380 } 384 }
381 } 385 }
382 386
383 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests); 387 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests,
388 _uncaughtErrorMessage);
384 } 389 }
385 390
386 String _fullSpec(String spec) { 391 String _fullSpec(String spec) {
387 if (spec === null) return '$_currentGroup'; 392 if (spec === null) return '$_currentGroup';
388 return _currentGroup != '' ? '$_currentGroup $spec' : spec; 393 return _currentGroup != '' ? '$_currentGroup $spec' : spec;
389 } 394 }
390 395
391 /** 396 /**
392 * Lazily initializes the test library if not already initialized. 397 * Lazily initializes the test library if not already initialized.
393 */ 398 */
394 _ensureInitialized() { 399 ensureInitialized() {
395 if (_state != _UNINITIALIZED) return; 400 if (_state != _UNINITIALIZED) return;
396 401
397 _tests = <TestCase>[]; 402 _tests = <TestCase>[];
398 _currentGroup = ''; 403 _currentGroup = '';
399 _state = _READY; 404 _state = _READY;
400 _testRunner = _nextBatch; 405 _testRunner = _nextBatch;
401 406
402 if (_config == null) { 407 if (_config == null) {
403 _config = new Configuration(); 408 _config = new Configuration();
404 } 409 }
405 _config.onInit(); 410 _config.onInit();
406 411
407 // Immediately queue the suite up. It will run after a timeout (i.e. after 412 // Immediately queue the suite up. It will run after a timeout (i.e. after
408 // main() has returned). 413 // main() has returned).
409 _defer(_runTests); 414 _defer(_runTests);
410 } 415 }
411 416
412 /** Signature for a test function. */ 417 /** Signature for a test function. */
413 typedef void TestFunction(); 418 typedef void TestFunction();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698