OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Test the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
6 library stream_controller_test; | 6 library stream_controller_test; |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'event_helper.dart'; | 10 import 'event_helper.dart'; |
11 | 11 |
12 testMultiController() { | 12 testMultiController() { |
13 // Test normal flow. | 13 // Test normal flow. |
14 var c = new StreamController(); | 14 var c = new StreamController(sync: true); |
15 Events expectedEvents = new Events() | 15 Events expectedEvents = new Events() |
16 ..add(42) | 16 ..add(42) |
17 ..add("dibs") | 17 ..add("dibs") |
18 ..error("error!") | 18 ..error("error!") |
19 ..error("error too!") | 19 ..error("error too!") |
20 ..close(); | 20 ..close(); |
21 Events actualEvents = new Events.capture(c.stream.asBroadcastStream()); | 21 Events actualEvents = new Events.capture(c.stream.asBroadcastStream()); |
22 expectedEvents.replay(c); | 22 expectedEvents.replay(c); |
23 Expect.listEquals(expectedEvents.events, actualEvents.events); | 23 Expect.listEquals(expectedEvents.events, actualEvents.events); |
24 | 24 |
25 // Test automatic unsubscription on error. | 25 // Test automatic unsubscription on error. |
26 c = new StreamController(); | 26 c = new StreamController(sync: true); |
27 expectedEvents = new Events()..add(42)..error("error"); | 27 expectedEvents = new Events()..add(42)..error("error"); |
28 actualEvents = new Events.capture(c.stream.asBroadcastStream(), | 28 actualEvents = new Events.capture(c.stream.asBroadcastStream(), |
29 cancelOnError: true); | 29 cancelOnError: true); |
30 Events sentEvents = | 30 Events sentEvents = |
31 new Events()..add(42)..error("error")..add("Are you there?"); | 31 new Events()..add(42)..error("error")..add("Are you there?"); |
32 sentEvents.replay(c); | 32 sentEvents.replay(c); |
33 Expect.listEquals(expectedEvents.events, actualEvents.events); | 33 Expect.listEquals(expectedEvents.events, actualEvents.events); |
34 | 34 |
35 // Test manual unsubscription. | 35 // Test manual unsubscription. |
36 c = new StreamController(); | 36 c = new StreamController(sync: true); |
37 expectedEvents = new Events()..add(42)..error("error")..add(37); | 37 expectedEvents = new Events()..add(42)..error("error")..add(37); |
38 actualEvents = new Events.capture(c.stream.asBroadcastStream(), | 38 actualEvents = new Events.capture(c.stream.asBroadcastStream(), |
39 cancelOnError: false); | 39 cancelOnError: false); |
40 expectedEvents.replay(c); | 40 expectedEvents.replay(c); |
41 actualEvents.subscription.cancel(); | 41 actualEvents.subscription.cancel(); |
42 c.add("Are you there"); // Not sent to actualEvents. | 42 c.add("Are you there"); // Not sent to actualEvents. |
43 Expect.listEquals(expectedEvents.events, actualEvents.events); | 43 Expect.listEquals(expectedEvents.events, actualEvents.events); |
44 | 44 |
45 // Test filter. | 45 // Test filter. |
46 c = new StreamController(); | 46 c = new StreamController(sync: true); |
47 expectedEvents = new Events() | 47 expectedEvents = new Events() |
48 ..add("a string")..add("another string")..close(); | 48 ..add("a string")..add("another string")..close(); |
49 sentEvents = new Events() | 49 sentEvents = new Events() |
50 ..add("a string")..add(42)..add("another string")..close(); | 50 ..add("a string")..add(42)..add("another string")..close(); |
51 actualEvents = new Events.capture(c.stream | 51 actualEvents = new Events.capture(c.stream |
52 .asBroadcastStream() | 52 .asBroadcastStream() |
53 .where((v) => v is String)); | 53 .where((v) => v is String)); |
54 sentEvents.replay(c); | 54 sentEvents.replay(c); |
55 Expect.listEquals(expectedEvents.events, actualEvents.events); | 55 Expect.listEquals(expectedEvents.events, actualEvents.events); |
56 | 56 |
57 // Test map. | 57 // Test map. |
58 c = new StreamController(); | 58 c = new StreamController(sync: true); |
59 expectedEvents = new Events()..add("abab")..error("error")..close(); | 59 expectedEvents = new Events()..add("abab")..error("error")..close(); |
60 sentEvents = new Events()..add("ab")..error("error")..close(); | 60 sentEvents = new Events()..add("ab")..error("error")..close(); |
61 actualEvents = new Events.capture(c.stream | 61 actualEvents = new Events.capture(c.stream |
62 .asBroadcastStream() | 62 .asBroadcastStream() |
63 .map((v) => "$v$v")); | 63 .map((v) => "$v$v")); |
64 sentEvents.replay(c); | 64 sentEvents.replay(c); |
65 Expect.listEquals(expectedEvents.events, actualEvents.events); | 65 Expect.listEquals(expectedEvents.events, actualEvents.events); |
66 | 66 |
67 // Test handleError. | 67 // Test handleError. |
68 c = new StreamController(); | 68 c = new StreamController(sync: true); |
69 expectedEvents = new Events()..add("ab")..error("[foo]"); | 69 expectedEvents = new Events()..add("ab")..error("[foo]"); |
70 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 70 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
71 actualEvents = new Events.capture(c.stream | 71 actualEvents = new Events.capture(c.stream |
72 .asBroadcastStream() | 72 .asBroadcastStream() |
73 .handleError((error) { | 73 .handleError((error) { |
74 if (error is String) { | 74 if (error is String) { |
75 // TODO(floitsch): this test originally changed the stacktrace. | 75 // TODO(floitsch): this test originally changed the stacktrace. |
76 throw "[${error}]"; | 76 throw "[${error}]"; |
77 } | 77 } |
78 }), cancelOnError: true); | 78 }), cancelOnError: true); |
79 sentEvents.replay(c); | 79 sentEvents.replay(c); |
80 Expect.listEquals(expectedEvents.events, actualEvents.events); | 80 Expect.listEquals(expectedEvents.events, actualEvents.events); |
81 | 81 |
82 // reduce is tested asynchronously and therefore not in this file. | 82 // reduce is tested asynchronously and therefore not in this file. |
83 | 83 |
84 // Test expand | 84 // Test expand |
85 c = new StreamController(); | 85 c = new StreamController(sync: true); |
86 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 86 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
87 expectedEvents = new Events()..add(1)..add(2)..add(3) | 87 expectedEvents = new Events()..add(1)..add(2)..add(3) |
88 ..add(1)..add(2) | 88 ..add(1)..add(2) |
89 ..add(1)..add(2)..add(3)..add(4) | 89 ..add(1)..add(2)..add(3)..add(4) |
90 ..close(); | 90 ..close(); |
91 actualEvents = new Events.capture(c.stream.asBroadcastStream().expand((v) { | 91 actualEvents = new Events.capture(c.stream.asBroadcastStream().expand((v) { |
92 var l = []; | 92 var l = []; |
93 for (int i = 0; i < v; i++) l.add(i + 1); | 93 for (int i = 0; i < v; i++) l.add(i + 1); |
94 return l; | 94 return l; |
95 })); | 95 })); |
96 sentEvents.replay(c); | 96 sentEvents.replay(c); |
97 Expect.listEquals(expectedEvents.events, actualEvents.events); | 97 Expect.listEquals(expectedEvents.events, actualEvents.events); |
98 | 98 |
99 // Test transform. | 99 // Test transform. |
100 c = new StreamController(); | 100 c = new StreamController(sync: true); |
101 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); | 101 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); |
102 expectedEvents = | 102 expectedEvents = |
103 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); | 103 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); |
104 actualEvents = new Events.capture(c.stream.asBroadcastStream().transform( | 104 actualEvents = new Events.capture(c.stream.asBroadcastStream().transform( |
105 new StreamTransformer( | 105 new StreamTransformer( |
106 handleData: (v, s) { s.addError(v); }, | 106 handleData: (v, s) { s.addError(v); }, |
107 handleError: (e, s) { s.add(e); }, | 107 handleError: (e, s) { s.add(e); }, |
108 handleDone: (s) { | 108 handleDone: (s) { |
109 | 109 |
110 s.add("foo"); | 110 s.add("foo"); |
111 | 111 |
112 s.close(); | 112 s.close(); |
113 | 113 |
114 }))); | 114 }))); |
115 sentEvents.replay(c); | 115 sentEvents.replay(c); |
116 Expect.listEquals(expectedEvents.events, actualEvents.events); | 116 Expect.listEquals(expectedEvents.events, actualEvents.events); |
117 | 117 |
118 // Test multiple filters. | 118 // Test multiple filters. |
119 c = new StreamController(); | 119 c = new StreamController(sync: true); |
120 sentEvents = new Events()..add(42) | 120 sentEvents = new Events()..add(42) |
121 ..add("snugglefluffy") | 121 ..add("snugglefluffy") |
122 ..add(7) | 122 ..add(7) |
123 ..add("42") | 123 ..add("42") |
124 ..error("not FormatException") // Unsubscribes. | 124 ..error("not FormatException") // Unsubscribes. |
125 ..close(); | 125 ..close(); |
126 expectedEvents = new Events()..add(42)..error("not FormatException"); | 126 expectedEvents = new Events()..add(42)..error("not FormatException"); |
127 actualEvents = new Events.capture( | 127 actualEvents = new Events.capture( |
128 c.stream.asBroadcastStream().where((v) => v is String) | 128 c.stream.asBroadcastStream().where((v) => v is String) |
129 .map((v) => int.parse(v)) | 129 .map((v) => int.parse(v)) |
130 .handleError((error) { | 130 .handleError((error) { |
131 if (error is! FormatException) throw error; | 131 if (error is! FormatException) throw error; |
132 }) | 132 }) |
133 .where((v) => v > 10), | 133 .where((v) => v > 10), |
134 cancelOnError: true); | 134 cancelOnError: true); |
135 sentEvents.replay(c); | 135 sentEvents.replay(c); |
136 Expect.listEquals(expectedEvents.events, actualEvents.events); | 136 Expect.listEquals(expectedEvents.events, actualEvents.events); |
137 | 137 |
138 // Test subscription changes while firing. | 138 // Test subscription changes while firing. |
139 c = new StreamController(); | 139 c = new StreamController(sync: true); |
140 var sink = c.sink; | 140 var sink = c.sink; |
141 var stream = c.stream.asBroadcastStream(); | 141 var stream = c.stream.asBroadcastStream(); |
142 var counter = 0; | 142 var counter = 0; |
143 var subscription = stream.listen(null); | 143 var subscription = stream.listen(null); |
144 subscription.onData((data) { | 144 subscription.onData((data) { |
145 counter += data; | 145 counter += data; |
146 subscription.cancel(); | 146 subscription.cancel(); |
147 stream.listen((data) { | 147 stream.listen((data) { |
148 counter += 10 * data; | 148 counter += 10 * data; |
149 }); | 149 }); |
150 var subscription2 = stream.listen(null); | 150 var subscription2 = stream.listen(null); |
151 subscription2.onData((data) { | 151 subscription2.onData((data) { |
152 counter += 100 * data; | 152 counter += 100 * data; |
153 if (data == 4) subscription2.cancel(); | 153 if (data == 4) subscription2.cancel(); |
154 }); | 154 }); |
155 }); | 155 }); |
156 sink.add(1); // seen by stream 1 | 156 sink.add(1); // seen by stream 1 |
157 sink.add(2); // seen by stream 10 and 100 | 157 sink.add(2); // seen by stream 10 and 100 |
158 sink.add(3); // -"- | 158 sink.add(3); // -"- |
159 sink.add(4); // -"- | 159 sink.add(4); // -"- |
160 sink.add(5); // seen by stream 10 | 160 sink.add(5); // seen by stream 10 |
161 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter); | 161 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter); |
162 } | 162 } |
163 | 163 |
164 testSingleController() { | 164 testSingleController() { |
165 // Test normal flow. | 165 // Test normal flow. |
166 var c = new StreamController(); | 166 var c = new StreamController(sync: true); |
167 Events expectedEvents = new Events() | 167 Events expectedEvents = new Events() |
168 ..add(42) | 168 ..add(42) |
169 ..add("dibs") | 169 ..add("dibs") |
170 ..error("error!") | 170 ..error("error!") |
171 ..error("error too!") | 171 ..error("error too!") |
172 ..close(); | 172 ..close(); |
173 Events actualEvents = new Events.capture(c.stream); | 173 Events actualEvents = new Events.capture(c.stream); |
174 expectedEvents.replay(c); | 174 expectedEvents.replay(c); |
175 Expect.listEquals(expectedEvents.events, actualEvents.events); | 175 Expect.listEquals(expectedEvents.events, actualEvents.events); |
176 | 176 |
177 // Test automatic unsubscription on error. | 177 // Test automatic unsubscription on error. |
178 c = new StreamController(); | 178 c = new StreamController(sync: true); |
179 expectedEvents = new Events()..add(42)..error("error"); | 179 expectedEvents = new Events()..add(42)..error("error"); |
180 actualEvents = new Events.capture(c.stream, cancelOnError: true); | 180 actualEvents = new Events.capture(c.stream, cancelOnError: true); |
181 Events sentEvents = | 181 Events sentEvents = |
182 new Events()..add(42)..error("error")..add("Are you there?"); | 182 new Events()..add(42)..error("error")..add("Are you there?"); |
183 sentEvents.replay(c); | 183 sentEvents.replay(c); |
184 Expect.listEquals(expectedEvents.events, actualEvents.events); | 184 Expect.listEquals(expectedEvents.events, actualEvents.events); |
185 | 185 |
186 // Test manual unsubscription. | 186 // Test manual unsubscription. |
187 c = new StreamController(); | 187 c = new StreamController(sync: true); |
188 expectedEvents = new Events()..add(42)..error("error")..add(37); | 188 expectedEvents = new Events()..add(42)..error("error")..add(37); |
189 actualEvents = new Events.capture(c.stream, cancelOnError: false); | 189 actualEvents = new Events.capture(c.stream, cancelOnError: false); |
190 expectedEvents.replay(c); | 190 expectedEvents.replay(c); |
191 actualEvents.subscription.cancel(); | 191 actualEvents.subscription.cancel(); |
192 c.add("Are you there"); // Not sent to actualEvents. | 192 c.add("Are you there"); // Not sent to actualEvents. |
193 Expect.listEquals(expectedEvents.events, actualEvents.events); | 193 Expect.listEquals(expectedEvents.events, actualEvents.events); |
194 | 194 |
195 // Test filter. | 195 // Test filter. |
196 c = new StreamController(); | 196 c = new StreamController(sync: true); |
197 expectedEvents = new Events() | 197 expectedEvents = new Events() |
198 ..add("a string")..add("another string")..close(); | 198 ..add("a string")..add("another string")..close(); |
199 sentEvents = new Events() | 199 sentEvents = new Events() |
200 ..add("a string")..add(42)..add("another string")..close(); | 200 ..add("a string")..add(42)..add("another string")..close(); |
201 actualEvents = new Events.capture(c.stream.where((v) => v is String)); | 201 actualEvents = new Events.capture(c.stream.where((v) => v is String)); |
202 sentEvents.replay(c); | 202 sentEvents.replay(c); |
203 Expect.listEquals(expectedEvents.events, actualEvents.events); | 203 Expect.listEquals(expectedEvents.events, actualEvents.events); |
204 | 204 |
205 // Test map. | 205 // Test map. |
206 c = new StreamController(); | 206 c = new StreamController(sync: true); |
207 expectedEvents = new Events()..add("abab")..error("error")..close(); | 207 expectedEvents = new Events()..add("abab")..error("error")..close(); |
208 sentEvents = new Events()..add("ab")..error("error")..close(); | 208 sentEvents = new Events()..add("ab")..error("error")..close(); |
209 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); | 209 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); |
210 sentEvents.replay(c); | 210 sentEvents.replay(c); |
211 Expect.listEquals(expectedEvents.events, actualEvents.events); | 211 Expect.listEquals(expectedEvents.events, actualEvents.events); |
212 | 212 |
213 // Test handleError. | 213 // Test handleError. |
214 c = new StreamController(); | 214 c = new StreamController(sync: true); |
215 expectedEvents = new Events()..add("ab")..error("[foo]"); | 215 expectedEvents = new Events()..add("ab")..error("[foo]"); |
216 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 216 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
217 actualEvents = new Events.capture(c.stream.handleError((error) { | 217 actualEvents = new Events.capture(c.stream.handleError((error) { |
218 if (error is String) { | 218 if (error is String) { |
219 // TODO(floitsch): this error originally changed the stack trace. | 219 // TODO(floitsch): this error originally changed the stack trace. |
220 throw "[${error}]"; | 220 throw "[${error}]"; |
221 } | 221 } |
222 }), cancelOnError: true); | 222 }), cancelOnError: true); |
223 sentEvents.replay(c); | 223 sentEvents.replay(c); |
224 Expect.listEquals(expectedEvents.events, actualEvents.events); | 224 Expect.listEquals(expectedEvents.events, actualEvents.events); |
225 | 225 |
226 // reduce is tested asynchronously and therefore not in this file. | 226 // reduce is tested asynchronously and therefore not in this file. |
227 | 227 |
228 // Test expand | 228 // Test expand |
229 c = new StreamController(); | 229 c = new StreamController(sync: true); |
230 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 230 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
231 expectedEvents = new Events()..add(1)..add(2)..add(3) | 231 expectedEvents = new Events()..add(1)..add(2)..add(3) |
232 ..add(1)..add(2) | 232 ..add(1)..add(2) |
233 ..add(1)..add(2)..add(3)..add(4) | 233 ..add(1)..add(2)..add(3)..add(4) |
234 ..close(); | 234 ..close(); |
235 actualEvents = new Events.capture(c.stream.expand((v) { | 235 actualEvents = new Events.capture(c.stream.expand((v) { |
236 var l = []; | 236 var l = []; |
237 for (int i = 0; i < v; i++) l.add(i + 1); | 237 for (int i = 0; i < v; i++) l.add(i + 1); |
238 return l; | 238 return l; |
239 })); | 239 })); |
240 sentEvents.replay(c); | 240 sentEvents.replay(c); |
241 Expect.listEquals(expectedEvents.events, actualEvents.events); | 241 Expect.listEquals(expectedEvents.events, actualEvents.events); |
242 | 242 |
243 // test contains. | 243 // test contains. |
244 { | 244 { |
245 c = new StreamController(); | 245 c = new StreamController(sync: true); |
246 // Error after match is not important. | 246 // Error after match is not important. |
247 sentEvents = new Events()..add("a")..add("x")..error("FAIL")..close(); | 247 sentEvents = new Events()..add("a")..add("x")..error("FAIL")..close(); |
248 Future<bool> contains = c.stream.contains("x"); | 248 Future<bool> contains = c.stream.contains("x"); |
249 contains.then((var c) { | 249 contains.then((var c) { |
250 Expect.isTrue(c); | 250 Expect.isTrue(c); |
251 }); | 251 }); |
252 sentEvents.replay(c); | 252 sentEvents.replay(c); |
253 } | 253 } |
254 | 254 |
255 { | 255 { |
256 c = new StreamController(); | 256 c = new StreamController(sync: true); |
257 // Not matching is ok. | 257 // Not matching is ok. |
258 sentEvents = new Events()..add("a")..add("x")..add("b")..close(); | 258 sentEvents = new Events()..add("a")..add("x")..add("b")..close(); |
259 Future<bool> contains = c.stream.contains("y"); | 259 Future<bool> contains = c.stream.contains("y"); |
260 contains.then((var c) { | 260 contains.then((var c) { |
261 Expect.isFalse(c); | 261 Expect.isFalse(c); |
262 }); | 262 }); |
263 sentEvents.replay(c); | 263 sentEvents.replay(c); |
264 } | 264 } |
265 | 265 |
266 { | 266 { |
267 c = new StreamController(); | 267 c = new StreamController(sync: true); |
268 // Error before match makes future err. | 268 // Error before match makes future err. |
269 sentEvents = new Events()..add("a")..error("FAIL")..add("b")..close(); | 269 sentEvents = new Events()..add("a")..error("FAIL")..add("b")..close(); |
270 Future<bool> contains = c.stream.contains("b"); | 270 Future<bool> contains = c.stream.contains("b"); |
271 contains.then((var c) { | 271 contains.then((var c) { |
272 Expect.fail("no value expected"); | 272 Expect.fail("no value expected"); |
273 }).catchError((error) { | 273 }).catchError((error) { |
274 Expect.equals("FAIL", error); | 274 Expect.equals("FAIL", error); |
275 }); | 275 }); |
276 sentEvents.replay(c); | 276 sentEvents.replay(c); |
277 } | 277 } |
278 | 278 |
279 // Test transform. | 279 // Test transform. |
280 c = new StreamController(); | 280 c = new StreamController(sync: true); |
281 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); | 281 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); |
282 expectedEvents = | 282 expectedEvents = |
283 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); | 283 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); |
284 actualEvents = new Events.capture(c.stream.transform( | 284 actualEvents = new Events.capture(c.stream.transform( |
285 new StreamTransformer( | 285 new StreamTransformer( |
286 handleData: (v, s) { s.addError(v); }, | 286 handleData: (v, s) { s.addError(v); }, |
287 handleError: (e, s) { s.add(e); }, | 287 handleError: (e, s) { s.add(e); }, |
288 handleDone: (s) { | 288 handleDone: (s) { |
289 s.add("foo"); | 289 s.add("foo"); |
290 s.close(); | 290 s.close(); |
291 }))); | 291 }))); |
292 sentEvents.replay(c); | 292 sentEvents.replay(c); |
293 Expect.listEquals(expectedEvents.events, actualEvents.events); | 293 Expect.listEquals(expectedEvents.events, actualEvents.events); |
294 | 294 |
295 // Test multiple filters. | 295 // Test multiple filters. |
296 c = new StreamController(); | 296 c = new StreamController(sync: true); |
297 sentEvents = new Events()..add(42) | 297 sentEvents = new Events()..add(42) |
298 ..add("snugglefluffy") | 298 ..add("snugglefluffy") |
299 ..add(7) | 299 ..add(7) |
300 ..add("42") | 300 ..add("42") |
301 ..error("not FormatException") // Unsubscribes. | 301 ..error("not FormatException") // Unsubscribes. |
302 ..close(); | 302 ..close(); |
303 expectedEvents = new Events()..add(42)..error("not FormatException"); | 303 expectedEvents = new Events()..add(42)..error("not FormatException"); |
304 actualEvents = new Events.capture( | 304 actualEvents = new Events.capture( |
305 c.stream.where((v) => v is String) | 305 c.stream.where((v) => v is String) |
306 .map((v) => int.parse(v)) | 306 .map((v) => int.parse(v)) |
307 .handleError((error) { | 307 .handleError((error) { |
308 if (error is! FormatException) throw error; | 308 if (error is! FormatException) throw error; |
309 }) | 309 }) |
310 .where((v) => v > 10), | 310 .where((v) => v > 10), |
311 cancelOnError: true); | 311 cancelOnError: true); |
312 sentEvents.replay(c); | 312 sentEvents.replay(c); |
313 Expect.listEquals(expectedEvents.events, actualEvents.events); | 313 Expect.listEquals(expectedEvents.events, actualEvents.events); |
314 | 314 |
315 // Test that only one subscription is allowed. | 315 // Test that only one subscription is allowed. |
316 c = new StreamController(); | 316 c = new StreamController(sync: true); |
317 var sink = c.sink; | 317 var sink = c.sink; |
318 var stream = c.stream; | 318 var stream = c.stream; |
319 var counter = 0; | 319 var counter = 0; |
320 var subscription = stream.listen((data) { counter += data; }); | 320 var subscription = stream.listen((data) { counter += data; }); |
321 Expect.throws(() => stream.listen(null), (e) => e is StateError); | 321 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
322 sink.add(1); | 322 sink.add(1); |
323 Expect.equals(1, counter); | 323 Expect.equals(1, counter); |
324 c.close(); | 324 c.close(); |
325 } | 325 } |
326 | 326 |
327 testExtraMethods() { | 327 testExtraMethods() { |
328 Events sentEvents = new Events()..add(1)..add(2)..add(3)..close(); | 328 Events sentEvents = new Events()..add(1)..add(2)..add(3)..close(); |
329 | 329 |
330 var c = new StreamController(); | 330 var c = new StreamController(sync: true); |
331 Events expectedEvents = new Events()..add(3)..close(); | 331 Events expectedEvents = new Events()..add(3)..close(); |
332 Events actualEvents = new Events.capture(c.stream.skip(2)); | 332 Events actualEvents = new Events.capture(c.stream.skip(2)); |
333 sentEvents.replay(c); | 333 sentEvents.replay(c); |
334 Expect.listEquals(expectedEvents.events, actualEvents.events); | 334 Expect.listEquals(expectedEvents.events, actualEvents.events); |
335 | 335 |
336 c = new StreamController(); | 336 c = new StreamController(sync: true); |
337 expectedEvents = new Events()..close(); | 337 expectedEvents = new Events()..close(); |
338 actualEvents = new Events.capture(c.stream.skip(3)); | 338 actualEvents = new Events.capture(c.stream.skip(3)); |
339 sentEvents.replay(c); | 339 sentEvents.replay(c); |
340 Expect.listEquals(expectedEvents.events, actualEvents.events); | 340 Expect.listEquals(expectedEvents.events, actualEvents.events); |
341 | 341 |
342 c = new StreamController(); | 342 c = new StreamController(sync: true); |
343 expectedEvents = new Events()..close(); | 343 expectedEvents = new Events()..close(); |
344 actualEvents = new Events.capture(c.stream.skip(7)); | 344 actualEvents = new Events.capture(c.stream.skip(7)); |
345 sentEvents.replay(c); | 345 sentEvents.replay(c); |
346 Expect.listEquals(expectedEvents.events, actualEvents.events); | 346 Expect.listEquals(expectedEvents.events, actualEvents.events); |
347 | 347 |
348 c = new StreamController(); | 348 c = new StreamController(sync: true); |
349 expectedEvents = sentEvents; | 349 expectedEvents = sentEvents; |
350 actualEvents = new Events.capture(c.stream.skip(0)); | 350 actualEvents = new Events.capture(c.stream.skip(0)); |
351 sentEvents.replay(c); | 351 sentEvents.replay(c); |
352 Expect.listEquals(expectedEvents.events, actualEvents.events); | 352 Expect.listEquals(expectedEvents.events, actualEvents.events); |
353 | 353 |
354 | 354 |
355 c = new StreamController(); | 355 c = new StreamController(sync: true); |
356 expectedEvents = new Events()..add(3)..close(); | 356 expectedEvents = new Events()..add(3)..close(); |
357 actualEvents = new Events.capture(c.stream.skipWhile((x) => x <= 2)); | 357 actualEvents = new Events.capture(c.stream.skipWhile((x) => x <= 2)); |
358 sentEvents.replay(c); | 358 sentEvents.replay(c); |
359 Expect.listEquals(expectedEvents.events, actualEvents.events); | 359 Expect.listEquals(expectedEvents.events, actualEvents.events); |
360 | 360 |
361 | 361 |
362 c = new StreamController(); | 362 c = new StreamController(sync: true); |
363 expectedEvents = new Events()..add(2)..add(3)..close(); | 363 expectedEvents = new Events()..add(2)..add(3)..close(); |
364 actualEvents = new Events.capture(c.stream.skipWhile((x) => x <= 1)); | 364 actualEvents = new Events.capture(c.stream.skipWhile((x) => x <= 1)); |
365 sentEvents.replay(c); | 365 sentEvents.replay(c); |
366 Expect.listEquals(expectedEvents.events, actualEvents.events); | 366 Expect.listEquals(expectedEvents.events, actualEvents.events); |
367 | 367 |
368 | 368 |
369 c = new StreamController(); | 369 c = new StreamController(sync: true); |
370 expectedEvents = new Events()..add(1)..add(2)..add(3)..close(); | 370 expectedEvents = new Events()..add(1)..add(2)..add(3)..close(); |
371 actualEvents = new Events.capture(c.stream.skipWhile((x) => false)); | 371 actualEvents = new Events.capture(c.stream.skipWhile((x) => false)); |
372 sentEvents.replay(c); | 372 sentEvents.replay(c); |
373 Expect.listEquals(expectedEvents.events, actualEvents.events); | 373 Expect.listEquals(expectedEvents.events, actualEvents.events); |
374 | 374 |
375 | 375 |
376 c = new StreamController(); | 376 c = new StreamController(sync: true); |
377 expectedEvents = new Events()..add(1)..add(2)..close(); | 377 expectedEvents = new Events()..add(1)..add(2)..close(); |
378 actualEvents = new Events.capture(c.stream.take(2)); | 378 actualEvents = new Events.capture(c.stream.take(2)); |
379 sentEvents.replay(c); | 379 sentEvents.replay(c); |
380 Expect.listEquals(expectedEvents.events, actualEvents.events); | 380 Expect.listEquals(expectedEvents.events, actualEvents.events); |
381 | 381 |
382 | 382 |
383 c = new StreamController(); | 383 c = new StreamController(sync: true); |
384 expectedEvents = new Events()..add(1)..add(2)..close(); | 384 expectedEvents = new Events()..add(1)..add(2)..close(); |
385 actualEvents = new Events.capture(c.stream.takeWhile((x) => x <= 2)); | 385 actualEvents = new Events.capture(c.stream.takeWhile((x) => x <= 2)); |
386 sentEvents.replay(c); | 386 sentEvents.replay(c); |
387 Expect.listEquals(expectedEvents.events, actualEvents.events); | 387 Expect.listEquals(expectedEvents.events, actualEvents.events); |
388 | 388 |
389 | 389 |
390 c = new StreamController(); | 390 c = new StreamController(sync: true); |
391 sentEvents = new Events() | 391 sentEvents = new Events() |
392 ..add(1)..add(1)..add(2)..add(1)..add(2)..add(2)..add(2)..close(); | 392 ..add(1)..add(1)..add(2)..add(1)..add(2)..add(2)..add(2)..close(); |
393 expectedEvents = new Events() | 393 expectedEvents = new Events() |
394 ..add(1)..add(2)..add(1)..add(2)..close(); | 394 ..add(1)..add(2)..add(1)..add(2)..close(); |
395 actualEvents = new Events.capture(c.stream.distinct()); | 395 actualEvents = new Events.capture(c.stream.distinct()); |
396 sentEvents.replay(c); | 396 sentEvents.replay(c); |
397 Expect.listEquals(expectedEvents.events, actualEvents.events); | 397 Expect.listEquals(expectedEvents.events, actualEvents.events); |
398 | 398 |
399 | 399 |
400 c = new StreamController(); | 400 c = new StreamController(sync: true); |
401 sentEvents = new Events() | 401 sentEvents = new Events() |
402 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close(); | 402 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close(); |
403 expectedEvents = new Events() | 403 expectedEvents = new Events() |
404 ..add(5)..add(4)..add(3)..add(1)..close(); | 404 ..add(5)..add(4)..add(3)..add(1)..close(); |
405 // Use 'distinct' as a filter with access to the previously emitted event. | 405 // Use 'distinct' as a filter with access to the previously emitted event. |
406 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); | 406 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); |
407 sentEvents.replay(c); | 407 sentEvents.replay(c); |
408 Expect.listEquals(expectedEvents.events, actualEvents.events); | 408 Expect.listEquals(expectedEvents.events, actualEvents.events); |
409 } | 409 } |
410 | 410 |
411 testClosed() { | 411 testClosed() { |
412 StreamController c = new StreamController(); | 412 StreamController c = new StreamController(sync: true); |
413 Expect.isFalse(c.isClosed); | 413 Expect.isFalse(c.isClosed); |
414 c.add(42); | 414 c.add(42); |
415 Expect.isFalse(c.isClosed); | 415 Expect.isFalse(c.isClosed); |
416 c.addError("bad"); | 416 c.addError("bad"); |
417 Expect.isFalse(c.isClosed); | 417 Expect.isFalse(c.isClosed); |
418 c.close(); | 418 c.close(); |
419 Expect.isTrue(c.isClosed); | 419 Expect.isTrue(c.isClosed); |
420 } | 420 } |
421 | 421 |
422 main() { | 422 main() { |
423 testMultiController(); | 423 testMultiController(); |
424 testSingleController(); | 424 testSingleController(); |
425 testExtraMethods(); | 425 testExtraMethods(); |
426 testClosed(); | 426 testClosed(); |
427 } | 427 } |
OLD | NEW |