@@ -105,15 +105,24 @@ class Declarer {
105
105
tags: tags));
106
106
107
107
_entries.add (new LocalTest (_prefix (name), metadata, () async {
108
- // TODO(nweiz): It might be useful to throw an error here if a test starts
109
- // running while other tests from the same declarer are also running,
110
- // since they might share closurized state.
108
+ var parents = [];
109
+ for (var declarer = this ; declarer != null ; declarer = declarer._parent) {
110
+ parents.add (declarer);
111
+ }
112
+
113
+ // Register all tear-down functions in all declarers. Iterate through
114
+ // parents outside-in so that the Invoker gets the functions in the order
115
+ // they were declared in source.
116
+ for (var declarer in parents.reversed) {
117
+ for (var tearDown in declarer._tearDowns) {
118
+ Invoker .current.addTearDown (tearDown);
119
+ }
120
+ }
111
121
112
122
await Invoker .current.waitForOutstandingCallbacks (() async {
113
123
await _runSetUps ();
114
124
await body ();
115
125
});
116
- await _runTearDowns ();
117
126
}, trace: _collectTraces ? new Trace .current (2 ) : null ));
118
127
}
119
128
@@ -197,23 +206,6 @@ class Declarer {
197
206
await Future .forEach (_setUps, (setUp) => setUp ());
198
207
}
199
208
200
- /// Run the tear-up functions for this and any parent groups.
201
- ///
202
- /// If no set-up functions are declared, this returns a [Future] that
203
- /// completes immediately.
204
- ///
205
- /// This should only be called within a test.
206
- Future _runTearDowns () {
207
- return Invoker .current.unclosable (() {
208
- var tearDowns = [];
209
- for (var declarer = this ; declarer != null ; declarer = declarer._parent) {
210
- tearDowns.addAll (declarer._tearDowns.reversed);
211
- }
212
-
213
- return Future .forEach (tearDowns, _errorsDontStopTest);
214
- });
215
- }
216
-
217
209
/// Returns a [Test] that runs the callbacks in [_setUpAll] .
218
210
Test get _setUpAll {
219
211
if (_setUpAlls.isEmpty) return null ;
@@ -229,24 +221,8 @@ class Declarer {
229
221
230
222
return new LocalTest (_prefix ("(tearDownAll)" ), _metadata, () {
231
223
return Invoker .current.unclosable (() {
232
- return Future .forEach (_tearDownAlls.reversed, _errorsDontStopTest );
224
+ return Future .forEach (_tearDownAlls.reversed, errorsDontStopTest );
233
225
});
234
226
}, trace: _tearDownAllTrace);
235
227
}
236
-
237
- /// Runs [body] with special error-handling behavior.
238
- ///
239
- /// Errors emitted [body] will still cause the current test to fail, but they
240
- /// won't cause it to *stop*. In particular, they won't remove any outstanding
241
- /// callbacks registered outside of [body] .
242
- Future _errorsDontStopTest (body ()) {
243
- var completer = new Completer ();
244
-
245
- Invoker .current.addOutstandingCallback ();
246
- Invoker .current.waitForOutstandingCallbacks (() {
247
- new Future .sync (body).whenComplete (completer.complete);
248
- }).then ((_) => Invoker .current.removeOutstandingCallback ());
249
-
250
- return completer.future;
251
- }
252
228
}
0 commit comments