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

Side by Side Diff: tests/lib/async/future2_test.dart

Issue 11788028: Fix future_test.dart and remove future2_test.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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
« no previous file with comments | « no previous file | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // TODO(ajohnsen): This test needs to be updated.
6 // Can Dart2JS on V8 somehow run it?
7
8 // Tests for Future.immediate
9 library future2_test;
10
11 import 'dart:async';
12 import 'dart:isolate';
13
14 testImmediate() {
15 final future = new Future<String>.immediate("42");
16 future.then((x) => Expect.equals("42", x));
17 }
18
19 // Tests for getters (value, exception, isComplete, isValue)
20
21 testNeverComplete() {
22 final completer = new Completer<int>();
23 final future = completer.future;
24 future.then((v) => Except.fails("Value not expected"));
25 future.catchError((e) => Except.fails("Value not expected"));
26 }
27
28 testComplete() {
29 final completer = new Completer<int>();
30 final future = completer.future;
31
32 completer.complete(3);
33
34 future.then((v) => Expect.equals(3, v));
35 }
36
37 // Tests for [then]
38
39 testCompleteWithSuccessHandlerBeforeComplete() {
40 final completer = new Completer<int>();
41 final future = completer.future;
42
43 int before;
44 future.then((int v) { before = v; });
45 Expect.isNull(before);
46 completer.complete(3);
47
48 Expect.equals(3, before);
49 }
50
51 testCompleteWithSuccessHandlerAfterComplete() {
52 final completer = new Completer<int>();
53 final future = completer.future;
54
55 int after;
56 completer.complete(3);
57 Expect.isNull(after);
58
59 future.then((int v) { after = v; });
60
61 Expect.equals(3, after);
62 }
63
64 testCompleteManySuccessHandlers() {
65 final completer = new Completer<int>();
66 final future = completer.future;
67 int before;
68 int after1;
69 int after2;
70
71 future.then((int v) { before = v; });
72 completer.complete(3);
73 future.then((int v) { after1 = v; });
74 future.then((int v) { after2 = v; });
75
76 Expect.equals(3, before);
77 Expect.equals(3, after1);
78 Expect.equals(3, after2);
79 }
80
81 // Tests for [handleException]
82
83 testException() {
84 final completer = new Completer<int>();
85 final future = completer.future;
86 final ex = new Exception();
87 // future.catchError((e) => print("got error"));//Expect.equals(e, ex));
88 future.then((v) {print(v);})
89 .catchError((e) => Expect.equals(e.error, ex));
90 completer.completeError(ex);
91 }
92
93 testExceptionNoSuccessListeners() {
94 final completer = new Completer<int>();
95 final future = completer.future;
96 final ex = new Exception();
97 completer.completeException(ex); // future.then is not called, so no exception
98 }
99
100 testExceptionHandler() {
101 final completer = new Completer<int>();
102 final future = completer.future;
103 final ex = new Exception();
104
105 var ex2;
106 future.catchError((e) { ex2 = e.error; });
107 completer.completeError(ex);
108 Expect.equals(ex, ex2);
109 }
110
111 testExceptionHandlerReturnsTrue() {
112 final completer = new Completer<int>();
113 final future = completer.future;
114 final ex = new Exception();
115
116 bool reached = false;
117 future.catchError((e) { });
118 future.catchError((e) { reached = true; }, test: (e) => false)
119 .catchError((e) {});
120 completer.completeError(ex);
121 Expect.isFalse(reached);
122 }
123
124 testExceptionHandlerReturnsTrue2() {
125 final completer = new Completer<int>();
126 final future = completer.future;
127 final ex = new Exception();
128
129 bool reached = false;
130 future.catchError((e) { }, test: (e) => false)
131 .catchError((e) { reached = true; });
132 completer.completeError(ex);
133 Expect.isTrue(reached);
134 }
135
136 testExceptionHandlerReturnsFalse() {
137 final completer = new Completer<int>();
138 final future = completer.future;
139 final ex = new Exception();
140
141 bool reached = false;
142
143 future.catchError((e) { });
144
145 future.catchError((e) { reached = true; }, test: (e) => false)
146 .catchError((e) { });
147
148 completer.completeError(ex);
149
150 Expect.isFalse(reached);
151 }
152
153 testExceptionHandlerReturnsFalse2() {
154 final completer = new Completer<int>();
155 final future = completer.future;
156 final ex = new Exception();
157
158 bool reached = false;
159 future.handleException((e) { return false; });
160 future.handleException((e) { reached = true; return false; }); // overshadowed
161 completer.completeException(ex); // future.then is not called, so no exception
162 Expect.isTrue(reached);
163 }
164
165 testExceptionHandlerAfterCompleteThenNotCalled() {
166 final completer = new Completer<int>();
167 final future = completer.future;
168 final ex = new Exception();
169
170 var ex2;
171 completer.completeException(ex);
172 future.handleException((e) { ex2 = e; return true; });
173 future.then((e) { });
174 Expect.equals(ex, ex2);
175 }
176
177 testExceptionHandlerAfterCompleteReturnsFalseThenThrows() {
178 final completer = new Completer<int>();
179 final future = completer.future;
180 final ex = new Exception();
181
182 var ex2;
183 completer.completeException(ex);
184 future.handleException((e) { ex2 = e; return false; });
185 Expect.throws(() { future.then((e) { }); });
186 Expect.equals(ex, ex2);
187 }
188
189 // Tests for accessing the exception call stack.
190
191 testCallStackThrowsIfNotComplete() {
192 var exception;
193 try {
194 new Completer().future.stackTrace;
195 } catch (ex) {
196 exception = ex;
197 }
198
199 Expect.isTrue(exception is FutureNotCompleteException);
200 }
201
202 testCallStackIsNullIfCompletedSuccessfully() {
203 Expect.isNull(new Future.immediate('blah').stackTrace);
204 }
205
206 testCallStackReturnsCallstackPassedToCompleteException() {
207 final completer = new Completer();
208 final future = completer.future;
209
210 final stackTrace = 'fake stack trace';
211 completer.completeException(new Exception(), stackTrace);
212 Expect.equals(stackTrace, future.stackTrace);
213 }
214
215 testCallStackIsCapturedIfTransformCallbackThrows() {
216 final completer = new Completer();
217 final transformed = completer.future.then((_) {
218 throw 'whoops!';
219 });
220
221 final stackTrace = 'fake stack trace';
222 completer.complete('blah');
223 Expect.isNotNull(transformed.stackTrace);
224 }
225
226 testCallStackIsCapturedIfChainCallbackThrows() {
227 final completer = new Completer();
228 final chained = completer.future.chain((_) {
229 throw 'whoops!';
230 });
231
232 final stackTrace = 'fake stack trace';
233 completer.complete('blah');
234 Expect.isNotNull(chained.stackTrace);
235 }
236
237 testCallStackIsPreservedIfExceptionIsRethrownInTransformException() {
238 final completer = new Completer();
239 var chained = completer.future.chain((_) {
240 throw 'whoops!';
241 });
242 var transformed = chained.transformException((e) {
243 throw e;
244 });
245
246 completer.complete('blah');
247 Expect.equals(transformed.stackTrace, chained.stackTrace);
248 }
249
250 // Tests for mixed usage of [onComplete], [then], and [handleException]
251
252 testCompleteWithCompletionAndSuccessHandlers() {
253 final completer = new Completer<int>();
254 final future = completer.future;
255
256 var valueFromSuccessHandler;
257 var valueFromCompletionHandler;
258 future.onComplete((f) {
259 Expect.isNotNull(valueFromSuccessHandler);
260 valueFromCompletionHandler = f.value;
261 });
262 future.then((v) {
263 Expect.isNull(valueFromCompletionHandler);
264 valueFromSuccessHandler = v;
265 });
266 completer.complete(42);
267 Expect.equals(42, valueFromSuccessHandler);
268 Expect.equals(42, valueFromCompletionHandler);
269 Expect.equals(42, future.value);
270 }
271
272 testExceptionWithCompletionAndSuccessHandlers() {
273 final completer = new Completer<int>();
274 final future = completer.future;
275 final ex = new Exception();
276
277 var exceptionFromCompleteHandler;
278 future.onComplete((f) {
279 Expect.equals(future, f);
280 Expect.isFalse(f.hasValue);
281 exceptionFromCompleteHandler = f.exception;
282 });
283 future.then((v) => Expect.fail("Should not succeed"));
284 Expect.throws(() => completer.completeException(ex), (e) => e.source == ex);
285 Expect.equals(ex, exceptionFromCompleteHandler);
286 }
287
288 testExceptionWithCompletionAndSuccessAndExceptionHandlers() {
289 final completer = new Completer<int>();
290 final future = completer.future;
291 final ex = new Exception();
292
293 var exceptionFromCompleteHandler;
294 var exceptionFromExceptionHandler;
295 future.onComplete((f) {
296 Expect.equals(future, f);
297 Expect.isFalse(f.hasValue);
298 exceptionFromCompleteHandler = f.exception;
299 });
300 future.handleException((e) {
301 exceptionFromExceptionHandler = e;
302 return true;
303 });
304 future.then((v) => Expect.fail("Should not succeed"));
305 completer.completeException(ex);
306 Expect.equals(ex, exceptionFromCompleteHandler);
307 Expect.equals(ex, exceptionFromExceptionHandler);
308 }
309
310 // Tests for Future.transform
311
312 testTransformSuccess() {
313 final completer = new Completer<String>();
314 final transformedFuture = completer.future.then((x) => "** $x **");
315 Expect.isFalse(transformedFuture.isComplete);
316 completer.complete("42");
317 Expect.equals("** 42 **", transformedFuture.value);
318 }
319
320 testTransformFutureFails() {
321 final completer = new Completer<String>();
322 final error = new Exception("Oh no!");
323 final transformedFuture = completer.future.then((x) {
324 Expect.fail("transformer shouldn't be called");
325 });
326 Expect.isFalse(transformedFuture.isComplete);
327 completer.completeException(error);
328 Expect.equals(error, transformedFuture.exception);
329 }
330
331 testTransformTransformerFails() {
332 final completer = new Completer<String>();
333 final error = new Exception("Oh no!");
334 final transformedFuture = completer.future.then((x) { throw error; });
335 Expect.isFalse(transformedFuture.isComplete);
336 transformedFuture.then((v) => null);
337 Expect.throws(() => completer.complete("42"), (e) => e.source == error);
338 Expect.equals(error, transformedFuture.exception);
339 }
340
341 // Tests for Future.chain
342
343 testChainSuccess() {
344 final completerA = new Completer<String>();
345 final completerB = new Completer<String>();
346 final chainedFuture = completerA.future.chain((x) {
347 Expect.equals("42", x);
348 return completerB.future;
349 });
350 Expect.isFalse(chainedFuture.isComplete);
351 completerA.complete("42");
352 Expect.isFalse(chainedFuture.isComplete);
353 completerB.complete("43");
354 Expect.equals("43", chainedFuture.value);
355 }
356
357 testChainFirstFutureFails() {
358 final completerA = new Completer<String>();
359 final error = new Exception("Oh no!");
360 final chainedFuture = completerA.future.chain((x) {
361 Expect.fail("transformer shouldn't be called");
362 });
363 Expect.isFalse(chainedFuture.isComplete);
364 completerA.completeException(error);
365 Expect.equals(error, chainedFuture.exception);
366 }
367
368 testChainTransformerFails() {
369 final completerA = new Completer<String>();
370 final error = new Exception("Oh no!");
371 final chainedFuture = completerA.future.chain((x) {
372 Expect.equals("42", x);
373 throw error;
374 });
375 chainedFuture.then((v) => null);
376 Expect.isFalse(chainedFuture.isComplete);
377 Expect.throws(() => completerA.complete("42"), (e) => e.source == error);
378 Expect.equals(error, chainedFuture.exception);
379 }
380
381 testChainSecondFutureFails() {
382 final completerA = new Completer<String>();
383 final completerB = new Completer<String>();
384 final error = new Exception("Oh no!");
385 final chainedFuture = completerA.future.chain((x) {
386 Expect.equals("42", x);
387 return completerB.future;
388 });
389 Expect.isFalse(chainedFuture.isComplete);
390 completerA.complete("42");
391 Expect.isFalse(chainedFuture.isComplete);
392 completerB.completeException(error);
393 Expect.equals(error, chainedFuture.exception);
394 }
395
396 // Tests for Future.transformException
397
398 testTransformExceptionCompletesNormally() {
399 final completer = new Completer<String>();
400 var called = false;
401
402 final transformedFuture = completer.future.transformException((ex) {
403 Expect.fail("should not get here");
404 });
405
406 completer.complete("value");
407 Expect.isTrue(transformedFuture.isComplete);
408 Expect.equals("value", transformedFuture.value);
409 }
410
411 testTransformExceptionThrows() {
412 final completer = new Completer<String>();
413 var called = false;
414
415 final transformedFuture = completer.future.transformException((ex) {
416 Expect.equals("original error", ex);
417 called = true;
418 throw "transformed error";
419 });
420
421 completer.completeException("original error");
422 Expect.isTrue(called);
423 Expect.isTrue(transformedFuture.isComplete);
424 Expect.equals("transformed error", transformedFuture.exception);
425 }
426
427 testTransformExceptionReturns() {
428 final completer = new Completer<String>();
429 var called = false;
430
431 final transformedFuture = completer.future.transformException((ex) {
432 Expect.equals("original error", ex);
433 called = true;
434 return "transformed value";
435 });
436
437 completer.completeException("original error");
438 Expect.isTrue(called);
439 Expect.isTrue(transformedFuture.isComplete);
440 Expect.equals("transformed value", transformedFuture.value);
441 }
442
443 testTransformExceptionReturnsAFuture() {
444 final completer = new Completer<String>();
445 var called = false;
446
447 final returnedCompleter = new Completer<String>();
448
449 final transformedFuture = completer.future.transformException((ex) {
450 Expect.equals("original error", ex);
451 called = true;
452 return returnedCompleter.future;
453 });
454
455 completer.completeException("original error");
456 Expect.isTrue(called);
457 Expect.isFalse(transformedFuture.isComplete);
458
459 returnedCompleter.complete("transformed value");
460 Expect.isTrue(transformedFuture.isComplete);
461 Expect.equals("transformed value", transformedFuture.value);
462 }
463
464 // Tests for branching exceptions
465
466 testExceptionTravelsAlongBothBranches() {
467 var results = <int>[];
468
469 var completer = new Completer();
470 var branch1 = completer.future.then((_) => null);
471 var branch2 = completer.future.then((_) => null);
472
473 branch1.handleException((e) {
474 results.add(1);
475 return true;
476 });
477
478 branch2.handleException((e) {
479 results.add(2);
480 return true;
481 });
482
483 completer.completeException("error");
484 Expect.setEquals([1, 2], results);
485 }
486
487 testExceptionTravelsAlongBothBranchesAfterComplete() {
488 var results = <int>[];
489
490 var completer = new Completer();
491 completer.completeException("error");
492
493 var branch1 = completer.future.then((_) => null);
494 var branch2 = completer.future.then((_) => null);
495
496 branch1.handleException((e) {
497 results.add(1);
498 return true;
499 });
500
501 branch2.handleException((e) {
502 results.add(2);
503 return true;
504 });
505
506 Expect.setEquals([1, 2], results);
507 }
508
509 testExceptionIsHandledInBaseAndBranch() {
510 var results = <String>[];
511
512 var completer = new Completer();
513 var branch = completer.future.then((_) => null);
514
515 completer.future.handleException((e) {
516 results.add("base");
517 return true;
518 });
519
520 branch.handleException((e) {
521 results.add("branch");
522 return true;
523 });
524
525 completer.completeException("error");
526 Expect.setEquals(["base", "branch"], results);
527 }
528
529 testExceptionIsHandledInBaseAndBranchAfterComplete() {
530 var results = <String>[];
531
532 var completer = new Completer();
533 completer.completeException("error");
534
535 var branch = completer.future.then((_) => null);
536
537 completer.future.handleException((e) {
538 results.add("base");
539 return true;
540 });
541
542 branch.handleException((e) {
543 results.add("branch");
544 return true;
545 });
546
547 Expect.setEquals(["base", "branch"], results);
548 }
549
550 main() {
551 // /*
552 testImmediate();
553 testNeverComplete();
554 testComplete();
555 testCompleteWithSuccessHandlerBeforeComplete();
556 testCompleteWithSuccessHandlerAfterComplete();
557 testCompleteManySuccessHandlers();
558 testException();
559 testExceptionHandler();
560 testExceptionHandlerReturnsTrue();
561 testExceptionHandlerReturnsTrue2();
562 testExceptionHandlerReturnsFalse();
563 // */
564 /*
565 */
566 /*
567 testExceptionHandlerReturnsFalse2();
568 testExceptionHandlerAfterCompleteThenNotCalled();
569 testExceptionHandlerAfterCompleteReturnsFalseThenThrows();
570 testCallStackThrowsIfNotComplete();
571 testCallStackIsNullIfCompletedSuccessfully();
572 testCallStackReturnsCallstackPassedToCompleteException();
573 testCallStackIsCapturedIfTransformCallbackThrows();
574 testCallStackIsCapturedIfChainCallbackThrows();
575 testCallStackIsPreservedIfExceptionIsRethrownInTransformException();
576 testCompleteWithCompletionAndSuccessHandlers();
577 testExceptionWithCompletionAndSuccessHandlers();
578 testExceptionWithCompletionAndSuccessAndExceptionHandlers();
579 testTransformSuccess();
580 testTransformFutureFails();
581 testTransformTransformerFails();
582 testChainSuccess();
583 testChainFirstFutureFails();
584 testChainTransformerFails();
585 testChainSecondFutureFails();
586 testTransformExceptionCompletesNormally();
587 testTransformExceptionThrows();
588 testTransformExceptionReturns();
589 testTransformExceptionReturnsAFuture();
590 testExceptionTravelsAlongBothBranches();
591 testExceptionTravelsAlongBothBranchesAfterComplete();
592 testExceptionIsHandledInBaseAndBranch();
593 testExceptionIsHandledInBaseAndBranchAfterComplete();
594 */
595 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698