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 * Support for writing Dart unit tests. | 6 * Support for writing Dart unit tests. |
7 * | 7 * |
8 * For information on installing and importing this library, see the | 8 * For information on installing and importing this library, see the |
9 * [unittest package on pub.dartlang.org] | 9 * [unittest package on pub.dartlang.org] |
10 * (http://pub.dartlang.org/packages/unittest). | 10 * (http://pub.dartlang.org/packages/unittest). |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 /** | 262 /** |
263 * Creates a new test case with the given description and body. The | 263 * Creates a new test case with the given description and body. The |
264 * description will include the descriptions of any surrounding group() | 264 * description will include the descriptions of any surrounding group() |
265 * calls. | 265 * calls. |
266 */ | 266 */ |
267 void test(String spec, TestFunction body) { | 267 void test(String spec, TestFunction body) { |
268 _requireNotRunning(); | 268 _requireNotRunning(); |
269 ensureInitialized(); | 269 ensureInitialized(); |
270 if (!_soloTestSeen || _soloNestingLevel > 0) { | 270 if (!_soloTestSeen || _soloNestingLevel > 0) { |
271 var testcase = new TestCase._internal(testCases.length + 1, _fullSpec(spec), | 271 var testcase = new TestCase._internal(testCases.length + 1, _fullSpec(spec), |
272 body); | 272 body); |
273 _testCases.add(testcase); | 273 _testCases.add(testcase); |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 /** Convenience function for skipping a test. */ | 277 /** Convenience function for skipping a test. */ |
278 void skip_test(String spec, TestFunction body){} | 278 void skip_test(String spec, TestFunction body) {} |
279 | 279 |
280 /** | 280 /** |
281 * Creates a new test case with the given description and body. The | 281 * Creates a new test case with the given description and body. The |
282 * description will include the descriptions of any surrounding group() | 282 * description will include the descriptions of any surrounding group() |
283 * calls. | 283 * calls. |
284 * | 284 * |
285 * If we use [solo_test] (or [solo_group]) instead of test, then all non-solo | 285 * If we use [solo_test] (or [solo_group]) instead of test, then all non-solo |
286 * tests will be disabled. Note that if we use [solo_group], all tests in | 286 * tests will be disabled. Note that if we use [solo_group], all tests in |
287 * the group will be enabled, regardless of whether they use [test] or | 287 * the group will be enabled, regardless of whether they use [test] or |
288 * [solo_test], or whether they are in a nested [group] vs [solo_group]. Put | 288 * [solo_test], or whether they are in a nested [group] vs [solo_group]. Put |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 unittestConfiguration.onInit(); | 664 unittestConfiguration.onInit(); |
665 | 665 |
666 if (configAutoStart && _config.autoStart) { | 666 if (configAutoStart && _config.autoStart) { |
667 // Immediately queue the suite up. It will run after a timeout (i.e. after | 667 // Immediately queue the suite up. It will run after a timeout (i.e. after |
668 // main() has returned). | 668 // main() has returned). |
669 scheduleMicrotask(runTests); | 669 scheduleMicrotask(runTests); |
670 } | 670 } |
671 } | 671 } |
672 | 672 |
673 /** Select a solo test by ID. */ | 673 /** Select a solo test by ID. */ |
674 void setSoloTest(int id) => | 674 void setSoloTest(int id) => _testCases.retainWhere((t) => t.id == id); |
675 _testCases.retainWhere((t) => t.id == id); | |
676 | 675 |
677 /** Enable/disable a test by ID. */ | 676 /** Enable/disable a test by ID. */ |
678 void _setTestEnabledState(int testId, bool state) { | 677 void _setTestEnabledState(int testId, bool state) { |
679 // Try fast path first. | 678 // Try fast path first. |
680 if (testCases.length > testId && testCases[testId].id == testId) { | 679 if (testCases.length > testId && testCases[testId].id == testId) { |
681 testCases[testId]._enabled = state; | 680 testCases[testId]._enabled = state; |
682 } else { | 681 } else { |
683 for (var i = 0; i < testCases.length; i++) { | 682 for (var i = 0; i < testCases.length; i++) { |
684 if (testCases[i].id == testId) { | 683 if (testCases[i].id == testId) { |
685 testCases[i]._enabled = state; | 684 testCases[i]._enabled = state; |
(...skipping 20 matching lines...) Expand all Loading... |
706 */ | 705 */ |
707 bool formatStacks = true; | 706 bool formatStacks = true; |
708 | 707 |
709 /** | 708 /** |
710 * A flag that controls whether we try to filter out irrelevant frames from | 709 * A flag that controls whether we try to filter out irrelevant frames from |
711 * the stack trace. Requires formatStacks to be set. | 710 * the stack trace. Requires formatStacks to be set. |
712 */ | 711 */ |
713 bool filterStacks = true; | 712 bool filterStacks = true; |
714 | 713 |
715 void _requireNotRunning() { | 714 void _requireNotRunning() { |
716 if(_currentTestCaseIndex != -1) { | 715 if (_currentTestCaseIndex != -1) { |
717 throw new StateError('Not allowed when tests are running.'); | 716 throw new StateError('Not allowed when tests are running.'); |
718 } | 717 } |
719 } | 718 } |
720 | 719 |
721 /** | 720 /** |
722 * Returns a Trace object from a StackTrace object or a String, or the | 721 * Returns a Trace object from a StackTrace object or a String, or the |
723 * unchanged input if formatStacks is false; | 722 * unchanged input if formatStacks is false; |
724 */ | 723 */ |
725 Trace _getTrace(stack) { | 724 Trace _getTrace(stack) { |
726 Trace trace; | 725 Trace trace; |
727 if (stack == null || !formatStacks) return null; | 726 if (stack == null || !formatStacks) return null; |
728 if (stack is String) { | 727 if (stack is String) { |
729 trace = new Trace.parse(stack); | 728 trace = new Trace.parse(stack); |
730 } else if (stack is StackTrace) { | 729 } else if (stack is StackTrace) { |
731 trace = new Trace.from(stack); | 730 trace = new Trace.from(stack); |
732 } else { | 731 } else { |
733 throw new Exception('Invalid stack type ${stack.runtimeType} for $stack.'); | 732 throw new Exception('Invalid stack type ${stack.runtimeType} for $stack.'); |
734 } | 733 } |
735 | 734 |
736 if (!filterStacks) return trace; | 735 if (!filterStacks) return trace; |
737 | 736 |
738 // Format the stack trace by removing everything above TestCase._runTest, | 737 // Format the stack trace by removing everything above TestCase._runTest, |
739 // which is usually going to be irrelevant. Also fold together unittest and | 738 // which is usually going to be irrelevant. Also fold together unittest and |
740 // core library calls so only the function the user called is visible. | 739 // core library calls so only the function the user called is visible. |
741 return new Trace(trace.frames.takeWhile((frame) { | 740 return new Trace(trace.frames.takeWhile((frame) { |
742 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 741 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
743 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 742 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
744 } | 743 } |
OLD | NEW |