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 908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 new Future<int>.microtask(() => value)); | 919 new Future<int>.microtask(() => value)); |
920 testType("Future.sync($value)", new Future<int>.sync(() => value)); /// 01:
ok | 920 testType("Future.sync($value)", new Future<int>.sync(() => value)); /// 01:
ok |
921 testType("Future.sync(future($value))", /// 01:
continued | 921 testType("Future.sync(future($value))", /// 01:
continued |
922 new Future<int>.sync(() async => new Future.value(value))); /// 01:
continued | 922 new Future<int>.sync(() async => new Future.value(value))); /// 01:
continued |
923 testType("Future.value($value)", new Future<int>.value(value)); | 923 testType("Future.value($value)", new Future<int>.value(value)); |
924 } | 924 } |
925 testType("Completer.future", new Completer<int>().future); | 925 testType("Completer.future", new Completer<int>().future); |
926 testType("Future.error", new Future<int>.error("ERR")..catchError((_){})); | 926 testType("Future.error", new Future<int>.error("ERR")..catchError((_){})); |
927 } | 927 } |
928 | 928 |
| 929 void testAnyValue() { |
| 930 asyncStart(); |
| 931 var cs = new List.generate(3, (_) => new Completer()); |
| 932 var result = Future.any(cs.map((x) => x.future)); |
| 933 |
| 934 result.then((v) { |
| 935 Expect.equals(42, v); |
| 936 asyncEnd(); |
| 937 }, onError: (e, s) { |
| 938 Expect.fail("Unexpected error: $e"); |
| 939 }); |
| 940 |
| 941 cs[1].complete(42); |
| 942 cs[2].complete(10); |
| 943 cs[0].complete(20); |
| 944 } |
| 945 |
| 946 void testAnyError() { |
| 947 asyncStart(); |
| 948 var cs = new List.generate(3, (_) => new Completer()); |
| 949 var result = Future.any(cs.map((x) => x.future)); |
| 950 |
| 951 result.then((v) { |
| 952 Expect.fail("Unexpected value: $v"); |
| 953 }, onError: (e, s) { |
| 954 Expect.equals(42, e); |
| 955 asyncEnd(); |
| 956 }); |
| 957 |
| 958 cs[1].completeError(42); |
| 959 cs[2].complete(10); |
| 960 cs[0].complete(20); |
| 961 } |
| 962 |
| 963 void testAnyIgnoreIncomplete() { |
| 964 asyncStart(); |
| 965 var cs = new List.generate(3, (_) => new Completer()); |
| 966 var result = Future.any(cs.map((x) => x.future)); |
| 967 |
| 968 result.then((v) { |
| 969 Expect.equals(42, v); |
| 970 asyncEnd(); |
| 971 }, onError: (e, s) { |
| 972 Expect.fail("Unexpected error: $e"); |
| 973 }); |
| 974 |
| 975 cs[1].complete(42); |
| 976 // The other two futures never complete. |
| 977 } |
| 978 |
| 979 void testAnyIgnoreError() { |
| 980 asyncStart(); |
| 981 var cs = new List.generate(3, (_) => new Completer()); |
| 982 var result = Future.any(cs.map((x) => x.future)); |
| 983 |
| 984 result.then((v) { |
| 985 Expect.equals(42, v); |
| 986 asyncEnd(); |
| 987 }, onError: (e, s) { |
| 988 Expect.fail("Unexpected error: $e"); |
| 989 }); |
| 990 |
| 991 cs[1].complete(42); |
| 992 // The errors are ignored, not uncaught. |
| 993 cs[2].completeError("BAD"); |
| 994 cs[0].completeError("BAD"); |
| 995 } |
| 996 |
929 main() { | 997 main() { |
930 asyncStart(); | 998 asyncStart(); |
931 | 999 |
932 testValue(); | 1000 testValue(); |
933 testSync(); | 1001 testSync(); |
934 testNeverComplete(); | 1002 testNeverComplete(); |
935 | 1003 |
936 testComplete(); | 1004 testComplete(); |
937 testCompleteWithSuccessHandlerBeforeComplete(); | 1005 testCompleteWithSuccessHandlerBeforeComplete(); |
938 testCompleteWithSuccessHandlerAfterComplete(); | 1006 testCompleteWithSuccessHandlerAfterComplete(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 | 1050 |
983 testSyncFuture_i13368(); | 1051 testSyncFuture_i13368(); |
984 | 1052 |
985 testWaitCleanUp(); | 1053 testWaitCleanUp(); |
986 testWaitCleanUpError(); | 1054 testWaitCleanUpError(); |
987 | 1055 |
988 testBadFuture(); | 1056 testBadFuture(); |
989 | 1057 |
990 testTypes(); | 1058 testTypes(); |
991 | 1059 |
| 1060 testAnyValue(); |
| 1061 testAnyError(); |
| 1062 testAnyIgnoreIncomplete(); |
| 1063 testAnyIgnoreError(); |
| 1064 |
992 asyncEnd(); | 1065 asyncEnd(); |
993 } | 1066 } |
994 | 1067 |
995 /// A Future that isn't recognizable as a _Future. | 1068 /// A Future that isn't recognizable as a _Future. |
996 class CustomFuture<T> implements Future<T> { | 1069 class CustomFuture<T> implements Future<T> { |
997 Future _realFuture; | 1070 Future _realFuture; |
998 CustomFuture(this._realFuture); | 1071 CustomFuture(this._realFuture); |
999 Future then(action(result), {Function onError}) => | 1072 Future then(action(result), {Function onError}) => |
1000 _realFuture.then(action, onError: onError); | 1073 _realFuture.then(action, onError: onError); |
1001 Future catchError(Function onError, {bool test(e)}) => | 1074 Future catchError(Function onError, {bool test(e)}) => |
(...skipping 16 matching lines...) Expand all Loading... |
1018 Future whenComplete(action()) { | 1091 Future whenComplete(action()) { |
1019 throw "finally GOTCHA!"; | 1092 throw "finally GOTCHA!"; |
1020 } | 1093 } |
1021 Stream<T> asStream() { | 1094 Stream<T> asStream() { |
1022 throw "asStream GOTCHA!"; | 1095 throw "asStream GOTCHA!"; |
1023 } | 1096 } |
1024 Future timeout(Duration duration, {onTimeout()}) { | 1097 Future timeout(Duration duration, {onTimeout()}) { |
1025 throw "timeout GOTCHA!"; | 1098 throw "timeout GOTCHA!"; |
1026 } | 1099 } |
1027 } | 1100 } |
OLD | NEW |