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