| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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, install the | 8 * To import this library, install the |
| 9 * [unittest package](http://pub.dartlang.org/packages/unittest) via the pub | 9 * [unittest package](http://pub.dartlang.org/packages/unittest) via the pub |
| 10 * package manager. See the [Getting Started](http://pub.dartlang.org/doc) | 10 * package manager. See the [Getting Started](http://pub.dartlang.org/doc) |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 191 |
| 192 /** Separator used between group names and test names. */ | 192 /** Separator used between group names and test names. */ |
| 193 String groupSep = ' '; | 193 String groupSep = ' '; |
| 194 | 194 |
| 195 /** Tests executed in this suite. */ | 195 /** Tests executed in this suite. */ |
| 196 List<TestCase> _tests; | 196 List<TestCase> _tests; |
| 197 | 197 |
| 198 /** Get the list of tests. */ | 198 /** Get the list of tests. */ |
| 199 List<TestCase> get testCases => _tests; | 199 List<TestCase> get testCases => _tests; |
| 200 | 200 |
| 201 /** | |
| 202 * Callback used to run tests. Entrypoints can replace this with their own | |
| 203 * if they want. | |
| 204 */ | |
| 205 Function _testRunner; | |
| 206 | |
| 207 /** Setup function called before each test in a group */ | 201 /** Setup function called before each test in a group */ |
| 208 Function _testSetup; | 202 Function _testSetup; |
| 209 | 203 |
| 210 /** Teardown function called after each test in a group */ | 204 /** Teardown function called after each test in a group */ |
| 211 Function _testTeardown; | 205 Function _testTeardown; |
| 212 | 206 |
| 213 /** Current test being executed. */ | 207 /** Current test being executed. */ |
| 214 int _currentTest = 0; | 208 int _currentTest = 0; |
| 215 TestCase _currentTestCase; | 209 TestCase _currentTestCase; |
| 216 | 210 |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 * case it must return a [Future]. | 625 * case it must return a [Future]. |
| 632 */ | 626 */ |
| 633 void tearDown(Function teardownTest) { | 627 void tearDown(Function teardownTest) { |
| 634 _testTeardown = teardownTest; | 628 _testTeardown = teardownTest; |
| 635 } | 629 } |
| 636 | 630 |
| 637 /** Advance to the next test case. */ | 631 /** Advance to the next test case. */ |
| 638 void _nextTestCase() { | 632 void _nextTestCase() { |
| 639 _defer(() { | 633 _defer(() { |
| 640 _currentTest++; | 634 _currentTest++; |
| 641 _testRunner(); | 635 _nextBatch(); |
| 642 }); | 636 }); |
| 643 } | 637 } |
| 644 | 638 |
| 645 /** | 639 /** |
| 646 * Utility function that can be used to notify the test framework that an | 640 * Utility function that can be used to notify the test framework that an |
| 647 * error was caught outside of this library. | 641 * error was caught outside of this library. |
| 648 */ | 642 */ |
| 649 void _reportTestError(String msg, String trace) { | 643 void _reportTestError(String msg, String trace) { |
| 650 if (_currentTest < _tests.length) { | 644 if (_currentTest < _tests.length) { |
| 651 final testCase = _tests[_currentTest]; | 645 final testCase = _tests[_currentTest]; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 _currentGroup = ''; | 692 _currentGroup = ''; |
| 699 | 693 |
| 700 // If we are soloing a test, remove all the others. | 694 // If we are soloing a test, remove all the others. |
| 701 if (_soloTest != null) { | 695 if (_soloTest != null) { |
| 702 filterTests((t) => t == _soloTest); | 696 filterTests((t) => t == _soloTest); |
| 703 } | 697 } |
| 704 | 698 |
| 705 _config.onStart(); | 699 _config.onStart(); |
| 706 | 700 |
| 707 _defer(() { | 701 _defer(() { |
| 708 _testRunner(); | 702 _nextBatch(); |
| 709 }); | 703 }); |
| 710 } | 704 } |
| 711 | 705 |
| 712 /** | 706 /** |
| 713 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is | 707 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is |
| 714 * passed to the corresponding test. | 708 * passed to the corresponding test. |
| 715 * | 709 * |
| 716 * The value returned by [tryBody] (if any) is returned by [guardAsync]. | 710 * The value returned by [tryBody] (if any) is returned by [guardAsync]. |
| 717 */ | 711 */ |
| 718 guardAsync(Function tryBody) { | 712 guardAsync(Function tryBody) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 733 /** | 727 /** |
| 734 * Registers that an exception was caught for the current test. | 728 * Registers that an exception was caught for the current test. |
| 735 */ | 729 */ |
| 736 void registerException(e, [trace]) { | 730 void registerException(e, [trace]) { |
| 737 _registerException(_currentTest, e, trace); | 731 _registerException(_currentTest, e, trace); |
| 738 } | 732 } |
| 739 | 733 |
| 740 /** | 734 /** |
| 741 * Registers that an exception was caught for the current test. | 735 * Registers that an exception was caught for the current test. |
| 742 */ | 736 */ |
| 743 _registerException(testNum, e, [trace]) { | 737 void _registerException(testNum, e, [trace]) { |
| 744 trace = trace == null ? '' : trace.toString(); | 738 trace = trace == null ? '' : trace.toString(); |
| 745 String message = (e is TestFailure) ? e.message : 'Caught $e'; | 739 String message = (e is TestFailure) ? e.message : 'Caught $e'; |
| 746 if (_tests[testNum].result == null) { | 740 if (_tests[testNum].result == null) { |
| 747 _tests[testNum].fail(message, trace); | 741 _tests[testNum].fail(message, trace); |
| 748 } else { | 742 } else { |
| 749 _tests[testNum].error(message, trace); | 743 _tests[testNum].error(message, trace); |
| 750 } | 744 } |
| 751 } | 745 } |
| 752 | 746 |
| 753 /** | 747 /** |
| 754 * Runs a batch of tests, yielding whenever an asynchronous test starts | 748 * Runs a batch of tests, yielding whenever an asynchronous test starts |
| 755 * running. Tests will resume executing when such asynchronous test calls | 749 * running. Tests will resume executing when such asynchronous test calls |
| 756 * [done] or if it fails with an exception. | 750 * [done] or if it fails with an exception. |
| 757 */ | 751 */ |
| 758 _nextBatch() { | 752 void _nextBatch() { |
| 759 while (true) { | 753 while (true) { |
| 760 if (_currentTest >= _tests.length) { | 754 if (_currentTest >= _tests.length) { |
| 761 _completeTests(); | 755 _completeTests(); |
| 762 break; | 756 break; |
| 763 } | 757 } |
| 764 final testCase = _tests[_currentTest]; | 758 final testCase = _tests[_currentTest]; |
| 765 var f = _guardAsync(testCase._run, null, _currentTest); | 759 var f = _guardAsync(testCase._run, null, _currentTest); |
| 766 if (f != null) { | 760 if (f != null) { |
| 767 f.whenComplete(() { | 761 f.whenComplete(() { |
| 768 _nextTestCase(); // Schedule the next test. | 762 _nextTestCase(); // Schedule the next test. |
| 769 }); | 763 }); |
| 770 break; | 764 break; |
| 771 } | 765 } |
| 772 _currentTest++; | 766 _currentTest++; |
| 773 } | 767 } |
| 774 } | 768 } |
| 775 | 769 |
| 776 /** Publish results on the page and notify controller. */ | 770 /** Publish results on the page and notify controller. */ |
| 777 _completeTests() { | 771 void _completeTests() { |
| 778 if (!_initialized) return; | 772 if (!_initialized) return; |
| 779 int passed = 0; | 773 int passed = 0; |
| 780 int failed = 0; | 774 int failed = 0; |
| 781 int errors = 0; | 775 int errors = 0; |
| 782 | 776 |
| 783 for (TestCase t in _tests) { | 777 for (TestCase t in _tests) { |
| 784 switch (t.result) { | 778 switch (t.result) { |
| 785 case PASS: passed++; break; | 779 case PASS: passed++; break; |
| 786 case FAIL: failed++; break; | 780 case FAIL: failed++; break; |
| 787 case ERROR: errors++; break; | 781 case ERROR: errors++; break; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 803 */ | 797 */ |
| 804 void ensureInitialized() { | 798 void ensureInitialized() { |
| 805 if (_initialized) { | 799 if (_initialized) { |
| 806 return; | 800 return; |
| 807 } | 801 } |
| 808 _initialized = true; | 802 _initialized = true; |
| 809 // Hook our async guard into the matcher library. | 803 // Hook our async guard into the matcher library. |
| 810 wrapAsync = (f, [id]) => expectAsync1(f, id: id); | 804 wrapAsync = (f, [id]) => expectAsync1(f, id: id); |
| 811 | 805 |
| 812 _tests = <TestCase>[]; | 806 _tests = <TestCase>[]; |
| 813 _testRunner = _nextBatch; | |
| 814 _uncaughtErrorMessage = null; | 807 _uncaughtErrorMessage = null; |
| 815 | 808 |
| 816 if (_config == null) { | 809 if (_config == null) { |
| 817 _config = new Configuration(); | 810 _config = new Configuration(); |
| 818 } | 811 } |
| 819 _config.onInit(); | 812 _config.onInit(); |
| 820 | 813 |
| 821 if (_config.autoStart) { | 814 if (_config.autoStart) { |
| 822 // Immediately queue the suite up. It will run after a timeout (i.e. after | 815 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 823 // main() has returned). | 816 // main() has returned). |
| (...skipping 27 matching lines...) Expand all Loading... |
| 851 } | 844 } |
| 852 | 845 |
| 853 /** Enable a test by ID. */ | 846 /** Enable a test by ID. */ |
| 854 void enableTest(int testId) => _setTestEnabledState(testId, true); | 847 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 855 | 848 |
| 856 /** Disable a test by ID. */ | 849 /** Disable a test by ID. */ |
| 857 void disableTest(int testId) => _setTestEnabledState(testId, false); | 850 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 858 | 851 |
| 859 /** Signature for a test function. */ | 852 /** Signature for a test function. */ |
| 860 typedef dynamic TestFunction(); | 853 typedef dynamic TestFunction(); |
| OLD | NEW |