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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 | 174 |
| 175 // TODO(amouravski): We should not need to import mock here, but it's necessary | 175 // TODO(amouravski): We should not need to import mock here, but it's necessary |
| 176 // to enable dartdoc on the mock library, as it's not picked up normally. | 176 // to enable dartdoc on the mock library, as it's not picked up normally. |
| 177 import 'mock.dart'; | 177 import 'mock.dart'; |
| 178 | 178 |
| 179 part 'src/config.dart'; | 179 part 'src/config.dart'; |
| 180 part 'src/test_case.dart'; | 180 part 'src/test_case.dart'; |
| 181 | 181 |
| 182 Configuration _config; | 182 Configuration _config; |
| 183 | 183 |
| 184 /** [Configuration] used by the unittest library. */ | 184 /** |
| 185 Configuration get unittestConfiguration => _config; | 185 * [Configuration] used by the unittest library. Note that if a |
| 186 * configuration has not been set, calling this getter will create | |
| 187 * a default configuration. | |
| 188 */ | |
| 189 Configuration get unittestConfiguration { | |
| 190 if (_config == null) { | |
| 191 _config = new Configuration(); | |
| 192 } | |
| 193 return _config; | |
| 194 } | |
| 186 | 195 |
| 187 /** | 196 /** |
| 188 * Sets the [Configuration] used by the unittest library. | 197 * Sets the [Configuration] used by the unittest library. |
| 189 * | 198 * |
| 190 * Throws a [StateError] if there is an existing, incompatible value. | 199 * Throws a [StateError] if there is an existing, incompatible value. |
| 191 */ | 200 */ |
| 192 void set unittestConfiguration(Configuration value) { | 201 void set unittestConfiguration(Configuration value) { |
| 193 if(!identical(_config, value)) { | 202 if(!identical(_config, value)) { |
| 194 if(_config != null) { | 203 if(_config != null) { |
| 195 throw new StateError('unittestConfiguration has already been set'); | 204 throw new StateError('unittestConfiguration has already been set'); |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 852 } | 861 } |
| 853 | 862 |
| 854 /** Enable a test by ID. */ | 863 /** Enable a test by ID. */ |
| 855 void enableTest(int testId) => _setTestEnabledState(testId, true); | 864 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 856 | 865 |
| 857 /** Disable a test by ID. */ | 866 /** Disable a test by ID. */ |
| 858 void disableTest(int testId) => _setTestEnabledState(testId, false); | 867 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 859 | 868 |
| 860 /** Signature for a test function. */ | 869 /** Signature for a test function. */ |
| 861 typedef dynamic TestFunction(); | 870 typedef dynamic TestFunction(); |
| 871 | |
| 872 // Stack formatting utility. Strips extraneous content from a stack trace. | |
| 873 // Stack fram lines are parsed with a regexp, which has been tested | |
|
Siggi Cherem (dart-lang)
2013/04/30 00:42:33
fram => frame
gram
2013/04/30 01:00:09
Done.
| |
| 874 // in Chrome, Firefox and the VM. If a line fails to be parsed it is | |
| 875 // included in the output to be conservative. | |
| 876 // | |
| 877 // The output stack consists of everything after the call to TestCase._run. | |
| 878 // If we see an 'expect' in the frame we will prune everything above that | |
| 879 // as well. | |
| 880 final _frameRegExp = new RegExp( | |
| 881 r'^\s*(?:(?:#\d+\s*)|(?:at )|(?:))(.+)\s*[@\(]((?:.+:\/\/.+\/[^:]*)|(?:dart: [^:]*)|(?:package:[^:]*)):([:\d]+)[\)]?$'); | |
|
Siggi Cherem (dart-lang)
2013/04/30 00:42:33
is there a way to split this string in pieces to m
gram
2013/04/30 01:00:09
Done.
| |
| 882 | |
| 883 String _formatStack(stack) { | |
| 884 var lines; | |
| 885 if (stack is StackTrace) { | |
| 886 lines = stack.toString().split('\n'); | |
| 887 } else if (stack is String) { | |
| 888 lines = stack.split('\n'); | |
| 889 } else { | |
| 890 return stack.toString(); | |
| 891 } | |
| 892 | |
| 893 // Calculate the max width of first column so we can | |
| 894 // pad to align the second columns. | |
| 895 int padding = lines.fold(0, (n, line) { | |
| 896 var match = _frameRegExp.firstMatch(line); | |
| 897 if (match == null) return n; | |
| 898 return match[1].length + 1; | |
|
Siggi Cherem (dart-lang)
2013/04/30 00:42:33
missing call to max(n, *) here?
gram
2013/04/30 01:00:09
Done.
| |
| 899 }); | |
| 900 | |
| 901 // We remove all entries that have a location in unittest. | |
| 902 // We strip out anything before _nextBatch too. | |
| 903 var sb = new StringBuffer(); | |
| 904 for (var i = 0; i < lines.length; i++) { | |
| 905 var line = lines[i]; | |
| 906 if (line == '') continue; | |
| 907 var match = _frameRegExp.firstMatch(line); | |
| 908 if (match == null) { | |
| 909 sb.write(line); | |
| 910 sb.write('\n'); | |
| 911 } else { | |
| 912 var member = match[1]; | |
| 913 var location = match[2]; | |
| 914 var position = match[3]; | |
| 915 if (member.indexOf('TestCase._runTest') >= 0) { | |
| 916 // Don't include anything after this. | |
| 917 break; | |
| 918 } else if (member.indexOf('expect') >= 0) { | |
| 919 // It looks like this was an expect() failure; | |
| 920 // drop all the frames up to here. | |
| 921 sb.clear(); | |
| 922 } else { | |
| 923 sb.write(member); | |
| 924 // Pad second column to a fixed position. | |
| 925 for (var j = 0; j <= padding - member.length; j++) { | |
| 926 sb.write(' '); | |
| 927 } | |
| 928 sb.write(location); | |
| 929 sb.write(' '); | |
| 930 sb.write(position); | |
| 931 sb.write('\n'); | |
| 932 } | |
| 933 } | |
| 934 } | |
| 935 return sb.toString(); | |
| 936 } | |
| OLD | NEW |