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