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 864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 asyncEnd(); | 875 asyncEnd(); |
876 }); | 876 }); |
877 } | 877 } |
878 | 878 |
879 void testBadFuture() { | 879 void testBadFuture() { |
880 var bad = new BadFuture(); | 880 var bad = new BadFuture(); |
881 // Completing with bad future (then call throws) puts error in result. | 881 // Completing with bad future (then call throws) puts error in result. |
882 asyncStart(); | 882 asyncStart(); |
883 Completer completer = new Completer(); | 883 Completer completer = new Completer(); |
884 completer.complete(bad); | 884 completer.complete(bad); |
885 completer.future.then((_) { fail("unreachable"); }, | 885 completer.future.then((_) { Expect.fail("unreachable"); }, |
886 onError: (e, s) { | 886 onError: (e, s) { |
887 Expect.isTrue(completer.isCompleted); | 887 Expect.isTrue(completer.isCompleted); |
888 asyncEnd(); | 888 asyncEnd(); |
889 }); | 889 }); |
890 | 890 |
891 asyncStart(); | 891 asyncStart(); |
892 var f = new Future.value().then((_) => bad); | 892 var f = new Future.value().then((_) => bad); |
893 f.then((_) { fail("unreachable"); }, | 893 f.then((_) { Expect.fail("unreachable"); }, |
894 onError: (e, s) { | 894 onError: (e, s) { |
895 asyncEnd(); | 895 asyncEnd(); |
896 }); | 896 }); |
897 } | 897 } |
898 | 898 |
| 899 void testTypes() { |
| 900 // Test that future is a Future<int> and not something less precise. |
| 901 testType(name, future, [depth = 2]) { |
| 902 var desc = "$name${".whenComplete"*(2-depth)}"; |
| 903 Expect.isTrue(future is Future<int>, "$desc is Future<int>"); |
| 904 Expect.isFalse(future is Future<String>, "$desc is! Future<String>"); |
| 905 var stream = future.asStream(); |
| 906 Expect.isTrue(stream is Stream<int>, "$desc.asStream() is Stream<int>"); |
| 907 Expect.isFalse(stream is Stream<String>, |
| 908 "$desc.asStream() is! Stream<String>"); |
| 909 if (depth > 0) { |
| 910 testType(name, future.whenComplete((){}), depth - 1); |
| 911 } |
| 912 } |
| 913 for (var value in [42, null]) { |
| 914 testType("Future($value)", |
| 915 new Future<int>(() => value)); |
| 916 testType("Future.delayed($value)", |
| 917 new Future<int>.delayed(Duration.ZERO, () => value)); |
| 918 testType("Future.microtask($value)", |
| 919 new Future<int>.microtask(() => value)); |
| 920 testType("Future.sync($value)", new Future<int>.sync(() => value)); /// 01:
ok |
| 921 testType("Future.sync(future($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)); |
| 924 } |
| 925 testType("Completer.future", new Completer<int>().future); |
| 926 testType("Future.error", new Future<int>.error("ERR")..catchError((_){})); |
| 927 } |
| 928 |
899 main() { | 929 main() { |
900 asyncStart(); | 930 asyncStart(); |
901 | 931 |
902 testValue(); | 932 testValue(); |
903 testSync(); | 933 testSync(); |
904 testNeverComplete(); | 934 testNeverComplete(); |
905 | 935 |
906 testComplete(); | 936 testComplete(); |
907 testCompleteWithSuccessHandlerBeforeComplete(); | 937 testCompleteWithSuccessHandlerBeforeComplete(); |
908 testCompleteWithSuccessHandlerAfterComplete(); | 938 testCompleteWithSuccessHandlerAfterComplete(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 testChainedFutureValueDelay(); | 980 testChainedFutureValueDelay(); |
951 testChainedFutureError(); | 981 testChainedFutureError(); |
952 | 982 |
953 testSyncFuture_i13368(); | 983 testSyncFuture_i13368(); |
954 | 984 |
955 testWaitCleanUp(); | 985 testWaitCleanUp(); |
956 testWaitCleanUpError(); | 986 testWaitCleanUpError(); |
957 | 987 |
958 testBadFuture(); | 988 testBadFuture(); |
959 | 989 |
| 990 testTypes(); |
| 991 |
960 asyncEnd(); | 992 asyncEnd(); |
961 } | 993 } |
962 | 994 |
963 /// A Future that isn't recognizable as a _Future. | 995 /// A Future that isn't recognizable as a _Future. |
964 class CustomFuture<T> implements Future<T> { | 996 class CustomFuture<T> implements Future<T> { |
965 Future _realFuture; | 997 Future _realFuture; |
966 CustomFuture(this._realFuture); | 998 CustomFuture(this._realFuture); |
967 Future then(action(result), {Function onError}) => | 999 Future then(action(result), {Function onError}) => |
968 _realFuture.then(action, onError: onError); | 1000 _realFuture.then(action, onError: onError); |
969 Future catchError(Function onError, {bool test(e)}) => | 1001 Future catchError(Function onError, {bool test(e)}) => |
970 _realFuture.catchError(onError, test: test); | 1002 _realFuture.catchError(onError, test: test); |
971 Future whenComplete(action()) => _realFuture.whenComplete(action); | 1003 Future whenComplete(action()) => _realFuture.whenComplete(action); |
972 Future timeout(Duration timeLimit, {void onTimeout()}) => | 1004 Future timeout(Duration timeLimit, {void onTimeout()}) => |
973 _realFuture.timeout(timeLimit, onTimeout: onTimeout); | 1005 _realFuture.timeout(timeLimit, onTimeout: onTimeout); |
974 Stream asStream() => _realFuture.asStream(); | 1006 Stream asStream() => _realFuture.asStream(); |
975 String toString() => "CustomFuture@${_realFuture.hashCode}"; | 1007 String toString() => "CustomFuture@${_realFuture.hashCode}"; |
976 int get hashCode => _realFuture.hashCode; | 1008 int get hashCode => _realFuture.hashCode; |
977 } | 1009 } |
978 | 1010 |
979 class BadFuture<T> implements Future<T> { | 1011 class BadFuture<T> implements Future<T> { |
980 Future then(action(result)) { | 1012 Future then(action(T result), {Function onError}) { |
981 throw "then GOTCHA!"; | 1013 throw "then GOTCHA!"; |
982 } | 1014 } |
983 Future catchError(Function onError) { | 1015 Future catchError(Function onError, {bool test(e)}) { |
984 throw "catch GOTCHA!"; | 1016 throw "catch GOTCHA!"; |
985 } | 1017 } |
986 Future whenComplete(action()) { | 1018 Future whenComplete(action()) { |
987 throw "finally GOTCHA!"; | 1019 throw "finally GOTCHA!"; |
988 } | 1020 } |
| 1021 Stream<T> asStream() { |
| 1022 throw "asStream GOTCHA!"; |
| 1023 } |
| 1024 Future timeout(Duration duration, {onTimeout()}) { |
| 1025 throw "timeout GOTCHA!"; |
| 1026 } |
989 } | 1027 } |
OLD | NEW |