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

Side by Side Diff: tests/corelib/future_test.dart

Issue 10517006: Adds a callback to Future that is invoked upon completion, whether success or failure. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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) 2012, the Dart project authors. Please see the AUTHORS file 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 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 // Tests for Future.immediate 5 // Tests for Future.immediate
6 6
7 testImmediate() { 7 testImmediate() {
8 final future = new Future<String>.immediate("42"); 8 final future = new Future<String>.immediate("42");
9 Expect.isTrue(future.isComplete); 9 Expect.isTrue(future.isComplete);
10 Expect.isTrue(future.hasValue); 10 Expect.isTrue(future.hasValue);
(...skipping 18 matching lines...) Expand all
29 final future = completer.future; 29 final future = completer.future;
30 30
31 completer.complete(3); 31 completer.complete(3);
32 32
33 Expect.isTrue(future.isComplete); 33 Expect.isTrue(future.isComplete);
34 Expect.isTrue(future.hasValue); 34 Expect.isTrue(future.hasValue);
35 Expect.equals(3, future.value); 35 Expect.equals(3, future.value);
36 Expect.isNull(future.exception); 36 Expect.isNull(future.exception);
37 } 37 }
38 38
39 // Tests for [onComplete]
40
41 testCompleteWithCompleteHandlerBeforeComplete() {
42 final completer = new Completer<int>();
43 final future = completer.future;
44
45 int before;
46 future.onComplete((f) {
47 Expect.equals(future, f);
48 Expect.isTrue(f.isComplete);
49 Expect.isTrue(f.hasValue);
50 before = f.value;
51 });
52 Expect.throws(() => future.value);
53 Expect.isNull(before);
54 completer.complete(3);
55
56 Expect.equals(3, future.value);
57 Expect.equals(3, before);
58 }
59
60 testExceptionWithCompleteHandlerBeforeComplete() {
61 final completer = new Completer<int>();
62 final future = completer.future;
63 final exception = new Exception();
64
65 var err;
66 future.onComplete((f) {
67 Expect.equals(future, f);
68 Expect.isTrue(f.isComplete);
69 Expect.isFalse(f.hasValue);
70 err = f.exception;
71 });
72 Expect.throws(() => future.exception);
73 Expect.isNull(err);
74 completer.completeException(exception);
75 Expect.equals(exception, future.exception);
76 Expect.equals(exception, err);
77 Expect.throws(() => future.value, check: (e) => e == exception);
78 }
79
80 testCompleteWithCompleteHandlerAfterComplete() {
81 final completer = new Completer<int>();
82 final future = completer.future;
83
84 int after;
85 completer.complete(3);
86 future.onComplete((f) {
87 Expect.equals(future, f);
88 Expect.isTrue(f.isComplete);
89 Expect.isTrue(f.hasValue);
90 after = f.value;
91 });
92 Expect.equals(3, future.value);
93 Expect.equals(3, after);
94 }
95
96 testExceptionWithCompleteHandlerAfterComplete() {
97 final completer = new Completer<int>();
98 final future = completer.future;
99 final exception = new Exception();
100
101 var err;
102 completer.completeException(exception);
103 future.onComplete((f) {
104 Expect.equals(future, f);
105 Expect.isTrue(f.isComplete);
106 Expect.isFalse(f.hasValue);
107 err = f.exception;
108 });
109 Expect.equals(exception, future.exception);
110 Expect.equals(exception, err);
111 Expect.throws(() => future.value, check: (e) => e == exception);
112 }
113
114 testCompleteWithManyCompleteHandlers() {
115 final completer = new Completer<int>();
116 final future = completer.future;
117 int before;
118 int after1;
119 int after2;
120
121 future.onComplete((f) { before = f.value; });
122 completer.complete(3);
123 future.onComplete((f) { after1 = f.value; });
124 future.onComplete((f) { after2 = f.value; });
125
126 Expect.equals(3, future.value);
127 Expect.equals(3, before);
128 Expect.equals(3, after1);
129 Expect.equals(3, after2);
130 }
131
132 testExceptionWithManyCompleteHandlers() {
133 final completer = new Completer<int>();
134 final future = completer.future;
135 final exception = new Exception();
136 var before;
137 var after1;
138 var after2;
139
140 future.onComplete((f) { before = f.exception; });
141 completer.completeException(exception);
142 future.onComplete((f) { after1 = f.exception; });
143 future.onComplete((f) { after2 = f.exception; });
144
145 Expect.equals(exception, future.exception);
146 Expect.equals(exception, before);
147 Expect.equals(exception, after1);
148 Expect.equals(exception, after2);
149 Expect.throws(() => future.value, check: (e) => e == exception);
150 }
151
39 // Tests for [then] 152 // Tests for [then]
40 153
41 testCompleteWithHandlerBeforeComplete() { 154 testCompleteWithSuccessHandlerBeforeComplete() {
42 final completer = new Completer<int>(); 155 final completer = new Completer<int>();
43 final future = completer.future; 156 final future = completer.future;
44 157
45 int before; 158 int before;
46 future.then((int v) { before = v; }); 159 future.then((int v) { before = v; });
47 Expect.throws(() { future.value; }); 160 Expect.throws(() { future.value; });
48 Expect.isNull(before); 161 Expect.isNull(before);
49 completer.complete(3); 162 completer.complete(3);
50 163
51 Expect.equals(3, future.value); 164 Expect.equals(3, future.value);
52 Expect.equals(3, before); 165 Expect.equals(3, before);
53 } 166 }
54 167
55 testCompleteWithHandlerAfterComplete() { 168 testCompleteWithSuccessHandlerAfterComplete() {
56 final completer = new Completer<int>(); 169 final completer = new Completer<int>();
57 final future = completer.future; 170 final future = completer.future;
58 171
59 int after; 172 int after;
60 completer.complete(3); 173 completer.complete(3);
61 Expect.equals(3, future.value); 174 Expect.equals(3, future.value);
62 Expect.isNull(after); 175 Expect.isNull(after);
63 176
64 future.then((int v) { after = v; }); 177 future.then((int v) { after = v; });
65 178
66 Expect.equals(3, future.value); 179 Expect.equals(3, future.value);
67 Expect.equals(3, after); 180 Expect.equals(3, after);
68 } 181 }
69 182
70 testCompleteManyHandlers() { 183 testCompleteManySuccessHandlers() {
71 final completer = new Completer<int>(); 184 final completer = new Completer<int>();
72 final future = completer.future; 185 final future = completer.future;
186 int before;
73 int after1; 187 int after1;
74 int after2; 188 int after2;
75 int after3;
76 189
190 future.then((int v) { before = v; });
191 completer.complete(3);
77 future.then((int v) { after1 = v; }); 192 future.then((int v) { after1 = v; });
78 completer.complete(3);
79 future.then((int v) { after2 = v; }); 193 future.then((int v) { after2 = v; });
80 future.then((int v) { after3 = v; });
81 194
82 Expect.equals(3, future.value); 195 Expect.equals(3, future.value);
196 Expect.equals(3, before);
83 Expect.equals(3, after1); 197 Expect.equals(3, after1);
84 Expect.equals(3, after2); 198 Expect.equals(3, after2);
85 Expect.equals(3, after3);
86 } 199 }
87 200
88 // Tests for [handleException] 201 // Tests for [handleException]
89 202
90 testException() { 203 testException() {
91 final completer = new Completer<int>(); 204 final completer = new Completer<int>();
92 final future = completer.future; 205 final future = completer.future;
93 final ex = new Exception(); 206 final ex = new Exception();
94 future.then((_) {}); // exception is thrown if we plan to use the value 207 future.then((_) {}); // exception is thrown if we plan to use the value
95 Expect.throws( 208 Expect.throws(
96 () { completer.completeException(ex); }, 209 () { completer.completeException(ex); },
97 check: (e) => e == ex); 210 check: (e) => e == ex);
98 } 211 }
99 212
100 testExceptionNoListeners() { 213 testExceptionNoSuccessListeners() {
101 final completer = new Completer<int>(); 214 final completer = new Completer<int>();
102 final future = completer.future; 215 final future = completer.future;
103 final ex = new Exception(); 216 final ex = new Exception();
104 completer.completeException(ex); // future.then is not called, so no exception 217 completer.completeException(ex); // future.then is not called, so no exception
105 } 218 }
106 219
107 testExceptionHandler() { 220 testExceptionHandler() {
108 final completer = new Completer<int>(); 221 final completer = new Completer<int>();
109 final future = completer.future; 222 final future = completer.future;
110 final ex = new Exception(); 223 final ex = new Exception();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 final future = completer.future; 296 final future = completer.future;
184 final ex = new Exception(); 297 final ex = new Exception();
185 298
186 var ex2; 299 var ex2;
187 completer.completeException(ex); 300 completer.completeException(ex);
188 future.handleException((e) { ex2 = e; return false; }); 301 future.handleException((e) { ex2 = e; return false; });
189 Expect.throws(() { future.then((e) { }); }); 302 Expect.throws(() { future.then((e) { }); });
190 Expect.equals(ex, ex2); 303 Expect.equals(ex, ex2);
191 } 304 }
192 305
306 // Tests for mixed usage of [onComplete], [then], and [handleException]
307
308 testExceptionWithCompletionAndSuccessHandlers() {
Jennifer Messerly 2012/06/04 20:11:17 it'd be good to have a version of "then" and "onCo
sam.mccall 2012/06/04 21:35:57 You didn't; will do.
309 final completer = new Completer<int>();
310 final future = completer.future;
311 final ex = new Exception();
312
313 var exceptionFromCompleteHandler;
314 future.onComplete((f) {
315 Expect.equals(future, f);
316 Expect.isFalse(f.hasValue);
317 exceptionFromCompleteHandler = f.exception;
318 });
319 future.then((v) => Expect.fail("Should not succeed"));
320 Expect.throws(() => completer.completeException(ex), check: (e) => ex == e);
321 Expect.equals(ex, exceptionFromCompleteHandler);
322 }
323
324 testExceptionWithCompletionAndSuccessAndExceptionHandlers() {
325 final completer = new Completer<int>();
326 final future = completer.future;
327 final ex = new Exception();
328
329 var exceptionFromCompleteHandler;
330 var exceptionFromExceptionHandler;
331 future.onComplete((f) {
332 Expect.equals(future, f);
333 Expect.isFalse(f.hasValue);
334 exceptionFromCompleteHandler = f.exception;
335 });
336 future.handleException((e) {
337 exceptionFromExceptionHandler = e;
338 return true;
339 });
340 future.then((v) => Expect.fail("Should not succeed"));
341 completer.completeException(ex);
342 Expect.equals(ex, exceptionFromCompleteHandler);
343 Expect.equals(ex, exceptionFromExceptionHandler);
344 }
345
193 // Tests for Future.transform 346 // Tests for Future.transform
194 347
195 testTransformSuccess() { 348 testTransformSuccess() {
196 final completer = new Completer<String>(); 349 final completer = new Completer<String>();
197 final transformedFuture = completer.future.transform((x) => "** $x **"); 350 final transformedFuture = completer.future.transform((x) => "** $x **");
198 Expect.isFalse(transformedFuture.isComplete); 351 Expect.isFalse(transformedFuture.isComplete);
199 completer.complete("42"); 352 completer.complete("42");
200 Expect.equals("** 42 **", transformedFuture.value); 353 Expect.equals("** 42 **", transformedFuture.value);
201 } 354 }
202 355
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 completerA.complete("42"); 424 completerA.complete("42");
272 Expect.isFalse(chainedFuture.isComplete); 425 Expect.isFalse(chainedFuture.isComplete);
273 completerB.completeException(error); 426 completerB.completeException(error);
274 Expect.equals(error, chainedFuture.exception); 427 Expect.equals(error, chainedFuture.exception);
275 } 428 }
276 429
277 main() { 430 main() {
278 testImmediate(); 431 testImmediate();
279 testNeverComplete(); 432 testNeverComplete();
280 testComplete(); 433 testComplete();
281 testCompleteWithHandlerBeforeComplete(); 434 testCompleteWithCompleteHandlerBeforeComplete();
282 testCompleteWithHandlerAfterComplete(); 435 testExceptionWithCompleteHandlerBeforeComplete();
283 testCompleteManyHandlers(); 436 testCompleteWithCompleteHandlerAfterComplete();
437 testExceptionWithCompleteHandlerAfterComplete();
438 testCompleteWithManyCompleteHandlers();
439 testExceptionWithManyCompleteHandlers();
440 testCompleteWithSuccessHandlerBeforeComplete();
441 testCompleteWithSuccessHandlerAfterComplete();
442 testCompleteManySuccessHandlers();
284 testException(); 443 testException();
285 testExceptionHandler(); 444 testExceptionHandler();
286 testExceptionHandlerReturnsTrue(); 445 testExceptionHandlerReturnsTrue();
287 testExceptionHandlerReturnsTrue2(); 446 testExceptionHandlerReturnsTrue2();
288 testExceptionHandlerReturnsFalse(); 447 testExceptionHandlerReturnsFalse();
289 testExceptionHandlerReturnsFalse2(); 448 testExceptionHandlerReturnsFalse2();
290 testExceptionHandlerAfterCompleteThenNotCalled(); 449 testExceptionHandlerAfterCompleteThenNotCalled();
291 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); 450 testExceptionHandlerAfterCompleteReturnsFalseThenThrows();
451 testExceptionWithCompletionAndSuccessHandlers();
452 testExceptionWithCompletionAndSuccessAndExceptionHandlers();
292 testTransformSuccess(); 453 testTransformSuccess();
293 testTransformFutureFails(); 454 testTransformFutureFails();
294 testTransformTransformerFails(); 455 testTransformTransformerFails();
295 testChainSuccess(); 456 testChainSuccess();
296 testChainFirstFutureFails(); 457 testChainFirstFutureFails();
297 testChainTransformerFails(); 458 testChainTransformerFails();
298 testChainSecondFutureFails(); 459 testChainSecondFutureFails();
299 } 460 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698