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

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

Issue 10544063: More tests and doc for futures (followup to http://codereview.chromium.org/10517006/) 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
« no previous file with comments | « corelib/src/future.dart ('k') | no next file » | 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) 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 386
387 testTransformTransformerFails() { 387 testTransformTransformerFails() {
388 final completer = new Completer<String>(); 388 final completer = new Completer<String>();
389 final error = new Exception("Oh no!"); 389 final error = new Exception("Oh no!");
390 final transformedFuture = completer.future.transform((x) { throw error; }); 390 final transformedFuture = completer.future.transform((x) { throw error; });
391 Expect.isFalse(transformedFuture.isComplete); 391 Expect.isFalse(transformedFuture.isComplete);
392 completer.complete("42"); 392 completer.complete("42");
393 Expect.equals(error, transformedFuture.exception); 393 Expect.equals(error, transformedFuture.exception);
394 } 394 }
395 395
396 // Tests [handleException] and [transform] on the same Future.
397 // An earlier implementation of [transform] didn't support this, and behavior
398 // depended on whether [handleException] or [transform] was called first.
399
400 testTransformAndHandleException() {
401 final completer = new Completer<String>();
402 final error = new Exception("Oh no!");
403 var futureError;
404 var transformedFutureError;
405
406 completer.future.transform((x) => "** $x **").handleException((e) {
407 Expect.isNotNull(futureError);
408 transformedFutureError = e;
409 return true;
410 });
411 completer.future.handleException((e) {
412 Expect.isNull(transformedFutureError);
413 futureError = e;
414 return true;
415 });
416 completer.completeException(error);
417
418 Expect.equals(error, futureError);
419 Expect.equals(error, transformedFutureError);
420 }
421
422 testHandleExceptionAndTransform() {
423 final completer = new Completer<String>();
424 final error = new Exception("Oh no!");
425 var futureError;
426 var transformedFutureError;
427
428 completer.future.handleException((e) {
429 Expect.isNull(transformedFutureError);
430 futureError = e;
431 return true;
432 });
433 completer.future.transform((x) => "** $x **").handleException((e) {
434 Expect.isNotNull(futureError);
435 transformedFutureError = e;
436 return true;
437 });
438 completer.completeException(error);
439
440 Expect.equals(error, futureError);
441 Expect.equals(error, transformedFutureError);
442 }
443
396 // Tests for Future.chain 444 // Tests for Future.chain
397 445
398 testChainSuccess() { 446 testChainSuccess() {
399 final completerA = new Completer<String>(); 447 final completerA = new Completer<String>();
400 final completerB = new Completer<String>(); 448 final completerB = new Completer<String>();
401 final chainedFuture = completerA.future.chain((x) { 449 final chainedFuture = completerA.future.chain((x) {
402 Expect.equals("42", x); 450 Expect.equals("42", x);
403 return completerB.future; 451 return completerB.future;
404 }); 452 });
405 Expect.isFalse(chainedFuture.isComplete); 453 Expect.isFalse(chainedFuture.isComplete);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 Expect.equals("42", x); 488 Expect.equals("42", x);
441 return completerB.future; 489 return completerB.future;
442 }); 490 });
443 Expect.isFalse(chainedFuture.isComplete); 491 Expect.isFalse(chainedFuture.isComplete);
444 completerA.complete("42"); 492 completerA.complete("42");
445 Expect.isFalse(chainedFuture.isComplete); 493 Expect.isFalse(chainedFuture.isComplete);
446 completerB.completeException(error); 494 completerB.completeException(error);
447 Expect.equals(error, chainedFuture.exception); 495 Expect.equals(error, chainedFuture.exception);
448 } 496 }
449 497
498 // Tests [handleException] and [chain] on the same Future.
499 // An earlier implementation of [chain] didn't support this, and behavior
500 // depended on whether [handleException] or [chain] was called first.
501
502 testChainAndHandleException() {
503 final completer = new Completer<String>();
504 final error = new Exception("Oh no!");
505 var futureError;
506 var chainedFutureError;
507
508 completer.future.chain((x) => new Future<int>.immediate(43)).handleException(( e) {
Siggi Cherem (dart-lang) 2012/06/07 23:29:59 80 col (here and below)
sammccall 2012/06/07 23:41:04 Done.
509 Expect.isNotNull(futureError);
510 chainedFutureError = e;
511 return true;
512 });
513 completer.future.handleException((e) {
514 Expect.isNull(chainedFutureError);
515 futureError = e;
516 return true;
517 });
518 completer.completeException(error);
519
520 Expect.equals(error, futureError);
521 Expect.equals(error, chainedFutureError);
522 }
523
524 testHandleExceptionAndChain() {
525 final completer = new Completer<String>();
526 final error = new Exception("Oh no!");
527 var futureError;
528 var chainedFutureError;
529
530 completer.future.handleException((e) {
531 Expect.isNull(chainedFutureError);
532 futureError = e;
533 return true;
534 });
535 completer.future.chain((x) => new Future<int>.immediate(43)).handleException(( e) {
536 Expect.isNotNull(futureError);
537 chainedFutureError = e;
538 return true;
539 });
540 completer.completeException(error);
541
542 Expect.equals(error, futureError);
543 Expect.equals(error, chainedFutureError);
544 }
545
450 main() { 546 main() {
451 testImmediate(); 547 testImmediate();
452 testNeverComplete(); 548 testNeverComplete();
453 testComplete(); 549 testComplete();
454 testCompleteWithCompleteHandlerBeforeComplete(); 550 testCompleteWithCompleteHandlerBeforeComplete();
455 testExceptionWithCompleteHandlerBeforeComplete(); 551 testExceptionWithCompleteHandlerBeforeComplete();
456 testCompleteWithCompleteHandlerAfterComplete(); 552 testCompleteWithCompleteHandlerAfterComplete();
457 testExceptionWithCompleteHandlerAfterComplete(); 553 testExceptionWithCompleteHandlerAfterComplete();
458 testCompleteWithManyCompleteHandlers(); 554 testCompleteWithManyCompleteHandlers();
459 testExceptionWithManyCompleteHandlers(); 555 testExceptionWithManyCompleteHandlers();
460 testCompleteWithSuccessHandlerBeforeComplete(); 556 testCompleteWithSuccessHandlerBeforeComplete();
461 testCompleteWithSuccessHandlerAfterComplete(); 557 testCompleteWithSuccessHandlerAfterComplete();
462 testCompleteManySuccessHandlers(); 558 testCompleteManySuccessHandlers();
463 testException(); 559 testException();
464 testExceptionHandler(); 560 testExceptionHandler();
465 testExceptionHandlerReturnsTrue(); 561 testExceptionHandlerReturnsTrue();
466 testExceptionHandlerReturnsTrue2(); 562 testExceptionHandlerReturnsTrue2();
467 testExceptionHandlerReturnsFalse(); 563 testExceptionHandlerReturnsFalse();
468 testExceptionHandlerReturnsFalse2(); 564 testExceptionHandlerReturnsFalse2();
469 testExceptionHandlerAfterCompleteThenNotCalled(); 565 testExceptionHandlerAfterCompleteThenNotCalled();
470 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); 566 testExceptionHandlerAfterCompleteReturnsFalseThenThrows();
471 testCompleteWithCompletionAndSuccessHandlers(); 567 testCompleteWithCompletionAndSuccessHandlers();
472 testExceptionWithCompletionAndSuccessHandlers(); 568 testExceptionWithCompletionAndSuccessHandlers();
473 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); 569 testExceptionWithCompletionAndSuccessAndExceptionHandlers();
474 testTransformSuccess(); 570 testTransformSuccess();
475 testTransformFutureFails(); 571 testTransformFutureFails();
476 testTransformTransformerFails(); 572 testTransformTransformerFails();
573 testTransformAndHandleException();
574 testHandleExceptionAndTransform();
477 testChainSuccess(); 575 testChainSuccess();
478 testChainFirstFutureFails(); 576 testChainFirstFutureFails();
479 testChainTransformerFails(); 577 testChainTransformerFails();
480 testChainSecondFutureFails(); 578 testChainSecondFutureFails();
579 testChainAndHandleException();
580 testHandleExceptionAndChain();
481 } 581 }
OLDNEW
« no previous file with comments | « corelib/src/future.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698