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