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