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 // 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 18 matching lines...) Expand all Loading... | |
| 29 final future = completer.future; | 29 final future = completer.future; |
| 30 | 30 |
| 31 completer.complete(3); | 31 completer.complete(3); |
| 32 | 32 |
| 33 Expect.isTrue(future.isComplete); | 33 Expect.isTrue(future.isComplete); |
| 34 Expect.isTrue(future.hasValue); | 34 Expect.isTrue(future.hasValue); |
| 35 Expect.equals(3, future.value); | 35 Expect.equals(3, future.value); |
| 36 Expect.isNull(future.exception); | 36 Expect.isNull(future.exception); |
| 37 } | 37 } |
| 38 | 38 |
| 39 // Tests for [onComplete] | |
| 40 | |
| 41 testCompleteWithCompleteHandlerBeforeComplete() { | |
| 42 final completer = new Completer<int>(); | |
| 43 final future = completer.future; | |
| 44 | |
| 45 int before; | |
| 46 future.onComplete((f) { | |
| 47 Expect.equals(future, f); | |
| 48 Expect.isTrue(f.isComplete); | |
| 49 Expect.isTrue(f.hasValue); | |
| 50 before = f.value; | |
| 51 }); | |
| 52 Expect.throws(() => future.value); | |
| 53 Expect.isNull(before); | |
| 54 completer.complete(3); | |
| 55 | |
| 56 Expect.equals(3, future.value); | |
| 57 Expect.equals(3, before); | |
| 58 } | |
| 59 | |
| 60 testExceptionWithCompleteHandlerBeforeComplete() { | |
| 61 final completer = new Completer<int>(); | |
| 62 final future = completer.future; | |
| 63 final exception = new Exception(); | |
| 64 | |
| 65 var err; | |
| 66 future.onComplete((f) { | |
| 67 Expect.equals(future, f); | |
| 68 Expect.isTrue(f.isComplete); | |
| 69 Expect.isFalse(f.hasValue); | |
| 70 err = f.exception; | |
| 71 }); | |
| 72 Expect.throws(() => future.exception); | |
| 73 Expect.isNull(err); | |
| 74 completer.completeException(exception); | |
| 75 Expect.equals(exception, future.exception); | |
| 76 Expect.equals(exception, err); | |
| 77 Expect.throws(() => future.value, check: (e) => e == exception); | |
| 78 } | |
| 79 | |
| 80 testCompleteWithCompleteHandlerAfterComplete() { | |
| 81 final completer = new Completer<int>(); | |
| 82 final future = completer.future; | |
| 83 | |
| 84 int after; | |
| 85 completer.complete(3); | |
| 86 future.onComplete((f) { | |
| 87 Expect.equals(future, f); | |
| 88 Expect.isTrue(f.isComplete); | |
| 89 Expect.isTrue(f.hasValue); | |
| 90 after = f.value; | |
| 91 }); | |
| 92 Expect.equals(3, future.value); | |
| 93 Expect.equals(3, after); | |
| 94 } | |
| 95 | |
| 96 testExceptionWithCompleteHandlerAfterComplete() { | |
| 97 final completer = new Completer<int>(); | |
| 98 final future = completer.future; | |
| 99 final exception = new Exception(); | |
| 100 | |
| 101 var err; | |
| 102 completer.completeException(exception); | |
| 103 future.onComplete((f) { | |
| 104 Expect.equals(future, f); | |
| 105 Expect.isTrue(f.isComplete); | |
| 106 Expect.isFalse(f.hasValue); | |
| 107 err = f.exception; | |
| 108 }); | |
| 109 Expect.equals(exception, future.exception); | |
| 110 Expect.equals(exception, err); | |
| 111 Expect.throws(() => future.value, check: (e) => e == exception); | |
| 112 } | |
| 113 | |
| 114 testCompleteWithManyCompleteHandlers() { | |
| 115 final completer = new Completer<int>(); | |
| 116 final future = completer.future; | |
| 117 int before; | |
| 118 int after1; | |
| 119 int after2; | |
| 120 | |
| 121 future.onComplete((f) { before = f.value; }); | |
| 122 completer.complete(3); | |
| 123 future.onComplete((f) { after1 = f.value; }); | |
| 124 future.onComplete((f) { after2 = f.value; }); | |
| 125 | |
| 126 Expect.equals(3, future.value); | |
| 127 Expect.equals(3, before); | |
| 128 Expect.equals(3, after1); | |
| 129 Expect.equals(3, after2); | |
| 130 } | |
| 131 | |
| 132 testExceptionWithManyCompleteHandlers() { | |
| 133 final completer = new Completer<int>(); | |
| 134 final future = completer.future; | |
| 135 final exception = new Exception(); | |
| 136 var before; | |
| 137 var after1; | |
| 138 var after2; | |
| 139 | |
| 140 future.onComplete((f) { before = f.exception; }); | |
| 141 completer.completeException(exception); | |
| 142 future.onComplete((f) { after1 = f.exception; }); | |
| 143 future.onComplete((f) { after2 = f.exception; }); | |
| 144 | |
| 145 Expect.equals(exception, future.exception); | |
| 146 Expect.equals(exception, before); | |
| 147 Expect.equals(exception, after1); | |
| 148 Expect.equals(exception, after2); | |
| 149 Expect.throws(() => future.value, check: (e) => e == exception); | |
| 150 } | |
| 151 | |
| 39 // Tests for [then] | 152 // Tests for [then] |
| 40 | 153 |
| 41 testCompleteWithHandlerBeforeComplete() { | 154 testCompleteWithSuccessHandlerBeforeComplete() { |
| 42 final completer = new Completer<int>(); | 155 final completer = new Completer<int>(); |
| 43 final future = completer.future; | 156 final future = completer.future; |
| 44 | 157 |
| 45 int before; | 158 int before; |
| 46 future.then((int v) { before = v; }); | 159 future.then((int v) { before = v; }); |
| 47 Expect.throws(() { future.value; }); | 160 Expect.throws(() { future.value; }); |
| 48 Expect.isNull(before); | 161 Expect.isNull(before); |
| 49 completer.complete(3); | 162 completer.complete(3); |
| 50 | 163 |
| 51 Expect.equals(3, future.value); | 164 Expect.equals(3, future.value); |
| 52 Expect.equals(3, before); | 165 Expect.equals(3, before); |
| 53 } | 166 } |
| 54 | 167 |
| 55 testCompleteWithHandlerAfterComplete() { | 168 testCompleteWithSuccessHandlerAfterComplete() { |
| 56 final completer = new Completer<int>(); | 169 final completer = new Completer<int>(); |
| 57 final future = completer.future; | 170 final future = completer.future; |
| 58 | 171 |
| 59 int after; | 172 int after; |
| 60 completer.complete(3); | 173 completer.complete(3); |
| 61 Expect.equals(3, future.value); | 174 Expect.equals(3, future.value); |
| 62 Expect.isNull(after); | 175 Expect.isNull(after); |
| 63 | 176 |
| 64 future.then((int v) { after = v; }); | 177 future.then((int v) { after = v; }); |
| 65 | 178 |
| 66 Expect.equals(3, future.value); | 179 Expect.equals(3, future.value); |
| 67 Expect.equals(3, after); | 180 Expect.equals(3, after); |
| 68 } | 181 } |
| 69 | 182 |
| 70 testCompleteManyHandlers() { | 183 testCompleteManySuccessHandlers() { |
| 71 final completer = new Completer<int>(); | 184 final completer = new Completer<int>(); |
| 72 final future = completer.future; | 185 final future = completer.future; |
| 186 int before; | |
| 73 int after1; | 187 int after1; |
| 74 int after2; | 188 int after2; |
| 75 int after3; | |
| 76 | 189 |
| 190 future.then((int v) { before = v; }); | |
| 191 completer.complete(3); | |
| 77 future.then((int v) { after1 = v; }); | 192 future.then((int v) { after1 = v; }); |
| 78 completer.complete(3); | |
| 79 future.then((int v) { after2 = v; }); | 193 future.then((int v) { after2 = v; }); |
| 80 future.then((int v) { after3 = v; }); | |
| 81 | 194 |
| 82 Expect.equals(3, future.value); | 195 Expect.equals(3, future.value); |
| 196 Expect.equals(3, before); | |
| 83 Expect.equals(3, after1); | 197 Expect.equals(3, after1); |
| 84 Expect.equals(3, after2); | 198 Expect.equals(3, after2); |
| 85 Expect.equals(3, after3); | |
| 86 } | 199 } |
| 87 | 200 |
| 88 // Tests for [handleException] | 201 // Tests for [handleException] |
| 89 | 202 |
| 90 testException() { | 203 testException() { |
| 91 final completer = new Completer<int>(); | 204 final completer = new Completer<int>(); |
| 92 final future = completer.future; | 205 final future = completer.future; |
| 93 final ex = new Exception(); | 206 final ex = new Exception(); |
| 94 future.then((_) {}); // exception is thrown if we plan to use the value | 207 future.then((_) {}); // exception is thrown if we plan to use the value |
| 95 Expect.throws( | 208 Expect.throws( |
| 96 () { completer.completeException(ex); }, | 209 () { completer.completeException(ex); }, |
| 97 check: (e) => e == ex); | 210 check: (e) => e == ex); |
| 98 } | 211 } |
| 99 | 212 |
| 100 testExceptionNoListeners() { | 213 testExceptionNoSuccessListeners() { |
| 101 final completer = new Completer<int>(); | 214 final completer = new Completer<int>(); |
| 102 final future = completer.future; | 215 final future = completer.future; |
| 103 final ex = new Exception(); | 216 final ex = new Exception(); |
| 104 completer.completeException(ex); // future.then is not called, so no exception | 217 completer.completeException(ex); // future.then is not called, so no exception |
| 105 } | 218 } |
| 106 | 219 |
| 107 testExceptionHandler() { | 220 testExceptionHandler() { |
| 108 final completer = new Completer<int>(); | 221 final completer = new Completer<int>(); |
| 109 final future = completer.future; | 222 final future = completer.future; |
| 110 final ex = new Exception(); | 223 final ex = new Exception(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 final future = completer.future; | 296 final future = completer.future; |
| 184 final ex = new Exception(); | 297 final ex = new Exception(); |
| 185 | 298 |
| 186 var ex2; | 299 var ex2; |
| 187 completer.completeException(ex); | 300 completer.completeException(ex); |
| 188 future.handleException((e) { ex2 = e; return false; }); | 301 future.handleException((e) { ex2 = e; return false; }); |
| 189 Expect.throws(() { future.then((e) { }); }); | 302 Expect.throws(() { future.then((e) { }); }); |
| 190 Expect.equals(ex, ex2); | 303 Expect.equals(ex, ex2); |
| 191 } | 304 } |
| 192 | 305 |
| 306 // Tests for mixed usage of [onComplete], [then], and [handleException] | |
| 307 | |
| 308 testCompleteWithCompletionAndSuccessHandlers() { | |
| 309 final completer = new Completer<int>(); | |
| 310 final future = completer.future; | |
| 311 | |
| 312 var valueFromSuccessHandler; | |
| 313 var valueFromCompletionHandler; | |
| 314 future.onComplete((f) { | |
| 315 Expect.isNotNull(valueFromSuccessHandler); | |
| 316 valueFromCompletionHandler = f.value; | |
| 317 }); | |
| 318 future.then((v) { | |
| 319 Expect.isNull(valueFromCompletionHandler); | |
| 320 valueFromSuccessHandler = v; | |
| 321 }); | |
| 322 completer.complete(42); | |
| 323 Expect.equals(42, valueFromSuccessHandler); | |
| 324 Expect.equals(42, valueFromCompletionHandler); | |
| 325 Expect.equals(42, future.value); | |
| 326 } | |
| 327 | |
| 328 testExceptionWithCompletionAndSuccessHandlers() { | |
| 329 final completer = new Completer<int>(); | |
| 330 final future = completer.future; | |
| 331 final ex = new Exception(); | |
| 332 | |
| 333 var exceptionFromCompleteHandler; | |
| 334 future.onComplete((f) { | |
| 335 Expect.equals(future, f); | |
| 336 Expect.isFalse(f.hasValue); | |
| 337 exceptionFromCompleteHandler = f.exception; | |
| 338 }); | |
| 339 future.then((v) => Expect.fail("Should not succeed")); | |
| 340 Expect.throws(() => completer.completeException(ex), check: (e) => ex == e); | |
| 341 Expect.equals(ex, exceptionFromCompleteHandler); | |
| 342 } | |
| 343 | |
| 344 testExceptionWithCompletionAndSuccessAndExceptionHandlers() { | |
| 345 final completer = new Completer<int>(); | |
| 346 final future = completer.future; | |
| 347 final ex = new Exception(); | |
| 348 | |
| 349 var exceptionFromCompleteHandler; | |
| 350 var exceptionFromExceptionHandler; | |
| 351 future.onComplete((f) { | |
| 352 Expect.equals(future, f); | |
| 353 Expect.isFalse(f.hasValue); | |
| 354 exceptionFromCompleteHandler = f.exception; | |
| 355 }); | |
| 356 future.handleException((e) { | |
| 357 exceptionFromExceptionHandler = e; | |
| 358 return true; | |
| 359 }); | |
| 360 future.then((v) => Expect.fail("Should not succeed")); | |
| 361 completer.completeException(ex); | |
| 362 Expect.equals(ex, exceptionFromCompleteHandler); | |
| 363 Expect.equals(ex, exceptionFromExceptionHandler); | |
| 364 } | |
| 365 | |
| 193 // Tests for Future.transform | 366 // Tests for Future.transform |
| 194 | 367 |
| 195 testTransformSuccess() { | 368 testTransformSuccess() { |
| 196 final completer = new Completer<String>(); | 369 final completer = new Completer<String>(); |
| 197 final transformedFuture = completer.future.transform((x) => "** $x **"); | 370 final transformedFuture = completer.future.transform((x) => "** $x **"); |
| 198 Expect.isFalse(transformedFuture.isComplete); | 371 Expect.isFalse(transformedFuture.isComplete); |
| 199 completer.complete("42"); | 372 completer.complete("42"); |
| 200 Expect.equals("** 42 **", transformedFuture.value); | 373 Expect.equals("** 42 **", transformedFuture.value); |
| 201 } | 374 } |
| 202 | 375 |
| 203 testTransformFutureFails() { | 376 testTransformFutureFails() { |
| 204 final completer = new Completer<String>(); | 377 final completer = new Completer<String>(); |
| 205 final error = new Exception("Oh no!"); | 378 final error = new Exception("Oh no!"); |
| 206 final transformedFuture = completer.future.transform((x) { | 379 final transformedFuture = completer.future.transform((x) { |
| 207 Expect.fail("transformer shouldn't be called"); | 380 Expect.fail("transformer shouldn't be called"); |
| 208 }); | 381 }); |
| 209 Expect.isFalse(transformedFuture.isComplete); | 382 Expect.isFalse(transformedFuture.isComplete); |
| 210 completer.completeException(error); | 383 completer.completeException(error); |
| 211 Expect.equals(error, transformedFuture.exception); | 384 Expect.equals(error, transformedFuture.exception); |
| 212 } | 385 } |
| 213 | 386 |
| 214 testTransformTransformerFails() { | 387 testTransformTransformerFails() { |
| 215 final completer = new Completer<String>(); | 388 final completer = new Completer<String>(); |
| 216 final error = new Exception("Oh no!"); | 389 final error = new Exception("Oh no!"); |
| 217 final transformedFuture = completer.future.transform((x) { throw error; }); | 390 final transformedFuture = completer.future.transform((x) { throw error; }); |
| 218 Expect.isFalse(transformedFuture.isComplete); | 391 Expect.isFalse(transformedFuture.isComplete); |
| 219 completer.complete("42"); | 392 completer.complete("42"); |
| 220 Expect.equals(error, transformedFuture.exception); | 393 Expect.equals(error, transformedFuture.exception); |
| 221 } | 394 } |
|
Siggi Cherem (dart-lang)
2012/06/06 20:42:49
could you add a test here registering exception ha
| |
| 222 | 395 |
| 223 // Tests for Future.chain | 396 // Tests for Future.chain |
| 224 | 397 |
| 225 testChainSuccess() { | 398 testChainSuccess() { |
| 226 final completerA = new Completer<String>(); | 399 final completerA = new Completer<String>(); |
| 227 final completerB = new Completer<String>(); | 400 final completerB = new Completer<String>(); |
| 228 final chainedFuture = completerA.future.chain((x) { | 401 final chainedFuture = completerA.future.chain((x) { |
| 229 Expect.equals("42", x); | 402 Expect.equals("42", x); |
| 230 return completerB.future; | 403 return completerB.future; |
| 231 }); | 404 }); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 completerA.complete("42"); | 444 completerA.complete("42"); |
| 272 Expect.isFalse(chainedFuture.isComplete); | 445 Expect.isFalse(chainedFuture.isComplete); |
| 273 completerB.completeException(error); | 446 completerB.completeException(error); |
| 274 Expect.equals(error, chainedFuture.exception); | 447 Expect.equals(error, chainedFuture.exception); |
| 275 } | 448 } |
| 276 | 449 |
| 277 main() { | 450 main() { |
| 278 testImmediate(); | 451 testImmediate(); |
| 279 testNeverComplete(); | 452 testNeverComplete(); |
| 280 testComplete(); | 453 testComplete(); |
| 281 testCompleteWithHandlerBeforeComplete(); | 454 testCompleteWithCompleteHandlerBeforeComplete(); |
| 282 testCompleteWithHandlerAfterComplete(); | 455 testExceptionWithCompleteHandlerBeforeComplete(); |
| 283 testCompleteManyHandlers(); | 456 testCompleteWithCompleteHandlerAfterComplete(); |
| 457 testExceptionWithCompleteHandlerAfterComplete(); | |
| 458 testCompleteWithManyCompleteHandlers(); | |
| 459 testExceptionWithManyCompleteHandlers(); | |
| 460 testCompleteWithSuccessHandlerBeforeComplete(); | |
| 461 testCompleteWithSuccessHandlerAfterComplete(); | |
| 462 testCompleteManySuccessHandlers(); | |
| 284 testException(); | 463 testException(); |
| 285 testExceptionHandler(); | 464 testExceptionHandler(); |
| 286 testExceptionHandlerReturnsTrue(); | 465 testExceptionHandlerReturnsTrue(); |
| 287 testExceptionHandlerReturnsTrue2(); | 466 testExceptionHandlerReturnsTrue2(); |
| 288 testExceptionHandlerReturnsFalse(); | 467 testExceptionHandlerReturnsFalse(); |
| 289 testExceptionHandlerReturnsFalse2(); | 468 testExceptionHandlerReturnsFalse2(); |
| 290 testExceptionHandlerAfterCompleteThenNotCalled(); | 469 testExceptionHandlerAfterCompleteThenNotCalled(); |
| 291 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); | 470 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); |
| 471 testCompleteWithCompletionAndSuccessHandlers(); | |
| 472 testExceptionWithCompletionAndSuccessHandlers(); | |
| 473 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); | |
| 292 testTransformSuccess(); | 474 testTransformSuccess(); |
| 293 testTransformFutureFails(); | 475 testTransformFutureFails(); |
| 294 testTransformTransformerFails(); | 476 testTransformTransformerFails(); |
| 295 testChainSuccess(); | 477 testChainSuccess(); |
| 296 testChainFirstFutureFails(); | 478 testChainFirstFutureFails(); |
| 297 testChainTransformerFails(); | 479 testChainTransformerFails(); |
| 298 testChainSecondFutureFails(); | 480 testChainSecondFutureFails(); |
| 299 } | 481 } |
| OLD | NEW |