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