| 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 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 // message at the end of the event loop. | 678 // message at the end of the event loop. |
| 679 // TODO(sigmund): expose this functionality somewhere in our libraries. | 679 // TODO(sigmund): expose this functionality somewhere in our libraries. |
| 680 final port = new ReceivePort(); | 680 final port = new ReceivePort(); |
| 681 port.receive((msg, reply) { | 681 port.receive((msg, reply) { |
| 682 callback(); | 682 callback(); |
| 683 port.close(); | 683 port.close(); |
| 684 }); | 684 }); |
| 685 port.toSendPort().send(null, null); | 685 port.toSendPort().send(null, null); |
| 686 } | 686 } |
| 687 | 687 |
| 688 rerunTests() { |
| 689 _uncaughtErrorMessage = null; |
| 690 runTests(); |
| 691 } |
| 692 |
| 688 /** Runs all queued tests, one at a time. */ | 693 /** Runs all queued tests, one at a time. */ |
| 689 runTests() { | 694 runTests() { |
| 690 _uncaughtErrorMessage = null; | |
| 691 _currentTest = 0; | 695 _currentTest = 0; |
| 692 _currentGroup = ''; | 696 _currentGroup = ''; |
| 693 | 697 |
| 694 // If we are soloing a test, remove all the others. | 698 // If we are soloing a test, remove all the others. |
| 695 if (_soloTest != null) { | 699 if (_soloTest != null) { |
| 696 _tests = _tests.filter((t) => t == _soloTest); | 700 _tests = _tests.filter((t) => t == _soloTest); |
| 697 } | 701 } |
| 698 | 702 |
| 699 if (filter != null) { | 703 if (filter != null) { |
| 700 RegExp re = new RegExp(filter); | 704 RegExp re = new RegExp(filter); |
| 701 _tests = _tests.filter((t) => re.hasMatch(t.description)); | 705 _tests = _tests.filter((t) => re.hasMatch(t.description)); |
| 702 } | 706 } |
| 703 | 707 |
| 704 _config.onStart(); | 708 _config.onStart(); |
| 705 | 709 |
| 706 _defer(() { | 710 _defer(() { |
| 707 expect(_currentTest, isZero); | |
| 708 _testRunner(); | 711 _testRunner(); |
| 709 }); | 712 }); |
| 710 } | 713 } |
| 711 | 714 |
| 712 /** | 715 /** |
| 713 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update | 716 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update |
| 714 * the [_currentTest] status accordingly. | 717 * the [_currentTest] status accordingly. |
| 715 */ | 718 */ |
| 716 guardAsync(tryBody, [finallyBody, testNum = -1]) { | 719 guardAsync(tryBody, [finallyBody, testNum = -1]) { |
| 717 if (testNum < 0) testNum = _currentTest; | 720 if (testNum < 0) testNum = _currentTest; |
| 718 try { | 721 try { |
| 719 return tryBody(); | 722 return tryBody(); |
| 720 } catch (var e, var trace) { | 723 } catch (var e, var trace) { |
| 721 registerException(testNum, e, trace); | 724 registerException(testNum, e, trace); |
| 722 } finally { | 725 } finally { |
| 723 if (finallyBody != null) finallyBody(); | 726 if (finallyBody != null) finallyBody(); |
| 724 } | 727 } |
| 725 } | 728 } |
| 726 | 729 |
| 727 /** | 730 /** |
| 728 * Registers that an exception was caught for the current test. | 731 * Registers that an exception was caught for the current test. |
| 729 */ | 732 */ |
| 730 registerException(testNum, e, [trace]) { | 733 registerException(testNum, e, [trace]) { |
| 731 trace = trace == null ? '' : trace.toString(); | 734 trace = trace == null ? '' : trace.toString(); |
| 732 if (_tests[testNum].result == null) { | 735 if (_tests[testNum].result == null) { |
| 733 if (e is ExpectException) { | 736 String message = (e is ExpectException) ? e.message : 'Caught $e'; |
| 734 _tests[testNum].fail(e.message, trace); | 737 _tests[testNum].fail(message, trace); |
| 735 } else { | |
| 736 _tests[testNum].fail('Caught $e', trace); | |
| 737 } | |
| 738 } else { | 738 } else { |
| 739 _tests[testNum].error('Caught $e', trace); | 739 _tests[testNum].error('Caught $e', trace); |
| 740 } | 740 } |
| 741 if (testNum == _currentTest) { | 741 if (testNum == _currentTest) { |
| 742 _nextTestCase(); | 742 _nextTestCase(); |
| 743 } | 743 } |
| 744 } | 744 } |
| 745 | 745 |
| 746 /** | 746 /** |
| 747 * Runs a batch of tests, yielding whenever an asynchronous test starts | 747 * Runs a batch of tests, yielding whenever an asynchronous test starts |
| (...skipping 24 matching lines...) Expand all Loading... |
| 772 int testsFailed_ = 0; | 772 int testsFailed_ = 0; |
| 773 int testsErrors_ = 0; | 773 int testsErrors_ = 0; |
| 774 | 774 |
| 775 for (TestCase t in _tests) { | 775 for (TestCase t in _tests) { |
| 776 switch (t.result) { | 776 switch (t.result) { |
| 777 case _PASS: testsPassed_++; break; | 777 case _PASS: testsPassed_++; break; |
| 778 case _FAIL: testsFailed_++; break; | 778 case _FAIL: testsFailed_++; break; |
| 779 case _ERROR: testsErrors_++; break; | 779 case _ERROR: testsErrors_++; break; |
| 780 } | 780 } |
| 781 } | 781 } |
| 782 | |
| 783 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests, | 782 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests, |
| 784 _uncaughtErrorMessage); | 783 _uncaughtErrorMessage); |
| 785 _initialized = false; | 784 _initialized = false; |
| 786 } | 785 } |
| 787 | 786 |
| 788 String _fullSpec(String spec) { | 787 String _fullSpec(String spec) { |
| 789 if (spec === null) return '$_currentGroup'; | 788 if (spec === null) return '$_currentGroup'; |
| 790 return _currentGroup != '' ? '$_currentGroup $spec' : spec; | 789 return _currentGroup != '' ? '$_currentGroup $spec' : spec; |
| 791 } | 790 } |
| 792 | 791 |
| 793 void _fail(String message) { | 792 void _fail(String message) { |
| 794 throw new ExpectException(message); | 793 throw new ExpectException(message); |
| 795 } | 794 } |
| 796 | 795 |
| 797 /** | 796 /** |
| 798 * Lazily initializes the test library if not already initialized. | 797 * Lazily initializes the test library if not already initialized. |
| 799 */ | 798 */ |
| 800 ensureInitialized() { | 799 ensureInitialized() { |
| 801 if (_initialized) return; | 800 if (_initialized) return; |
| 802 _initialized = true; | 801 _initialized = true; |
| 803 | 802 |
| 804 _tests = <TestCase>[]; | 803 _tests = <TestCase>[]; |
| 805 _testRunner = _nextBatch; | 804 _testRunner = _nextBatch; |
| 805 _uncaughtErrorMessage = null; |
| 806 | 806 |
| 807 if (_config == null) { | 807 if (_config == null) { |
| 808 _config = new Configuration(); | 808 _config = new Configuration(); |
| 809 } | 809 } |
| 810 _config.onInit(); | 810 _config.onInit(); |
| 811 | 811 |
| 812 if (_config.autoStart) { | 812 if (_config.autoStart) { |
| 813 // Immediately queue the suite up. It will run after a timeout (i.e. after | 813 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 814 // main() has returned). | 814 // main() has returned). |
| 815 _defer(runTests); | 815 _defer(runTests); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 842 } | 842 } |
| 843 | 843 |
| 844 /** Enable a test by ID. */ | 844 /** Enable a test by ID. */ |
| 845 void enableTest(int testId) => _setTestEnabledState(testId, true); | 845 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 846 | 846 |
| 847 /** Disable a test by ID. */ | 847 /** Disable a test by ID. */ |
| 848 void disableTest(int testId) => _setTestEnabledState(testId, false); | 848 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 849 | 849 |
| 850 /** Signature for a test function. */ | 850 /** Signature for a test function. */ |
| 851 typedef void TestFunction(); | 851 typedef void TestFunction(); |
| OLD | NEW |