| OLD | NEW |
| (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 testImmediate() { |
| 6 final future = new Future<String>.immediate("42"); |
| 7 Expect.isTrue(future.isComplete); |
| 8 var value = null; |
| 9 future.then((x) => value = x); |
| 10 Expect.equals("42", value); |
| 11 } |
| 12 |
| 13 testTransformSuccess() { |
| 14 final completer = new Completer<String>(); |
| 15 final transformedFuture = completer.future.transform((x) => "** $x **"); |
| 16 Expect.isFalse(transformedFuture.isComplete); |
| 17 completer.complete("42"); |
| 18 Expect.equals("** 42 **", transformedFuture.value); |
| 19 } |
| 20 |
| 21 testTransformFutureFails() { |
| 22 final completer = new Completer<String>(); |
| 23 final error = new Exception("Oh no!"); |
| 24 final transformedFuture = completer.future.transform((x) { |
| 25 Expect.fail("transformer shouldn't be called"); |
| 26 }); |
| 27 Expect.isFalse(transformedFuture.isComplete); |
| 28 completer.completeException(error); |
| 29 Expect.equals(error, transformedFuture.exception); |
| 30 } |
| 31 |
| 32 testTransformTransformerFails() { |
| 33 final completer = new Completer<String>(); |
| 34 final error = new Exception("Oh no!"); |
| 35 final transformedFuture = completer.future.transform((x) { throw error; }); |
| 36 Expect.isFalse(transformedFuture.isComplete); |
| 37 completer.complete("42"); |
| 38 Expect.equals(error, transformedFuture.exception); |
| 39 } |
| 40 |
| 41 testChainSuccess() { |
| 42 final completerA = new Completer<String>(); |
| 43 final completerB = new Completer<String>(); |
| 44 final chainedFuture = completerA.future.chain((x) { |
| 45 Expect.equals("42", x); |
| 46 return completerB.future; |
| 47 }); |
| 48 Expect.isFalse(chainedFuture.isComplete); |
| 49 completerA.complete("42"); |
| 50 Expect.isFalse(chainedFuture.isComplete); |
| 51 completerB.complete("43"); |
| 52 Expect.equals("43", chainedFuture.value); |
| 53 } |
| 54 |
| 55 testChainFirstFutureFails() { |
| 56 final completerA = new Completer<String>(); |
| 57 final error = new Exception("Oh no!"); |
| 58 final chainedFuture = completerA.future.chain((x) { |
| 59 Expect.fail("transformer shouldn't be called"); |
| 60 }); |
| 61 Expect.isFalse(chainedFuture.isComplete); |
| 62 completerA.completeException(error); |
| 63 Expect.equals(error, chainedFuture.exception); |
| 64 } |
| 65 |
| 66 testChainTransformerFails() { |
| 67 final completerA = new Completer<String>(); |
| 68 final error = new Exception("Oh no!"); |
| 69 final chainedFuture = completerA.future.chain((x) { |
| 70 Expect.equals("42", x); |
| 71 throw error; |
| 72 }); |
| 73 Expect.isFalse(chainedFuture.isComplete); |
| 74 completerA.complete("42"); |
| 75 Expect.equals(error, chainedFuture.exception); |
| 76 } |
| 77 |
| 78 testChainSecondFutureFails() { |
| 79 final completerA = new Completer<String>(); |
| 80 final completerB = new Completer<String>(); |
| 81 final error = new Exception("Oh no!"); |
| 82 final chainedFuture = completerA.future.chain((x) { |
| 83 Expect.equals("42", x); |
| 84 return completerB.future; |
| 85 }); |
| 86 Expect.isFalse(chainedFuture.isComplete); |
| 87 completerA.complete("42"); |
| 88 Expect.isFalse(chainedFuture.isComplete); |
| 89 completerB.completeException(error); |
| 90 Expect.equals(error, chainedFuture.exception); |
| 91 } |
| 92 |
| 93 main() { |
| 94 testImmediate(); |
| 95 testTransformSuccess(); |
| 96 testTransformFutureFails(); |
| 97 testTransformTransformerFails(); |
| 98 testChainSuccess(); |
| 99 testChainFirstFutureFails(); |
| 100 testChainTransformerFails(); |
| 101 testChainSecondFutureFails(); |
| 102 } |
| OLD | NEW |