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 * ## Installing ## | 8 * ## Installing ## |
9 * | 9 * |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | 10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 * [Issue 2706](http://dartbug.com/2706)). | 139 * [Issue 2706](http://dartbug.com/2706)). |
140 * | 140 * |
141 * [pub]: http://pub.dartlang.org | 141 * [pub]: http://pub.dartlang.org |
142 * [pkg]: http://pub.dartlang.org/packages/unittest | 142 * [pkg]: http://pub.dartlang.org/packages/unittest |
143 */ | 143 */ |
144 library unittest; | 144 library unittest; |
145 | 145 |
146 import 'dart:async'; | 146 import 'dart:async'; |
147 import 'dart:collection'; | 147 import 'dart:collection'; |
148 import 'dart:isolate'; | 148 import 'dart:isolate'; |
| 149 import 'package:stack_trace/stack_trace.dart'; |
| 150 |
149 import 'matcher.dart'; | 151 import 'matcher.dart'; |
150 export 'matcher.dart'; | 152 export 'matcher.dart'; |
151 | 153 |
152 import 'package:stack_trace/stack_trace.dart'; | 154 import 'src/utils.dart'; |
153 | 155 |
154 import 'src/utils.dart'; | 156 part 'src/configuration.dart'; |
155 part 'src/config.dart'; | 157 part 'src/simple_configuration.dart'; |
156 part 'src/test_case.dart'; | 158 part 'src/test_case.dart'; |
157 | 159 |
158 Configuration _config; | 160 Configuration _config; |
159 | 161 |
160 /** | 162 /** |
161 * [Configuration] used by the unittest library. Note that if a | 163 * [Configuration] used by the unittest library. Note that if a |
162 * configuration has not been set, calling this getter will create | 164 * configuration has not been set, calling this getter will create |
163 * a default configuration. | 165 * a default configuration. |
164 */ | 166 */ |
165 Configuration get unittestConfiguration { | 167 Configuration get unittestConfiguration { |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 void tearDown(Function teardownTest) { | 651 void tearDown(Function teardownTest) { |
650 _currentContext.testTeardown = teardownTest; | 652 _currentContext.testTeardown = teardownTest; |
651 } | 653 } |
652 | 654 |
653 /** Advance to the next test case. */ | 655 /** Advance to the next test case. */ |
654 void _nextTestCase() { | 656 void _nextTestCase() { |
655 _currentTestCaseIndex++; | 657 _currentTestCaseIndex++; |
656 _runTest(); | 658 _runTest(); |
657 } | 659 } |
658 | 660 |
659 /** | 661 /** Handle errors that happen outside the tests. */ |
660 * Utility function that can be used to notify the test framework that an | 662 // TODO(vsm): figure out how to expose the stack trace here |
661 * error was caught outside of this library. | 663 // Currently e.message works in dartium, but not in dartc. |
662 */ | 664 void handleExternalError(e, String message, [stack]) { |
663 void _reportTestError(String msg, trace) { | 665 var msg = '$message\nCaught $e'; |
664 if (_currentTestCaseIndex < testCases.length) { | 666 |
665 final testCase = testCases[_currentTestCaseIndex]; | 667 if (currentTestCase != null) { |
666 testCase.error(msg, trace); | 668 currentTestCase.error(msg, stack); |
667 } else { | 669 } else { |
668 _uncaughtErrorMessage = "$msg: $trace"; | 670 _uncaughtErrorMessage = "$msg: $stack"; |
669 } | 671 } |
670 } | 672 } |
671 | 673 |
672 void rerunTests() { | 674 void rerunTests() { |
673 _uncaughtErrorMessage = null; | 675 _uncaughtErrorMessage = null; |
674 _initialized = true; // We don't want to reset the test array. | 676 _initialized = true; // We don't want to reset the test array. |
675 runTests(); | 677 runTests(); |
676 } | 678 } |
677 | 679 |
678 /** | 680 /** |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 | 866 |
865 if (!formatStacks) return trace; | 867 if (!formatStacks) return trace; |
866 | 868 |
867 // Format the stack trace by removing everything above TestCase._runTest, | 869 // Format the stack trace by removing everything above TestCase._runTest, |
868 // which is usually going to be irrelevant. Also fold together unittest and | 870 // which is usually going to be irrelevant. Also fold together unittest and |
869 // core library calls so only the function the user called is visible. | 871 // core library calls so only the function the user called is visible. |
870 return new Trace(trace.frames.takeWhile((frame) { | 872 return new Trace(trace.frames.takeWhile((frame) { |
871 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 873 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
872 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 874 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
873 } | 875 } |
OLD | NEW |