| 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 library future_test; | 5 library future_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 | 9 |
| 10 const Duration MS = const Duration(milliseconds: 1); | 10 const Duration MS = const Duration(milliseconds: 1); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 testNeverComplete() { | 46 testNeverComplete() { |
| 47 final completer = new Completer<int>(); | 47 final completer = new Completer<int>(); |
| 48 final future = completer.future; | 48 final future = completer.future; |
| 49 future.then((v) => Expect.fails("Value not expected")); | 49 future.then((v) => Expect.fails("Value not expected")); |
| 50 future.catchError((e) => Expect.fails("Value not expected")); | 50 future.catchError((e) => Expect.fails("Value not expected")); |
| 51 } | 51 } |
| 52 | 52 |
| 53 testComplete() { | 53 testComplete() { |
| 54 final completer = new Completer<int>(); | 54 final completer = new Completer<int>(); |
| 55 final future = completer.future; | 55 final future = completer.future; |
| 56 Expect.isFalse(completer.completed); |
| 56 | 57 |
| 57 completer.complete(3); | 58 completer.complete(3); |
| 59 Expect.isTrue(completer.completed); |
| 58 | 60 |
| 59 future.then((v) => Expect.equals(3, v)); | 61 future.then((v) => Expect.equals(3, v)); |
| 60 } | 62 } |
| 61 | 63 |
| 62 // Tests for [then] | 64 // Tests for [then] |
| 63 | 65 |
| 64 testCompleteWithSuccessHandlerBeforeComplete() { | 66 testCompleteWithSuccessHandlerBeforeComplete() { |
| 65 final completer = new Completer<int>(); | 67 final completer = new Completer<int>(); |
| 66 final future = completer.future; | 68 final future = completer.future; |
| 67 | 69 |
| 68 int value; | 70 int value; |
| 69 future.then((int v) { value = v; }); | 71 future.then((int v) { value = v; }); |
| 70 Expect.isNull(value); | 72 Expect.isNull(value); |
| 73 |
| 74 Expect.isFalse(completer.completed); |
| 71 completer.complete(3); | 75 completer.complete(3); |
| 76 Expect.isTrue(completer.completed); |
| 72 | 77 |
| 73 Expect.equals(3, value); | 78 Expect.equals(3, value); |
| 74 } | 79 } |
| 75 | 80 |
| 76 testCompleteWithSuccessHandlerAfterComplete() { | 81 testCompleteWithSuccessHandlerAfterComplete() { |
| 77 final completer = new Completer<int>(); | 82 final completer = new Completer<int>(); |
| 78 final future = completer.future; | 83 final future = completer.future; |
| 79 | 84 |
| 80 int after; | 85 int after; |
| 81 completer.complete(3); | 86 completer.complete(3); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 completer.completeError(ex); | 133 completer.completeError(ex); |
| 129 } | 134 } |
| 130 | 135 |
| 131 testExceptionHandler() { | 136 testExceptionHandler() { |
| 132 final completer = new Completer<int>(); | 137 final completer = new Completer<int>(); |
| 133 final future = completer.future; | 138 final future = completer.future; |
| 134 final ex = new Exception(); | 139 final ex = new Exception(); |
| 135 | 140 |
| 136 var ex2; | 141 var ex2; |
| 137 var done = future.catchError((e) { ex2 = e.error; }); | 142 var done = future.catchError((e) { ex2 = e.error; }); |
| 143 |
| 144 Expect.isFalse(completer.completed); |
| 138 completer.completeError(ex); | 145 completer.completeError(ex); |
| 146 Expect.isTrue(completer.completed); |
| 139 | 147 |
| 140 var port = new ReceivePort(); | 148 var port = new ReceivePort(); |
| 141 done.then((_) { | 149 done.then((_) { |
| 142 Expect.equals(ex, ex2); | 150 Expect.equals(ex, ex2); |
| 143 port.close(); | 151 port.close(); |
| 144 }); | 152 }); |
| 145 } | 153 } |
| 146 | 154 |
| 147 testExceptionHandlerReturnsTrue() { | 155 testExceptionHandlerReturnsTrue() { |
| 148 final completer = new Completer<int>(); | 156 final completer = new Completer<int>(); |
| 149 final future = completer.future; | 157 final future = completer.future; |
| 150 final ex = new Exception(); | 158 final ex = new Exception(); |
| 151 | 159 |
| 152 bool reached = false; | 160 bool reached = false; |
| 153 future.catchError((e) { }); | 161 future.catchError((e) { }); |
| 154 future.catchError((e) { reached = true; }, test: (e) => false) | 162 future.catchError((e) { reached = true; }, test: (e) => false) |
| 155 .catchError((e) {}); | 163 .catchError((e) {}); |
| 164 Expect.isFalse(completer.completed); |
| 156 completer.completeError(ex); | 165 completer.completeError(ex); |
| 166 Expect.isTrue(completer.completed); |
| 157 Expect.isFalse(reached); | 167 Expect.isFalse(reached); |
| 158 } | 168 } |
| 159 | 169 |
| 160 testExceptionHandlerReturnsTrue2() { | 170 testExceptionHandlerReturnsTrue2() { |
| 161 final completer = new Completer<int>(); | 171 final completer = new Completer<int>(); |
| 162 final future = completer.future; | 172 final future = completer.future; |
| 163 final ex = new Exception(); | 173 final ex = new Exception(); |
| 164 | 174 |
| 165 bool reached = false; | 175 bool reached = false; |
| 166 var done = future | 176 var done = future |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 testFutureCatchThrowsAsync(); | 642 testFutureCatchThrowsAsync(); |
| 633 testFutureWhenThrowsAsync(); | 643 testFutureWhenThrowsAsync(); |
| 634 testFutureCatchRethrowsAsync(); | 644 testFutureCatchRethrowsAsync(); |
| 635 | 645 |
| 636 testChainedFutureValue(); | 646 testChainedFutureValue(); |
| 637 testChainedFutureValueDelay(); | 647 testChainedFutureValueDelay(); |
| 638 testChainedFutureError(); | 648 testChainedFutureError(); |
| 639 } | 649 } |
| 640 | 650 |
| 641 | 651 |
| OLD | NEW |