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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 class _SpreadArgsHelper { | 309 class _SpreadArgsHelper { |
299 Function _callback; | 310 Function _callback; |
300 int _expectedCalls; | 311 int _expectedCalls; |
301 int _calls = 0; | 312 int _calls = 0; |
302 TestCase _testCase; | 313 TestCase _testCase; |
303 Function _shouldCallBack; | 314 Function _shouldCallBack; |
304 Function _isDone; | 315 Function _isDone; |
305 | 316 |
306 _init(Function callback, Function shouldCallBack, Function isDone, | 317 _init(Function callback, Function shouldCallBack, Function isDone, |
307 [expectedCalls = 0]) { | 318 [expectedCalls = 0]) { |
| 319 ensureInitialized(); |
308 assert(_currentTest < _tests.length); | 320 assert(_currentTest < _tests.length); |
309 _callback = callback; | 321 _callback = callback; |
310 _shouldCallBack = shouldCallBack; | 322 _shouldCallBack = shouldCallBack; |
311 _isDone = isDone; | 323 _isDone = isDone; |
312 _expectedCalls = expectedCalls; | 324 _expectedCalls = expectedCalls; |
313 _testCase = _tests[_currentTest]; | 325 _testCase = _tests[_currentTest]; |
314 if (expectedCalls > 0) { | 326 if (expectedCalls > 0) { |
315 _testCase.callbacks++; | 327 _testCase.callbacks++; |
316 } | 328 } |
317 } | 329 } |
318 | 330 |
319 _SpreadArgsHelper(callback, shouldCallBack, isDone) { | 331 _SpreadArgsHelper(callback, shouldCallBack, isDone) { |
320 _init(callback, shouldCallBack, isDone); | 332 _init(callback, shouldCallBack, isDone); |
321 } | 333 } |
322 | 334 |
323 _SpreadArgsHelper.fixedCallCount(callback, expectedCalls) { | 335 _SpreadArgsHelper.fixedCallCount(callback, expectedCalls) { |
324 _init(callback, _checkCallCount, _allCallsDone, expectedCalls); | 336 _init(callback, _checkCallCount, _allCallsDone, expectedCalls); |
325 } | 337 } |
326 | 338 |
327 _SpreadArgsHelper.variableCallCount(callback, isDone) { | 339 _SpreadArgsHelper.variableCallCount(callback, isDone) { |
328 _init(callback, _always, isDone); | 340 _init(callback, _always, isDone, 1); |
329 } | 341 } |
330 | 342 |
331 _after() { | 343 _after() { |
332 if (_isDone()) { | 344 if (_isDone()) { |
333 _handleAllCallbacksDone(); | 345 _handleAllCallbacksDone(); |
334 } | 346 } |
335 } | 347 } |
336 | 348 |
337 _allCallsDone() => _calls == _expectedCalls; | 349 _allCallsDone() => _calls == _expectedCalls; |
338 | 350 |
339 _always() { | 351 _always() { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 } | 513 } |
502 | 514 |
503 /** | 515 /** |
504 * Creates a new named group of tests. Calls to group() or test() within the | 516 * 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. | 517 * body of the function passed to this will inherit this group's description. |
506 */ | 518 */ |
507 void group(String description, void body()) { | 519 void group(String description, void body()) { |
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. |
| 553 * [setUp] and [tearDown] should be called within the [group] before any |
| 554 * calls to [test]. |
| 555 */ |
| 556 void setUp(Function setupTest) { |
| 557 _testSetup = setupTest; |
| 558 } |
| 559 |
| 560 /** |
| 561 * Register a [tearDown] function for a test [group]. This function will |
| 562 * be called after each test in the group is run. Note that if groups |
| 563 * are nested only the most locally scoped [tearDown] function will be run. |
| 564 * [setUp] and [tearDown] should be called within the [group] before any |
| 565 * calls to [test]. |
| 566 */ |
| 567 void tearDown(Function teardownTest) { |
| 568 _testTeardown = teardownTest; |
| 569 } |
| 570 |
528 /** Called by subclasses to indicate that an asynchronous test completed. */ | 571 /** Called by subclasses to indicate that an asynchronous test completed. */ |
529 void _handleAllCallbacksDone() { | 572 void _handleAllCallbacksDone() { |
530 // TODO (gram): we defer this to give the nextBatch recursive | 573 // TODO (gram): we defer this to give the nextBatch recursive |
531 // stack a chance to unwind. This is a temporary hack but | 574 // 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 | 575 // really a bunch of code here needs to be fixed. We have a |
533 // single array that is being iterated through by nextBatch(), | 576 // single array that is being iterated through by nextBatch(), |
534 // which is recursively invoked in the case of async tests that | 577 // which is recursively invoked in the case of async tests that |
535 // run synchronously. Bad things can then happen. | 578 // run synchronously. Bad things can then happen. |
536 _defer(() { | 579 _defer(() { |
537 _callbacksCalled++; | 580 _callbacksCalled++; |
538 if (_currentTest < _tests.length) { | 581 if (_currentTest < _tests.length) { |
539 final testCase = _tests[_currentTest]; | 582 final testCase = _tests[_currentTest]; |
540 if (_callbacksCalled > testCase.callbacks) { | 583 if (_callbacksCalled > testCase.callbacks) { |
541 final expected = testCase.callbacks; | 584 final expected = testCase.callbacks; |
542 testCase.error( | 585 testCase.error( |
543 'More calls to callbackDone() than expected. ' | 586 'More calls to _handleAllCallbacksDone() than expected. ' |
544 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); | 587 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); |
545 _state = _UNCAUGHT_ERROR; | 588 _state = _UNCAUGHT_ERROR; |
546 } else if ((_callbacksCalled == testCase.callbacks) && | 589 } else if ((_callbacksCalled == testCase.callbacks) && |
547 (_state != _RUNNING_TEST)) { | 590 (_state != _RUNNING_TEST)) { |
548 if (testCase.result == null) { | 591 if (testCase.result == null) { |
549 testCase.pass(); | 592 testCase.pass(); |
550 } | 593 } |
551 _currentTest++; | 594 _currentTest++; |
552 _testRunner(); | 595 _testRunner(); |
553 } | 596 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 * Runs a batch of tests, yielding whenever an asynchronous test starts | 684 * Runs a batch of tests, yielding whenever an asynchronous test starts |
642 * running. Tests will resume executing when such asynchronous test calls | 685 * running. Tests will resume executing when such asynchronous test calls |
643 * [done] or if it fails with an exception. | 686 * [done] or if it fails with an exception. |
644 */ | 687 */ |
645 _nextBatch() { | 688 _nextBatch() { |
646 while (_currentTest < _tests.length) { | 689 while (_currentTest < _tests.length) { |
647 final testCase = _tests[_currentTest]; | 690 final testCase = _tests[_currentTest]; |
648 guardAsync(() { | 691 guardAsync(() { |
649 _callbacksCalled = 0; | 692 _callbacksCalled = 0; |
650 _state = _RUNNING_TEST; | 693 _state = _RUNNING_TEST; |
651 testCase.test(); | 694 testCase.run(); |
652 | 695 |
653 if (_state != _UNCAUGHT_ERROR) { | 696 if (_state != _UNCAUGHT_ERROR) { |
654 if (testCase.callbacks == _callbacksCalled) { | 697 if (testCase.callbacks == _callbacksCalled) { |
655 testCase.pass(); | 698 testCase.pass(); |
656 } | 699 } |
657 } | 700 } |
658 }); | 701 }); |
659 | 702 |
660 if (!testCase.isComplete && testCase.callbacks > 0) return; | 703 if (!testCase.isComplete && testCase.callbacks > 0) return; |
661 | 704 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 _config.onInit(); | 756 _config.onInit(); |
714 | 757 |
715 // Immediately queue the suite up. It will run after a timeout (i.e. after | 758 // Immediately queue the suite up. It will run after a timeout (i.e. after |
716 // main() has returned). | 759 // main() has returned). |
717 _defer(_runTests); | 760 _defer(_runTests); |
718 } | 761 } |
719 | 762 |
720 /** Signature for a test function. */ | 763 /** Signature for a test function. */ |
721 typedef void TestFunction(); | 764 typedef void TestFunction(); |
722 | 765 |
OLD | NEW |