Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 * See the [Getting Started](http://pub.dartlang.org/doc) | 21 * See the [Getting Started](http://pub.dartlang.org/doc) |
| 22 * guide for more details. | 22 * guide for more details. |
| 23 * | 23 * |
| 24 * ##Concepts## | 24 * ##Concepts## |
| 25 * | 25 * |
| 26 * * __Tests__: Tests are specified via the top-level function [test], they can be | 26 * * __Tests__: Tests are specified via the top-level function [test], they can be |
| 27 * organized together using [group]. | 27 * organized together using [group]. |
| 28 * * __Checks__: Test expectations can be specified via [expect] | 28 * * __Checks__: Test expectations can be specified via [expect] |
| 29 * * __Matchers__: [expect] assertions are written declaratively using the | 29 * * __Matchers__: [expect] assertions are written declaratively using the |
| 30 * [Matcher] class. | 30 * [Matcher] class. |
| 31 * * __Configuration__: The framework can be adapted by calling [configure] wit h a | 31 * * __Configuration__: The framework can be adapted by setting |
| 32 * [Configuration]. See the other libraries in the `unittest` package for | 32 * [unittestConfiguration] with a [Configuration]. See the other libraries |
|
gram
2013/05/13 16:30:03
Remove trailing whitespace.
| |
| 33 * alternative implementations of [Configuration] including | 33 * in the `unittest` package for alternative implementations of |
| 34 * `compact_vm_config.dart`, `html_config.dart` and | 34 * [Configuration] including `compact_vm_config.dart`, `html_config.dart` and |
| 35 * `html_enhanced_config.dart`. | 35 * `html_enhanced_config.dart`. |
| 36 * | 36 * |
| 37 * ##Examples## | 37 * ##Examples## |
| 38 * | 38 * |
| 39 * A trivial test: | 39 * A trivial test: |
| 40 * | 40 * |
| 41 * import 'package:unittest/unittest.dart'; | 41 * import 'package:unittest/unittest.dart'; |
| 42 * main() { | 42 * main() { |
| 43 * test('this is a test', () { | 43 * test('this is a test', () { |
| 44 * int x = 2 + 3; | 44 * int x = 2 + 3; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 import 'dart:math' show max; | 172 import 'dart:math' show max; |
| 173 import 'matcher.dart'; | 173 import 'matcher.dart'; |
| 174 export 'matcher.dart'; | 174 export 'matcher.dart'; |
| 175 | 175 |
| 176 part 'src/config.dart'; | 176 part 'src/config.dart'; |
| 177 part 'src/test_case.dart'; | 177 part 'src/test_case.dart'; |
| 178 | 178 |
| 179 Configuration _config; | 179 Configuration _config; |
| 180 | 180 |
| 181 /** | 181 /** |
| 182 * [Configuration] used by the unittest library. Note that if a | 182 * [Configuration] used by the unittest library. Note that if a |
| 183 * configuration has not been set, calling this getter will create | 183 * configuration has not been set, calling this getter will create |
| 184 * a default configuration. | 184 * a default configuration. |
| 185 */ | 185 */ |
| 186 Configuration get unittestConfiguration { | 186 Configuration get unittestConfiguration { |
| 187 if (_config == null) { | 187 if (_config == null) { |
| 188 _config = new Configuration(); | 188 _config = new Configuration(); |
| 189 } | 189 } |
| 190 return _config; | 190 return _config; |
| 191 } | 191 } |
| 192 | 192 |
| (...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 void enableTest(int testId) => _setTestEnabledState(testId, true); | 858 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 859 | 859 |
| 860 /** Disable a test by ID. */ | 860 /** Disable a test by ID. */ |
| 861 void disableTest(int testId) => _setTestEnabledState(testId, false); | 861 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 862 | 862 |
| 863 /** Signature for a test function. */ | 863 /** Signature for a test function. */ |
| 864 typedef dynamic TestFunction(); | 864 typedef dynamic TestFunction(); |
| 865 | 865 |
| 866 // Stack formatting utility. Strips extraneous content from a stack trace. | 866 // Stack formatting utility. Strips extraneous content from a stack trace. |
| 867 // Stack frame lines are parsed with a regexp, which has been tested | 867 // Stack frame lines are parsed with a regexp, which has been tested |
| 868 // in Chrome, Firefox and the VM. If a line fails to be parsed it is | 868 // in Chrome, Firefox and the VM. If a line fails to be parsed it is |
| 869 // included in the output to be conservative. | 869 // included in the output to be conservative. |
| 870 // | 870 // |
| 871 // The output stack consists of everything after the call to TestCase._run. | 871 // The output stack consists of everything after the call to TestCase._run. |
| 872 // If we see an 'expect' in the frame we will prune everything above that | 872 // If we see an 'expect' in the frame we will prune everything above that |
| 873 // as well. | 873 // as well. |
| 874 final _frameRegExp = new RegExp( | 874 final _frameRegExp = new RegExp( |
| 875 r'^\s*' // Skip leading spaces. | 875 r'^\s*' // Skip leading spaces. |
| 876 r'(?:' // Group of choices for the prefix. | 876 r'(?:' // Group of choices for the prefix. |
| 877 r'(?:#\d+\s*)|' // Skip VM's #<frameNumber>. | 877 r'(?:#\d+\s*)|' // Skip VM's #<frameNumber>. |
| 878 r'(?:at )|' // Skip Firefox's 'at '. | 878 r'(?:at )|' // Skip Firefox's 'at '. |
| 879 r'(?:))' // Other environments have nothing here. | 879 r'(?:))' // Other environments have nothing here. |
| 880 r'(.+)' // Extract the function/method. | 880 r'(.+)' // Extract the function/method. |
| 881 r'\s*[@\(]' // Skip space and @ or (. | 881 r'\s*[@\(]' // Skip space and @ or (. |
| 882 r'(' // This group of choices is for the source file. | 882 r'(' // This group of choices is for the source file. |
| 883 r'(?:.+:\/\/.+\/[^:]*)|' // Handle file:// or http:// URLs. | 883 r'(?:.+:\/\/.+\/[^:]*)|' // Handle file:// or http:// URLs. |
| 884 r'(?:dart:[^:]*)|' // Handle dart:<lib>. | 884 r'(?:dart:[^:]*)|' // Handle dart:<lib>. |
| 885 r'(?:package:[^:]*)' // Handle package:<path> | 885 r'(?:package:[^:]*)' // Handle package:<path> |
| 886 r'):([:\d]+)[\)]?$'); // Get the line number and optional column number. | 886 r'):([:\d]+)[\)]?$'); // Get the line number and optional column number. |
| 887 | 887 |
| 888 String _formatStack(stack) { | 888 String _formatStack(stack) { |
| 889 var lines; | 889 var lines; |
| 890 if (stack is StackTrace) { | 890 if (stack is StackTrace) { |
| 891 lines = stack.toString().split('\n'); | 891 lines = stack.toString().split('\n'); |
| 892 } else if (stack is String) { | 892 } else if (stack is String) { |
| 893 lines = stack.split('\n'); | 893 lines = stack.split('\n'); |
| 894 } else { | 894 } else { |
| 895 return stack.toString(); | 895 return stack.toString(); |
| 896 } | 896 } |
| 897 | 897 |
| 898 // Calculate the max width of first column so we can | 898 // Calculate the max width of first column so we can |
| 899 // pad to align the second columns. | 899 // pad to align the second columns. |
| 900 int padding = lines.fold(0, (n, line) { | 900 int padding = lines.fold(0, (n, line) { |
| 901 var match = _frameRegExp.firstMatch(line); | 901 var match = _frameRegExp.firstMatch(line); |
| 902 if (match == null) return n; | 902 if (match == null) return n; |
| 903 return max(n, match[1].length + 1); | 903 return max(n, match[1].length + 1); |
| 904 }); | 904 }); |
| 905 | 905 |
| 906 // We remove all entries that have a location in unittest. | 906 // We remove all entries that have a location in unittest. |
| 907 // We strip out anything before _nextBatch too. | 907 // We strip out anything before _nextBatch too. |
| 908 var sb = new StringBuffer(); | 908 var sb = new StringBuffer(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 932 } | 932 } |
| 933 sb.write(location); | 933 sb.write(location); |
| 934 sb.write(' '); | 934 sb.write(' '); |
| 935 sb.write(position); | 935 sb.write(position); |
| 936 sb.write('\n'); | 936 sb.write('\n'); |
| 937 } | 937 } |
| 938 } | 938 } |
| 939 } | 939 } |
| 940 return sb.toString(); | 940 return sb.toString(); |
| 941 } | 941 } |
| OLD | NEW |