OLD | NEW |
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 Loading... |
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 Loading... |
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 var chainedFuture = completer.future |
| 509 .chain((_) => new Future<int>.immediate(43)); |
| 510 chainedFuture.handleException((e) { |
| 511 Expect.isNotNull(futureError); |
| 512 chainedFutureError = e; |
| 513 return true; |
| 514 }); |
| 515 completer.future.handleException((e) { |
| 516 Expect.isNull(chainedFutureError); |
| 517 futureError = e; |
| 518 return true; |
| 519 }); |
| 520 completer.completeException(error); |
| 521 |
| 522 Expect.equals(error, futureError); |
| 523 Expect.equals(error, chainedFutureError); |
| 524 } |
| 525 |
| 526 testHandleExceptionAndChain() { |
| 527 final completer = new Completer<String>(); |
| 528 final error = new Exception("Oh no!"); |
| 529 var futureError; |
| 530 var chainedFutureError; |
| 531 |
| 532 completer.future.handleException((e) { |
| 533 Expect.isNull(chainedFutureError); |
| 534 futureError = e; |
| 535 return true; |
| 536 }); |
| 537 var chainedFuture = completer.future |
| 538 .chain((_) => new Future<int>.immediate(43)); |
| 539 chainedFuture.handleException((e) { |
| 540 Expect.isNotNull(futureError); |
| 541 chainedFutureError = e; |
| 542 return true; |
| 543 }); |
| 544 completer.completeException(error); |
| 545 |
| 546 Expect.equals(error, futureError); |
| 547 Expect.equals(error, chainedFutureError); |
| 548 } |
| 549 |
450 main() { | 550 main() { |
451 testImmediate(); | 551 testImmediate(); |
452 testNeverComplete(); | 552 testNeverComplete(); |
453 testComplete(); | 553 testComplete(); |
454 testCompleteWithCompleteHandlerBeforeComplete(); | 554 testCompleteWithCompleteHandlerBeforeComplete(); |
455 testExceptionWithCompleteHandlerBeforeComplete(); | 555 testExceptionWithCompleteHandlerBeforeComplete(); |
456 testCompleteWithCompleteHandlerAfterComplete(); | 556 testCompleteWithCompleteHandlerAfterComplete(); |
457 testExceptionWithCompleteHandlerAfterComplete(); | 557 testExceptionWithCompleteHandlerAfterComplete(); |
458 testCompleteWithManyCompleteHandlers(); | 558 testCompleteWithManyCompleteHandlers(); |
459 testExceptionWithManyCompleteHandlers(); | 559 testExceptionWithManyCompleteHandlers(); |
460 testCompleteWithSuccessHandlerBeforeComplete(); | 560 testCompleteWithSuccessHandlerBeforeComplete(); |
461 testCompleteWithSuccessHandlerAfterComplete(); | 561 testCompleteWithSuccessHandlerAfterComplete(); |
462 testCompleteManySuccessHandlers(); | 562 testCompleteManySuccessHandlers(); |
463 testException(); | 563 testException(); |
464 testExceptionHandler(); | 564 testExceptionHandler(); |
465 testExceptionHandlerReturnsTrue(); | 565 testExceptionHandlerReturnsTrue(); |
466 testExceptionHandlerReturnsTrue2(); | 566 testExceptionHandlerReturnsTrue2(); |
467 testExceptionHandlerReturnsFalse(); | 567 testExceptionHandlerReturnsFalse(); |
468 testExceptionHandlerReturnsFalse2(); | 568 testExceptionHandlerReturnsFalse2(); |
469 testExceptionHandlerAfterCompleteThenNotCalled(); | 569 testExceptionHandlerAfterCompleteThenNotCalled(); |
470 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); | 570 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); |
471 testCompleteWithCompletionAndSuccessHandlers(); | 571 testCompleteWithCompletionAndSuccessHandlers(); |
472 testExceptionWithCompletionAndSuccessHandlers(); | 572 testExceptionWithCompletionAndSuccessHandlers(); |
473 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); | 573 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); |
474 testTransformSuccess(); | 574 testTransformSuccess(); |
475 testTransformFutureFails(); | 575 testTransformFutureFails(); |
476 testTransformTransformerFails(); | 576 testTransformTransformerFails(); |
| 577 testTransformAndHandleException(); |
| 578 testHandleExceptionAndTransform(); |
477 testChainSuccess(); | 579 testChainSuccess(); |
478 testChainFirstFutureFails(); | 580 testChainFirstFutureFails(); |
479 testChainTransformerFails(); | 581 testChainTransformerFails(); |
480 testChainSecondFutureFails(); | 582 testChainSecondFutureFails(); |
| 583 testChainAndHandleException(); |
| 584 testHandleExceptionAndChain(); |
481 } | 585 } |
OLD | NEW |