|
4 | 4 |
|
5 | 5 | @Deprecated('package:test_core is not intended for general use. ' |
6 | 6 | 'Please use package:test.') |
7 | | -library test_core.scaffolding; |
| 7 | +library; |
8 | 8 |
|
9 | | -import 'dart:async'; |
10 | | - |
11 | | -import 'package:meta/meta.dart' show isTest, isTestGroup; |
12 | | -import 'package:path/path.dart' as p; |
13 | | -import 'package:test_api/backend.dart'; |
14 | | -import 'package:test_api/scaffolding.dart' show Timeout, pumpEventQueue; |
15 | | -import 'package:test_api/src/backend/declarer.dart'; // ignore: implementation_imports |
16 | | -import 'package:test_api/src/backend/invoker.dart'; // ignore: implementation_imports |
17 | | - |
18 | | -import 'src/runner/engine.dart'; |
19 | | -import 'src/runner/plugin/environment.dart'; |
20 | | -import 'src/runner/reporter/expanded.dart'; |
21 | | -import 'src/runner/runner_suite.dart'; |
22 | | -import 'src/runner/suite.dart'; |
23 | | -import 'src/util/async.dart'; |
24 | | -import 'src/util/os.dart'; |
25 | | -import 'src/util/print_sink.dart'; |
26 | | - |
27 | | -// Hide implementations which don't support being run directly. |
28 | | -// This file is an almost direct copy of import below, but with the global |
29 | | -// declarer added. |
30 | | -export 'package:test_api/scaffolding.dart' |
31 | | - hide group, setUp, setUpAll, tearDown, tearDownAll, test; |
32 | | - |
33 | | -/// The global declarer. |
34 | | -/// |
35 | | -/// This is used if a test file is run directly, rather than through the runner. |
36 | | -Declarer? _globalDeclarer; |
37 | | - |
38 | | -/// Gets the declarer for the current scope. |
39 | | -/// |
40 | | -/// When using the runner, this returns the [Zone]-scoped declarer that's set by |
41 | | -/// [RemoteListener]. If the test file is run directly, this returns |
42 | | -/// [_globalDeclarer] (and sets it up on the first call). |
43 | | -Declarer get _declarer { |
44 | | - var declarer = Declarer.current; |
45 | | - if (declarer != null) return declarer; |
46 | | - if (_globalDeclarer != null) return _globalDeclarer!; |
47 | | - |
48 | | - // Since there's no Zone-scoped declarer, the test file is being run directly. |
49 | | - // In order to run the tests, we set up our own Declarer via |
50 | | - // [_globalDeclarer], and pump the event queue as a best effort to wait for |
51 | | - // all tests to be defined before starting them. |
52 | | - _globalDeclarer = Declarer(); |
53 | | - |
54 | | - () async { |
55 | | - await pumpEventQueue(); |
56 | | - |
57 | | - var suite = RunnerSuite( |
58 | | - const PluginEnvironment(), |
59 | | - SuiteConfiguration.empty, |
60 | | - _globalDeclarer!.build(), |
61 | | - SuitePlatform(Runtime.vm, compiler: null, os: currentOSGuess), |
62 | | - path: p.prettyUri(Uri.base)); |
63 | | - |
64 | | - var engine = Engine(); |
65 | | - engine.suiteSink.add(suite); |
66 | | - engine.suiteSink.close(); |
67 | | - ExpandedReporter.watch(engine, PrintSink(), |
68 | | - color: true, printPath: false, printPlatform: false); |
69 | | - |
70 | | - var success = await runZoned(() => Invoker.guard(engine.run), |
71 | | - zoneValues: {#test.declarer: _globalDeclarer}); |
72 | | - if (success == true) return null; |
73 | | - print(''); |
74 | | - unawaited(Future.error('Dummy exception to set exit code.')); |
75 | | - }(); |
76 | | - |
77 | | - return _globalDeclarer!; |
78 | | -} |
79 | | - |
80 | | -// TODO(nweiz): This and other top-level functions should throw exceptions if |
81 | | -// they're called after the declarer has finished declaring. |
82 | | -/// Creates a new test case with the given description (converted to a string) |
83 | | -/// and body. |
84 | | -/// |
85 | | -/// The description will be added to the descriptions of any surrounding |
86 | | -/// [group]s. If [testOn] is passed, it's parsed as a [platform selector][]; the |
87 | | -/// test will only be run on matching platforms. |
88 | | -/// |
89 | | -/// [platform selector]: https://github.com/dart-lang/test/tree/master/pkgs/test#platform-selectors |
90 | | -/// |
91 | | -/// If [timeout] is passed, it's used to modify or replace the default timeout |
92 | | -/// of 30 seconds. Timeout modifications take precedence in suite-group-test |
93 | | -/// order, so [timeout] will also modify any timeouts set on the group or suite. |
94 | | -/// |
95 | | -/// If [skip] is a String or `true`, the test is skipped. If it's a String, it |
96 | | -/// should explain why the test is skipped; this reason will be printed instead |
97 | | -/// of running the test. |
98 | | -/// |
99 | | -/// If [tags] is passed, it declares user-defined tags that are applied to the |
100 | | -/// test. These tags can be used to select or skip the test on the command line, |
101 | | -/// or to do bulk test configuration. All tags should be declared in the |
102 | | -/// [package configuration file][configuring tags]. The parameter can be an |
103 | | -/// [Iterable] of tag names, or a [String] representing a single tag. |
104 | | -/// |
105 | | -/// If [retry] is passed, the test will be retried the provided number of times |
106 | | -/// before being marked as a failure. |
107 | | -/// |
108 | | -/// [configuring tags]: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/configuration.md#configuring-tags |
109 | | -/// |
110 | | -/// [onPlatform] allows tests to be configured on a platform-by-platform |
111 | | -/// basis. It's a map from strings that are parsed as [PlatformSelector]s to |
112 | | -/// annotation classes: [Timeout], [Skip], or lists of those. These |
113 | | -/// annotations apply only on the given platforms. For example: |
114 | | -/// |
115 | | -/// test('potentially slow test', () { |
116 | | -/// // ... |
117 | | -/// }, onPlatform: { |
118 | | -/// // This test is especially slow on Windows. |
119 | | -/// 'windows': Timeout.factor(2), |
120 | | -/// 'browser': [ |
121 | | -/// Skip('TODO: add browser support'), |
122 | | -/// // This will be slow on browsers once it works on them. |
123 | | -/// Timeout.factor(2) |
124 | | -/// ] |
125 | | -/// }); |
126 | | -/// |
127 | | -/// If multiple platforms match, the annotations apply in order as through |
128 | | -/// they were in nested groups. |
129 | | -/// |
130 | | -/// If the `solo` flag is `true`, only tests and groups marked as |
131 | | -/// "solo" will be be run. This only restricts tests *within this test |
132 | | -/// suite*—tests in other suites will run as normal. We recommend that users |
133 | | -/// avoid this flag if possible and instead use the test runner flag `-n` to |
134 | | -/// filter tests by name. |
135 | | -@isTest |
136 | | -void test(Object? description, dynamic Function() body, |
137 | | - {String? testOn, |
138 | | - Timeout? timeout, |
139 | | - Object? skip, |
140 | | - Object? tags, |
141 | | - Map<String, dynamic>? onPlatform, |
142 | | - int? retry, |
143 | | - @Deprecated('Debug only') bool solo = false}) { |
144 | | - _declarer.test(description.toString(), body, |
145 | | - testOn: testOn, |
146 | | - timeout: timeout, |
147 | | - skip: skip, |
148 | | - onPlatform: onPlatform, |
149 | | - tags: tags, |
150 | | - retry: retry, |
151 | | - solo: solo); |
152 | | - |
153 | | - // Force dart2js not to inline this function. We need it to be separate from |
154 | | - // `main()` in JS stack traces in order to properly determine the line and |
155 | | - // column where the test was defined. See sdk#26705. |
156 | | - return; |
157 | | - return; // ignore: dead_code |
158 | | -} |
159 | | - |
160 | | -/// Creates a group of tests. |
161 | | -/// |
162 | | -/// A group's description (converted to a string) is included in the descriptions |
163 | | -/// of any tests or sub-groups it contains. [setUp] and [tearDown] are also scoped |
164 | | -/// to the containing group. |
165 | | -/// |
166 | | -/// If [testOn] is passed, it's parsed as a [platform selector][]; the test will |
167 | | -/// only be run on matching platforms. |
168 | | -/// |
169 | | -/// [platform selector]: https://github.com/dart-lang/test/tree/master/pkgs/test#platform-selectors |
170 | | -/// |
171 | | -/// If [timeout] is passed, it's used to modify or replace the default timeout |
172 | | -/// of 30 seconds. Timeout modifications take precedence in suite-group-test |
173 | | -/// order, so [timeout] will also modify any timeouts set on the suite, and will |
174 | | -/// be modified by any timeouts set on individual tests. |
175 | | -/// |
176 | | -/// If [skip] is a String or `true`, the group is skipped. If it's a String, it |
177 | | -/// should explain why the group is skipped; this reason will be printed instead |
178 | | -/// of running the group's tests. |
179 | | -/// |
180 | | -/// If [tags] is passed, it declares user-defined tags that are applied to the |
181 | | -/// test. These tags can be used to select or skip the test on the command line, |
182 | | -/// or to do bulk test configuration. All tags should be declared in the |
183 | | -/// [package configuration file][configuring tags]. The parameter can be an |
184 | | -/// [Iterable] of tag names, or a [String] representing a single tag. |
185 | | -/// |
186 | | -/// [configuring tags]: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/configuration.md#configuring-tags |
187 | | -/// |
188 | | -/// [onPlatform] allows groups to be configured on a platform-by-platform |
189 | | -/// basis. It's a map from strings that are parsed as [PlatformSelector]s to |
190 | | -/// annotation classes: [Timeout], [Skip], or lists of those. These |
191 | | -/// annotations apply only on the given platforms. For example: |
192 | | -/// |
193 | | -/// group('potentially slow tests', () { |
194 | | -/// // ... |
195 | | -/// }, onPlatform: { |
196 | | -/// // These tests are especially slow on Windows. |
197 | | -/// 'windows': Timeout.factor(2), |
198 | | -/// 'browser': [ |
199 | | -/// Skip('TODO: add browser support'), |
200 | | -/// // They'll be slow on browsers once it works on them. |
201 | | -/// Timeout.factor(2) |
202 | | -/// ] |
203 | | -/// }); |
204 | | -/// |
205 | | -/// If multiple platforms match, the annotations apply in order as through |
206 | | -/// they were in nested groups. |
207 | | -/// |
208 | | -/// If the `solo` flag is `true`, only tests and groups marked as |
209 | | -/// "solo" will be be run. This only restricts tests *within this test |
210 | | -/// suite*—tests in other suites will run as normal. We recommend that users |
211 | | -/// avoid this flag if possible, and instead use the test runner flag `-n` to |
212 | | -/// filter tests by name. |
213 | | -@isTestGroup |
214 | | -void group(Object? description, dynamic Function() body, |
215 | | - {String? testOn, |
216 | | - Timeout? timeout, |
217 | | - Object? skip, |
218 | | - Object? tags, |
219 | | - Map<String, dynamic>? onPlatform, |
220 | | - int? retry, |
221 | | - @Deprecated('Debug only') bool solo = false}) { |
222 | | - _declarer.group(description.toString(), body, |
223 | | - testOn: testOn, |
224 | | - timeout: timeout, |
225 | | - skip: skip, |
226 | | - tags: tags, |
227 | | - onPlatform: onPlatform, |
228 | | - retry: retry, |
229 | | - solo: solo); |
230 | | - |
231 | | - // Force dart2js not to inline this function. We need it to be separate from |
232 | | - // `main()` in JS stack traces in order to properly determine the line and |
233 | | - // column where the test was defined. See sdk#26705. |
234 | | - return; |
235 | | - return; // ignore: dead_code |
236 | | -} |
237 | | - |
238 | | -/// Registers a function to be run before tests. |
239 | | -/// |
240 | | -/// This function will be called before each test is run. [callback] may be |
241 | | -/// asynchronous; if so, it must return a [Future]. |
242 | | -/// |
243 | | -/// If this is called within a test group, it applies only to tests in that |
244 | | -/// group. [callback] will be run after any set-up callbacks in parent groups or |
245 | | -/// at the top level. |
246 | | -/// |
247 | | -/// Each callback at the top level or in a given group will be run in the order |
248 | | -/// they were declared. |
249 | | -void setUp(dynamic Function() callback) => _declarer.setUp(callback); |
250 | | - |
251 | | -/// Registers a function to be run after tests. |
252 | | -/// |
253 | | -/// This function will be called after each test is run. [callback] may be |
254 | | -/// asynchronous; if so, it must return a [Future]. |
255 | | -/// |
256 | | -/// If this is called within a test group, it applies only to tests in that |
257 | | -/// group. [callback] will be run before any tear-down callbacks in parent |
258 | | -/// groups or at the top level. |
259 | | -/// |
260 | | -/// Each callback at the top level or in a given group will be run in the |
261 | | -/// reverse of the order they were declared. |
262 | | -/// |
263 | | -/// See also [addTearDown], which adds tear-downs to a running test. |
264 | | -void tearDown(dynamic Function() callback) => _declarer.tearDown(callback); |
265 | | - |
266 | | -/// Registers a function to be run once before all tests. |
267 | | -/// |
268 | | -/// [callback] may be asynchronous; if so, it must return a [Future]. |
269 | | -/// |
270 | | -/// If this is called within a test group, [callback] will run before all tests |
271 | | -/// in that group. It will be run after any [setUpAll] callbacks in parent |
272 | | -/// groups or at the top level. It won't be run if none of the tests in the |
273 | | -/// group are run. |
274 | | -/// |
275 | | -/// **Note**: This function makes it very easy to accidentally introduce hidden |
276 | | -/// dependencies between tests that should be isolated. In general, you should |
277 | | -/// prefer [setUp], and only use [setUpAll] if the callback is prohibitively |
278 | | -/// slow. |
279 | | -void setUpAll(dynamic Function() callback) => _declarer.setUpAll(callback); |
280 | | - |
281 | | -/// Registers a function to be run once after all tests. |
282 | | -/// |
283 | | -/// If this is called within a test group, [callback] will run after all tests |
284 | | -/// in that group. It will be run before any [tearDownAll] callbacks in parent |
285 | | -/// groups or at the top level. It won't be run if none of the tests in the |
286 | | -/// group are run. |
287 | | -/// |
288 | | -/// **Note**: This function makes it very easy to accidentally introduce hidden |
289 | | -/// dependencies between tests that should be isolated. In general, you should |
290 | | -/// prefer [tearDown], and only use [tearDownAll] if the callback is |
291 | | -/// prohibitively slow. |
292 | | -void tearDownAll(dynamic Function() callback) => |
293 | | - _declarer.tearDownAll(callback); |
| 9 | +export 'src/scaffolding.dart'; |
0 commit comments