OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.backend.group; | 5 library test.backend.group; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import '../utils.dart'; | 9 import '../utils.dart'; |
10 import 'invoker.dart'; | 10 import 'invoker.dart'; |
(...skipping 12 matching lines...) Expand all Loading... |
23 /// group. | 23 /// group. |
24 final String _description; | 24 final String _description; |
25 | 25 |
26 /// The metadata for this group, including the metadata of any parent groups. | 26 /// The metadata for this group, including the metadata of any parent groups. |
27 Metadata get metadata { | 27 Metadata get metadata { |
28 if (parent == null) return _metadata; | 28 if (parent == null) return _metadata; |
29 return parent.metadata.merge(_metadata); | 29 return parent.metadata.merge(_metadata); |
30 } | 30 } |
31 final Metadata _metadata; | 31 final Metadata _metadata; |
32 | 32 |
33 /// The set-up function for this group, or `null`. | 33 /// The set-up functions for this group. |
34 AsyncFunction setUp; | 34 final setUps = new List<AsyncFunction>(); |
35 | 35 |
36 /// The tear-down function for this group, or `null`. | 36 /// The tear-down functions for this group. |
37 AsyncFunction tearDown; | 37 final tearDowns = new List<AsyncFunction>(); |
38 | 38 |
39 /// Returns the description for this group, including the description of any | 39 /// Returns the description for this group, including the description of any |
40 /// parent groups. | 40 /// parent groups. |
41 /// | 41 /// |
42 /// If this is the root group, returns `null`. | 42 /// If this is the root group, returns `null`. |
43 String get description { | 43 String get description { |
44 if (parent == null || parent.description == null) return _description; | 44 if (parent == null || parent.description == null) return _description; |
45 return "${parent.description} $_description"; | 45 return "${parent.description} $_description"; |
46 } | 46 } |
47 | 47 |
48 /// Creates a new root group. | 48 /// Creates a new root group. |
49 /// | 49 /// |
50 /// This is the implicit group that exists outside of any calls to `group()`. | 50 /// This is the implicit group that exists outside of any calls to `group()`. |
51 Group.root() | 51 Group.root() |
52 : this(null, null, new Metadata()); | 52 : this(null, null, new Metadata()); |
53 | 53 |
54 Group(this.parent, this._description, this._metadata); | 54 Group(this.parent, this._description, this._metadata); |
55 | 55 |
56 /// Run the set-up functions for this and any parent groups. | 56 /// Run the set-up functions for this and any parent groups. |
57 /// | 57 /// |
58 /// If no set-up functions are declared, this returns a [Future] that | 58 /// If no set-up functions are declared, this returns a [Future] that |
59 /// completes immediately. | 59 /// completes immediately. |
60 Future runSetUp() { | 60 Future runSetUps() { |
61 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two | 61 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two |
62 // stable versions. | 62 // stable versions. |
63 if (parent != null) { | 63 if (parent != null) { |
64 return parent.runSetUp().then((_) { | 64 return parent.runSetUps().then((_) { |
65 if (setUp != null) return setUp(); | 65 return Future.forEach(setUps, (setUp) => setUp()); |
66 }); | 66 }); |
67 } | 67 } |
68 | 68 |
69 if (setUp != null) return new Future.sync(setUp); | 69 return Future.forEach(setUps, (setUp) => setUp()); |
70 return new Future.value(); | |
71 } | 70 } |
72 | 71 |
73 /// Run the tear-up functions for this and any parent groups. | 72 /// Run the tear-up functions for this and any parent groups. |
74 /// | 73 /// |
75 /// If no set-up functions are declared, this returns a [Future] that | 74 /// If no set-up functions are declared, this returns a [Future] that |
76 /// completes immediately. | 75 /// completes immediately. |
77 Future runTearDown() { | 76 Future runTearDowns() { |
78 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two | 77 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two |
79 // stable versions. | 78 // stable versions. |
80 if (parent == null) { | 79 if (parent == null) { |
81 return tearDown == null ? new Future.value() : new Future.sync(tearDown); | 80 return Future.forEach(tearDowns.reversed, _errorsDontStopTest); |
82 } | 81 } |
83 | 82 |
84 return _errorsDontStopTest(() { | 83 return Future.forEach(tearDowns.reversed, _errorsDontStopTest) |
85 if (tearDown != null) return tearDown(); | 84 .then((_) => parent.runTearDowns()); |
86 }).then((_) => parent.runTearDown()); | |
87 } | 85 } |
88 | 86 |
89 /// Runs [body] with special error-handling behavior. | 87 /// Runs [body] with special error-handling behavior. |
90 /// | 88 /// |
91 /// Errors emitted [body] will still cause be the test to fail, but they won't | 89 /// Errors emitted [body] will still cause be the test to fail, but they won't |
92 /// cause it to *stop*. In particular, they won't remove any outstanding | 90 /// cause it to *stop*. In particular, they won't remove any outstanding |
93 /// callbacks registered outside of [body]. | 91 /// callbacks registered outside of [body]. |
94 Future _errorsDontStopTest(body()) { | 92 Future _errorsDontStopTest(body()) { |
95 var completer = new Completer(); | 93 var completer = new Completer(); |
| 94 |
| 95 Invoker.current.addOutstandingCallback(); |
96 Invoker.current.waitForOutstandingCallbacks(() { | 96 Invoker.current.waitForOutstandingCallbacks(() { |
97 new Future.sync(body).whenComplete(completer.complete); | 97 new Future.sync(body).whenComplete(completer.complete); |
98 }); | 98 }).then((_) => Invoker.current.removeOutstandingCallback()); |
| 99 |
99 return completer.future; | 100 return completer.future; |
100 } | 101 } |
101 } | 102 } |
OLD | NEW |