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

Side by Side Diff: test/utils.dart

Issue 934413002: Replace the existing unittest APIs with the new runner infrastructure. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 10 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 | « test/throws_matchers_test.dart ('k') | test/vm_listener_test.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 unittest.test.utils; 5 library unittest.test.utils;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'package:unittest/src/invoker.dart';
10 import 'package:unittest/src/live_test.dart'; 11 import 'package:unittest/src/live_test.dart';
11 import 'package:unittest/src/load_exception.dart'; 12 import 'package:unittest/src/load_exception.dart';
12 import 'package:unittest/src/remote_exception.dart'; 13 import 'package:unittest/src/remote_exception.dart';
13 import 'package:unittest/src/state.dart'; 14 import 'package:unittest/src/state.dart';
15 import 'package:unittest/src/suite.dart';
14 import 'package:unittest/unittest.dart'; 16 import 'package:unittest/unittest.dart';
15 17
16 // The last state change detected via [expectStates]. 18 // The last state change detected via [expectStates].
17 State lastState; 19 State lastState;
18 20
19 /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange]. 21 /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange].
20 /// 22 ///
21 /// The most recent emitted state is stored in [_lastState]. 23 /// The most recent emitted state is stored in [_lastState].
22 void expectStates(LiveTest liveTest, Iterable<State> statesIter) { 24 void expectStates(LiveTest liveTest, Iterable<State> statesIter) {
23 var states = new Queue.from(statesIter); 25 var states = new Queue.from(statesIter);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 const State(Status.complete, Result.error) 58 const State(Status.complete, Result.error)
57 ]); 59 ]);
58 60
59 expectErrors(liveTest, [(error) { 61 expectErrors(liveTest, [(error) {
60 expect(lastState.status, equals(Status.complete)); 62 expect(lastState.status, equals(Status.complete));
61 expect(error, equals("oh no")); 63 expect(error, equals("oh no"));
62 }]); 64 }]);
63 } 65 }
64 66
65 /// Returns a matcher that matches a [TestFailure] with the given [message]. 67 /// Returns a matcher that matches a [TestFailure] with the given [message].
66 Matcher isTestFailure(String message) => predicate( 68 ///
67 (error) => error is TestFailure && error.message == message, 69 /// [message] can be a string or a [Matcher].
68 'is a TestFailure with message "$message"'); 70 Matcher isTestFailure(message) => new _IsTestFailure(wrapMatcher(message));
71
72 class _IsTestFailure extends Matcher {
73 final Matcher _message;
74
75 _IsTestFailure(this._message);
76
77 bool matches(item, Map matchState) =>
78 item is TestFailure && _message.matches(item.message, matchState);
79
80 Description describe(Description description) =>
81 description.add('a TestFailure with message ').addDescriptionOf(_message);
82
83 Description describeMismatch(item, Description mismatchDescription,
84 Map matchState, bool verbose) {
85 if (item is! TestFailure) {
86 return mismatchDescription.addDescriptionOf(item)
87 .add('is not a TestFailure');
88 } else {
89 return mismatchDescription
90 .add('message ')
91 .addDescriptionOf(item.message)
92 .add(' is not ')
93 .addDescriptionOf(_message);
94 }
95 }
96 }
69 97
70 /// Returns a matcher that matches a [RemoteException] with the given [message]. 98 /// Returns a matcher that matches a [RemoteException] with the given [message].
71 Matcher isRemoteException(String message) => predicate( 99 ///
72 (error) => error is RemoteException && error.message == message, 100 /// [message] can be a string or a [Matcher].
73 'is a RemoteException with message "$message"'); 101 Matcher isRemoteException(message) =>
102 new _IsRemoteException(wrapMatcher(message));
74 103
75 /// Returns a matcher that matches a [LoadException] with the given [message]. 104 class _IsRemoteException extends Matcher {
76 Matcher isLoadException(String message) => predicate( 105 final Matcher _message;
77 (error) => error is LoadException && error.innerError == message, 106
78 'is a LoadException with message "$message"'); 107 _IsRemoteException(this._message);
108
109 bool matches(item, Map matchState) =>
110 item is RemoteException && _message.matches(item.message, matchState);
111
112 Description describe(Description description) =>
113 description.add('a RemoteException with message ')
114 .addDescriptionOf(_message);
115
116 Description describeMismatch(item, Description mismatchDescription,
117 Map matchState, bool verbose) {
118 if (item is! RemoteException) {
119 return mismatchDescription.addDescriptionOf(item)
120 .add('is not a RemoteException');
121 } else {
122 return mismatchDescription
123 .add('message ')
124 .addDescriptionOf(item)
125 .add(' is not ')
126 .addDescriptionOf(_message);
127 }
128 }
129 }
130
131 /// Returns a matcher that matches a [LoadException] with the given
132 /// [innerError].
133 ///
134 /// [innerError] can be a string or a [Matcher].
135 Matcher isLoadException(innerError) =>
136 new _IsLoadException(wrapMatcher(innerError));
137
138 class _IsLoadException extends Matcher {
139 final Matcher _innerError;
140
141 _IsLoadException(this._innerError);
142
143 bool matches(item, Map matchState) =>
144 item is LoadException && _innerError.matches(item.innerError, matchState);
145
146 Description describe(Description description) =>
147 description.add('a LoadException with message ')
148 .addDescriptionOf(_innerError);
149
150 Description describeMismatch(item, Description mismatchDescription,
151 Map matchState, bool verbose) {
152 if (item is! LoadException) {
153 return mismatchDescription.addDescriptionOf(item)
154 .add('is not a LoadException');
155 } else {
156 return mismatchDescription
157 .add('inner error ')
158 .addDescriptionOf(item)
159 .add(' is not ')
160 .addDescriptionOf(_innerError);
161 }
162 }
163 }
79 164
80 /// Returns a [Future] that completes after pumping the event queue [times] 165 /// Returns a [Future] that completes after pumping the event queue [times]
81 /// times. 166 /// times.
82 /// 167 ///
83 /// By default, this should pump the event queue enough times to allow any code 168 /// By default, this should pump the event queue enough times to allow any code
84 /// to run, as long as it's not waiting on some external event. 169 /// to run, as long as it's not waiting on some external event.
85 Future pumpEventQueue([int times=20]) { 170 Future pumpEventQueue([int times=20]) {
86 if (times == 0) return new Future.value(); 171 if (times == 0) return new Future.value();
87 // Use [new Future] future to allow microtask events to finish. The [new 172 // Use [new Future] future to allow microtask events to finish. The [new
88 // Future.value] constructor uses scheduleMicrotask itself and would therefore 173 // Future.value] constructor uses scheduleMicrotask itself and would therefore
89 // not wait for microtask callbacks that are scheduled after invoking this 174 // not wait for microtask callbacks that are scheduled after invoking this
90 // method. 175 // method.
91 return new Future(() => pumpEventQueue(times - 1)); 176 return new Future(() => pumpEventQueue(times - 1));
92 } 177 }
178
179 /// Returns a local [LiveTest] that runs [body].
180 LiveTest createTest(body()) {
181 var test = new LocalTest("test", body);
182 var suite = new Suite("suite", [test]);
183 return test.load(suite);
184 }
185
186 /// Runs [body] as a test.
187 ///
188 /// Once it completes, returns the [LiveTest] used to run it.
189 Future<LiveTest> runTest(body()) {
190 var liveTest = createTest(body);
191 return liveTest.run().then((_) => liveTest);
192 }
193
194 /// Asserts that [liveTest] has completed and passed.
195 ///
196 /// If the test had any errors, they're surfaced nicely into the outer test.
197 void expectTestPassed(LiveTest liveTest) {
198 // Since the test is expected to pass, we forward any current or future errors
199 // to the outer test, because they're definitely unexpected.
200 for (var error in liveTest.errors) {
201 registerException(error.error, error.stackTrace);
202 }
203 liveTest.onError.listen((error) {
204 registerException(error.error, error.stackTrace);
205 });
206
207 expect(liveTest.state.status, equals(Status.complete));
208 expect(liveTest.state.result, equals(Result.success));
209 }
210
211 /// Asserts that [liveTest] failed with a single [TestFailure] whose message
212 /// matches [message].
213 void expectTestFailed(LiveTest liveTest, message) {
214 expect(liveTest.state.status, equals(Status.complete));
215 expect(liveTest.state.result, equals(Result.failure));
216 expect(liveTest.errors, hasLength(1));
217 expect(liveTest.errors.first.error, isTestFailure(message));
218 }
219
220 /// Assert that the [test] callback causes a test to block until [stopBlocking]
221 /// is called at some later time.
222 ///
223 /// [stopBlocking] is passed the return value of [test].
224 Future expectTestBlocks(test(), stopBlocking(value)) {
225 var liveTest;
226 var future;
227 liveTest = createTest(() {
228 var value = test();
229 future = pumpEventQueue().then((_) {
230 expect(liveTest.state.status, equals(Status.running));
231 stopBlocking(value);
232 });
233 });
234
235 return liveTest.run().then((_) {
236 expectTestPassed(liveTest);
237 // Ensure that the outer test doesn't complete until the inner future
238 // completes.
239 return future;
240 });
241 }
OLDNEW
« no previous file with comments | « test/throws_matchers_test.dart ('k') | test/vm_listener_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698