| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 Expect.equals(future, f); | 67 Expect.equals(future, f); |
| 68 Expect.isTrue(f.isComplete); | 68 Expect.isTrue(f.isComplete); |
| 69 Expect.isFalse(f.hasValue); | 69 Expect.isFalse(f.hasValue); |
| 70 err = f.exception; | 70 err = f.exception; |
| 71 }); | 71 }); |
| 72 Expect.throws(() => future.exception); | 72 Expect.throws(() => future.exception); |
| 73 Expect.isNull(err); | 73 Expect.isNull(err); |
| 74 completer.completeException(exception); | 74 completer.completeException(exception); |
| 75 Expect.equals(exception, future.exception); | 75 Expect.equals(exception, future.exception); |
| 76 Expect.equals(exception, err); | 76 Expect.equals(exception, err); |
| 77 Expect.throws(() => future.value, check: (e) => e == exception); | 77 Expect.throws(() => future.value, (e) => e == exception); |
| 78 } | 78 } |
| 79 | 79 |
| 80 testCompleteWithCompleteHandlerAfterComplete() { | 80 testCompleteWithCompleteHandlerAfterComplete() { |
| 81 final completer = new Completer<int>(); | 81 final completer = new Completer<int>(); |
| 82 final future = completer.future; | 82 final future = completer.future; |
| 83 | 83 |
| 84 int after; | 84 int after; |
| 85 completer.complete(3); | 85 completer.complete(3); |
| 86 future.onComplete((f) { | 86 future.onComplete((f) { |
| 87 Expect.equals(future, f); | 87 Expect.equals(future, f); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 101 var err; | 101 var err; |
| 102 completer.completeException(exception); | 102 completer.completeException(exception); |
| 103 future.onComplete((f) { | 103 future.onComplete((f) { |
| 104 Expect.equals(future, f); | 104 Expect.equals(future, f); |
| 105 Expect.isTrue(f.isComplete); | 105 Expect.isTrue(f.isComplete); |
| 106 Expect.isFalse(f.hasValue); | 106 Expect.isFalse(f.hasValue); |
| 107 err = f.exception; | 107 err = f.exception; |
| 108 }); | 108 }); |
| 109 Expect.equals(exception, future.exception); | 109 Expect.equals(exception, future.exception); |
| 110 Expect.equals(exception, err); | 110 Expect.equals(exception, err); |
| 111 Expect.throws(() => future.value, check: (e) => e == exception); | 111 Expect.throws(() => future.value, (e) => e == exception); |
| 112 } | 112 } |
| 113 | 113 |
| 114 testCompleteWithManyCompleteHandlers() { | 114 testCompleteWithManyCompleteHandlers() { |
| 115 final completer = new Completer<int>(); | 115 final completer = new Completer<int>(); |
| 116 final future = completer.future; | 116 final future = completer.future; |
| 117 int before; | 117 int before; |
| 118 int after1; | 118 int after1; |
| 119 int after2; | 119 int after2; |
| 120 | 120 |
| 121 future.onComplete((f) { before = f.value; }); | 121 future.onComplete((f) { before = f.value; }); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 139 | 139 |
| 140 future.onComplete((f) { before = f.exception; }); | 140 future.onComplete((f) { before = f.exception; }); |
| 141 completer.completeException(exception); | 141 completer.completeException(exception); |
| 142 future.onComplete((f) { after1 = f.exception; }); | 142 future.onComplete((f) { after1 = f.exception; }); |
| 143 future.onComplete((f) { after2 = f.exception; }); | 143 future.onComplete((f) { after2 = f.exception; }); |
| 144 | 144 |
| 145 Expect.equals(exception, future.exception); | 145 Expect.equals(exception, future.exception); |
| 146 Expect.equals(exception, before); | 146 Expect.equals(exception, before); |
| 147 Expect.equals(exception, after1); | 147 Expect.equals(exception, after1); |
| 148 Expect.equals(exception, after2); | 148 Expect.equals(exception, after2); |
| 149 Expect.throws(() => future.value, check: (e) => e == exception); | 149 Expect.throws(() => future.value, (e) => e == exception); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Tests for [then] | 152 // Tests for [then] |
| 153 | 153 |
| 154 testCompleteWithSuccessHandlerBeforeComplete() { | 154 testCompleteWithSuccessHandlerBeforeComplete() { |
| 155 final completer = new Completer<int>(); | 155 final completer = new Completer<int>(); |
| 156 final future = completer.future; | 156 final future = completer.future; |
| 157 | 157 |
| 158 int before; | 158 int before; |
| 159 future.then((int v) { before = v; }); | 159 future.then((int v) { before = v; }); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 200 |
| 201 // Tests for [handleException] | 201 // Tests for [handleException] |
| 202 | 202 |
| 203 testException() { | 203 testException() { |
| 204 final completer = new Completer<int>(); | 204 final completer = new Completer<int>(); |
| 205 final future = completer.future; | 205 final future = completer.future; |
| 206 final ex = new Exception(); | 206 final ex = new Exception(); |
| 207 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 |
| 208 Expect.throws( | 208 Expect.throws( |
| 209 () { completer.completeException(ex); }, | 209 () { completer.completeException(ex); }, |
| 210 check: (e) => e == ex); | 210 (e) => e == ex); |
| 211 } | 211 } |
| 212 | 212 |
| 213 testExceptionNoSuccessListeners() { | 213 testExceptionNoSuccessListeners() { |
| 214 final completer = new Completer<int>(); | 214 final completer = new Completer<int>(); |
| 215 final future = completer.future; | 215 final future = completer.future; |
| 216 final ex = new Exception(); | 216 final ex = new Exception(); |
| 217 completer.completeException(ex); // future.then is not called, so no exception | 217 completer.completeException(ex); // future.then is not called, so no exception |
| 218 } | 218 } |
| 219 | 219 |
| 220 testExceptionHandler() { | 220 testExceptionHandler() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 final completer = new Completer<int>(); | 256 final completer = new Completer<int>(); |
| 257 final future = completer.future; | 257 final future = completer.future; |
| 258 final ex = new Exception(); | 258 final ex = new Exception(); |
| 259 | 259 |
| 260 bool reached = false; | 260 bool reached = false; |
| 261 future.then((_) {}); // ensure exception is thrown... | 261 future.then((_) {}); // ensure exception is thrown... |
| 262 future.handleException((e) { return false; }); | 262 future.handleException((e) { return false; }); |
| 263 future.handleException((e) { reached = true; return false; }); // overshadowed | 263 future.handleException((e) { reached = true; return false; }); // overshadowed |
| 264 Expect.throws( | 264 Expect.throws( |
| 265 () { completer.completeException(ex); }, | 265 () { completer.completeException(ex); }, |
| 266 check: (e) => e == ex); | 266 (e) => e == ex); |
| 267 Expect.isTrue(reached); | 267 Expect.isTrue(reached); |
| 268 } | 268 } |
| 269 | 269 |
| 270 testExceptionHandlerReturnsFalse2() { | 270 testExceptionHandlerReturnsFalse2() { |
| 271 final completer = new Completer<int>(); | 271 final completer = new Completer<int>(); |
| 272 final future = completer.future; | 272 final future = completer.future; |
| 273 final ex = new Exception(); | 273 final ex = new Exception(); |
| 274 | 274 |
| 275 bool reached = false; | 275 bool reached = false; |
| 276 future.handleException((e) { return false; }); | 276 future.handleException((e) { return false; }); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 final future = completer.future; | 378 final future = completer.future; |
| 379 final ex = new Exception(); | 379 final ex = new Exception(); |
| 380 | 380 |
| 381 var exceptionFromCompleteHandler; | 381 var exceptionFromCompleteHandler; |
| 382 future.onComplete((f) { | 382 future.onComplete((f) { |
| 383 Expect.equals(future, f); | 383 Expect.equals(future, f); |
| 384 Expect.isFalse(f.hasValue); | 384 Expect.isFalse(f.hasValue); |
| 385 exceptionFromCompleteHandler = f.exception; | 385 exceptionFromCompleteHandler = f.exception; |
| 386 }); | 386 }); |
| 387 future.then((v) => Expect.fail("Should not succeed")); | 387 future.then((v) => Expect.fail("Should not succeed")); |
| 388 Expect.throws(() => completer.completeException(ex), check: (e) => ex == e); | 388 Expect.throws(() => completer.completeException(ex), (e) => ex == e); |
| 389 Expect.equals(ex, exceptionFromCompleteHandler); | 389 Expect.equals(ex, exceptionFromCompleteHandler); |
| 390 } | 390 } |
| 391 | 391 |
| 392 testExceptionWithCompletionAndSuccessAndExceptionHandlers() { | 392 testExceptionWithCompletionAndSuccessAndExceptionHandlers() { |
| 393 final completer = new Completer<int>(); | 393 final completer = new Completer<int>(); |
| 394 final future = completer.future; | 394 final future = completer.future; |
| 395 final ex = new Exception(); | 395 final ex = new Exception(); |
| 396 | 396 |
| 397 var exceptionFromCompleteHandler; | 397 var exceptionFromCompleteHandler; |
| 398 var exceptionFromExceptionHandler; | 398 var exceptionFromExceptionHandler; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 completer.completeException(error); | 431 completer.completeException(error); |
| 432 Expect.equals(error, transformedFuture.exception); | 432 Expect.equals(error, transformedFuture.exception); |
| 433 } | 433 } |
| 434 | 434 |
| 435 testTransformTransformerFails() { | 435 testTransformTransformerFails() { |
| 436 final completer = new Completer<String>(); | 436 final completer = new Completer<String>(); |
| 437 final error = new Exception("Oh no!"); | 437 final error = new Exception("Oh no!"); |
| 438 final transformedFuture = completer.future.transform((x) { throw error; }); | 438 final transformedFuture = completer.future.transform((x) { throw error; }); |
| 439 Expect.isFalse(transformedFuture.isComplete); | 439 Expect.isFalse(transformedFuture.isComplete); |
| 440 transformedFuture.then((v) => null); | 440 transformedFuture.then((v) => null); |
| 441 Expect.throws(() => completer.complete("42"), check: (e) => e == error); | 441 Expect.throws(() => completer.complete("42"), (e) => e == error); |
| 442 Expect.equals(error, transformedFuture.exception); | 442 Expect.equals(error, transformedFuture.exception); |
| 443 } | 443 } |
| 444 | 444 |
| 445 // Tests for Future.chain | 445 // Tests for Future.chain |
| 446 | 446 |
| 447 testChainSuccess() { | 447 testChainSuccess() { |
| 448 final completerA = new Completer<String>(); | 448 final completerA = new Completer<String>(); |
| 449 final completerB = new Completer<String>(); | 449 final completerB = new Completer<String>(); |
| 450 final chainedFuture = completerA.future.chain((x) { | 450 final chainedFuture = completerA.future.chain((x) { |
| 451 Expect.equals("42", x); | 451 Expect.equals("42", x); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 471 | 471 |
| 472 testChainTransformerFails() { | 472 testChainTransformerFails() { |
| 473 final completerA = new Completer<String>(); | 473 final completerA = new Completer<String>(); |
| 474 final error = new Exception("Oh no!"); | 474 final error = new Exception("Oh no!"); |
| 475 final chainedFuture = completerA.future.chain((x) { | 475 final chainedFuture = completerA.future.chain((x) { |
| 476 Expect.equals("42", x); | 476 Expect.equals("42", x); |
| 477 throw error; | 477 throw error; |
| 478 }); | 478 }); |
| 479 chainedFuture.then((v) => null); | 479 chainedFuture.then((v) => null); |
| 480 Expect.isFalse(chainedFuture.isComplete); | 480 Expect.isFalse(chainedFuture.isComplete); |
| 481 Expect.throws(() => completerA.complete("42"), check: (e) => e == error); | 481 Expect.throws(() => completerA.complete("42"), (e) => e == error); |
| 482 Expect.equals(error, chainedFuture.exception); | 482 Expect.equals(error, chainedFuture.exception); |
| 483 } | 483 } |
| 484 | 484 |
| 485 testChainSecondFutureFails() { | 485 testChainSecondFutureFails() { |
| 486 final completerA = new Completer<String>(); | 486 final completerA = new Completer<String>(); |
| 487 final completerB = new Completer<String>(); | 487 final completerB = new Completer<String>(); |
| 488 final error = new Exception("Oh no!"); | 488 final error = new Exception("Oh no!"); |
| 489 final chainedFuture = completerA.future.chain((x) { | 489 final chainedFuture = completerA.future.chain((x) { |
| 490 Expect.equals("42", x); | 490 Expect.equals("42", x); |
| 491 return completerB.future; | 491 return completerB.future; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 testTransformTransformerFails(); | 599 testTransformTransformerFails(); |
| 600 testChainSuccess(); | 600 testChainSuccess(); |
| 601 testChainFirstFutureFails(); | 601 testChainFirstFutureFails(); |
| 602 testChainTransformerFails(); | 602 testChainTransformerFails(); |
| 603 testChainSecondFutureFails(); | 603 testChainSecondFutureFails(); |
| 604 testTransformExceptionCompletesNormally(); | 604 testTransformExceptionCompletesNormally(); |
| 605 testTransformExceptionThrows(); | 605 testTransformExceptionThrows(); |
| 606 testTransformExceptionReturns(); | 606 testTransformExceptionReturns(); |
| 607 testTransformExceptionReturnsAFuture(); | 607 testTransformExceptionReturnsAFuture(); |
| 608 } | 608 } |
| OLD | NEW |