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 * pkg/unittest/unittest.dart. | 9 * pkg/unittest/unittest.dart. |
10 * | 10 * |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 #source('matcher.dart'); | 149 #source('matcher.dart'); |
150 #source('mock.dart'); | 150 #source('mock.dart'); |
151 #source('numeric_matchers.dart'); | 151 #source('numeric_matchers.dart'); |
152 #source('operator_matchers.dart'); | 152 #source('operator_matchers.dart'); |
153 #source('string_matchers.dart'); | 153 #source('string_matchers.dart'); |
154 #source('test_case.dart'); | 154 #source('test_case.dart'); |
155 | 155 |
156 /** [Configuration] used by the unittest library. */ | 156 /** [Configuration] used by the unittest library. */ |
157 Configuration _config = null; | 157 Configuration _config = null; |
158 | 158 |
| 159 Configuration get config => _config; |
| 160 |
159 /** | 161 /** |
160 * Set the [Configuration] used by the unittest library. Returns any | 162 * Set the [Configuration] used by the unittest library. Returns any |
161 * previous configuration. | 163 * previous configuration. |
| 164 * TODO: consider deprecating in favor of a setter now we have a getter. |
162 */ | 165 */ |
163 Configuration configure(Configuration config) { | 166 Configuration configure(Configuration config) { |
164 Configuration _oldConfig = _config; | 167 Configuration _oldConfig = _config; |
165 _config = config; | 168 _config = config; |
166 return _oldConfig; | 169 return _oldConfig; |
167 } | 170 } |
168 | 171 |
169 void logMessage(String message) => _config.log(message); | 172 void logMessage(String message) => _config.log(message); |
170 | 173 |
171 /** | 174 /** |
172 * Description text of the current test group. If multiple groups are nested, | 175 * Description text of the current test group. If multiple groups are nested, |
173 * this will contain all of their text concatenated. | 176 * this will contain all of their text concatenated. |
174 */ | 177 */ |
175 String _currentGroup = ''; | 178 String _currentGroup = ''; |
176 | 179 |
| 180 /** Separator used between group names and test names. */ |
| 181 String groupSep = ' '; |
| 182 |
177 /** Tests executed in this suite. */ | 183 /** Tests executed in this suite. */ |
178 List<TestCase> _tests; | 184 List<TestCase> _tests; |
179 | 185 |
180 /** Get the list of tests. */ | 186 /** Get the list of tests. */ |
181 get testCases() => _tests; | 187 get testCases => _tests; |
182 | 188 |
183 /** | 189 /** |
184 * Callback used to run tests. Entrypoints can replace this with their own | 190 * Callback used to run tests. Entrypoints can replace this with their own |
185 * if they want. | 191 * if they want. |
186 */ | 192 */ |
187 Function _testRunner; | 193 Function _testRunner; |
188 | 194 |
189 /** Setup function called before each test in a group */ | 195 /** Setup function called before each test in a group */ |
190 Function _testSetup; | 196 Function _testSetup; |
191 | 197 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 if (result == false) { | 235 if (result == false) { |
230 _fail('Exception:\n$e\ndid not match expectation.'); | 236 _fail('Exception:\n$e\ndid not match expectation.'); |
231 } | 237 } |
232 } | 238 } |
233 } | 239 } |
234 | 240 |
235 if (threw != true) _fail('An expected exception was not thrown.'); | 241 if (threw != true) _fail('An expected exception was not thrown.'); |
236 } | 242 } |
237 | 243 |
238 /** | 244 /** |
239 * The regexp pattern filter which constrains which tests to run | |
240 * based on their descriptions. | |
241 */ | |
242 | |
243 String filter = null; | |
244 | |
245 /** | |
246 * Creates a new test case with the given description and body. The | 245 * Creates a new test case with the given description and body. The |
247 * description will include the descriptions of any surrounding group() | 246 * description will include the descriptions of any surrounding group() |
248 * calls. | 247 * calls. |
249 */ | 248 */ |
250 void test(String spec, TestFunction body) { | 249 void test(String spec, TestFunction body) { |
251 ensureInitialized(); | 250 ensureInitialized(); |
252 | |
253 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); | 251 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); |
254 } | 252 } |
255 | 253 |
256 /** | 254 /** |
257 * (Deprecated) Creates a new async test case with the given description | 255 * (Deprecated) Creates a new async test case with the given description |
258 * and body. The description will include the descriptions of any surrounding | 256 * and body. The description will include the descriptions of any surrounding |
259 * group() calls. | 257 * group() calls. |
260 */ | 258 */ |
261 // TODO(sigmund): deprecate this API | 259 // TODO(sigmund): deprecate this API |
262 void asyncTest(String spec, int callbacks, TestFunction body) { | 260 void asyncTest(String spec, int callbacks, TestFunction body) { |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 Function protectAsync2(Function callback) { | 553 Function protectAsync2(Function callback) { |
556 return new _SpreadArgsHelper.optionalCalls(callback).invoke2; | 554 return new _SpreadArgsHelper.optionalCalls(callback).invoke2; |
557 } | 555 } |
558 | 556 |
559 /** | 557 /** |
560 * Creates a new named group of tests. Calls to group() or test() within the | 558 * Creates a new named group of tests. Calls to group() or test() within the |
561 * body of the function passed to this will inherit this group's description. | 559 * body of the function passed to this will inherit this group's description. |
562 */ | 560 */ |
563 void group(String description, void body()) { | 561 void group(String description, void body()) { |
564 ensureInitialized(); | 562 ensureInitialized(); |
565 | |
566 // Concatenate the new group. | 563 // Concatenate the new group. |
567 final parentGroup = _currentGroup; | 564 final parentGroup = _currentGroup; |
568 if (_currentGroup != '') { | 565 if (_currentGroup != '') { |
569 // Add a space. | 566 // Add a space. |
570 _currentGroup = '$_currentGroup $description'; | 567 _currentGroup = '$_currentGroup$groupSep$description'; |
571 } else { | 568 } else { |
572 // The first group. | 569 // The first group. |
573 _currentGroup = description; | 570 _currentGroup = description; |
574 } | 571 } |
575 | 572 |
576 // Groups can be nested, so we need to preserve the current | 573 // Groups can be nested, so we need to preserve the current |
577 // settings for test setup/teardown. | 574 // settings for test setup/teardown. |
578 Function parentSetup = _testSetup; | 575 Function parentSetup = _testSetup; |
579 Function parentTeardown = _testTeardown; | 576 Function parentTeardown = _testTeardown; |
580 | 577 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 }); | 683 }); |
687 port.toSendPort().send(null, null); | 684 port.toSendPort().send(null, null); |
688 } | 685 } |
689 | 686 |
690 rerunTests() { | 687 rerunTests() { |
691 _uncaughtErrorMessage = null; | 688 _uncaughtErrorMessage = null; |
692 _initialized = true; // We don't want to reset the test array. | 689 _initialized = true; // We don't want to reset the test array. |
693 runTests(); | 690 runTests(); |
694 } | 691 } |
695 | 692 |
| 693 /** |
| 694 * Filter the tests. [testFilter] can be a [RegExp], a [String] or a |
| 695 * predicate function. This is different to enabling/disabling tests |
| 696 * in that it removes the tests completely. |
| 697 */ |
| 698 void filterTests(testFilter) { |
| 699 var filterFunction; |
| 700 if (testFilter is String) { |
| 701 RegExp re = new RegExp(testFilter); |
| 702 filterFunction = (t) => re.hasMatch(t.description); |
| 703 } else if (testFilter is RegExp) { |
| 704 filterFunction = (t) => testFilter.hasMatch(t.description); |
| 705 } else if (testFilter is Function) { |
| 706 filterFunction = testFilter; |
| 707 } |
| 708 _tests = _tests.filter(filterFunction); |
| 709 } |
| 710 |
696 /** Runs all queued tests, one at a time. */ | 711 /** Runs all queued tests, one at a time. */ |
697 runTests() { | 712 runTests() { |
698 _currentTest = 0; | 713 _currentTest = 0; |
699 _currentGroup = ''; | 714 _currentGroup = ''; |
700 | 715 |
701 // If we are soloing a test, remove all the others. | 716 // If we are soloing a test, remove all the others. |
702 if (_soloTest != null) { | 717 if (_soloTest != null) { |
703 _tests = _tests.filter((t) => t == _soloTest); | 718 filterTests((t) => t == _soloTest); |
704 } | |
705 | |
706 if (filter != null) { | |
707 RegExp re = new RegExp(filter); | |
708 _tests = _tests.filter((t) => re.hasMatch(t.description)); | |
709 } | 719 } |
710 | 720 |
711 _config.onStart(); | 721 _config.onStart(); |
712 | 722 |
713 _defer(() { | 723 _defer(() { |
714 _testRunner(); | 724 _testRunner(); |
715 }); | 725 }); |
716 } | 726 } |
717 | 727 |
718 /** | 728 /** |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 case _ERROR: testsErrors_++; break; | 799 case _ERROR: testsErrors_++; break; |
790 } | 800 } |
791 } | 801 } |
792 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests, | 802 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests, |
793 _uncaughtErrorMessage); | 803 _uncaughtErrorMessage); |
794 _initialized = false; | 804 _initialized = false; |
795 } | 805 } |
796 | 806 |
797 String _fullSpec(String spec) { | 807 String _fullSpec(String spec) { |
798 if (spec === null) return '$_currentGroup'; | 808 if (spec === null) return '$_currentGroup'; |
799 return _currentGroup != '' ? '$_currentGroup $spec' : spec; | 809 return _currentGroup != '' ? '$_currentGroup$groupSep$spec' : spec; |
800 } | 810 } |
801 | 811 |
802 void _fail(String message) { | 812 void _fail(String message) { |
803 throw new ExpectException(message); | 813 throw new ExpectException(message); |
804 } | 814 } |
805 | 815 |
806 /** | 816 /** |
807 * Lazily initializes the test library if not already initialized. | 817 * Lazily initializes the test library if not already initialized. |
808 */ | 818 */ |
809 ensureInitialized() { | 819 ensureInitialized() { |
810 if (_initialized) return; | 820 if (_initialized) { |
| 821 return; |
| 822 } |
811 _initialized = true; | 823 _initialized = true; |
812 | 824 |
813 _tests = <TestCase>[]; | 825 _tests = <TestCase>[]; |
814 _testRunner = _nextBatch; | 826 _testRunner = _nextBatch; |
815 _uncaughtErrorMessage = null; | 827 _uncaughtErrorMessage = null; |
816 | 828 |
817 if (_config == null) { | 829 if (_config == null) { |
818 _config = new Configuration(); | 830 _config = new Configuration(); |
819 } | 831 } |
820 _config.onInit(); | 832 _config.onInit(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 } | 864 } |
853 | 865 |
854 /** Enable a test by ID. */ | 866 /** Enable a test by ID. */ |
855 void enableTest(int testId) => _setTestEnabledState(testId, true); | 867 void enableTest(int testId) => _setTestEnabledState(testId, true); |
856 | 868 |
857 /** Disable a test by ID. */ | 869 /** Disable a test by ID. */ |
858 void disableTest(int testId) => _setTestEnabledState(testId, false); | 870 void disableTest(int testId) => _setTestEnabledState(testId, false); |
859 | 871 |
860 /** Signature for a test function. */ | 872 /** Signature for a test function. */ |
861 typedef void TestFunction(); | 873 typedef void TestFunction(); |
OLD | NEW |