Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1191)

Side by Side Diff: lib/src/backend/declarer.dart

Issue 1379203002: Refactor groups to pipe them through to the runner. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/src/backend/group.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.declarer; 5 library test.backend.declarer;
6 6
7 import 'dart:collection'; 7 import 'dart:async';
8 8
9 import '../frontend/timeout.dart'; 9 import '../frontend/timeout.dart';
10 import '../utils.dart';
10 import 'group.dart'; 11 import 'group.dart';
11 import 'invoker.dart'; 12 import 'invoker.dart';
12 import 'metadata.dart'; 13 import 'metadata.dart';
13 import 'test.dart'; 14 import 'suite_entry.dart';
14 15
15 /// A class that manages the state of tests as they're declared. 16 /// A class that manages the state of tests as they're declared.
16 /// 17 ///
17 /// This is in charge of tracking the current group, set-up, and tear-down 18 /// A nested tree of Declarers tracks the current group, set-up, and tear-down
18 /// functions. It produces a list of runnable [tests]. 19 /// functions. Each Declarer in the tree corresponds to a group. This tree is
20 /// tracked by a zone-scoped "current" Declarer; the current declarer can be set
21 /// for a block using [Declarer.declare], and it can be accessed using
22 /// [Declarer.current].
19 class Declarer { 23 class Declarer {
20 /// The current group. 24 /// The parent declarer, or `null` if this corresponds to the root group.
21 var _group = new Group.root(); 25 final Declarer _parent;
22 26
23 /// The list of tests that have been defined. 27 /// The name of the current test group, including the name of any parent
24 List<Test> get tests => new UnmodifiableListView<Test>(_tests); 28 /// groups.
25 final _tests = new List<Test>(); 29 ///
30 /// This is `null` if this is the root group.
31 final String _name;
26 32
27 Declarer(); 33 /// The metadata for this group, including the metadata of any parent groups
34 /// and of the test suite.
35 final Metadata _metadata;
28 36
29 /// Defines a test case with the given description and body. 37 /// The set-up functions for this group.
30 void test(String description, body(), {String testOn, Timeout timeout, 38 final _setUps = new List<AsyncFunction>();
31 skip, Map<String, dynamic> onPlatform}) {
32 // TODO(nweiz): Once tests have begun running, throw an error if [test] is
33 // called.
34 var prefix = _group.description;
35 if (prefix != null) description = "$prefix $description";
36 39
37 var metadata = _group.metadata.merge(new Metadata.parse( 40 /// The tear-down functions for this group.
41 final _tearDowns = new List<AsyncFunction>();
42
43 /// The children of this group, either tests or sub-groups.
44 final _entries = new List<SuiteEntry>();
45
46 /// Whether [build] has been called for this declarer.
47 bool _built = false;
48
49 /// The current zone-scoped declarer.
50 static Declarer get current => Zone.current[#test.declarer];
51
52 /// Creates a new declarer for the root group.
53 ///
54 /// This is the implicit group that exists outside of any calls to `group()`.
55 /// [metadata] should be the suite's metadata, if available.
56 Declarer([Metadata metadata])
57 : this._(null, null, metadata == null ? new Metadata() : metadata);
58
59 Declarer._(this._parent, this._name, this._metadata);
60
61 /// Runs [body] with this declarer as [Declarer.current].
62 ///
63 /// Returns the return value of [body].
64 declare(body()) => runZoned(body, zoneValues: {#test.declarer: this});
65
66 /// Defines a test case with the given name and body.
67 void test(String name, body(), {String testOn, Timeout timeout, skip,
68 Map<String, dynamic> onPlatform}) {
69 if (_built) {
70 throw new StateError("Can't call test() once tests have begun running.");
71 }
72
73 var metadata = _metadata.merge(new Metadata.parse(
38 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform)); 74 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform));
39 75
40 var group = _group; 76 _entries.add(new LocalTest(_prefix(name), metadata, () {
41 _tests.add(new LocalTest(description, metadata, () {
42 // TODO(nweiz): It might be useful to throw an error here if a test starts 77 // TODO(nweiz): It might be useful to throw an error here if a test starts
43 // running while other tests from the same declarer are also running, 78 // running while other tests from the same declarer are also running,
44 // since they might share closurized state. 79 // since they might share closurized state.
45 80
46 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in 81 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in
47 // two stable versions. 82 // two stable versions.
48 return Invoker.current.waitForOutstandingCallbacks(() { 83 return Invoker.current.waitForOutstandingCallbacks(() {
49 return group.runSetUps().then((_) => body()); 84 return _runSetUps().then((_) => body());
50 }).then((_) => group.runTearDowns()); 85 }).then((_) => _runTearDowns());
51 })); 86 }));
52 } 87 }
53 88
54 /// Creates a group of tests. 89 /// Creates a group of tests.
55 void group(String description, void body(), {String testOn, 90 void group(String name, void body(), {String testOn, Timeout timeout, skip,
56 Timeout timeout, skip, Map<String, dynamic> onPlatform}) { 91 Map<String, dynamic> onPlatform}) {
57 var oldGroup = _group; 92 if (_built) {
93 throw new StateError("Can't call group() once tests have begun running.");
94 }
58 95
59 var metadata = new Metadata.parse( 96 var metadata = _metadata.merge(new Metadata.parse(
60 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); 97 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform));
61 98
62 // Don' load the tests for a skipped group. 99 // Don't load the tests for a skipped group.
63 if (metadata.skip) { 100 if (metadata.skip) {
64 _tests.add(new LocalTest(description, metadata, () {})); 101 _entries.add(new Group(name, metadata, []));
65 return; 102 return;
66 } 103 }
67 104
68 _group = new Group(oldGroup, description, metadata); 105 var declarer = new Declarer._(this, _prefix(name), metadata);
69 try { 106 declarer.declare(body);
70 body(); 107 _entries.add(new Group(declarer._name, metadata, declarer.build()));
71 } finally {
72 _group = oldGroup;
73 }
74 } 108 }
75 109
76 /// Registers a function to be run before tests. 110 /// Returns [name] prefixed with this declarer's group name.
111 String _prefix(String name) => _name == null ? name : "$_name $name";
112
113 /// Registers a function to be run before each test in this group.
77 void setUp(callback()) { 114 void setUp(callback()) {
78 _group.setUps.add(callback); 115 if (_built) {
116 throw new StateError("Can't call setUp() once tests have begun running.");
117 }
118
119 _setUps.add(callback);
79 } 120 }
80 121
81 /// Registers a function to be run after tests. 122 /// Registers a function to be run after each test in this group.
82 void tearDown(callback()) { 123 void tearDown(callback()) {
83 _group.tearDowns.add(callback); 124 if (_built) {
125 throw new StateError(
126 "Can't call tearDown() once tests have begun running.");
127 }
128
129 _tearDowns.add(callback);
130 }
131
132 /// Finalizes and returns the tests and groups being declared.
133 List<SuiteEntry> build() {
134 if (_built) {
135 throw new StateError("Can't call Declarer.build() more than once.");
136 }
137
138 _built = true;
139 return _entries.toList();
140 }
141
142 /// Run the set-up functions for this and any parent groups.
143 ///
144 /// If no set-up functions are declared, this returns a [Future] that
145 /// completes immediately.
146 Future _runSetUps() {
147 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two
148 // stable versions.
149 if (_parent != null) {
150 return _parent._runSetUps().then((_) {
151 return Future.forEach(_setUps, (setUp) => setUp());
152 });
153 }
154
155 return Future.forEach(_setUps, (setUp) => setUp());
156 }
157
158 /// Run the tear-up functions for this and any parent groups.
159 ///
160 /// If no set-up functions are declared, this returns a [Future] that
161 /// completes immediately.
162 ///
163 /// This should only be called within a test.
164 Future _runTearDowns() {
165 return Invoker.current.unclosable(() {
166 var tearDowns = [];
167 for (var declarer = this; declarer != null; declarer = declarer._parent) {
168 tearDowns.addAll(declarer._tearDowns.reversed);
169 }
170
171 return Future.forEach(tearDowns, _errorsDontStopTest);
172 });
173 }
174
175 /// Runs [body] with special error-handling behavior.
176 ///
177 /// Errors emitted [body] will still cause the current test to fail, but they
178 /// won't cause it to *stop*. In particular, they won't remove any outstanding
179 /// callbacks registered outside of [body].
180 Future _errorsDontStopTest(body()) {
181 var completer = new Completer();
182
183 Invoker.current.addOutstandingCallback();
184 Invoker.current.waitForOutstandingCallbacks(() {
185 new Future.sync(body).whenComplete(completer.complete);
186 }).then((_) => Invoker.current.removeOutstandingCallback());
187
188 return completer.future;
84 } 189 }
85 } 190 }
OLDNEW
« no previous file with comments | « no previous file | lib/src/backend/group.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698