OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 that transformations like `map` and `where` preserve broadcast flag. | 5 // Test that transformations like `map` and `where` preserve broadcast flag. |
6 library stream_join_test; | 6 library stream_join_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'event_helper.dart'; | 9 import 'event_helper.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:test/test.dart'; |
11 import "package:expect/expect.dart"; | |
12 | 11 |
13 main() { | 12 main() { |
14 testStream("singlesub", () => new StreamController(), (c) => c.stream); | 13 testStream("singlesub", () => new StreamController(), (c) => c.stream); |
15 testStream("broadcast", () => new StreamController.broadcast(), | 14 testStream("broadcast", () => new StreamController.broadcast(), |
16 (c) => c.stream); | 15 (c) => c.stream); |
17 testStream("asBroadcast", () => new StreamController(), | 16 testStream("asBroadcast", () => new StreamController(), |
18 (c) => c.stream.asBroadcastStream()); | 17 (c) => c.stream.asBroadcastStream()); |
19 testStream("broadcast.asBroadcast", () => new StreamController.broadcast(), | 18 testStream("broadcast.asBroadcast", () => new StreamController.broadcast(), |
20 (c) => c.stream.asBroadcastStream()); | 19 (c) => c.stream.asBroadcastStream()); |
21 } | 20 } |
22 | 21 |
23 void testStream(String name, | 22 void testStream(String name, |
24 StreamController create(), | 23 StreamController create(), |
25 Stream getStream(controller)) { | 24 Stream getStream(controller)) { |
26 test("$name-map", () { | 25 test("$name-map", () { |
27 var c = create(); | 26 var c = create(); |
28 var s = getStream(c); | 27 var s = getStream(c); |
29 Stream newStream = s.map((x) => x + 1); | 28 Stream newStream = s.map((x) => x + 1); |
30 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 29 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
31 newStream.single.then(expectAsync((v) { | 30 newStream.single.then(expectAsync((v) { |
32 Expect.equals(43, v); | 31 expect(43, equals(v)); |
33 })); | 32 })); |
34 c.add(42); | 33 c.add(42); |
35 c.close(); | 34 c.close(); |
36 }); | 35 }); |
37 test("$name-where", () { | 36 test("$name-where", () { |
38 var c = create(); | 37 var c = create(); |
39 var s = getStream(c); | 38 var s = getStream(c); |
40 Stream newStream = s.where((x) => x.isEven); | 39 Stream newStream = s.where((x) => x.isEven); |
41 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 40 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
42 newStream.single.then(expectAsync((v) { | 41 newStream.single.then(expectAsync((v) { |
43 Expect.equals(42, v); | 42 expect(42, equals(v)); |
44 })); | 43 })); |
45 c.add(37); | 44 c.add(37); |
46 c.add(42); | 45 c.add(42); |
47 c.add(87); | 46 c.add(87); |
48 c.close(); | 47 c.close(); |
49 }); | 48 }); |
50 test("$name-handleError", () { | 49 test("$name-handleError", () { |
51 var c = create(); | 50 var c = create(); |
52 var s = getStream(c); | 51 var s = getStream(c); |
53 Stream newStream = s.handleError((x, s) {}); | 52 Stream newStream = s.handleError((x, s) {}); |
54 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 53 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
55 newStream.single.then(expectAsync((v) { | 54 newStream.single.then(expectAsync((v) { |
56 Expect.equals(42, v); | 55 expect(42, equals(v)); |
57 })); | 56 })); |
58 c.addError("BAD1"); | 57 c.addError("BAD1"); |
59 c.add(42); | 58 c.add(42); |
60 c.addError("BAD2"); | 59 c.addError("BAD2"); |
61 c.close(); | 60 c.close(); |
62 }); | 61 }); |
63 test("$name-expand", () { | 62 test("$name-expand", () { |
64 var c = create(); | 63 var c = create(); |
65 var s = getStream(c); | 64 var s = getStream(c); |
66 Stream newStream = s.expand((x) => x.isEven ? [x] : []); | 65 Stream newStream = s.expand((x) => x.isEven ? [x] : []); |
67 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 66 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
68 newStream.single.then(expectAsync((v) { | 67 newStream.single.then(expectAsync((v) { |
69 Expect.equals(42, v); | 68 expect(42, equals(v)); |
70 })); | 69 })); |
71 c.add(37); | 70 c.add(37); |
72 c.add(42); | 71 c.add(42); |
73 c.add(87); | 72 c.add(87); |
74 c.close(); | 73 c.close(); |
75 }); | 74 }); |
76 test("$name-transform", () { | 75 test("$name-transform", () { |
77 var c = create(); | 76 var c = create(); |
78 var s = getStream(c); | 77 var s = getStream(c); |
79 // TODO: find name of default transformer | 78 // TODO: find name of default transformer |
80 var t = new StreamTransformer.fromHandlers( | 79 var t = new StreamTransformer.fromHandlers( |
81 handleData: (value, EventSink sink) { sink.add(value); } | 80 handleData: (value, EventSink sink) { sink.add(value); } |
82 ); | 81 ); |
83 Stream newStream = s.transform(t); | 82 Stream newStream = s.transform(t); |
84 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 83 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
85 newStream.single.then(expectAsync((v) { | 84 newStream.single.then(expectAsync((v) { |
86 Expect.equals(42, v); | 85 expect(42, equals(v)); |
87 })); | 86 })); |
88 c.add(42); | 87 c.add(42); |
89 c.close(); | 88 c.close(); |
90 }); | 89 }); |
91 test("$name-take", () { | 90 test("$name-take", () { |
92 var c = create(); | 91 var c = create(); |
93 var s = getStream(c); | 92 var s = getStream(c); |
94 Stream newStream = s.take(1); | 93 Stream newStream = s.take(1); |
95 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 94 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
96 newStream.single.then(expectAsync((v) { | 95 newStream.single.then(expectAsync((v) { |
97 Expect.equals(42, v); | 96 expect(42, equals(v)); |
98 })); | 97 })); |
99 c.add(42); | 98 c.add(42); |
100 c.add(37); | 99 c.add(37); |
101 c.close(); | 100 c.close(); |
102 }); | 101 }); |
103 test("$name-takeWhile", () { | 102 test("$name-takeWhile", () { |
104 var c = create(); | 103 var c = create(); |
105 var s = getStream(c); | 104 var s = getStream(c); |
106 Stream newStream = s.takeWhile((x) => x.isEven); | 105 Stream newStream = s.takeWhile((x) => x.isEven); |
107 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 106 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
108 newStream.single.then(expectAsync((v) { | 107 newStream.single.then(expectAsync((v) { |
109 Expect.equals(42, v); | 108 expect(42, equals(v)); |
110 })); | 109 })); |
111 c.add(42); | 110 c.add(42); |
112 c.add(37); | 111 c.add(37); |
113 c.close(); | 112 c.close(); |
114 }); | 113 }); |
115 test("$name-skip", () { | 114 test("$name-skip", () { |
116 var c = create(); | 115 var c = create(); |
117 var s = getStream(c); | 116 var s = getStream(c); |
118 Stream newStream = s.skip(1); | 117 Stream newStream = s.skip(1); |
119 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 118 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
120 newStream.single.then(expectAsync((v) { | 119 newStream.single.then(expectAsync((v) { |
121 Expect.equals(42, v); | 120 expect(42, equals(v)); |
122 })); | 121 })); |
123 c.add(37); | 122 c.add(37); |
124 c.add(42); | 123 c.add(42); |
125 c.close(); | 124 c.close(); |
126 }); | 125 }); |
127 test("$name-skipWhile", () { | 126 test("$name-skipWhile", () { |
128 var c = create(); | 127 var c = create(); |
129 var s = getStream(c); | 128 var s = getStream(c); |
130 Stream newStream = s.skipWhile((x) => x.isOdd); | 129 Stream newStream = s.skipWhile((x) => x.isOdd); |
131 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 130 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
132 newStream.single.then(expectAsync((v) { | 131 newStream.single.then(expectAsync((v) { |
133 Expect.equals(42, v); | 132 expect(42, equals(v)); |
134 })); | 133 })); |
135 c.add(37); | 134 c.add(37); |
136 c.add(42); | 135 c.add(42); |
137 c.close(); | 136 c.close(); |
138 }); | 137 }); |
139 test("$name-distinct", () { | 138 test("$name-distinct", () { |
140 var c = create(); | 139 var c = create(); |
141 var s = getStream(c); | 140 var s = getStream(c); |
142 Stream newStream = s.distinct(); | 141 Stream newStream = s.distinct(); |
143 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 142 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
144 newStream.single.then(expectAsync((v) { | 143 newStream.single.then(expectAsync((v) { |
145 Expect.equals(42, v); | 144 expect(42, equals(v)); |
146 })); | 145 })); |
147 c.add(42); | 146 c.add(42); |
148 c.add(42); | 147 c.add(42); |
149 c.close(); | 148 c.close(); |
150 }); | 149 }); |
151 test("$name-timeout", () { | 150 test("$name-timeout", () { |
152 var c = create(); | 151 var c = create(); |
153 var s = getStream(c); | 152 var s = getStream(c); |
154 Stream newStream = s.timeout(const Duration(seconds: 1)); | 153 Stream newStream = s.timeout(const Duration(seconds: 1)); |
155 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 154 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
156 newStream.single.then(expectAsync((v) { | 155 newStream.single.then(expectAsync((v) { |
157 Expect.equals(42, v); | 156 expect(42, equals(v)); |
158 })); | 157 })); |
159 c.add(42); | 158 c.add(42); |
160 c.close(); | 159 c.close(); |
161 }); | 160 }); |
162 test("$name-asyncMap", () { | 161 test("$name-asyncMap", () { |
163 var c = create(); | 162 var c = create(); |
164 var s = getStream(c); | 163 var s = getStream(c); |
165 Stream newStream = s.asyncMap((x) => new Future.value(x + 1)); | 164 Stream newStream = s.asyncMap((x) => new Future.value(x + 1)); |
166 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 165 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
167 newStream.single.then(expectAsync((v) { | 166 newStream.single.then(expectAsync((v) { |
168 Expect.equals(43, v); | 167 expect(43, equals(v)); |
169 })); | 168 })); |
170 c.add(42); | 169 c.add(42); |
171 c.close(); | 170 c.close(); |
172 }); | 171 }); |
173 test("$name-asyncExpand", () { | 172 test("$name-asyncExpand", () { |
174 var c = create(); | 173 var c = create(); |
175 var s = getStream(c); | 174 var s = getStream(c); |
176 Stream newStream = s.asyncExpand((x) => new Stream.fromIterable([x + 1])); | 175 Stream newStream = s.asyncExpand((x) => new Stream.fromIterable([x + 1])); |
177 Expect.equals(s.isBroadcast, newStream.isBroadcast); | 176 expect(s.isBroadcast, equals(newStream.isBroadcast)); |
178 newStream.single.then(expectAsync((v) { | 177 newStream.single.then(expectAsync((v) { |
179 Expect.equals(43, v); | 178 expect(43, equals(v)); |
180 })); | 179 })); |
181 c.add(42); | 180 c.add(42); |
182 c.close(); | 181 c.close(); |
183 }); | 182 }); |
184 | 183 |
185 // The following tests are only on broadcast streams, they require listening | 184 // The following tests are only on broadcast streams, they require listening |
186 // more than once. | 185 // more than once. |
187 if (name.startsWith("singlesub")) return; | 186 if (name.startsWith("singlesub")) return; |
188 | 187 |
189 test("$name-skip-multilisten", () { | 188 test("$name-skip-multilisten", () { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 scheduleMicrotask(loop); | 314 scheduleMicrotask(loop); |
316 }); | 315 }); |
317 | 316 |
318 test("$name-takeWhile-multilisten", () { | 317 test("$name-takeWhile-multilisten", () { |
319 var c = create(); | 318 var c = create(); |
320 var s = getStream(c); | 319 var s = getStream(c); |
321 Stream newStream = s.takeWhile((x) => (x % 10) != 5); | 320 Stream newStream = s.takeWhile((x) => (x % 10) != 5); |
322 // Listen immediately, to ensure that an asBroadcast stream is started. | 321 // Listen immediately, to ensure that an asBroadcast stream is started. |
323 var sub = newStream.listen((_){}); | 322 var sub = newStream.listen((_){}); |
324 int i = 0; | 323 int i = 0; |
325 // Non-overlapping ranges means the test must not remember its first | 324 // Non-overlapping ranges means the test must not remember its first |
326 // failure. | 325 // failure. |
327 var expect1 = 6; | 326 var expect1 = 6; |
328 var expect2 = 16; | 327 var expect2 = 16; |
329 var handler2 = expectAsync((v) { | 328 var handler2 = expectAsync((v) { |
330 expect(v, expect2); | 329 expect(v, expect2); |
331 expect(v <= 25, isTrue); | 330 expect(v <= 25, isTrue); |
332 expect2++; | 331 expect2++; |
333 }, count: 9); | 332 }, count: 9); |
334 var handler1 = expectAsync((v) { | 333 var handler1 = expectAsync((v) { |
335 expect(v, expect1); | 334 expect(v, expect1); |
(...skipping 17 matching lines...) Expand all Loading... |
353 if (i < 25) { | 352 if (i < 25) { |
354 scheduleMicrotask(loop); | 353 scheduleMicrotask(loop); |
355 } else { | 354 } else { |
356 sub.cancel(); | 355 sub.cancel(); |
357 c.close(); | 356 c.close(); |
358 } | 357 } |
359 }, count: 25); | 358 }, count: 25); |
360 scheduleMicrotask(loop); | 359 scheduleMicrotask(loop); |
361 }); | 360 }); |
362 } | 361 } |
OLD | NEW |