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