| 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 UglyFuture(5); |
| 1001 // Sanity check that our future is as mis-behaved as we think. |
| 1002 f.then((v) { Expect.isTrue(v is Future); }); |
| 1003 |
| 1004 var v = await f; |
| 1005 // The static type of await is Flatten(static-type-of-expression), so it |
| 1006 // suggests that it flattens. In practice it currently doesn't. |
| 1007 // The specification doesn't say anything special, so v should be the |
| 1008 // completion value of the UglyFuture future which is a future. |
| 1009 Expect.isTrue(v is Future); |
| 1010 |
| 1011 // This used to hit an assert in checked mode. |
| 1012 // The CL adding this test changed the behavior to actually flatten the |
| 1013 // the future returned by the then-callback. |
| 1014 var w = new Future.value(42).then((_) => f); |
| 1015 Expect.equals(42, await w); |
| 1016 asyncEnd(); |
| 1017 }(); |
| 1018 } |
| 1019 |
| 997 main() { | 1020 main() { |
| 998 asyncStart(); | 1021 asyncStart(); |
| 999 | 1022 |
| 1000 testValue(); | 1023 testValue(); |
| 1001 testSync(); | 1024 testSync(); |
| 1002 testNeverComplete(); | 1025 testNeverComplete(); |
| 1003 | 1026 |
| 1004 testComplete(); | 1027 testComplete(); |
| 1005 testCompleteWithSuccessHandlerBeforeComplete(); | 1028 testCompleteWithSuccessHandlerBeforeComplete(); |
| 1006 testCompleteWithSuccessHandlerAfterComplete(); | 1029 testCompleteWithSuccessHandlerAfterComplete(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 | 1078 |
| 1056 testBadFuture(); | 1079 testBadFuture(); |
| 1057 | 1080 |
| 1058 testTypes(); | 1081 testTypes(); |
| 1059 | 1082 |
| 1060 testAnyValue(); | 1083 testAnyValue(); |
| 1061 testAnyError(); | 1084 testAnyError(); |
| 1062 testAnyIgnoreIncomplete(); | 1085 testAnyIgnoreIncomplete(); |
| 1063 testAnyIgnoreError(); | 1086 testAnyIgnoreError(); |
| 1064 | 1087 |
| 1088 testFutureResult(); |
| 1089 |
| 1065 asyncEnd(); | 1090 asyncEnd(); |
| 1066 } | 1091 } |
| 1067 | 1092 |
| 1068 /// A Future that isn't recognizable as a _Future. | 1093 /// A well-behaved Future that isn't recognizable as a _Future. |
| 1069 class CustomFuture<T> implements Future<T> { | 1094 class CustomFuture<T> implements Future<T> { |
| 1070 Future _realFuture; | 1095 Future _realFuture; |
| 1071 CustomFuture(this._realFuture); | 1096 CustomFuture(this._realFuture); |
| 1072 Future then(action(result), {Function onError}) => | 1097 Future then(action(result), {Function onError}) => |
| 1073 _realFuture.then(action, onError: onError); | 1098 _realFuture.then(action, onError: onError); |
| 1074 Future catchError(Function onError, {bool test(e)}) => | 1099 Future catchError(Function onError, {bool test(e)}) => |
| 1075 _realFuture.catchError(onError, test: test); | 1100 _realFuture.catchError(onError, test: test); |
| 1076 Future whenComplete(action()) => _realFuture.whenComplete(action); | 1101 Future whenComplete(action()) => _realFuture.whenComplete(action); |
| 1077 Future timeout(Duration timeLimit, {void onTimeout()}) => | 1102 Future timeout(Duration timeLimit, {void onTimeout()}) => |
| 1078 _realFuture.timeout(timeLimit, onTimeout: onTimeout); | 1103 _realFuture.timeout(timeLimit, onTimeout: onTimeout); |
| 1079 Stream asStream() => _realFuture.asStream(); | 1104 Stream asStream() => _realFuture.asStream(); |
| 1080 String toString() => "CustomFuture@${_realFuture.hashCode}"; | 1105 String toString() => "CustomFuture@${_realFuture.hashCode}"; |
| 1081 int get hashCode => _realFuture.hashCode; | 1106 int get hashCode => _realFuture.hashCode; |
| 1082 } | 1107 } |
| 1083 | 1108 |
| 1109 /// A bad future that throws on every method. |
| 1084 class BadFuture<T> implements Future<T> { | 1110 class BadFuture<T> implements Future<T> { |
| 1085 Future then(action(T result), {Function onError}) { | 1111 Future then(action(T result), {Function onError}) { |
| 1086 throw "then GOTCHA!"; | 1112 throw "then GOTCHA!"; |
| 1087 } | 1113 } |
| 1088 Future catchError(Function onError, {bool test(e)}) { | 1114 Future catchError(Function onError, {bool test(e)}) { |
| 1089 throw "catch GOTCHA!"; | 1115 throw "catch GOTCHA!"; |
| 1090 } | 1116 } |
| 1091 Future whenComplete(action()) { | 1117 Future whenComplete(action()) { |
| 1092 throw "finally GOTCHA!"; | 1118 throw "finally GOTCHA!"; |
| 1093 } | 1119 } |
| 1094 Stream<T> asStream() { | 1120 Stream<T> asStream() { |
| 1095 throw "asStream GOTCHA!"; | 1121 throw "asStream GOTCHA!"; |
| 1096 } | 1122 } |
| 1097 Future timeout(Duration duration, {onTimeout()}) { | 1123 Future timeout(Duration duration, {onTimeout()}) { |
| 1098 throw "timeout GOTCHA!"; | 1124 throw "timeout GOTCHA!"; |
| 1099 } | 1125 } |
| 1100 } | 1126 } |
| 1127 |
| 1128 // An evil future that completes with another future. |
| 1129 class UglyFuture implements Future<dynamic> { |
| 1130 final _result; |
| 1131 UglyFuture(int badness) : |
| 1132 _result = (badness == 0) ? 42 : new UglyFuture(badness - 1); |
| 1133 Future then(action(value), {onError(error, StackTrace stack)}) { |
| 1134 var c = new Completer(); |
| 1135 c.complete(new Future.microtask(() => action(_result))); |
| 1136 return c.future; |
| 1137 } |
| 1138 Future catchError(onError, {test}) {} // Never an error. |
| 1139 Future whenComplete(action()) { |
| 1140 return new Future.microtask(action).then((_) => this); |
| 1141 } |
| 1142 } |
| OLD | NEW |