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

Side by Side Diff: tests/corelib/src/FutureTest.dart

Issue 10244009: test rename overhaul: step 7 - corelib tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
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.
4
5 // Tests for Future.immediate
6
7 testImmediate() {
8 final future = new Future<String>.immediate("42");
9 Expect.isTrue(future.isComplete);
10 Expect.isTrue(future.hasValue);
11 var value = null;
12 future.then((x) => value = x);
13 Expect.equals("42", value);
14 }
15
16 // Tests for getters (value, exception, isComplete, isValue)
17
18 testNeverComplete() {
19 final completer = new Completer<int>();
20 final future = completer.future;
21 Expect.isFalse(future.isComplete);
22 Expect.isFalse(future.hasValue);
23 Expect.throws(() { future.value; });
24 Expect.throws(() { future.exception; });
25 }
26
27 testComplete() {
28 final completer = new Completer<int>();
29 final future = completer.future;
30
31 completer.complete(3);
32
33 Expect.isTrue(future.isComplete);
34 Expect.isTrue(future.hasValue);
35 Expect.equals(3, future.value);
36 Expect.isNull(future.exception);
37 }
38
39 // Tests for [then]
40
41 testCompleteWithHandlerBeforeComplete() {
42 final completer = new Completer<int>();
43 final future = completer.future;
44
45 int before;
46 future.then((int v) { before = v; });
47 Expect.throws(() { future.value; });
48 Expect.isNull(before);
49 completer.complete(3);
50
51 Expect.equals(3, future.value);
52 Expect.equals(3, before);
53 }
54
55 testCompleteWithHandlerAfterComplete() {
56 final completer = new Completer<int>();
57 final future = completer.future;
58
59 int after;
60 completer.complete(3);
61 Expect.equals(3, future.value);
62 Expect.isNull(after);
63
64 future.then((int v) { after = v; });
65
66 Expect.equals(3, future.value);
67 Expect.equals(3, after);
68 }
69
70 testCompleteManyHandlers() {
71 final completer = new Completer<int>();
72 final future = completer.future;
73 int after1;
74 int after2;
75 int after3;
76
77 future.then((int v) { after1 = v; });
78 completer.complete(3);
79 future.then((int v) { after2 = v; });
80 future.then((int v) { after3 = v; });
81
82 Expect.equals(3, future.value);
83 Expect.equals(3, after1);
84 Expect.equals(3, after2);
85 Expect.equals(3, after3);
86 }
87
88 // Tests for [handleException]
89
90 testException() {
91 final completer = new Completer<int>();
92 final future = completer.future;
93 final ex = new Exception();
94 future.then((_) {}); // exception is thrown if we plan to use the value
95 Expect.throws(
96 () { completer.completeException(ex); },
97 check: (e) => e == ex);
98 }
99
100 testExceptionNoListeners() {
101 final completer = new Completer<int>();
102 final future = completer.future;
103 final ex = new Exception();
104 completer.completeException(ex); // future.then is not called, so no exception
105 }
106
107 testExceptionHandler() {
108 final completer = new Completer<int>();
109 final future = completer.future;
110 final ex = new Exception();
111
112 var ex2;
113 future.handleException((e) { ex2 = e; return true; });
114 completer.completeException(ex);
115 Expect.equals(ex, ex2);
116 }
117
118 testExceptionHandlerReturnsTrue() {
119 final completer = new Completer<int>();
120 final future = completer.future;
121 final ex = new Exception();
122
123 bool reached = false;
124 future.handleException((e) { return true; });
125 future.handleException((e) { reached = true; return false; }); // overshadowed
126 completer.completeException(ex);
127 Expect.isFalse(reached);
128 }
129
130 testExceptionHandlerReturnsTrue2() {
131 final completer = new Completer<int>();
132 final future = completer.future;
133 final ex = new Exception();
134
135 bool reached = false;
136 future.handleException((e) { return false; });
137 future.handleException((e) { reached = true; return true; });
138 completer.completeException(ex);
139 Expect.isTrue(reached);
140 }
141
142 testExceptionHandlerReturnsFalse() {
143 final completer = new Completer<int>();
144 final future = completer.future;
145 final ex = new Exception();
146
147 bool reached = false;
148 future.then((_) {}); // ensure exception is thrown...
149 future.handleException((e) { return false; });
150 future.handleException((e) { reached = true; return false; }); // overshadowed
151 Expect.throws(
152 () { completer.completeException(ex); },
153 check: (e) => e == ex);
154 Expect.isTrue(reached);
155 }
156
157 testExceptionHandlerReturnsFalse2() {
158 final completer = new Completer<int>();
159 final future = completer.future;
160 final ex = new Exception();
161
162 bool reached = false;
163 future.handleException((e) { return false; });
164 future.handleException((e) { reached = true; return false; }); // overshadowed
165 completer.completeException(ex); // future.then is not called, so no exception
166 Expect.isTrue(reached);
167 }
168
169 testExceptionHandlerAfterCompleteThenNotCalled() {
170 final completer = new Completer<int>();
171 final future = completer.future;
172 final ex = new Exception();
173
174 var ex2;
175 completer.completeException(ex);
176 future.handleException((e) { ex2 = e; return true; });
177 future.then((e) { });
178 Expect.equals(ex, ex2);
179 }
180
181 testExceptionHandlerAfterCompleteReturnsFalseThenThrows() {
182 final completer = new Completer<int>();
183 final future = completer.future;
184 final ex = new Exception();
185
186 var ex2;
187 completer.completeException(ex);
188 future.handleException((e) { ex2 = e; return false; });
189 Expect.throws(() { future.then((e) { }); });
190 Expect.equals(ex, ex2);
191 }
192
193 // Tests for Future.transform
194
195 testTransformSuccess() {
196 final completer = new Completer<String>();
197 final transformedFuture = completer.future.transform((x) => "** $x **");
198 Expect.isFalse(transformedFuture.isComplete);
199 completer.complete("42");
200 Expect.equals("** 42 **", transformedFuture.value);
201 }
202
203 testTransformFutureFails() {
204 final completer = new Completer<String>();
205 final error = new Exception("Oh no!");
206 final transformedFuture = completer.future.transform((x) {
207 Expect.fail("transformer shouldn't be called");
208 });
209 Expect.isFalse(transformedFuture.isComplete);
210 completer.completeException(error);
211 Expect.equals(error, transformedFuture.exception);
212 }
213
214 testTransformTransformerFails() {
215 final completer = new Completer<String>();
216 final error = new Exception("Oh no!");
217 final transformedFuture = completer.future.transform((x) { throw error; });
218 Expect.isFalse(transformedFuture.isComplete);
219 completer.complete("42");
220 Expect.equals(error, transformedFuture.exception);
221 }
222
223 // Tests for Future.chain
224
225 testChainSuccess() {
226 final completerA = new Completer<String>();
227 final completerB = new Completer<String>();
228 final chainedFuture = completerA.future.chain((x) {
229 Expect.equals("42", x);
230 return completerB.future;
231 });
232 Expect.isFalse(chainedFuture.isComplete);
233 completerA.complete("42");
234 Expect.isFalse(chainedFuture.isComplete);
235 completerB.complete("43");
236 Expect.equals("43", chainedFuture.value);
237 }
238
239 testChainFirstFutureFails() {
240 final completerA = new Completer<String>();
241 final error = new Exception("Oh no!");
242 final chainedFuture = completerA.future.chain((x) {
243 Expect.fail("transformer shouldn't be called");
244 });
245 Expect.isFalse(chainedFuture.isComplete);
246 completerA.completeException(error);
247 Expect.equals(error, chainedFuture.exception);
248 }
249
250 testChainTransformerFails() {
251 final completerA = new Completer<String>();
252 final error = new Exception("Oh no!");
253 final chainedFuture = completerA.future.chain((x) {
254 Expect.equals("42", x);
255 throw error;
256 });
257 Expect.isFalse(chainedFuture.isComplete);
258 completerA.complete("42");
259 Expect.equals(error, chainedFuture.exception);
260 }
261
262 testChainSecondFutureFails() {
263 final completerA = new Completer<String>();
264 final completerB = new Completer<String>();
265 final error = new Exception("Oh no!");
266 final chainedFuture = completerA.future.chain((x) {
267 Expect.equals("42", x);
268 return completerB.future;
269 });
270 Expect.isFalse(chainedFuture.isComplete);
271 completerA.complete("42");
272 Expect.isFalse(chainedFuture.isComplete);
273 completerB.completeException(error);
274 Expect.equals(error, chainedFuture.exception);
275 }
276
277 main() {
278 testImmediate();
279 testNeverComplete();
280 testComplete();
281 testCompleteWithHandlerBeforeComplete();
282 testCompleteWithHandlerAfterComplete();
283 testCompleteManyHandlers();
284 testException();
285 testExceptionHandler();
286 testExceptionHandlerReturnsTrue();
287 testExceptionHandlerReturnsTrue2();
288 testExceptionHandlerReturnsFalse();
289 testExceptionHandlerReturnsFalse2();
290 testExceptionHandlerAfterCompleteThenNotCalled();
291 testExceptionHandlerAfterCompleteReturnsFalseThenThrows();
292 testTransformSuccess();
293 testTransformFutureFails();
294 testTransformTransformerFails();
295 testChainSuccess();
296 testChainFirstFutureFails();
297 testChainTransformerFails();
298 testChainSecondFutureFails();
299 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698