| 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 testImmediate() { |
| 11 final future = new Future<String>.immediate("42"); |
| 12 var port = new ReceivePort(); |
| 13 future.then((x) { |
| 14 Expect.equals("42", x); |
| 15 port.close(); |
| 16 }); |
| 17 } |
| 18 |
| 19 testNeverComplete() { |
| 20 final completer = new Completer<int>(); |
| 21 final future = completer.future; |
| 22 future.then((v) => Except.fails("Value not expected")); |
| 23 future.catchError((e) => Except.fails("Value not expected")); |
| 24 } |
| 25 |
| 26 testComplete() { |
| 27 final completer = new Completer<int>(); |
| 28 final future = completer.future; |
| 29 |
| 30 completer.complete(3); |
| 31 |
| 32 future.then((v) => Expect.equals(3, v)); |
| 33 } |
| 34 |
| 35 // Tests for [then] |
| 36 |
| 37 testCompleteWithSuccessHandlerBeforeComplete() { |
| 38 final completer = new Completer<int>(); |
| 39 final future = completer.future; |
| 40 |
| 41 int value; |
| 42 future.then((int v) { value = v; }); |
| 43 Expect.isNull(value); |
| 44 completer.complete(3); |
| 45 |
| 46 Expect.equals(3, value); |
| 47 } |
| 48 |
| 49 testCompleteWithSuccessHandlerAfterComplete() { |
| 50 final completer = new Completer<int>(); |
| 51 final future = completer.future; |
| 52 |
| 53 int after; |
| 54 completer.complete(3); |
| 55 Expect.isNull(after); |
| 56 |
| 57 var port = new ReceivePort(); |
| 58 future.then((int v) { after = v; }) |
| 59 .then((_) { |
| 60 Expect.equals(3, after); |
| 61 port.close(); |
| 62 }); |
| 63 } |
| 64 |
| 65 testCompleteManySuccessHandlers() { |
| 66 final completer = new Completer<int>(); |
| 67 final future = completer.future; |
| 68 int before; |
| 69 int after1; |
| 70 int after2; |
| 71 |
| 72 var futures = []; |
| 73 futures.add(future.then((int v) { before = v; })); |
| 74 completer.complete(3); |
| 75 futures.add(future.then((int v) { after1 = v; })); |
| 76 futures.add(future.then((int v) { after2 = v; })); |
| 77 |
| 78 var port = new ReceivePort(); |
| 79 new Future.wait(futures).then((_) { |
| 80 Expect.equals(3, before); |
| 81 Expect.equals(3, after1); |
| 82 Expect.equals(3, after2); |
| 83 port.close(); |
| 84 }); |
| 85 } |
| 86 |
| 87 // Tests for [catchError] |
| 88 |
| 89 testException() { |
| 90 final completer = new Completer<int>(); |
| 91 final future = completer.future; |
| 92 final ex = new Exception(); |
| 93 |
| 94 var port = new ReceivePort(); |
| 95 future |
| 96 .then((v) { throw "Value not expected"; }) |
| 97 .catchError((e) { |
| 98 Expect.equals(e.error, ex); |
| 99 port.close(); |
| 100 }, test: (e) => e == ex); |
| 101 completer.completeError(ex); |
| 102 } |
| 103 |
| 104 testExceptionNoSuccessListeners() { |
| 105 final completer = new Completer<int>(); |
| 106 final future = completer.future; |
| 107 final ex = new Exception(); |
| 108 completer.completeException(ex); // future.then is not called, so no exception |
| 109 } |
| 110 |
| 111 testExceptionHandler() { |
| 112 final completer = new Completer<int>(); |
| 113 final future = completer.future; |
| 114 final ex = new Exception(); |
| 115 |
| 116 var ex2; |
| 117 var done = future.catchError((e) { ex2 = e.error; }); |
| 118 completer.completeError(ex); |
| 119 |
| 120 var port = new ReceivePort(); |
| 121 done.then((_) { |
| 122 Expect.equals(ex, ex2); |
| 123 port.close(); |
| 124 }); |
| 125 } |
| 126 |
| 127 testExceptionHandlerReturnsTrue() { |
| 128 final completer = new Completer<int>(); |
| 129 final future = completer.future; |
| 130 final ex = new Exception(); |
| 131 |
| 132 bool reached = false; |
| 133 future.catchError((e) { }); |
| 134 future.catchError((e) { reached = true; }, test: (e) => false) |
| 135 .catchError((e) {}); |
| 136 completer.completeError(ex); |
| 137 Expect.isFalse(reached); |
| 138 } |
| 139 |
| 140 testExceptionHandlerReturnsTrue2() { |
| 141 final completer = new Completer<int>(); |
| 142 final future = completer.future; |
| 143 final ex = new Exception(); |
| 144 |
| 145 bool reached = false; |
| 146 var done = future |
| 147 .catchError((e) { }, test: (e) => false) |
| 148 .catchError((e) { reached = true; }); |
| 149 completer.completeError(ex); |
| 150 |
| 151 var port = new ReceivePort(); |
| 152 done.then((_) { |
| 153 Expect.isTrue(reached); |
| 154 port.close(); |
| 155 }); |
| 156 } |
| 157 |
| 158 testExceptionHandlerReturnsFalse() { |
| 159 final completer = new Completer<int>(); |
| 160 final future = completer.future; |
| 161 final ex = new Exception(); |
| 162 |
| 163 bool reached = false; |
| 164 |
| 165 future.catchError((e) { }); |
| 166 |
| 167 future.catchError((e) { reached = true; }, test: (e) => false) |
| 168 .catchError((e) { }); |
| 169 |
| 170 completer.completeError(ex); |
| 171 |
| 172 Expect.isFalse(reached); |
| 173 } |
| 174 |
| 10 testFutureAsStreamCompleteAfter() { | 175 testFutureAsStreamCompleteAfter() { |
| 11 var completer = new Completer(); | 176 var completer = new Completer(); |
| 12 bool gotValue = false; | 177 bool gotValue = false; |
| 13 var port = new ReceivePort(); | 178 var port = new ReceivePort(); |
| 14 completer.future.asStream().listen( | 179 completer.future.asStream().listen( |
| 15 (data) { | 180 (data) { |
| 16 Expect.isFalse(gotValue); | 181 Expect.isFalse(gotValue); |
| 17 gotValue = true; | 182 gotValue = true; |
| 18 Expect.equals("value", data); | 183 Expect.equals("value", data); |
| 19 }, | 184 }, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 new Timer(0, () { | 342 new Timer(0, () { |
| 178 Future later = future.whenComplete(countDown); | 343 Future later = future.whenComplete(countDown); |
| 179 later.then((v) { | 344 later.then((v) { |
| 180 Expect.equals(42, v); | 345 Expect.equals(42, v); |
| 181 countDown(); | 346 countDown(); |
| 182 }); | 347 }); |
| 183 }); | 348 }); |
| 184 } | 349 } |
| 185 | 350 |
| 186 main() { | 351 main() { |
| 352 testImmediate(); |
| 353 testNeverComplete(); |
| 354 |
| 355 testComplete(); |
| 356 testCompleteWithSuccessHandlerBeforeComplete(); |
| 357 testCompleteWithSuccessHandlerAfterComplete(); |
| 358 testCompleteManySuccessHandlers(); |
| 359 |
| 360 testException(); |
| 361 testExceptionHandler(); |
| 362 testExceptionHandlerReturnsTrue(); |
| 363 testExceptionHandlerReturnsTrue2(); |
| 364 testExceptionHandlerReturnsFalse(); |
| 365 |
| 187 testFutureAsStreamCompleteAfter(); | 366 testFutureAsStreamCompleteAfter(); |
| 188 testFutureAsStreamCompleteBefore(); | 367 testFutureAsStreamCompleteBefore(); |
| 189 testFutureAsStreamCompleteImmediate(); | 368 testFutureAsStreamCompleteImmediate(); |
| 190 testFutureAsStreamCompleteErrorAfter(); | 369 testFutureAsStreamCompleteErrorAfter(); |
| 191 testFutureAsStreamWrapper(); | 370 testFutureAsStreamWrapper(); |
| 192 | 371 |
| 193 testFutureWhenCompleteValue(); | 372 testFutureWhenCompleteValue(); |
| 194 testFutureWhenCompleteError(); | 373 testFutureWhenCompleteError(); |
| 195 testFutureWhenCompleteValueNewError(); | 374 testFutureWhenCompleteValueNewError(); |
| 196 testFutureWhenCompleteErrorNewError(); | 375 testFutureWhenCompleteErrorNewError(); |
| 197 } | 376 } |
| 198 | 377 |
| OLD | NEW |