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/stream_subscription.dart"; |
| 9 import "package:test/test.dart"; |
| 10 |
| 11 import '../utils.dart'; |
| 12 |
| 13 void main() { |
| 14 group("with valid types, forwards", () { |
| 15 var controller; |
| 16 var wrapper; |
| 17 var isCanceled; |
| 18 setUp(() { |
| 19 controller = new StreamController<Object>(onCancel: () { |
| 20 isCanceled = true; |
| 21 }); |
| 22 wrapper = new TypeSafeStreamSubscription<int>( |
| 23 controller.stream.listen(null)); |
| 24 }); |
| 25 |
| 26 test("onData()", () { |
| 27 wrapper.onData(expectAsync((data) { |
| 28 expect(data, equals(1)); |
| 29 })); |
| 30 controller.add(1); |
| 31 }); |
| 32 |
| 33 test("onError()", () { |
| 34 wrapper.onError(expectAsync((error) { |
| 35 expect(error, equals("oh no")); |
| 36 })); |
| 37 controller.addError("oh no"); |
| 38 }); |
| 39 |
| 40 test("onDone()", () { |
| 41 wrapper.onDone(expectAsync(() {})); |
| 42 controller.close(); |
| 43 }); |
| 44 |
| 45 test("pause(), resume(), and isPaused", () async { |
| 46 expect(wrapper.isPaused, isFalse); |
| 47 |
| 48 wrapper.pause(); |
| 49 await flushMicrotasks(); |
| 50 expect(controller.isPaused, isTrue); |
| 51 expect(wrapper.isPaused, isTrue); |
| 52 |
| 53 wrapper.resume(); |
| 54 await flushMicrotasks(); |
| 55 expect(controller.isPaused, isFalse); |
| 56 expect(wrapper.isPaused, isFalse); |
| 57 }); |
| 58 |
| 59 test("cancel()", () async { |
| 60 wrapper.cancel(); |
| 61 await flushMicrotasks(); |
| 62 expect(isCanceled, isTrue); |
| 63 }); |
| 64 |
| 65 test("asFuture()", () { |
| 66 expect(wrapper.asFuture(12), completion(equals(12))); |
| 67 controller.close(); |
| 68 }); |
| 69 }); |
| 70 |
| 71 group("with invalid types,", () { |
| 72 var controller; |
| 73 var wrapper; |
| 74 var isCanceled; |
| 75 setUp(() { |
| 76 controller = new StreamController<Object>(onCancel: () { |
| 77 isCanceled = true; |
| 78 }); |
| 79 wrapper = new TypeSafeStreamSubscription<int>( |
| 80 controller.stream.listen(null)); |
| 81 }); |
| 82 |
| 83 group("throws a CastError for", () { |
| 84 test("onData()", () { |
| 85 expect(() { |
| 86 // TODO(nweiz): Use the wrapper declared in setUp when sdk#26226 is |
| 87 // fixed. |
| 88 controller = new StreamController<Object>(); |
| 89 wrapper = new TypeSafeStreamSubscription<int>( |
| 90 controller.stream.listen(null)); |
| 91 |
| 92 wrapper.onData(expectAsync((_) {}, count: 0)); |
| 93 controller.add("foo"); |
| 94 }, throwsZonedCastError); |
| 95 }); |
| 96 }); |
| 97 |
| 98 group("doesn't throw a CastError for", () { |
| 99 test("onError()", () { |
| 100 wrapper.onError(expectAsync((error) { |
| 101 expect(error, equals("oh no")); |
| 102 })); |
| 103 controller.add("foo"); |
| 104 controller.addError("oh no"); |
| 105 }); |
| 106 |
| 107 test("onDone()", () { |
| 108 wrapper.onDone(expectAsync(() {})); |
| 109 controller.add("foo"); |
| 110 controller.close(); |
| 111 }); |
| 112 |
| 113 test("pause(), resume(), and isPaused", () async { |
| 114 controller.add("foo"); |
| 115 |
| 116 expect(wrapper.isPaused, isFalse); |
| 117 |
| 118 wrapper.pause(); |
| 119 await flushMicrotasks(); |
| 120 expect(controller.isPaused, isTrue); |
| 121 expect(wrapper.isPaused, isTrue); |
| 122 |
| 123 wrapper.resume(); |
| 124 await flushMicrotasks(); |
| 125 expect(controller.isPaused, isFalse); |
| 126 expect(wrapper.isPaused, isFalse); |
| 127 }); |
| 128 |
| 129 test("cancel()", () async { |
| 130 controller.add("foo"); |
| 131 |
| 132 wrapper.cancel(); |
| 133 await flushMicrotasks(); |
| 134 expect(isCanceled, isTrue); |
| 135 }); |
| 136 |
| 137 test("asFuture()", () { |
| 138 expect(wrapper.asFuture(12), completion(equals(12))); |
| 139 controller.add("foo"); |
| 140 controller.close(); |
| 141 }); |
| 142 }); |
| 143 }); |
| 144 } |
OLD | NEW |