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 library test; | 5 library test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 if (declarer != null) return declarer; | 43 if (declarer != null) return declarer; |
44 if (_globalDeclarer != null) return _globalDeclarer; | 44 if (_globalDeclarer != null) return _globalDeclarer; |
45 | 45 |
46 // Since there's no Zone-scoped declarer, the test file is being run directly. | 46 // Since there's no Zone-scoped declarer, the test file is being run directly. |
47 // In order to run the tests, we set up our own Declarer via | 47 // In order to run the tests, we set up our own Declarer via |
48 // [_globalDeclarer], and schedule a microtask to run the tests once they're | 48 // [_globalDeclarer], and schedule a microtask to run the tests once they're |
49 // finished being defined. | 49 // finished being defined. |
50 _globalDeclarer = new Declarer(); | 50 _globalDeclarer = new Declarer(); |
51 scheduleMicrotask(() { | 51 scheduleMicrotask(() { |
52 var suite = | 52 var suite = |
53 new Suite(_globalDeclarer.tests, | 53 new Suite(_globalDeclarer.tests, |
54 path: p.prettyUri(Uri.base), | 54 path: p.prettyUri(Uri.base), |
55 platform: "VM") | 55 platform: "VM") |
56 .filter(TestPlatform.vm, os: currentOSGuess); | 56 .forPlatform(TestPlatform.vm, os: currentOSGuess); |
57 // TODO(nweiz): Set the exit code on the VM when issue 6943 is fixed. | 57 // TODO(nweiz): Set the exit code on the VM when issue 6943 is fixed. |
58 new NoIoCompactReporter([suite], color: true).run(); | 58 new NoIoCompactReporter([suite], color: true).run(); |
59 }); | 59 }); |
60 return _globalDeclarer; | 60 return _globalDeclarer; |
61 } | 61 } |
62 | 62 |
63 // TODO(nweiz): This and other top-level functions should throw exceptions if | 63 // TODO(nweiz): This and other top-level functions should throw exceptions if |
64 // they're called after the declarer has finished declaring. | 64 // they're called after the declarer has finished declaring. |
65 /// Creates a new test case with the given description and body. | 65 /// Creates a new test case with the given description and body. |
66 /// | 66 /// |
67 /// The description will be added to the descriptions of any surrounding | 67 /// The description will be added to the descriptions of any surrounding |
68 /// [group]s. If [testOn] is passed, it's parsed as a [platform selector][]; the | 68 /// [group]s. If [testOn] is passed, it's parsed as a [platform selector][]; the |
69 /// test will only be run on matching platforms. | 69 /// test will only be run on matching platforms. |
70 /// | 70 /// |
71 /// [platform selector]: https://github.com/dart-lang/test/#platform-selector-sy
ntax | 71 /// [platform selector]: https://github.com/dart-lang/test/#platform-selector-sy
ntax |
72 /// | 72 /// |
73 /// If [timeout] is passed, it's used to modify or replace the default timeout | 73 /// If [timeout] is passed, it's used to modify or replace the default timeout |
74 /// of 30 seconds. Timeout modifications take precedence in suite-group-test | 74 /// of 30 seconds. Timeout modifications take precedence in suite-group-test |
75 /// order, so [timeout] will also modify any timeouts set on the group or suite. | 75 /// order, so [timeout] will also modify any timeouts set on the group or suite. |
76 /// | 76 /// |
77 /// If [skip] is a String or `true`, the test is skipped. If it's a String, it | 77 /// If [skip] is a String or `true`, the test is skipped. If it's a String, it |
78 /// should explain why the test is skipped; this reason will be printed instead | 78 /// should explain why the test is skipped; this reason will be printed instead |
79 /// of running the test. | 79 /// of running the test. |
| 80 /// |
| 81 /// [onPlatform] allows tests to be configured on a platform-by-platform |
| 82 /// basis. It's a map from strings that are parsed as [PlatformSelector]s to |
| 83 /// annotation classes: [Timeout], [Skip], or lists of those. These |
| 84 /// annotations apply only on the given platforms. For example: |
| 85 /// |
| 86 /// test("potentially slow test", () { |
| 87 /// // ... |
| 88 /// }, onPlatform: { |
| 89 /// // This test is especially slow on Windows. |
| 90 /// "windows": new Timeout.factor(2), |
| 91 /// "browser": [ |
| 92 /// new Skip("TODO: add browser support"), |
| 93 /// // This will be slow on browsers once it works on them. |
| 94 /// new Timeout.factor(2) |
| 95 /// ] |
| 96 /// }); |
| 97 /// |
| 98 /// If multiple platforms match, the annotations apply in order as through |
| 99 /// they were in nested groups. |
80 void test(String description, body(), {String testOn, Timeout timeout, | 100 void test(String description, body(), {String testOn, Timeout timeout, |
81 skip}) => | 101 skip, Map<String, dynamic> onPlatform}) => |
82 _declarer.test(description, body, | 102 _declarer.test(description, body, |
83 testOn: testOn, timeout: timeout, skip: skip); | 103 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); |
84 | 104 |
85 /// Creates a group of tests. | 105 /// Creates a group of tests. |
86 /// | 106 /// |
87 /// A group's description is included in the descriptions of any tests or | 107 /// A group's description is included in the descriptions of any tests or |
88 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the | 108 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the |
89 /// containing group. | 109 /// containing group. |
90 /// | 110 /// |
91 /// If [testOn] is passed, it's parsed as a [platform selector][]; the test will | 111 /// If [testOn] is passed, it's parsed as a [platform selector][]; the test will |
92 /// only be run on matching platforms. | 112 /// only be run on matching platforms. |
93 /// | 113 /// |
94 /// [platform selector]: https://github.com/dart-lang/test/#platform-selector-sy
ntax | 114 /// [platform selector]: https://github.com/dart-lang/test/#platform-selector-sy
ntax |
95 /// | 115 /// |
96 /// If [timeout] is passed, it's used to modify or replace the default timeout | 116 /// If [timeout] is passed, it's used to modify or replace the default timeout |
97 /// of 30 seconds. Timeout modifications take precedence in suite-group-test | 117 /// of 30 seconds. Timeout modifications take precedence in suite-group-test |
98 /// order, so [timeout] will also modify any timeouts set on the suite, and will | 118 /// order, so [timeout] will also modify any timeouts set on the suite, and will |
99 /// be modified by any timeouts set on individual tests. | 119 /// be modified by any timeouts set on individual tests. |
100 /// | 120 /// |
101 /// If [skip] is a String or `true`, the group is skipped. If it's a String, it | 121 /// If [skip] is a String or `true`, the group is skipped. If it's a String, it |
102 /// should explain why the group is skipped; this reason will be printed instead | 122 /// should explain why the group is skipped; this reason will be printed instead |
103 /// of running the group's tests. | 123 /// of running the group's tests. |
| 124 /// |
| 125 /// [onPlatform] allows groups to be configured on a platform-by-platform |
| 126 /// basis. It's a map from strings that are parsed as [PlatformSelector]s to |
| 127 /// annotation classes: [Timeout], [Skip], or lists of those. These |
| 128 /// annotations apply only on the given platforms. For example: |
| 129 /// |
| 130 /// group("potentially slow tests", () { |
| 131 /// // ... |
| 132 /// }, onPlatform: { |
| 133 /// // These tests are especially slow on Windows. |
| 134 /// "windows": new Timeout.factor(2), |
| 135 /// "browser": [ |
| 136 /// new Skip("TODO: add browser support"), |
| 137 /// // They'll be slow on browsers once it works on them. |
| 138 /// new Timeout.factor(2) |
| 139 /// ] |
| 140 /// }); |
| 141 /// |
| 142 /// If multiple platforms match, the annotations apply in order as through |
| 143 /// they were in nested groups. |
104 void group(String description, void body(), {String testOn, Timeout timeout, | 144 void group(String description, void body(), {String testOn, Timeout timeout, |
105 skip}) => | 145 skip, Map<String, dynamic> onPlatform}) => |
106 _declarer.group(description, body, | 146 _declarer.group(description, body, |
107 testOn: testOn, timeout: timeout, skip: skip); | 147 testOn: testOn, timeout: timeout, skip: skip); |
108 | 148 |
109 /// Registers a function to be run before tests. | 149 /// Registers a function to be run before tests. |
110 /// | 150 /// |
111 /// This function will be called before each test is run. [callback] may be | 151 /// This function will be called before each test is run. [callback] may be |
112 /// asynchronous; if so, it must return a [Future]. | 152 /// asynchronous; if so, it must return a [Future]. |
113 /// | 153 /// |
114 /// If this is called within a test group, it applies only to tests in that | 154 /// If this is called within a test group, it applies only to tests in that |
115 /// group. [callback] will be run after any set-up callbacks in parent groups or | 155 /// group. [callback] will be run after any set-up callbacks in parent groups or |
(...skipping 12 matching lines...) Expand all Loading... |
128 | 168 |
129 /// Handle an error that occurs outside of any test. | 169 /// Handle an error that occurs outside of any test. |
130 void handleExternalError(error, String message, [stackTrace]) { | 170 void handleExternalError(error, String message, [stackTrace]) { |
131 // TODO(nweiz): handle this better. | 171 // TODO(nweiz): handle this better. |
132 registerException(error, stackTrace); | 172 registerException(error, stackTrace); |
133 } | 173 } |
134 | 174 |
135 /// Registers an exception that was caught for the current test. | 175 /// Registers an exception that was caught for the current test. |
136 void registerException(error, [StackTrace stackTrace]) => | 176 void registerException(error, [StackTrace stackTrace]) => |
137 Invoker.current.handleError(error, stackTrace); | 177 Invoker.current.handleError(error, stackTrace); |
OLD | NEW |