OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import "package:async/async.dart"; | |
8 import "package:async/src/typed/future.dart"; | 7 import "package:async/src/typed/future.dart"; |
9 import "package:test/test.dart"; | 8 import "package:test/test.dart"; |
10 | 9 |
11 import '../utils.dart'; | 10 import '../utils.dart'; |
12 | 11 |
13 void main() { | 12 void main() { |
14 group("with valid types, forwards", () { | 13 group("with valid types, forwards", () { |
15 var wrapper; | 14 var wrapper; |
16 var errorWrapper; | 15 TypeSafeFuture<int> errorWrapper; |
17 setUp(() { | 16 setUp(() { |
18 wrapper = new TypeSafeFuture<int>(new Future<Object>.value(12)); | 17 wrapper = new TypeSafeFuture<int>(new Future<Object>.value(12)); |
19 | 18 |
20 var error = new Future<Object>.error("oh no"); | 19 var error = new Future<Object>.error("oh no"); |
21 error.catchError((_) {}); // Don't let the error cause the test to fail. | 20 error.catchError((_) {}); // Don't let the error cause the test to fail. |
22 errorWrapper = new TypeSafeFuture<int>(error); | 21 errorWrapper = new TypeSafeFuture<int>(error); |
23 }); | 22 }); |
24 | 23 |
25 test("asStream()", () { | 24 test("asStream()", () { |
26 expect(wrapper.asStream().toList(), completion(equals([12]))); | 25 expect(wrapper.asStream().toList(), completion(equals([12]))); |
27 expect(errorWrapper.asStream().first, throwsA("oh no")); | 26 expect(errorWrapper.asStream().first, throwsA("oh no")); |
28 }); | 27 }); |
29 | 28 |
30 test("catchError()", () { | 29 test("catchError()", () { |
31 expect( | 30 expect( |
32 wrapper.catchError(expectAsync((_) {}, count: 0), | 31 wrapper.catchError(expectAsync((_) {}, count: 0), |
33 test: expectAsync((_) {}, count: 0)), | 32 test: expectAsync((_) {}, count: 0)), |
34 completion(equals(12))); | 33 completion(equals(12))); |
35 | 34 |
36 expect(errorWrapper.catchError(expectAsync((error) { | 35 expect(errorWrapper.catchError(expectAsync((error) { |
37 expect(error, equals("oh no")); | 36 expect(error, equals("oh no")); |
38 return "value"; | 37 return 42; |
39 }), test: expectAsync((error) { | 38 }), test: expectAsync((error) { |
40 expect(error, equals("oh no")); | 39 expect(error, equals("oh no")); |
41 return true; | 40 return true; |
42 })), completion(equals("value"))); | 41 })), completion(equals(42))); |
43 }); | 42 }); |
44 | 43 |
45 test("then()", () { | 44 test("then()", () { |
46 expect(wrapper.then((value) => value.toString()), | 45 expect(wrapper.then((value) => value.toString()), |
47 completion(equals("12"))); | 46 completion(equals("12"))); |
48 expect(errorWrapper.then(expectAsync((_) {}, count: 0)), | 47 expect(errorWrapper.then(expectAsync((_) {}, count: 0)), |
49 throwsA("oh no")); | 48 throwsA("oh no")); |
50 }); | 49 }); |
51 | 50 |
52 test("whenComplete()", () { | 51 test("whenComplete()", () { |
(...skipping 11 matching lines...) Expand all Loading... |
64 throwsA(new isInstanceOf<TimeoutException>())); | 63 throwsA(new isInstanceOf<TimeoutException>())); |
65 | 64 |
66 expect( | 65 expect( |
67 new TypeSafeFuture<int>(new Completer<Object>().future) | 66 new TypeSafeFuture<int>(new Completer<Object>().future) |
68 .timeout(Duration.ZERO, onTimeout: expectAsync(() => 15)), | 67 .timeout(Duration.ZERO, onTimeout: expectAsync(() => 15)), |
69 completion(equals(15))); | 68 completion(equals(15))); |
70 }); | 69 }); |
71 }); | 70 }); |
72 | 71 |
73 group("with invalid types", () { | 72 group("with invalid types", () { |
74 var wrapper; | 73 TypeSafeFuture<int> wrapper; |
75 setUp(() { | 74 setUp(() { |
76 wrapper = new TypeSafeFuture<int>(new Future<Object>.value("foo")); | 75 wrapper = new TypeSafeFuture<int>(new Future<Object>.value("foo")); |
77 }); | 76 }); |
78 | 77 |
79 group("throws a CastError for", () { | 78 group("throws a CastError for", () { |
80 test("asStream()", () { | 79 test("asStream()", () { |
81 expect(wrapper.asStream().first, throwsCastError); | 80 expect(wrapper.asStream().first, throwsCastError); |
82 }); | 81 }); |
83 | 82 |
84 test("then()", () { | 83 test("then()", () { |
(...skipping 12 matching lines...) Expand all Loading... |
97 expect(wrapper.timeout(new Duration(seconds: 3)).then((_) {}), | 96 expect(wrapper.timeout(new Duration(seconds: 3)).then((_) {}), |
98 throwsCastError); | 97 throwsCastError); |
99 | 98 |
100 expect( | 99 expect( |
101 new TypeSafeFuture<int>(new Completer<Object>().future) | 100 new TypeSafeFuture<int>(new Completer<Object>().future) |
102 .timeout(Duration.ZERO, onTimeout: expectAsync(() => "foo")) | 101 .timeout(Duration.ZERO, onTimeout: expectAsync(() => "foo")) |
103 .then((_) {}), | 102 .then((_) {}), |
104 throwsCastError); | 103 throwsCastError); |
105 }); | 104 }); |
106 }); | 105 }); |
107 | |
108 group("doesn't throw a CastError for", () { | |
109 test("catchError()", () { | |
110 // catchError has a Future<dynamic> return type, so even if there's no | |
111 // error we don't re-wrap the returned future. | |
112 expect(wrapper.catchError(expectAsync((_) {}, count: 0)), | |
113 completion(equals("foo"))); | |
114 }); | |
115 }); | |
116 }); | 106 }); |
117 } | 107 } |
OLD | NEW |