Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 int calls = 0; | 278 int calls = 0; |
| 279 TestCase testCase; | 279 TestCase testCase; |
| 280 _SpreadArgsHelper(this.callback, this.expectedCalls) { | 280 _SpreadArgsHelper(this.callback, this.expectedCalls) { |
| 281 Expect.isTrue(_currentTest < _tests.length); | 281 Expect.isTrue(_currentTest < _tests.length); |
| 282 testCase = _tests[_currentTest]; | 282 testCase = _tests[_currentTest]; |
| 283 testCase.callbacks++; | 283 testCase.callbacks++; |
| 284 } | 284 } |
| 285 | 285 |
| 286 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, | 286 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, |
| 287 arg3 = _sentinel, arg4 = _sentinel]) { | 287 arg3 = _sentinel, arg4 = _sentinel]) { |
| 288 return _guard(() { | 288 return guard(() { |
| 289 if (!_incrementCall()) { | 289 if (!_incrementCall()) { |
| 290 return; | 290 return; |
| 291 } else if (arg0 == _sentinel) { | 291 } else if (arg0 == _sentinel) { |
| 292 return callback(); | 292 return callback(); |
| 293 } else if (arg1 == _sentinel) { | 293 } else if (arg1 == _sentinel) { |
| 294 return callback(arg0); | 294 return callback(arg0); |
| 295 } else if (arg2 == _sentinel) { | 295 } else if (arg2 == _sentinel) { |
| 296 return callback(arg0, arg1); | 296 return callback(arg0, arg1); |
| 297 } else if (arg3 == _sentinel) { | 297 } else if (arg3 == _sentinel) { |
| 298 return callback(arg0, arg1, arg2); | 298 return callback(arg0, arg1, arg2); |
| 299 } else if (arg4 == _sentinel) { | 299 } else if (arg4 == _sentinel) { |
| 300 return callback(arg0, arg1, arg2, arg3); | 300 return callback(arg0, arg1, arg2, arg3); |
| 301 } else { | 301 } else { |
| 302 testCase.error( | 302 testCase.error( |
| 303 'unittest lib does not support callbacks with more than 4 arguments', | 303 'unittest lib does not support callbacks with more than 4 arguments', |
| 304 ''); | 304 ''); |
| 305 _state = _UNCAUGHT_ERROR; | 305 _state = _UNCAUGHT_ERROR; |
| 306 } | 306 } |
| 307 }, () { if (calls == expectedCalls) callbackDone(); }); | 307 }, () { if (calls == expectedCalls) callbackDone(); }); |
| 308 } | 308 } |
| 309 | 309 |
| 310 invoke0() { | 310 invoke0() { |
| 311 return _guard( | 311 return guard( |
| 312 () => _incrementCall() ? callback() : null, | 312 () => _incrementCall() ? callback() : null, |
| 313 () { if (calls == expectedCalls) callbackDone(); }); | 313 () { if (calls == expectedCalls) callbackDone(); }); |
| 314 } | 314 } |
| 315 | 315 |
| 316 invoke1(arg1) { | 316 invoke1(arg1) { |
| 317 return _guard( | 317 return guard( |
| 318 () => _incrementCall() ? callback(arg1) : null, | 318 () => _incrementCall() ? callback(arg1) : null, |
| 319 () { if (calls == expectedCalls) callbackDone(); }); | 319 () { if (calls == expectedCalls) callbackDone(); }); |
| 320 } | 320 } |
| 321 | 321 |
| 322 invoke2(arg1, arg2) { | 322 invoke2(arg1, arg2) { |
| 323 return _guard( | 323 return guard( |
| 324 () => _incrementCall() ? callback(arg1, arg2) : null, | 324 () => _incrementCall() ? callback(arg1, arg2) : null, |
| 325 () { if (calls == expectedCalls) callbackDone(); }); | 325 () { if (calls == expectedCalls) callbackDone(); }); |
| 326 } | 326 } |
| 327 | 327 |
| 328 /** Returns false if we exceded the number of expected calls. */ | 328 /** Returns false if we exceded the number of expected calls. */ |
| 329 bool _incrementCall() { | 329 bool _incrementCall() { |
| 330 calls++; | 330 calls++; |
| 331 if (calls > expectedCalls) { | 331 if (calls > expectedCalls) { |
| 332 testCase.error( | 332 testCase.error( |
| 333 'Callback called more times than expected ($expectedCalls)', | 333 'Callback called more times than expected ($expectedCalls)', |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 _defer(() { | 461 _defer(() { |
| 462 assert (_currentTest == 0); | 462 assert (_currentTest == 0); |
| 463 _testRunner(); | 463 _testRunner(); |
| 464 }); | 464 }); |
| 465 } | 465 } |
| 466 | 466 |
| 467 /** | 467 /** |
| 468 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update | 468 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update |
| 469 * the [_currentTest] status accordingly. | 469 * the [_currentTest] status accordingly. |
| 470 */ | 470 */ |
| 471 _guard(tryBody, [finallyBody]) { | 471 guard(tryBody, [finallyBody]) { |
|
Bob Nystrom
2012/05/10 20:15:10
If we're going to make this public, we should give
nweiz
2012/05/10 21:01:09
I don't have any great ideas for this name. Maybe
| |
| 472 try { | 472 try { |
| 473 return tryBody(); | 473 return tryBody(); |
| 474 } catch (ExpectException e, var trace) { | 474 } catch (ExpectException e, var trace) { |
| 475 if (_state != _UNCAUGHT_ERROR) { | 475 if (_state != _UNCAUGHT_ERROR) { |
| 476 _tests[_currentTest].fail(e.message, | 476 _tests[_currentTest].fail(e.message, |
| 477 trace == null ? '' : trace.toString()); | 477 trace == null ? '' : trace.toString()); |
| 478 } | 478 } |
| 479 } catch (var e, var trace) { | 479 } catch (var e, var trace) { |
| 480 if (_state == _RUNNING_TEST) { | 480 if (_state == _RUNNING_TEST) { |
| 481 // If a random exception is thrown from within a test, we consider that | 481 // If a random exception is thrown from within a test, we consider that |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 494 } | 494 } |
| 495 | 495 |
| 496 /** | 496 /** |
| 497 * Runs a batch of tests, yielding whenever an asynchronous test starts | 497 * Runs a batch of tests, yielding whenever an asynchronous test starts |
| 498 * running. Tests will resume executing when such asynchronous test calls | 498 * running. Tests will resume executing when such asynchronous test calls |
| 499 * [done] or if it fails with an exception. | 499 * [done] or if it fails with an exception. |
| 500 */ | 500 */ |
| 501 _nextBatch() { | 501 _nextBatch() { |
| 502 while (_currentTest < _tests.length) { | 502 while (_currentTest < _tests.length) { |
| 503 final testCase = _tests[_currentTest]; | 503 final testCase = _tests[_currentTest]; |
| 504 _guard(() { | 504 guard(() { |
| 505 _callbacksCalled = 0; | 505 _callbacksCalled = 0; |
| 506 _state = _RUNNING_TEST; | 506 _state = _RUNNING_TEST; |
| 507 | 507 |
| 508 testCase.test(); | 508 testCase.test(); |
| 509 | 509 |
| 510 if (_state != _UNCAUGHT_ERROR) { | 510 if (_state != _UNCAUGHT_ERROR) { |
| 511 if (testCase.callbacks == _callbacksCalled) { | 511 if (testCase.callbacks == _callbacksCalled) { |
| 512 testCase.pass(); | 512 testCase.pass(); |
| 513 } | 513 } |
| 514 } | 514 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 567 } | 567 } |
| 568 _config.onInit(); | 568 _config.onInit(); |
| 569 | 569 |
| 570 // Immediately queue the suite up. It will run after a timeout (i.e. after | 570 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 571 // main() has returned). | 571 // main() has returned). |
| 572 _defer(_runTests); | 572 _defer(_runTests); |
| 573 } | 573 } |
| 574 | 574 |
| 575 /** Signature for a test function. */ | 575 /** Signature for a test function. */ |
| 576 typedef void TestFunction(); | 576 typedef void TestFunction(); |
| OLD | NEW |