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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 | 168 |
169 /** Tests executed in this suite. */ | 169 /** Tests executed in this suite. */ |
170 List<TestCase> _tests; | 170 List<TestCase> _tests; |
171 | 171 |
172 /** | 172 /** |
173 * Callback used to run tests. Entrypoints can replace this with their own | 173 * Callback used to run tests. Entrypoints can replace this with their own |
174 * if they want. | 174 * if they want. |
175 */ | 175 */ |
176 Function _testRunner; | 176 Function _testRunner; |
177 | 177 |
178 /** Setup function called before each test in a group */ | |
179 Function _testSetup; | |
180 | |
181 /** Teardown function called after each test in a group */ | |
182 Function _testTeardown; | |
183 | |
178 /** Current test being executed. */ | 184 /** Current test being executed. */ |
179 int _currentTest = 0; | 185 int _currentTest = 0; |
180 | 186 |
181 /** Total number of callbacks that have been executed in the current test. */ | 187 /** Total number of callbacks that have been executed in the current test. */ |
182 int _callbacksCalled = 0; | 188 int _callbacksCalled = 0; |
183 | 189 |
184 final _UNINITIALIZED = 0; | 190 final _UNINITIALIZED = 0; |
185 final _READY = 1; | 191 final _READY = 1; |
186 final _RUNNING_TEST = 2; | 192 final _RUNNING_TEST = 2; |
187 | 193 |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 */ | 503 */ |
498 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 504 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
499 Function expectAsyncUntil2(Function callback, Function isDone) { | 505 Function expectAsyncUntil2(Function callback, Function isDone) { |
500 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; | 506 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; |
501 } | 507 } |
502 | 508 |
503 /** | 509 /** |
504 * Creates a new named group of tests. Calls to group() or test() within the | 510 * Creates a new named group of tests. Calls to group() or test() within the |
505 * body of the function passed to this will inherit this group's description. | 511 * body of the function passed to this will inherit this group's description. |
506 */ | 512 */ |
507 void group(String description, void body()) { | 513 void group(String description, void body(), |
514 [Function setupTest, Function teardownTest]) { | |
Bob Nystrom
2012/06/19 23:38:33
Instead of taking these functions as arguments, ho
gram
2012/06/20 17:44:14
Done.
| |
508 ensureInitialized(); | 515 ensureInitialized(); |
509 | 516 |
510 // Concatenate the new group. | 517 // Concatenate the new group. |
511 final oldGroup = _currentGroup; | 518 final parentGroup = _currentGroup; |
512 if (_currentGroup != '') { | 519 if (_currentGroup != '') { |
513 // Add a space. | 520 // Add a space. |
514 _currentGroup = '$_currentGroup $description'; | 521 _currentGroup = '$_currentGroup $description'; |
515 } else { | 522 } else { |
516 // The first group. | 523 // The first group. |
517 _currentGroup = description; | 524 _currentGroup = description; |
518 } | 525 } |
519 | 526 |
527 // Groups can be nested, so we need to preserve the current | |
528 // settings for test setup/teardown | |
Bob Nystrom
2012/06/19 23:38:33
.
gram
2012/06/20 17:44:14
Done.
| |
529 Function parentSetup = _testSetup; | |
530 Function parentTeardown = _testTeardown; | |
531 | |
520 try { | 532 try { |
533 _testSetup = setupTest; | |
534 _testTeardown = teardownTest; | |
521 body(); | 535 body(); |
522 } finally { | 536 } finally { |
523 // Now that the group is over, restore the previous one. | 537 // Now that the group is over, restore the previous one. |
524 _currentGroup = oldGroup; | 538 _currentGroup = parentGroup; |
539 _testSetup = parentSetup; | |
540 _testTeardown = parentTeardown; | |
525 } | 541 } |
526 } | 542 } |
527 | 543 |
528 /** Called by subclasses to indicate that an asynchronous test completed. */ | 544 /** Called by subclasses to indicate that an asynchronous test completed. */ |
529 void _handleAllCallbacksDone() { | 545 void _handleAllCallbacksDone() { |
530 // TODO (gram): we defer this to give the nextBatch recursive | 546 // TODO (gram): we defer this to give the nextBatch recursive |
531 // stack a chance to unwind. This is a temporary hack but | 547 // stack a chance to unwind. This is a temporary hack but |
532 // really a bunch of code here needs to be fixed. We have a | 548 // really a bunch of code here needs to be fixed. We have a |
533 // single array that is being iterated through by nextBatch(), | 549 // single array that is being iterated through by nextBatch(), |
534 // which is recursively invoked in the case of async tests that | 550 // which is recursively invoked in the case of async tests that |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 * Runs a batch of tests, yielding whenever an asynchronous test starts | 657 * Runs a batch of tests, yielding whenever an asynchronous test starts |
642 * running. Tests will resume executing when such asynchronous test calls | 658 * running. Tests will resume executing when such asynchronous test calls |
643 * [done] or if it fails with an exception. | 659 * [done] or if it fails with an exception. |
644 */ | 660 */ |
645 _nextBatch() { | 661 _nextBatch() { |
646 while (_currentTest < _tests.length) { | 662 while (_currentTest < _tests.length) { |
647 final testCase = _tests[_currentTest]; | 663 final testCase = _tests[_currentTest]; |
648 guardAsync(() { | 664 guardAsync(() { |
649 _callbacksCalled = 0; | 665 _callbacksCalled = 0; |
650 _state = _RUNNING_TEST; | 666 _state = _RUNNING_TEST; |
651 testCase.test(); | 667 testCase.run(); |
652 | 668 |
653 if (_state != _UNCAUGHT_ERROR) { | 669 if (_state != _UNCAUGHT_ERROR) { |
654 if (testCase.callbacks == _callbacksCalled) { | 670 if (testCase.callbacks == _callbacksCalled) { |
655 testCase.pass(); | 671 testCase.pass(); |
656 } | 672 } |
657 } | 673 } |
658 }); | 674 }); |
659 | 675 |
660 if (!testCase.isComplete && testCase.callbacks > 0) return; | 676 if (!testCase.isComplete && testCase.callbacks > 0) return; |
661 | 677 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 _config.onInit(); | 729 _config.onInit(); |
714 | 730 |
715 // Immediately queue the suite up. It will run after a timeout (i.e. after | 731 // Immediately queue the suite up. It will run after a timeout (i.e. after |
716 // main() has returned). | 732 // main() has returned). |
717 _defer(_runTests); | 733 _defer(_runTests); |
718 } | 734 } |
719 | 735 |
720 /** Signature for a test function. */ | 736 /** Signature for a test function. */ |
721 typedef void TestFunction(); | 737 typedef void TestFunction(); |
722 | 738 |
OLD | NEW |