OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import "package:async/async.dart"; |
| 8 import "package:async/src/typed/future.dart"; |
| 9 import "package:test/test.dart"; |
| 10 |
| 11 import '../utils.dart'; |
| 12 |
| 13 void main() { |
| 14 group("with valid types, forwards", () { |
| 15 var wrapper; |
| 16 var errorWrapper; |
| 17 setUp(() { |
| 18 wrapper = new TypeSafeFuture<int>(new Future<Object>.value(12)); |
| 19 |
| 20 var error = new Future<Object>.error("oh no"); |
| 21 error.catchError((_) {}); // Don't let the error cause the test to fail. |
| 22 errorWrapper = new TypeSafeFuture<int>(error); |
| 23 }); |
| 24 |
| 25 test("asStream()", () { |
| 26 expect(wrapper.asStream().toList(), completion(equals([12]))); |
| 27 expect(errorWrapper.asStream().first, throwsA("oh no")); |
| 28 }); |
| 29 |
| 30 test("catchError()", () { |
| 31 expect( |
| 32 wrapper.catchError(expectAsync((_) {}, count: 0), |
| 33 test: expectAsync((_) {}, count: 0)), |
| 34 completion(equals(12))); |
| 35 |
| 36 expect(errorWrapper.catchError(expectAsync((error) { |
| 37 expect(error, equals("oh no")); |
| 38 return "value"; |
| 39 }), test: expectAsync((error) { |
| 40 expect(error, equals("oh no")); |
| 41 return true; |
| 42 })), completion(equals("value"))); |
| 43 }); |
| 44 |
| 45 test("then()", () { |
| 46 expect(wrapper.then((value) => value.toString()), |
| 47 completion(equals("12"))); |
| 48 expect(errorWrapper.then(expectAsync((_) {}, count: 0)), |
| 49 throwsA("oh no")); |
| 50 }); |
| 51 |
| 52 test("whenComplete()", () { |
| 53 expect(wrapper.whenComplete(expectAsync(() {})), completion(equals(12))); |
| 54 expect(errorWrapper.whenComplete(expectAsync(() {})), throwsA("oh no")); |
| 55 }); |
| 56 |
| 57 test("timeout()", () { |
| 58 expect(wrapper.timeout(new Duration(seconds: 1)), completion(equals(12))); |
| 59 expect(errorWrapper.timeout(new Duration(seconds: 1)), throwsA("oh no")); |
| 60 |
| 61 expect( |
| 62 new TypeSafeFuture<int>(new Completer<Object>().future) |
| 63 .timeout(Duration.ZERO), |
| 64 throwsA(new isInstanceOf<TimeoutException>())); |
| 65 |
| 66 expect( |
| 67 new TypeSafeFuture<int>(new Completer<Object>().future) |
| 68 .timeout(Duration.ZERO, onTimeout: expectAsync(() => 15)), |
| 69 completion(equals(15))); |
| 70 }); |
| 71 }); |
| 72 |
| 73 group("with invalid types", () { |
| 74 var wrapper; |
| 75 setUp(() { |
| 76 wrapper = new TypeSafeFuture<int>(new Future<Object>.value("foo")); |
| 77 }); |
| 78 |
| 79 group("throws a CastError for", () { |
| 80 test("asStream()", () { |
| 81 expect(wrapper.asStream().first, throwsCastError); |
| 82 }); |
| 83 |
| 84 test("then()", () { |
| 85 expect( |
| 86 wrapper.then(expectAsync((_) {}, count: 0), |
| 87 onError: expectAsync((_) {}, count: 0)), |
| 88 throwsCastError); |
| 89 }); |
| 90 |
| 91 test("whenComplete()", () { |
| 92 expect(wrapper.whenComplete(expectAsync(() {})).then((_) {}), |
| 93 throwsCastError); |
| 94 }); |
| 95 |
| 96 test("timeout()", () { |
| 97 expect(wrapper.timeout(new Duration(seconds: 3)).then((_) {}), |
| 98 throwsCastError); |
| 99 |
| 100 expect( |
| 101 new TypeSafeFuture<int>(new Completer<Object>().future) |
| 102 .timeout(Duration.ZERO, onTimeout: expectAsync(() => "foo")) |
| 103 .then((_) {}), |
| 104 throwsCastError); |
| 105 }); |
| 106 }); |
| 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 }); |
| 117 } |
OLD | NEW |