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 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| (...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 }, onError: (e, s) { | 987 }, onError: (e, s) { |
| 988 Expect.fail("Unexpected error: $e"); | 988 Expect.fail("Unexpected error: $e"); |
| 989 }); | 989 }); |
| 990 | 990 |
| 991 cs[1].complete(42); | 991 cs[1].complete(42); |
| 992 // The errors are ignored, not uncaught. | 992 // The errors are ignored, not uncaught. |
| 993 cs[2].completeError("BAD"); | 993 cs[2].completeError("BAD"); |
| 994 cs[0].completeError("BAD"); | 994 cs[0].completeError("BAD"); |
| 995 } | 995 } |
| 996 | 996 |
| 997 void testFutureResult() { | |
| 998 asyncStart(); | |
| 999 () async { | |
| 1000 var f = new Feature(5); | |
| 1001 f.then((v) { Expect.isTrue(v is Future); }); // Feature is designed that wa y. | |
|
floitsch
2016/05/10 15:39:51
long line.
Lasse Reichstein Nielsen
2016/05/11 10:19:37
Done.
| |
| 1002 var v = await f; | |
| 1003 // The static type of await is Flatten(static-type-of-expression), so it | |
| 1004 // suggests that it flattens. In practice it currently doesn't. | |
| 1005 // The specification doesn't say anything special, so v should be the | |
| 1006 // completion value of the Feature future which is a future. | |
| 1007 Expect.isTrue(v is Future); | |
| 1008 | |
| 1009 // This used to hit an assert in checked mode. | |
| 1010 // The CL adding this test changed the behavior to actually flatten the | |
| 1011 // the future returned by the then-callback. | |
| 1012 var w = new Future.value(42).then((_) => f); | |
| 1013 Expect.equals(42, await w); | |
| 1014 asyncEnd(); | |
| 1015 }(); | |
| 1016 } | |
| 1017 | |
| 997 main() { | 1018 main() { |
| 998 asyncStart(); | 1019 asyncStart(); |
| 999 | 1020 |
| 1000 testValue(); | 1021 testValue(); |
| 1001 testSync(); | 1022 testSync(); |
| 1002 testNeverComplete(); | 1023 testNeverComplete(); |
| 1003 | 1024 |
| 1004 testComplete(); | 1025 testComplete(); |
| 1005 testCompleteWithSuccessHandlerBeforeComplete(); | 1026 testCompleteWithSuccessHandlerBeforeComplete(); |
| 1006 testCompleteWithSuccessHandlerAfterComplete(); | 1027 testCompleteWithSuccessHandlerAfterComplete(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1055 | 1076 |
| 1056 testBadFuture(); | 1077 testBadFuture(); |
| 1057 | 1078 |
| 1058 testTypes(); | 1079 testTypes(); |
| 1059 | 1080 |
| 1060 testAnyValue(); | 1081 testAnyValue(); |
| 1061 testAnyError(); | 1082 testAnyError(); |
| 1062 testAnyIgnoreIncomplete(); | 1083 testAnyIgnoreIncomplete(); |
| 1063 testAnyIgnoreError(); | 1084 testAnyIgnoreError(); |
| 1064 | 1085 |
| 1086 testFutureResult(); | |
| 1087 | |
| 1065 asyncEnd(); | 1088 asyncEnd(); |
| 1066 } | 1089 } |
| 1067 | 1090 |
| 1068 /// A Future that isn't recognizable as a _Future. | 1091 /// A Future that isn't recognizable as a _Future. |
| 1069 class CustomFuture<T> implements Future<T> { | 1092 class CustomFuture<T> implements Future<T> { |
| 1070 Future _realFuture; | 1093 Future _realFuture; |
| 1071 CustomFuture(this._realFuture); | 1094 CustomFuture(this._realFuture); |
| 1072 Future then(action(result), {Function onError}) => | 1095 Future then(action(result), {Function onError}) => |
| 1073 _realFuture.then(action, onError: onError); | 1096 _realFuture.then(action, onError: onError); |
| 1074 Future catchError(Function onError, {bool test(e)}) => | 1097 Future catchError(Function onError, {bool test(e)}) => |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 1091 Future whenComplete(action()) { | 1114 Future whenComplete(action()) { |
| 1092 throw "finally GOTCHA!"; | 1115 throw "finally GOTCHA!"; |
| 1093 } | 1116 } |
| 1094 Stream<T> asStream() { | 1117 Stream<T> asStream() { |
| 1095 throw "asStream GOTCHA!"; | 1118 throw "asStream GOTCHA!"; |
| 1096 } | 1119 } |
| 1097 Future timeout(Duration duration, {onTimeout()}) { | 1120 Future timeout(Duration duration, {onTimeout()}) { |
| 1098 throw "timeout GOTCHA!"; | 1121 throw "timeout GOTCHA!"; |
| 1099 } | 1122 } |
| 1100 } | 1123 } |
| 1124 | |
| 1125 // Bad future that completes with another future. | |
| 1126 class Feature implements Future<dynamic> { | |
|
floitsch
2016/05/10 15:39:51
Please rename. It took me several attempts to read
Lasse Reichstein Nielsen
2016/05/11 10:19:37
Done. Now being clever in a more easily distinguis
| |
| 1127 final _result; | |
| 1128 Feature(int badness) : _result = badness == 0 ? 42 : new Feature(badness - 1); | |
| 1129 Future then(action(value), {onError(error, StackTrace stack)}) { | |
| 1130 var c = new Completer(); | |
| 1131 c.complete(new Future.microtask(() => action(_result))); | |
| 1132 return c.future; | |
| 1133 } | |
| 1134 Future catchError(onError, {test}) {} // Never an error. | |
| 1135 Future whenComplete(action()) { | |
| 1136 return new Future.microtask(action).then((_) => this); | |
| 1137 } | |
| 1138 } | |
| OLD | NEW |