| 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 * To import this library, specify the relative path to | 8 * To import this library, specify the relative path to |
| 9 * lib/unittest/unittest.dart. | 9 * lib/unittest/unittest.dart. |
| 10 * | 10 * |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 | 290 |
| 291 // TODO(sigmund): make a singleton const field when frog supports passing those | 291 // TODO(sigmund): make a singleton const field when frog supports passing those |
| 292 // as default values to named arguments. | 292 // as default values to named arguments. |
| 293 final _sentinel = const _Sentinel(); | 293 final _sentinel = const _Sentinel(); |
| 294 | 294 |
| 295 /** Simulates spread arguments using named arguments. */ | 295 /** Simulates spread arguments using named arguments. */ |
| 296 // TODO(sigmund): remove this class and simply use a closure with named | 296 // TODO(sigmund): remove this class and simply use a closure with named |
| 297 // arguments inside [_expectAsync], once bug 282 is fixed or frog is replaced by | 297 // arguments inside [_expectAsync], once bug 282 is fixed or frog is replaced by |
| 298 // dart2js. | 298 // dart2js. |
| 299 class _SpreadArgsHelper { | 299 class _SpreadArgsHelper { |
| 300 Function _callback; | 300 Function callback; |
| 301 int _expectedCalls = 0; | 301 int expectedCalls; |
| 302 int _calls = 0; | 302 int calls = 0; |
| 303 TestCase _testCase; | 303 TestCase testCase; |
| 304 Function _shouldCallBack; | 304 _SpreadArgsHelper(this.callback, this.expectedCalls) { |
| 305 Function _isDone; | 305 Expect.isTrue(_currentTest < _tests.length); |
| 306 | 306 testCase = _tests[_currentTest]; |
| 307 _init(Function callback, Function shouldCallBack, Function isDone, | 307 testCase.callbacks++; |
| 308 [expectedCalls = 0]) { | |
| 309 assert(_currentTest < _tests.length); | |
| 310 _callback = callback; | |
| 311 _shouldCallBack = shouldCallBack; | |
| 312 _isDone = isDone; | |
| 313 _expectedCalls = expectedCalls; | |
| 314 _testCase = _tests[_currentTest]; | |
| 315 _testCase.callbacks++; | |
| 316 } | |
| 317 | |
| 318 _SpreadArgsHelper(callback, shouldCallBack, isDone) { | |
| 319 _init(callback, shouldCallBack, isDone); | |
| 320 } | |
| 321 | |
| 322 _SpreadArgsHelper.fixedCallCount(callback, expectedCalls) { | |
| 323 _init(callback, _checkCallCount, _allCallsDone, expectedCalls); | |
| 324 } | |
| 325 | |
| 326 _SpreadArgsHelper.variableCallCount(callback, isDone) { | |
| 327 _init(callback, _always, isDone); | |
| 328 } | |
| 329 | |
| 330 _after() { | |
| 331 if (_isDone()) { | |
| 332 _handleAllCallbacksDone(); | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 _allCallsDone() => _calls == _expectedCalls; | |
| 337 | |
| 338 _always() { | |
| 339 // Always run except if the test is done. | |
| 340 if (_testCase.isComplete) { | |
| 341 _testCase.error( | |
| 342 'Callback called after already being marked as done ($_calls)', | |
| 343 ''); | |
| 344 _state = _UNCAUGHT_ERROR; | |
| 345 return false; | |
| 346 } else { | |
| 347 return true; | |
| 348 } | |
| 349 } | 308 } |
| 350 | 309 |
| 351 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, | 310 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, |
| 352 arg3 = _sentinel, arg4 = _sentinel]) { | 311 arg3 = _sentinel, arg4 = _sentinel]) { |
| 353 return guardAsync(() { | 312 return guardAsync(() { |
| 354 ++_calls; | 313 if (!_incrementCall()) { |
| 355 if (!_shouldCallBack()) { | |
| 356 return; | 314 return; |
| 357 } else if (arg0 == _sentinel) { | 315 } else if (arg0 == _sentinel) { |
| 358 return _callback(); | 316 return callback(); |
| 359 } else if (arg1 == _sentinel) { | 317 } else if (arg1 == _sentinel) { |
| 360 return _callback(arg0); | 318 return callback(arg0); |
| 361 } else if (arg2 == _sentinel) { | 319 } else if (arg2 == _sentinel) { |
| 362 return _callback(arg0, arg1); | 320 return callback(arg0, arg1); |
| 363 } else if (arg3 == _sentinel) { | 321 } else if (arg3 == _sentinel) { |
| 364 return _callback(arg0, arg1, arg2); | 322 return callback(arg0, arg1, arg2); |
| 365 } else if (arg4 == _sentinel) { | 323 } else if (arg4 == _sentinel) { |
| 366 return _callback(arg0, arg1, arg2, arg3); | 324 return callback(arg0, arg1, arg2, arg3); |
| 367 } else { | 325 } else { |
| 368 _testCase.error( | 326 testCase.error( |
| 369 'unittest lib does not support callbacks with more than 4 arguments', | 327 'unittest lib does not support callbacks with more than 4 arguments', |
| 370 ''); | 328 ''); |
| 371 _state = _UNCAUGHT_ERROR; | 329 _state = _UNCAUGHT_ERROR; |
| 372 } | 330 } |
| 373 }, | 331 }, () { if (calls == expectedCalls) callbackDone(); }); |
| 374 _after); | |
| 375 } | 332 } |
| 376 | 333 |
| 377 invoke0() { | 334 invoke0() { |
| 378 return guardAsync( | 335 return guardAsync( |
| 379 () { if (_shouldCallBack()) _callback(); }, | 336 () => _incrementCall() ? callback() : null, |
| 380 _after); | 337 () { if (calls == expectedCalls) callbackDone(); }); |
| 381 } | 338 } |
| 382 | 339 |
| 383 invoke1(arg1) { | 340 invoke1(arg1) { |
| 384 return guardAsync( | 341 return guardAsync( |
| 385 () { if (_shouldCallBack()) _callback(arg1); }, | 342 () => _incrementCall() ? callback(arg1) : null, |
| 386 _after); | 343 () { if (calls == expectedCalls) callbackDone(); }); |
| 387 } | 344 } |
| 388 | 345 |
| 389 invoke2(arg1, arg2) { | 346 invoke2(arg1, arg2) { |
| 390 return guardAsync( | 347 return guardAsync( |
| 391 () { if (_shouldCallBack()) _callback(arg1, arg2); }, | 348 () => _incrementCall() ? callback(arg1, arg2) : null, |
| 392 _after); | 349 () { if (calls == expectedCalls) callbackDone(); }); |
| 393 } | 350 } |
| 394 | 351 |
| 395 /** Returns false if we exceded the number of expected calls. */ | 352 /** Returns false if we exceded the number of expected calls. */ |
| 396 bool _checkCallCount() { | 353 bool _incrementCall() { |
| 397 if (_calls > _expectedCalls) { | 354 calls++; |
| 398 _testCase.error( | 355 if (calls > expectedCalls) { |
| 399 'Callback called more times than expected ' | 356 testCase.error( |
| 400 '($_calls > $_expectedCalls)', | 357 'Callback called more times than expected ($calls > $expectedCalls)', |
| 401 ''); | 358 ''); |
| 402 _state = _UNCAUGHT_ERROR; | 359 _state = _UNCAUGHT_ERROR; |
| 403 return false; | 360 return false; |
| 404 } | 361 } |
| 405 return true; | 362 return true; |
| 406 } | 363 } |
| 407 } | 364 } |
| 408 | 365 |
| 409 /** | 366 /** |
| 410 * Indicate that [callback] is expected to be called a [count] number of times | 367 * Indicate that [callback] is expected to be called a [count] number of times |
| 411 * (by default 1). The unittest framework will wait for the callback to run the | 368 * (by default 1). The unittest framework will wait for the callback to run the |
| 412 * specified [count] times before it continues with the following test. Using | 369 * specified [count] times before it continues with the following test. Using |
| 413 * [_expectAsync] will also ensure that errors that occur within [callback] are | 370 * [_expectAsync] will also ensure that errors that occur within [callback] are |
| 414 * tracked and reported. [callback] should take between 0 and 4 positional | 371 * tracked and reported. [callback] should take between 0 and 4 positional |
| 415 * arguments (named arguments are not supported here). | 372 * arguments (named arguments are not supported here). |
| 416 */ | 373 */ |
| 417 Function _expectAsync(Function callback, [int count = 1]) { | 374 Function _expectAsync(Function callback, [int count = 1]) { |
| 418 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke; | 375 return new _SpreadArgsHelper(callback, count).invoke; |
| 419 } | 376 } |
| 420 | 377 |
| 421 /** | 378 /** |
| 422 * Indicate that [callback] is expected to be called a [count] number of times | 379 * Indicate that [callback] is expected to be called a [count] number of times |
| 423 * (by default 1). The unittest framework will wait for the callback to run the | 380 * (by default 1). The unittest framework will wait for the callback to run the |
| 424 * specified [count] times before it continues with the following test. Using | 381 * specified [count] times before it continues with the following test. Using |
| 425 * [expectAsync0] will also ensure that errors that occur within [callback] are | 382 * [expectAsync0] will also ensure that errors that occur within [callback] are |
| 426 * tracked and reported. [callback] should take 0 positional arguments (named | 383 * tracked and reported. [callback] should take 0 positional arguments (named |
| 427 * arguments are not supported). | 384 * arguments are not supported). |
| 428 */ | 385 */ |
| 429 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 386 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 430 Function expectAsync0(Function callback, [int count = 1]) { | 387 Function expectAsync0(Function callback, [int count = 1]) { |
| 431 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke0; | 388 return new _SpreadArgsHelper(callback, count).invoke0; |
| 432 } | 389 } |
| 433 | 390 |
| 434 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ | 391 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ |
| 435 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 392 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 436 Function expectAsync1(Function callback, [int count = 1]) { | 393 Function expectAsync1(Function callback, [int count = 1]) { |
| 437 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke1; | 394 return new _SpreadArgsHelper(callback, count).invoke1; |
| 438 } | 395 } |
| 439 | 396 |
| 440 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ | 397 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ |
| 441 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 398 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 442 Function expectAsync2(Function callback, [int count = 1]) { | 399 Function expectAsync2(Function callback, [int count = 1]) { |
| 443 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke2; | 400 return new _SpreadArgsHelper(callback, count).invoke2; |
| 444 } | |
| 445 | |
| 446 /** | |
| 447 * Indicate that [callback] is expected to be called until [isDone] returns | |
| 448 * true. The unittest framework checks [isDone] after each callback and only | |
| 449 * when it returns true will it continue with the following test. Using | |
| 450 * [expectAsyncUntil] will also ensure that errors that occur within | |
| 451 * [callback] are tracked and reported. [callback] should take between 0 and | |
| 452 * 4 positional arguments (named arguments are not supported). | |
| 453 */ | |
| 454 Function _expectAsyncUntil(Function callback, Function isDone) { | |
| 455 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke; | |
| 456 } | |
| 457 | |
| 458 /** | |
| 459 * Indicate that [callback] is expected to be called until [isDone] returns | |
| 460 * true. The unittest framework check [isDone] after each callback and only | |
| 461 * when it returns true will it continue with the following test. Using | |
| 462 * [expectAsyncUntil0] will also ensure that errors that occur within | |
| 463 * [callback] are tracked and reported. [callback] should take 0 positional | |
| 464 * arguments (named arguments are not supported). | |
| 465 */ | |
| 466 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | |
| 467 Function expectAsyncUntil0(Function callback, Function isDone) { | |
| 468 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke0; | |
| 469 } | |
| 470 | |
| 471 /** | |
| 472 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. | |
| 473 */ | |
| 474 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | |
| 475 Function expectAsyncUntil1(Function callback, Function isDone) { | |
| 476 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke1; | |
| 477 } | |
| 478 | |
| 479 /** | |
| 480 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | |
| 481 */ | |
| 482 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | |
| 483 Function expectAsyncUntil2(Function callback, Function isDone) { | |
| 484 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; | |
| 485 } | 401 } |
| 486 | 402 |
| 487 /** | 403 /** |
| 488 * Creates a new named group of tests. Calls to group() or test() within the | 404 * Creates a new named group of tests. Calls to group() or test() within the |
| 489 * body of the function passed to this will inherit this group's description. | 405 * body of the function passed to this will inherit this group's description. |
| 490 */ | 406 */ |
| 491 void group(String description, void body()) { | 407 void group(String description, void body()) { |
| 492 ensureInitialized(); | 408 ensureInitialized(); |
| 493 | 409 |
| 494 // Concatenate the new group. | 410 // Concatenate the new group. |
| 495 final oldGroup = _currentGroup; | 411 final oldGroup = _currentGroup; |
| 496 if (_currentGroup != '') { | 412 if (_currentGroup != '') { |
| 497 // Add a space. | 413 // Add a space. |
| 498 _currentGroup = '$_currentGroup $description'; | 414 _currentGroup = '$_currentGroup $description'; |
| 499 } else { | 415 } else { |
| 500 // The first group. | 416 // The first group. |
| 501 _currentGroup = description; | 417 _currentGroup = description; |
| 502 } | 418 } |
| 503 | 419 |
| 504 try { | 420 try { |
| 505 body(); | 421 body(); |
| 506 } finally { | 422 } finally { |
| 507 // Now that the group is over, restore the previous one. | 423 // Now that the group is over, restore the previous one. |
| 508 _currentGroup = oldGroup; | 424 _currentGroup = oldGroup; |
| 509 } | 425 } |
| 510 } | 426 } |
| 511 | 427 |
| 512 /** Called by subclasses to indicate that an asynchronous test completed. */ | 428 /** Called by subclasses to indicate that an asynchronous test completed. */ |
| 513 void _handleAllCallbacksDone() { | 429 void callbackDone() { |
| 514 // TODO (gram): we defer this to give the nextBatch recursive | 430 // TODO (gram): we defer this to give the nextBatch recursive |
| 515 // stack a chance to unwind. This is a temporary hack but | 431 // stack a chance to unwind. This is a temporary hack but |
| 516 // really a bunch of code here needs to be fixed. We have a | 432 // really a bunch of code here needs to be fixed. We have a |
| 517 // single array that is being iterated through by nextBatch(), | 433 // single array that is being iterated through by nextBatch(), |
| 518 // which is recursively invoked in the case of async tests that | 434 // which is recursively invoked in the case of async tests that |
| 519 // run synchronously. Bad things can then happen. | 435 // run synchronously. Bad things can then happen. |
| 520 _defer(() { | 436 _defer(() { |
| 521 _callbacksCalled++; | 437 _callbacksCalled++; |
| 522 if (_currentTest < _tests.length) { | 438 if (_currentTest < _tests.length) { |
| 523 final testCase = _tests[_currentTest]; | 439 final testCase = _tests[_currentTest]; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 * Runs a batch of tests, yielding whenever an asynchronous test starts | 535 * Runs a batch of tests, yielding whenever an asynchronous test starts |
| 620 * running. Tests will resume executing when such asynchronous test calls | 536 * running. Tests will resume executing when such asynchronous test calls |
| 621 * [done] or if it fails with an exception. | 537 * [done] or if it fails with an exception. |
| 622 */ | 538 */ |
| 623 _nextBatch() { | 539 _nextBatch() { |
| 624 while (_currentTest < _tests.length) { | 540 while (_currentTest < _tests.length) { |
| 625 final testCase = _tests[_currentTest]; | 541 final testCase = _tests[_currentTest]; |
| 626 guardAsync(() { | 542 guardAsync(() { |
| 627 _callbacksCalled = 0; | 543 _callbacksCalled = 0; |
| 628 _state = _RUNNING_TEST; | 544 _state = _RUNNING_TEST; |
| 545 |
| 629 testCase.test(); | 546 testCase.test(); |
| 630 | 547 |
| 631 if (_state != _UNCAUGHT_ERROR) { | 548 if (_state != _UNCAUGHT_ERROR) { |
| 632 if (testCase.callbacks == _callbacksCalled) { | 549 if (testCase.callbacks == _callbacksCalled) { |
| 633 testCase.pass(); | 550 testCase.pass(); |
| 634 } | 551 } |
| 635 } | 552 } |
| 636 }); | 553 }); |
| 637 | 554 |
| 638 if (!testCase.isComplete && testCase.callbacks > 0) return; | 555 if (!testCase.isComplete && testCase.callbacks > 0) return; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 throw new ExpectException(message); | 589 throw new ExpectException(message); |
| 673 } | 590 } |
| 674 | 591 |
| 675 /** | 592 /** |
| 676 * Lazily initializes the test library if not already initialized. | 593 * Lazily initializes the test library if not already initialized. |
| 677 */ | 594 */ |
| 678 ensureInitialized() { | 595 ensureInitialized() { |
| 679 if (_state != _UNINITIALIZED) return; | 596 if (_state != _UNINITIALIZED) return; |
| 680 | 597 |
| 681 _tests = <TestCase>[]; | 598 _tests = <TestCase>[]; |
| 682 _uncaughtErrorMessage = null; | |
| 683 _currentTest = 0; | |
| 684 _currentGroup = ''; | 599 _currentGroup = ''; |
| 685 _state = _READY; | 600 _state = _READY; |
| 686 _testRunner = _nextBatch; | 601 _testRunner = _nextBatch; |
| 687 | 602 |
| 688 if (_config == null) { | 603 if (_config == null) { |
| 689 _config = new Configuration(); | 604 _config = new Configuration(); |
| 690 } | 605 } |
| 691 _config.onInit(); | 606 _config.onInit(); |
| 692 | 607 |
| 693 // Immediately queue the suite up. It will run after a timeout (i.e. after | 608 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 694 // main() has returned). | 609 // main() has returned). |
| 695 _defer(_runTests); | 610 _defer(_runTests); |
| 696 } | 611 } |
| 697 | 612 |
| 698 /** Signature for a test function. */ | 613 /** Signature for a test function. */ |
| 699 typedef void TestFunction(); | 614 typedef void TestFunction(); |
| OLD | NEW |