Chromium Code Reviews| Index: tests/corelib/src/FutureTest.dart |
| diff --git a/tests/corelib/src/FutureTest.dart b/tests/corelib/src/FutureTest.dart |
| index 871cc75064cc088d2629a3ff67a837e455ea0f1b..be76d2100e1dcf6e720a24acbbbd3aa4a27ed483 100644 |
| --- a/tests/corelib/src/FutureTest.dart |
| +++ b/tests/corelib/src/FutureTest.dart |
| @@ -2,14 +2,172 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +// Tests for Future.immediate |
| + |
| testImmediate() { |
| final future = new Future<String>.immediate("42"); |
| Expect.isTrue(future.isComplete); |
| + Expect.isTrue(future.hasValue); |
| var value = null; |
| future.then((x) => value = x); |
| Expect.equals("42", value); |
| } |
| +// Tests for getters (value, exception, isComplete, isValue) |
| + |
| +testNeverComplete() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + Expect.isFalse(future.isComplete); |
| + Expect.isFalse(future.hasValue); |
| + Expect.throws(() { future.value; }); |
| + Expect.throws(() { future.exception; }); |
| +} |
|
mattsh
2012/02/14 04:21:10
good.
|
| + |
| +testComplete() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + |
| + completer.complete(3); |
| + |
| + Expect.isTrue(future.isComplete); |
| + Expect.isTrue(future.hasValue); |
| + Expect.equals(3, future.value); |
| + Expect.isNull(future.exception); |
| +} |
| + |
| +// Tests for [then] |
| + |
| +testCompleteWithHandlerBeforeComplete() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + |
| + int before = null; |
|
mattsh
2012/02/14 04:21:10
isn't "= null" redundant here (i.e., aren't all va
Siggi Cherem (dart-lang)
2012/02/14 16:12:17
True. Fixed.
|
| + future.then((int v) { before = v; }); |
| + Expect.throws(() { future.value; }); |
| + Expect.isNull(before); |
| + completer.complete(3); |
| + |
| + Expect.equals(3, future.value); |
| + Expect.equals(3, before); |
| +} |
| + |
| +testCompleteWithHandlerAfterComplete() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + |
| + int after = null; |
| + completer.complete(3); |
| + Expect.equals(3, future.value); |
| + Expect.isNull(after); |
| + |
| + future.then((int v) { after = v; }); |
| + |
| + Expect.equals(3, future.value); |
| + Expect.equals(3, after); |
| +} |
| + |
| +testCompleteManyHandlers() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + int after1 = null; |
| + int after2 = null; |
| + int after3 = null; |
| + |
| + future.then((int v) { after1 = v; }); |
| + completer.complete(3); |
| + future.then((int v) { after2 = v; }); |
| + future.then((int v) { after3 = v; }); |
| + |
| + Expect.equals(3, future.value); |
| + Expect.equals(3, after1); |
| + Expect.equals(3, after2); |
| + Expect.equals(3, after3); |
| +} |
| + |
| +// Tests for [handleException] |
| + |
| +testException() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + future.then((_) {}); // exception is thrown if we plan to use the value |
| + Expect.throws( |
| + () { completer.completeException(ex); }, |
| + check: (e) => e == ex); |
| +} |
| + |
| +testExceptionNoListeners() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + completer.completeException(ex); // future.then is not called, so no exception |
| +} |
| + |
| +testExceptionHandler() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + |
| + var ex2 = null; |
| + future.handleException((e) { ex2 = e; return true; }); |
| + completer.completeException(ex); |
| + Expect.equals(ex, ex2); |
| +} |
| + |
| +testExceptionHandlerReturnsTrue() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + |
| + bool reached = false; |
| + future.handleException((e) { return true; }); |
| + future.handleException((e) { reached = true; return false; }); // overshadowed |
| + completer.completeException(ex); |
| + Expect.isFalse(reached); |
|
mattsh
2012/02/14 04:21:10
good test
Siggi Cherem (dart-lang)
2012/02/14 16:12:17
thx
|
| +} |
| + |
| +testExceptionHandlerReturnsTrue2() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + |
| + bool reached = false; |
| + future.handleException((e) { return false; }); |
| + future.handleException((e) { reached = true; return true; }); |
| + completer.completeException(ex); |
| + Expect.isTrue(reached); |
| +} |
| + |
| +testExceptionHandlerReturnsFalse() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + |
| + bool reached = false; |
| + future.then((_) {}); // ensure exception is thrown... |
| + future.handleException((e) { return false; }); |
| + future.handleException((e) { reached = true; return false; }); // overshadowed |
| + Expect.throws( |
| + () { completer.completeException(ex); }, |
| + check: (e) => e == ex); |
| + Expect.isTrue(reached); |
| +} |
| + |
| +testExceptionHandlerReturnsFalse2() { |
| + final completer = new Completer<int>(); |
| + final future = completer.future; |
| + final ex = new Exception(); |
| + |
| + bool reached = false; |
| + future.handleException((e) { return false; }); |
| + future.handleException((e) { reached = true; return false; }); // overshadowed |
| + completer.completeException(ex); // future.then is not called, so no exception |
| + Expect.isTrue(reached); |
| +} |
| + |
| +// Tests for Future.transform |
| + |
| testTransformSuccess() { |
| final completer = new Completer<String>(); |
| final transformedFuture = completer.future.transform((x) => "** $x **"); |
| @@ -38,6 +196,8 @@ testTransformTransformerFails() { |
| Expect.equals(error, transformedFuture.exception); |
| } |
| +// Tests for Future.chain |
| + |
| testChainSuccess() { |
| final completerA = new Completer<String>(); |
| final completerB = new Completer<String>(); |
| @@ -92,6 +252,17 @@ testChainSecondFutureFails() { |
| main() { |
| testImmediate(); |
| + testNeverComplete(); |
| + testComplete(); |
| + testCompleteWithHandlerBeforeComplete(); |
| + testCompleteWithHandlerAfterComplete(); |
| + testCompleteManyHandlers(); |
| + testException(); |
| + testExceptionHandler(); |
| + testExceptionHandlerReturnsTrue(); |
| + testExceptionHandlerReturnsTrue2(); |
| + testExceptionHandlerReturnsFalse(); |
| + testExceptionHandlerReturnsFalse2(); |
| testTransformSuccess(); |
| testTransformFutureFails(); |
| testTransformTransformerFails(); |
| @@ -99,4 +270,4 @@ main() { |
| testChainFirstFutureFails(); |
| testChainTransformerFails(); |
| testChainSecondFutureFails(); |
| -} |
| +} |