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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 #source('map_matchers.dart'); | 148 #source('map_matchers.dart'); |
149 #source('matcher.dart'); | 149 #source('matcher.dart'); |
150 #source('numeric_matchers.dart'); | 150 #source('numeric_matchers.dart'); |
151 #source('operator_matchers.dart'); | 151 #source('operator_matchers.dart'); |
152 #source('string_matchers.dart'); | 152 #source('string_matchers.dart'); |
153 #source('test_case.dart'); | 153 #source('test_case.dart'); |
154 | 154 |
155 /** [Configuration] used by the unittest library. */ | 155 /** [Configuration] used by the unittest library. */ |
156 Configuration _config = null; | 156 Configuration _config = null; |
157 | 157 |
158 /** Set the [Configuration] used by the unittest library. */ | 158 /** |
159 void configure(Configuration config) { | 159 * Set the [Configuration] used by the unittest library. Returns any |
160 * previous configuration. | |
161 */ | |
162 Configuration configure(Configuration config) { | |
163 Configuration _oldConfig = _config; | |
160 _config = config; | 164 _config = config; |
165 return _oldConfig; | |
161 } | 166 } |
162 | 167 |
163 /** | 168 /** |
164 * Description text of the current test group. If multiple groups are nested, | 169 * Description text of the current test group. If multiple groups are nested, |
165 * this will contain all of their text concatenated. | 170 * this will contain all of their text concatenated. |
166 */ | 171 */ |
167 String _currentGroup = ''; | 172 String _currentGroup = ''; |
168 | 173 |
169 /** Tests executed in this suite. */ | 174 /** Tests executed in this suite. */ |
170 List<TestCase> _tests; | 175 List<TestCase> _tests; |
171 | 176 |
172 /** | 177 /** |
173 * Callback used to run tests. Entrypoints can replace this with their own | 178 * Callback used to run tests. Entrypoints can replace this with their own |
174 * if they want. | 179 * if they want. |
175 */ | 180 */ |
176 Function _testRunner; | 181 Function _testRunner; |
177 | 182 |
183 /** Setup function called before each test in a group */ | |
184 Function _testSetup; | |
185 | |
186 /** Teardown function called after each test in a group */ | |
187 Function _testTeardown; | |
188 | |
178 /** Current test being executed. */ | 189 /** Current test being executed. */ |
179 int _currentTest = 0; | 190 int _currentTest = 0; |
180 | 191 |
181 /** Total number of callbacks that have been executed in the current test. */ | 192 /** Total number of callbacks that have been executed in the current test. */ |
182 int _callbacksCalled = 0; | 193 int _callbacksCalled = 0; |
183 | 194 |
184 final _UNINITIALIZED = 0; | 195 final _UNINITIALIZED = 0; |
185 final _READY = 1; | 196 final _READY = 1; |
186 final _RUNNING_TEST = 2; | 197 final _RUNNING_TEST = 2; |
187 | 198 |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 */ | 508 */ |
498 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 509 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
499 Function expectAsyncUntil2(Function callback, Function isDone) { | 510 Function expectAsyncUntil2(Function callback, Function isDone) { |
500 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; | 511 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; |
501 } | 512 } |
502 | 513 |
503 /** | 514 /** |
504 * Creates a new named group of tests. Calls to group() or test() within the | 515 * 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. | 516 * body of the function passed to this will inherit this group's description. |
506 */ | 517 */ |
507 void group(String description, void body()) { | 518 void group(String description, void body(), |
519 [Function setupTest, Function teardownTest]) { | |
Bob Nystrom
2012/06/20 20:21:22
Since you added setUp() and tearDown(), do you sti
gram
2012/06/20 21:11:47
Done.
| |
508 ensureInitialized(); | 520 ensureInitialized(); |
509 | 521 |
510 // Concatenate the new group. | 522 // Concatenate the new group. |
511 final oldGroup = _currentGroup; | 523 final parentGroup = _currentGroup; |
512 if (_currentGroup != '') { | 524 if (_currentGroup != '') { |
513 // Add a space. | 525 // Add a space. |
514 _currentGroup = '$_currentGroup $description'; | 526 _currentGroup = '$_currentGroup $description'; |
515 } else { | 527 } else { |
516 // The first group. | 528 // The first group. |
517 _currentGroup = description; | 529 _currentGroup = description; |
518 } | 530 } |
519 | 531 |
532 // Groups can be nested, so we need to preserve the current | |
533 // settings for test setup/teardown. | |
534 Function parentSetup = _testSetup; | |
535 Function parentTeardown = _testTeardown; | |
536 | |
520 try { | 537 try { |
538 _testSetup = null; | |
539 _testTeardown = null; | |
521 body(); | 540 body(); |
522 } finally { | 541 } finally { |
523 // Now that the group is over, restore the previous one. | 542 // Now that the group is over, restore the previous one. |
524 _currentGroup = oldGroup; | 543 _currentGroup = parentGroup; |
544 _testSetup = parentSetup; | |
545 _testTeardown = parentTeardown; | |
525 } | 546 } |
526 } | 547 } |
527 | 548 |
549 /** | |
550 * Register a [setUp] function for a test [group]. This function will | |
551 * be called before each test in the group is run. Note that if groups | |
552 * are nested only the most locally scoped [setUp] function will be run. | |
Bob Nystrom
2012/06/20 20:21:22
It might be useful at some point to actually allow
gram
2012/06/20 21:11:47
I think this could be tricky. There may be cases w
| |
553 * [setUp] and [tearDown] should be called within the [group] before any | |
554 * calls to [test]. | |
555 */ | |
556 | |
557 void setUp(Function setupTest) { | |
Bob Nystrom
2012/06/20 20:21:22
Remove blank line above this.
gram
2012/06/20 21:11:47
Done.
| |
558 _testSetup = setupTest; | |
559 } | |
560 | |
561 /** | |
562 * Register a [tearDown] function for a test [group]. This function will | |
563 * be called after each test in the group is run. Note that if groups | |
564 * are nested only the most locally scoped [tearDown] function will be run. | |
565 * [setUp] and [tearDown] should be called within the [group] before any | |
566 * calls to [test]. | |
567 */ | |
568 | |
569 void tearDown(Function teardownTest) { | |
Bob Nystrom
2012/06/20 20:21:22
Remove blank line above this.
gram
2012/06/20 21:11:47
Done.
| |
570 _testTeardown = teardownTest; | |
571 } | |
572 | |
528 /** Called by subclasses to indicate that an asynchronous test completed. */ | 573 /** Called by subclasses to indicate that an asynchronous test completed. */ |
529 void _handleAllCallbacksDone() { | 574 void _handleAllCallbacksDone() { |
530 // TODO (gram): we defer this to give the nextBatch recursive | 575 // TODO (gram): we defer this to give the nextBatch recursive |
531 // stack a chance to unwind. This is a temporary hack but | 576 // 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 | 577 // really a bunch of code here needs to be fixed. We have a |
533 // single array that is being iterated through by nextBatch(), | 578 // single array that is being iterated through by nextBatch(), |
534 // which is recursively invoked in the case of async tests that | 579 // which is recursively invoked in the case of async tests that |
535 // run synchronously. Bad things can then happen. | 580 // run synchronously. Bad things can then happen. |
536 _defer(() { | 581 _defer(() { |
537 _callbacksCalled++; | 582 _callbacksCalled++; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 * Runs a batch of tests, yielding whenever an asynchronous test starts | 686 * Runs a batch of tests, yielding whenever an asynchronous test starts |
642 * running. Tests will resume executing when such asynchronous test calls | 687 * running. Tests will resume executing when such asynchronous test calls |
643 * [done] or if it fails with an exception. | 688 * [done] or if it fails with an exception. |
644 */ | 689 */ |
645 _nextBatch() { | 690 _nextBatch() { |
646 while (_currentTest < _tests.length) { | 691 while (_currentTest < _tests.length) { |
647 final testCase = _tests[_currentTest]; | 692 final testCase = _tests[_currentTest]; |
648 guardAsync(() { | 693 guardAsync(() { |
649 _callbacksCalled = 0; | 694 _callbacksCalled = 0; |
650 _state = _RUNNING_TEST; | 695 _state = _RUNNING_TEST; |
651 testCase.test(); | 696 testCase.run(); |
652 | 697 |
653 if (_state != _UNCAUGHT_ERROR) { | 698 if (_state != _UNCAUGHT_ERROR) { |
654 if (testCase.callbacks == _callbacksCalled) { | 699 if (testCase.callbacks == _callbacksCalled) { |
655 testCase.pass(); | 700 testCase.pass(); |
656 } | 701 } |
657 } | 702 } |
658 }); | 703 }); |
659 | 704 |
660 if (!testCase.isComplete && testCase.callbacks > 0) return; | 705 if (!testCase.isComplete && testCase.callbacks > 0) return; |
661 | 706 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 _config.onInit(); | 758 _config.onInit(); |
714 | 759 |
715 // Immediately queue the suite up. It will run after a timeout (i.e. after | 760 // Immediately queue the suite up. It will run after a timeout (i.e. after |
716 // main() has returned). | 761 // main() has returned). |
717 _defer(_runTests); | 762 _defer(_runTests); |
718 } | 763 } |
719 | 764 |
720 /** Signature for a test function. */ | 765 /** Signature for a test function. */ |
721 typedef void TestFunction(); | 766 typedef void TestFunction(); |
722 | 767 |
OLD | NEW |