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

Side by Side Diff: pkg/scheduled_test/lib/scheduled_test.dart

Issue 337843008: Add a tearDown function to scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // TODO(nweiz): Add support for calling [schedule] while the schedule is already 5 // TODO(nweiz): Add support for calling [schedule] while the schedule is already
6 // running. 6 // running.
7 // TODO(nweiz): Port the non-Pub-specific scheduled test libraries from Pub. 7 // TODO(nweiz): Port the non-Pub-specific scheduled test libraries from Pub.
8 library scheduled_test; 8 library scheduled_test;
9 9
10 import 'dart:async'; 10 import 'dart:async';
(...skipping 12 matching lines...) Expand all
23 export 'src/scheduled_future_matchers.dart'; 23 export 'src/scheduled_future_matchers.dart';
24 export 'src/task.dart'; 24 export 'src/task.dart';
25 25
26 /// The [Schedule] for the current test. This is used to add new tasks and 26 /// The [Schedule] for the current test. This is used to add new tasks and
27 /// inspect the state of the schedule. 27 /// inspect the state of the schedule.
28 /// 28 ///
29 /// This is `null` when there's no test currently running. 29 /// This is `null` when there's no test currently running.
30 Schedule get currentSchedule => _currentSchedule; 30 Schedule get currentSchedule => _currentSchedule;
31 Schedule _currentSchedule; 31 Schedule _currentSchedule;
32 32
33 /// The user-provided setUp function. This is set for each test during 33 /// The user-provided set-up function for the currently-running test.
34 /// `unittest.setUp`. 34 ///
35 /// This is set for each test during `unittest.setUp`.
35 Function _setUpFn; 36 Function _setUpFn;
36 37
38 /// The user-provided tear-down function for the currently-running test.
39 ///
40 /// This is set for each test during `unittest.setUp`.
Bob Nystrom 2014/06/27 17:04:53 tearDown.
nweiz 2014/06/30 21:10:20 It actually is set during unittest's setUp.
41 Function _tearDownFn;
42
43 /// The user-provided set-up function for the current test scope.
44 Function _setUpForGroup;
45
46 /// The user-provided tear-down function for the current test scope.
47 Function _tearDownForGroup;
48
37 /// Creates a new test case with the given description and body. 49 /// Creates a new test case with the given description and body.
38 /// 50 ///
39 /// This has the same semantics as [unittest.test]. 51 /// This has the same semantics as [unittest.test].
40 /// 52 ///
41 /// If [body] returns a [Future], that future will automatically be wrapped with 53 /// If [body] returns a [Future], that future will automatically be wrapped with
42 /// [wrapFuture]. 54 /// [wrapFuture].
43 void test(String description, body()) => 55 void test(String description, body()) =>
44 _test(description, body, unittest.test); 56 _test(description, body, unittest.test);
45 57
46 /// Creates a new test case with the given description and body that will be the 58 /// Creates a new test case with the given description and body that will be the
47 /// only test run in this file. 59 /// only test run in this file.
48 /// 60 ///
49 /// This has the same semantics as [unittest.solo_test]. 61 /// This has the same semantics as [unittest.solo_test].
50 /// 62 ///
51 /// If [body] returns a [Future], that future will automatically be wrapped with 63 /// If [body] returns a [Future], that future will automatically be wrapped with
52 /// [wrapFuture]. 64 /// [wrapFuture].
53 void solo_test(String description, body()) => 65 void solo_test(String description, body()) =>
54 _test(description, body, unittest.solo_test); 66 _test(description, body, unittest.solo_test);
55 67
56 void _test(String description, body(), Function testFn) { 68 void _test(String description, body(), Function testFn) {
57 maybeWrapFuture(future, description) { 69 maybeWrapFuture(future, description) {
58 if (future != null) wrapFuture(future, description); 70 if (future != null) wrapFuture(future, description);
59 } 71 }
60 72
61 unittest.ensureInitialized(); 73 unittest.ensureInitialized();
62 _ensureSetUpForTopLevel(); 74 _initializeForGroup();
63 testFn(description, () { 75 testFn(description, () {
64 var completer = new Completer(); 76 var completer = new Completer();
65 77
66 // Capture this in a local variable in case we capture an out-of-band error 78 // Capture this in a local variable in case we capture an out-of-band error
67 // after the schedule completes. 79 // after the schedule completes.
68 var errorHandler; 80 var errorHandler;
69 81
70 Chain.capture(() { 82 Chain.capture(() {
71 _currentSchedule = new Schedule(); 83 _currentSchedule = new Schedule();
72 errorHandler = _currentSchedule.signalError; 84 errorHandler = _currentSchedule.signalError;
73 return currentSchedule.run(() { 85 return currentSchedule.run(() {
74 if (_setUpFn != null) maybeWrapFuture(_setUpFn(), "set up"); 86 if (_setUpFn != null) maybeWrapFuture(_setUpFn(), "set up");
75 maybeWrapFuture(body(), "test body"); 87 maybeWrapFuture(body(), "test body");
88 if (_tearDownFn != null) maybeWrapFuture(_tearDownFn(), "tear down");
76 }).catchError((error, stackTrace) { 89 }).catchError((error, stackTrace) {
77 if (error is ScheduleError) { 90 if (error is ScheduleError) {
78 assert(error.schedule.errors.contains(error)); 91 assert(error.schedule.errors.contains(error));
79 assert(error.schedule == currentSchedule); 92 assert(error.schedule == currentSchedule);
80 unittest.registerException(error.schedule.errorString()); 93 unittest.registerException(error.schedule.errorString());
81 } else { 94 } else {
82 unittest.registerException(error, new Chain.forTrace(stackTrace)); 95 unittest.registerException(error, new Chain.forTrace(stackTrace));
83 } 96 }
84 }).then(completer.complete); 97 }).then(completer.complete);
85 }, onError: (error, stackTrace) => errorHandler(error, stackTrace)); 98 }, onError: (error, stackTrace) => errorHandler(error, stackTrace));
86 99
87 return completer.future; 100 return completer.future;
88 }); 101 });
89 } 102 }
90 103
91 /// Whether or not the tests currently being defined are in a group. This is 104 /// Whether or not the tests currently being defined are in a group. This is
92 /// only true when defining tests, not when executing them. 105 /// only true when defining tests, not when executing them.
93 bool _inGroup = false; 106 bool _inGroup = false;
94 107
95 /// Creates a new named group of tests. This has the same semantics as 108 /// Creates a new named group of tests. This has the same semantics as
96 /// [unittest.group]. 109 /// [unittest.group].
97 void group(String description, void body()) { 110 void group(String description, void body()) {
98 unittest.ensureInitialized(); 111 unittest.ensureInitialized();
99 _ensureSetUpForTopLevel(); 112 _initializeForGroup();
100 unittest.group(description, () { 113 unittest.group(description, () {
114 var oldSetUp = _setUpForGroup;
115 var oldTearDown = _tearDownForGroup;
116 var wasInitializedForGroup = _initializedForGroup;
101 var wasInGroup = _inGroup; 117 var wasInGroup = _inGroup;
118 _setUpForGroup = null;
119 _tearDownForGroup = null;
120 _initializedForGroup = false;
102 _inGroup = true; 121 _inGroup = true;
103 body(); 122 body();
123 _setUpForGroup = oldSetUp;
124 _tearDownForGroup = oldTearDown;
125 _initializedForGroup = wasInitializedForGroup;
Bob Nystrom 2014/06/27 17:04:53 Is it worth doing this in a finally block?
nweiz 2014/06/30 21:10:20 I really doubt anyone is going to be doing serious
104 _inGroup = wasInGroup; 126 _inGroup = wasInGroup;
105 }); 127 });
106 } 128 }
107 129
108 /// Schedules a task, [fn], to run asynchronously as part of the main task queue 130 /// Schedules a task, [fn], to run asynchronously as part of the main task queue
109 /// of [currentSchedule]. Tasks will be run in the order they're scheduled. If 131 /// of [currentSchedule]. Tasks will be run in the order they're scheduled. If
110 /// [fn] returns a [Future], tasks after it won't be run until that [Future] 132 /// [fn] returns a [Future], tasks after it won't be run until that [Future]
111 /// completes. 133 /// completes.
112 /// 134 ///
113 /// The return value will be completed once the scheduled task has finished 135 /// The return value will be completed once the scheduled task has finished
114 /// running. Its return value is the same as the return value of [fn], or the 136 /// running. Its return value is the same as the return value of [fn], or the
115 /// value it completes to if it's a [Future]. 137 /// value it completes to if it's a [Future].
116 /// 138 ///
117 /// If [description] is passed, it's used to describe the task for debugging 139 /// If [description] is passed, it's used to describe the task for debugging
118 /// purposes when an error occurs. 140 /// purposes when an error occurs.
119 /// 141 ///
120 /// If this is called when a task queue is currently running, it will run [fn] 142 /// If this is called when a task queue is currently running, it will run [fn]
121 /// on the next event loop iteration rather than adding it to a queue. The 143 /// on the next event loop iteration rather than adding it to a queue. The
122 /// current task will not complete until [fn] (and any [Future] it returns) has 144 /// current task will not complete until [fn] (and any [Future] it returns) has
123 /// finished running. Any errors in [fn] will automatically be handled. 145 /// finished running. Any errors in [fn] will automatically be handled.
124 Future schedule(fn(), [String description]) => 146 Future schedule(fn(), [String description]) =>
125 currentSchedule.tasks.schedule(fn, description); 147 currentSchedule.tasks.schedule(fn, description);
126 148
127 /// Register a [setUp] function for a test [group]. This has the same semantics 149 /// Register a [setUp] function for a test [group].
128 /// as [unittest.setUp]. Tasks may be scheduled using [schedule] within
129 /// [setUpFn], and [currentSchedule] may be accessed as well.
130 /// 150 ///
131 /// Note that there is no associated [tearDown] function. Instead, tasks should 151 /// This has the same semantics as [unittest.setUp]. Tasks may be scheduled
132 /// be scheduled for [currentSchedule.onComplete] or 152 /// using [schedule] within [setUpFn], and [currentSchedule] may be accessed as
133 /// [currentSchedule.onException]. These tasks will be run after each test's 153 /// well.
134 /// schedule is completed.
135 void setUp(setUpFn()) { 154 void setUp(setUpFn()) {
136 _setUpScheduledTest(setUpFn); 155 _setUpForGroup = setUpFn;
137 } 156 }
138 157
139 /// Whether [unittest.setUp] has been called in the top level scope. 158 /// Register a [tearDown] function for a test [group].
140 bool _setUpForTopLevel = false; 159 ///
141 160 /// This has the same semantics as [unittest.tearDown]. Tasks may be scheduled
142 /// If we're in the top-level scope (that is, not in any [group]s) and 161 /// using [schedule] within [tearDownFn], and [currentSchedule] may be accessed
143 /// [unittest.setUp] hasn't been called yet, call it. 162 /// as well. Note that [tearDownFn] will be run synchronously after the test
144 void _ensureSetUpForTopLevel() { 163 /// body finishes running, which means it will run before any scheduled tasks
145 if (_inGroup || _setUpForTopLevel) return; 164 /// have begun.
Bob Nystrom 2014/06/27 17:04:53 Would be good to point the reader to docs for the
nweiz 2014/06/30 21:10:20 Done.
146 _setUpScheduledTest(); 165 void tearDown(tearDownFn()) {
166 _tearDownForGroup = tearDownFn;
147 } 167 }
148 168
169 /// Whether [_initializeForGroup] has been called in this group scope.
170 bool _initializedForGroup = false;
171
149 /// Registers callbacks for [unittest.setUp] and [unittest.tearDown] that set up 172 /// Registers callbacks for [unittest.setUp] and [unittest.tearDown] that set up
150 /// and tear down the scheduled test infrastructure. 173 /// and tear down the scheduled test infrastructure and run the user's [setUp]
151 void _setUpScheduledTest([void setUpFn()]) { 174 /// and [tearDown] callbacks.
152 if (!_inGroup) { 175 void _initializeForGroup() {
153 _setUpForTopLevel = true; 176 if (_initializedForGroup) return;
154 var oldWrapAsync = unittest.wrapAsync; 177 _initializedForGroup = true;
155 unittest.setUp(() {
156 if (currentSchedule != null) {
157 throw new StateError('There seems to be another scheduled test '
158 'still running.');
159 }
160 178
161 unittest.wrapAsync = (f, [description]) { 179 var setUpFn = _setUpForGroup;
162 // It's possible that this setup is run before a vanilla unittest test 180 var tearDownFn = _tearDownForGroup;
163 // if [unittest.test] is run in the same context as
164 // [scheduled_test.test]. In that case, [currentSchedule] will never be
165 // set and we should forward to the [unittest.wrapAsync].
166 if (currentSchedule == null) return oldWrapAsync(f, description);
167 return currentSchedule.wrapAsync(f, description);
168 };
169 181
170 if (_setUpFn != null) { 182 if (_inGroup) {
171 var parentFn = _setUpFn; 183 unittest.setUp(() => _addSetUpTearDown(setUpFn, tearDownFn));
172 _setUpFn = () { parentFn(); setUpFn(); }; 184 return;
173 } else { 185 }
174 _setUpFn = setUpFn;
175 }
176 });
177 186
178 unittest.tearDown(() { 187 var oldWrapAsync = unittest.wrapAsync;
179 unittest.wrapAsync = oldWrapAsync; 188 unittest.setUp(() {
180 _currentSchedule = null; 189 if (currentSchedule != null) {
181 _setUpFn = null; 190 throw new StateError('There seems to be another scheduled test '
182 }); 191 'still running.');
183 } else { 192 }
184 unittest.setUp(() { 193
185 if (_setUpFn != null) { 194 unittest.wrapAsync = (f, [description]) {
186 var parentFn = _setUpFn; 195 // It's possible that this setup is run before a vanilla unittest test
187 _setUpFn = () { parentFn(); setUpFn(); }; 196 // if [unittest.test] is run in the same context as
188 } else { 197 // [scheduled_test.test]. In that case, [currentSchedule] will never be
189 _setUpFn = setUpFn; 198 // set and we should forward to the [unittest.wrapAsync].
190 } 199 if (currentSchedule == null) return oldWrapAsync(f, description);
191 }); 200 return currentSchedule.wrapAsync(f, description);
201 };
202
203 _addSetUpTearDown(setUpFn, tearDownFn);
204 });
205
206 unittest.tearDown(() {
207 unittest.wrapAsync = oldWrapAsync;
208 _currentSchedule = null;
209 _setUpFn = null;
210 _tearDownFn = null;
211 });
212 }
213
214 /// Set [_setUpFn] and [_tearDownFn] appropriately.
215 void _addSetUpTearDown(void setUpFn(), void tearDownFn()) {
216 if (setUpFn != null) {
217 if (_setUpFn != null) {
218 var parentFn = _setUpFn;
219 _setUpFn = () { parentFn(); setUpFn(); };
220 } else {
221 _setUpFn = setUpFn;
222 }
223 }
224
225 if (tearDownFn != null) {
226 if (_tearDownFn != null) {
227 var parentFn = _tearDownFn;
228 _tearDownFn = () { parentFn(); tearDownFn(); };
229 } else {
230 _tearDownFn = tearDownFn;
231 }
192 } 232 }
193 } 233 }
194 234
195 /// Like [wrapAsync], this ensures that the current task queue waits for 235 /// Like [wrapAsync], this ensures that the current task queue waits for
196 /// out-of-band asynchronous code, and that errors raised in that code are 236 /// out-of-band asynchronous code, and that errors raised in that code are
197 /// handled correctly. However, [wrapFuture] wraps a [Future] chain rather than 237 /// handled correctly. However, [wrapFuture] wraps a [Future] chain rather than
198 /// a single callback. 238 /// a single callback.
199 /// 239 ///
200 /// The returned [Future] completes to the same value or error as [future]. 240 /// The returned [Future] completes to the same value or error as [future].
201 /// 241 ///
202 /// [description] provides an optional description of the future, which is 242 /// [description] provides an optional description of the future, which is
203 /// used when generating error messages. 243 /// used when generating error messages.
204 Future wrapFuture(Future future, [String description]) { 244 Future wrapFuture(Future future, [String description]) {
205 if (currentSchedule == null) { 245 if (currentSchedule == null) {
206 throw new StateError("Unexpected call to wrapFuture with no current " 246 throw new StateError("Unexpected call to wrapFuture with no current "
207 "schedule."); 247 "schedule.");
208 } 248 }
209 249
210 return currentSchedule.wrapFuture(future, description); 250 return currentSchedule.wrapFuture(future, description);
211 } 251 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698