Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: tests/lib/async/stream_controller_test.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Test the basic StreamController and StreamController.singleSubscription.
6 import 'dart:async';
7 import 'event_helper.dart';
8
9 testController() {
10 // Test normal flow.
11 var c = new StreamController();
12 Events expectedEvents = new Events()
13 ..add(42)
14 ..add("dibs")
15 ..error("error!")
16 ..error("error too!")
17 ..close();
18 Events actualEvents = new Events.capture(c);
19 expectedEvents.replay(c);
20 Expect.listEquals(expectedEvents.events, actualEvents.events);
21
22 // Test automatic unsubscription on error.
23 c = new StreamController();
24 expectedEvents = new Events()..add(42)..error("error");
25 actualEvents = new Events.capture(c, unsubscribeOnError: true);
26 Events sentEvents =
27 new Events()..add(42)..error("error")..add("Are you there?");
28 sentEvents.replay(c);
29 Expect.listEquals(expectedEvents.events, actualEvents.events);
30
31 // Test manual unsubscription.
32 c = new StreamController();
33 expectedEvents = new Events()..add(42)..error("error")..add(37);
34 actualEvents = new Events.capture(c, unsubscribeOnError: false);
35 expectedEvents.replay(c);
36 actualEvents.subscription.cancel();
37 c.add("Are you there"); // Not sent to actualEvents.
38 Expect.listEquals(expectedEvents.events, actualEvents.events);
39
40 // Test filter.
41 c = new StreamController();
42 expectedEvents = new Events()
43 ..add("a string")..add("another string")..close();
44 sentEvents = new Events()
45 ..add("a string")..add(42)..add("another string")..close();
46 actualEvents = new Events.capture(c.where((v) => v is String));
47 sentEvents.replay(c);
48 Expect.listEquals(expectedEvents.events, actualEvents.events);
49
50 // Test map.
51 c = new StreamController();
52 expectedEvents = new Events()..add("abab")..error("error")..close();
53 sentEvents = new Events()..add("ab")..error("error")..close();
54 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v"));
55 sentEvents.replay(c);
56 Expect.listEquals(expectedEvents.events, actualEvents.events);
57
58 // Test handleError.
59 c = new StreamController();
60 expectedEvents = new Events()..add("ab")..error("[foo]");
61 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close();
62 actualEvents = new Events.capture(c.handleError((v) {
63 if (v.error is String) {
64 return new AsyncError("[${v.error}]",
65 "other stack");
66 }
67 }), unsubscribeOnError: true);
68 sentEvents.replay(c);
69 Expect.listEquals(expectedEvents.events, actualEvents.events);
70
71 // reduce is tested asynchronously and therefore not in this file.
72
73 // Test expand
74 c = new StreamController();
75 sentEvents = new Events()..add(3)..add(2)..add(4)..close();
76 expectedEvents = new Events()..add(1)..add(2)..add(3)
77 ..add(1)..add(2)
78 ..add(1)..add(2)..add(3)..add(4)
79 ..close();
80 actualEvents = new Events.capture(c.expand((v) {
81 var l = [];
82 for (int i = 0; i < v; i++) l.add(i + 1);
83 return l;
84 }));
85 sentEvents.replay(c);
86 Expect.listEquals(expectedEvents.events, actualEvents.events);
87
88 // Test transform.
89 c = new StreamController();
90 sentEvents = new Events()..add("a")..error(42)..add("b")..close();
91 expectedEvents =
92 new Events()..error("a")..add(42)..error("b")..add("foo")..close();
93 actualEvents = new Events.capture(c.transform(new StreamTransformer.from(
94 onData: (v, s) { s.signalError(new AsyncError(v)); },
95 onError: (e, s) { s.add(e.error); },
96 onDone: (s) {
97 s.add("foo");
98 s.close();
99 })));
100 sentEvents.replay(c);
101 Expect.listEquals(expectedEvents.events, actualEvents.events);
102
103 // Test multiple filters.
104 c = new StreamController();
105 sentEvents = new Events()..add(42)
106 ..add("snugglefluffy")
107 ..add(7)
108 ..add("42")
109 ..error("not FormatException") // Unsubscribes.
110 ..close();
111 expectedEvents = new Events()..add(42)..error("not FormatException");
112 actualEvents = new Events.capture(
113 c.where((v) => v is String)
114 .mappedBy((v) => int.parse(v))
115 .handleError((v) {
116 if (v.error is! FormatException) return v;
117 })
118 .where((v) => v > 10),
119 unsubscribeOnError: true);
120 sentEvents.replay(c);
121 Expect.listEquals(expectedEvents.events, actualEvents.events);
122
123 // Test subscription changes while firing.
124 c = new StreamController();
125 var sink = c.sink;
126 var stream = c.stream;
127 var counter = 0;
128 var subscription = stream.listen(null);
129 subscription.onData((data) {
130 counter += data;
131 subscription.cancel();
132 stream.listen((data) {
133 counter += 10 * data;
134 });
135 var subscription2 = stream.listen(null);
136 subscription2.onData((data) {
137 counter += 100 * data;
138 if (data == 4) subscription2.cancel();
139 });
140 });
141 sink.add(1); // seen by stream 1
142 sink.add(2); // seen by stream 10 and 100
143 sink.add(3); // -"-
144 sink.add(4); // -"-
145 sink.add(5); // seen by stream 10
146 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter);
147 }
148
149 testSingleController() {
150 // Test normal flow.
151 var c = new StreamController.singleSubscription();
152 Events expectedEvents = new Events()
153 ..add(42)
154 ..add("dibs")
155 ..error("error!")
156 ..error("error too!")
157 ..close();
158 Events actualEvents = new Events.capture(c);
159 expectedEvents.replay(c);
160 Expect.listEquals(expectedEvents.events, actualEvents.events);
161
162 // Test automatic unsubscription on error.
163 c = new StreamController.singleSubscription();
164 expectedEvents = new Events()..add(42)..error("error");
165 actualEvents = new Events.capture(c, unsubscribeOnError: true);
166 Events sentEvents =
167 new Events()..add(42)..error("error")..add("Are you there?");
168 sentEvents.replay(c);
169 Expect.listEquals(expectedEvents.events, actualEvents.events);
170
171 // Test manual unsubscription.
172 c = new StreamController.singleSubscription();
173 expectedEvents = new Events()..add(42)..error("error")..add(37);
174 actualEvents = new Events.capture(c, unsubscribeOnError: false);
175 expectedEvents.replay(c);
176 actualEvents.subscription.cancel();
177 c.add("Are you there"); // Not sent to actualEvents.
178 Expect.listEquals(expectedEvents.events, actualEvents.events);
179
180 // Test filter.
181 c = new StreamController.singleSubscription();
182 expectedEvents = new Events()
183 ..add("a string")..add("another string")..close();
184 sentEvents = new Events()
185 ..add("a string")..add(42)..add("another string")..close();
186 actualEvents = new Events.capture(c.where((v) => v is String));
187 sentEvents.replay(c);
188 Expect.listEquals(expectedEvents.events, actualEvents.events);
189
190 // Test map.
191 c = new StreamController.singleSubscription();
192 expectedEvents = new Events()..add("abab")..error("error")..close();
193 sentEvents = new Events()..add("ab")..error("error")..close();
194 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v"));
195 sentEvents.replay(c);
196 Expect.listEquals(expectedEvents.events, actualEvents.events);
197
198 // Test handleError.
199 c = new StreamController.singleSubscription();
200 expectedEvents = new Events()..add("ab")..error("[foo]");
201 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close();
202 actualEvents = new Events.capture(c.handleError((v) {
203 if (v.error is String) {
204 return new AsyncError("[${v.error}]",
205 "other stack");
206 }
207 }), unsubscribeOnError: true);
208 sentEvents.replay(c);
209 Expect.listEquals(expectedEvents.events, actualEvents.events);
210
211 // reduce is tested asynchronously and therefore not in this file.
212
213 // Test expand
214 c = new StreamController.singleSubscription();
215 sentEvents = new Events()..add(3)..add(2)..add(4)..close();
216 expectedEvents = new Events()..add(1)..add(2)..add(3)
217 ..add(1)..add(2)
218 ..add(1)..add(2)..add(3)..add(4)
219 ..close();
220 actualEvents = new Events.capture(c.expand((v) {
221 var l = [];
222 for (int i = 0; i < v; i++) l.add(i + 1);
223 return l;
224 }));
225 sentEvents.replay(c);
226 Expect.listEquals(expectedEvents.events, actualEvents.events);
227
228 // pipe is tested asynchronously and therefore not in this file.
229 c = new StreamController.singleSubscription();
230 var list = <int>[];
231 c.pipeInto(new CollectionSink<int>(list))
232 .then(() { Expect.listEquals(<int>[1,2,9,3,9], list); });
233 c.add(1);
234 c.add(2);
235 c.add(9);
236 c.add(3);
237 c.add(9);
238 c.close();
239
240 // Test transform.
241 c = new StreamController.singleSubscription();
242 sentEvents = new Events()..add("a")..error(42)..add("b")..close();
243 expectedEvents =
244 new Events()..error("a")..add(42)..error("b")..add("foo")..close();
245 actualEvents = new Events.capture(c.transform(new StreamTransformer.from(
246 onData: (v, s) { s.signalError(new AsyncError(v)); },
247 onError: (e, s) { s.add(e.error); },
248 onDone: (s) {
249 s.add("foo");
250 s.close();
251 })));
252 sentEvents.replay(c);
253 Expect.listEquals(expectedEvents.events, actualEvents.events);
254
255 // Test multiple filters.
256 c = new StreamController.singleSubscription();
257 sentEvents = new Events()..add(42)
258 ..add("snugglefluffy")
259 ..add(7)
260 ..add("42")
261 ..error("not FormatException") // Unsubscribes.
262 ..close();
263 expectedEvents = new Events()..add(42)..error("not FormatException");
264 actualEvents = new Events.capture(
265 c.where((v) => v is String)
266 .mappedBy((v) => int.parse(v))
267 .handleError((v) {
268 if (v.error is! FormatException) return v;
269 })
270 .where((v) => v > 10),
271 unsubscribeOnError: true);
272 sentEvents.replay(c);
273 Expect.listEquals(expectedEvents.events, actualEvents.events);
274
275 // Test that only one subscription is allowed.
276 c = new StreamController.singleSubscription();
277 var sink = c.sink;
278 var stream = c.stream;
279 var counter = 0;
280 var subscription = stream.listen((data) { counter += data; });
281 Expect.throws(() => stream.listen(null), (e) => e is StateError);
282 sink.add(1);
283 Expect.equals(1, counter);
284 c.close();
285 }
286
287 testExtraMethods() {
288 Events sentEvents = new Events()..add(1)..add(2)..add(3)..close();
289
290 var c = new StreamController();
291 Events expectedEvents = new Events()..add(3)..close();
292 Events actualEvents = new Events.capture(c.skip(2));
293 sentEvents.replay(c);
294 Expect.listEquals(expectedEvents.events, actualEvents.events);
295
296 c = new StreamController();
297 expectedEvents = new Events()..close();
298 actualEvents = new Events.capture(c.skip(3));
299 sentEvents.replay(c);
300 Expect.listEquals(expectedEvents.events, actualEvents.events);
301
302 c = new StreamController();
303 expectedEvents = new Events()..close();
304 actualEvents = new Events.capture(c.skip(7));
305 sentEvents.replay(c);
306 Expect.listEquals(expectedEvents.events, actualEvents.events);
307
308 c = new StreamController();
309 expectedEvents = sentEvents;
310 actualEvents = new Events.capture(c.skip(0));
311 sentEvents.replay(c);
312 Expect.listEquals(expectedEvents.events, actualEvents.events);
313
314
315 c = new StreamController();
316 expectedEvents = new Events()..add(3)..close();
317 actualEvents = new Events.capture(c.skipWhile((x) => x <= 2));
318 sentEvents.replay(c);
319 Expect.listEquals(expectedEvents.events, actualEvents.events);
320
321
322 c = new StreamController();
323 expectedEvents = new Events()..add(1)..add(2)..close();
324 actualEvents = new Events.capture(c.take(2));
325 sentEvents.replay(c);
326 Expect.listEquals(expectedEvents.events, actualEvents.events);
327
328
329 c = new StreamController();
330 expectedEvents = new Events()..add(1)..add(2)..close();
331 actualEvents = new Events.capture(c.takeWhile((x) => x <= 2));
332 sentEvents.replay(c);
333 Expect.listEquals(expectedEvents.events, actualEvents.events);
334
335 c = new StreamController();
336 sentEvents = new Events()
337 ..add(1)..add(1)..add(2)..add(1)..add(2)..add(2)..add(2)..close();
338 expectedEvents = new Events()
339 ..add(1)..add(2)..add(1)..add(2)..close();
340 actualEvents = new Events.capture(c.distinct());
341 sentEvents.replay(c);
342 Expect.listEquals(expectedEvents.events, actualEvents.events);
343
344 c = new StreamController();
345 sentEvents = new Events()
346 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close();
347 expectedEvents = new Events()
348 ..add(5)..add(4)..add(3)..add(1)..close();
349 // Use 'distinct' as a filter with access to the previously emitted event.
350 actualEvents = new Events.capture(c.distinct((a, b) => a < b));
351 sentEvents.replay(c);
352 Expect.listEquals(expectedEvents.events, actualEvents.events);
353 }
354
355 main() {
356 testController();
357 testSingleController();
358 testExtraMethods();
359 }
OLDNEW
« no previous file with comments | « tests/lib/async/stream_controller_async_test.dart ('k') | tests/lib/async/stream_min_max_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698