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_async_test; | 6 library stream_controller_async_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 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 11 matching lines...) Expand all Loading... |
22 })); | 22 })); |
23 c.add(10); | 23 c.add(10); |
24 c.add(32); | 24 c.add(32); |
25 c.close(); | 25 c.close(); |
26 }); | 26 }); |
27 | 27 |
28 test("StreamController.fold throws", () { | 28 test("StreamController.fold throws", () { |
29 StreamController c = new StreamController.broadcast(); | 29 StreamController c = new StreamController.broadcast(); |
30 Stream stream = c.stream; | 30 Stream stream = c.stream; |
31 stream.fold(0, (a,b) { throw "Fnyf!"; }) | 31 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
32 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 32 .catchError(expectAsync1((error) { Expect.equals("Fnyf!", error); })); |
33 c.add(42); | 33 c.add(42); |
34 }); | 34 }); |
35 } | 35 } |
36 | 36 |
37 testSingleController() { | 37 testSingleController() { |
38 test("Single-subscription StreamController.fold", () { | 38 test("Single-subscription StreamController.fold", () { |
39 StreamController c = new StreamController(); | 39 StreamController c = new StreamController(); |
40 Stream stream = c.stream; | 40 Stream stream = c.stream; |
41 stream.fold(0, (a,b) => a + b) | 41 stream.fold(0, (a,b) => a + b) |
42 .then(expectAsync1((int v) { Expect.equals(42, v); })); | 42 .then(expectAsync1((int v) { Expect.equals(42, v); })); |
43 c.add(10); | 43 c.add(10); |
44 c.add(32); | 44 c.add(32); |
45 c.close(); | 45 c.close(); |
46 }); | 46 }); |
47 | 47 |
48 test("Single-subscription StreamController.fold throws", () { | 48 test("Single-subscription StreamController.fold throws", () { |
49 StreamController c = new StreamController(); | 49 StreamController c = new StreamController(); |
50 Stream stream = c.stream; | 50 Stream stream = c.stream; |
51 stream.fold(0, (a,b) { throw "Fnyf!"; }) | 51 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
52 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 52 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e); })); |
53 c.add(42); | 53 c.add(42); |
54 }); | 54 }); |
55 | 55 |
56 test("Single-subscription StreamController subscription changes", () { | 56 test("Single-subscription StreamController subscription changes", () { |
57 StreamController c = new StreamController(); | 57 StreamController c = new StreamController(); |
58 EventSink sink = c.sink; | 58 EventSink sink = c.sink; |
59 Stream stream = c.stream; | 59 Stream stream = c.stream; |
60 int counter = 0; | 60 int counter = 0; |
61 var subscription; | 61 var subscription; |
62 subscription = stream.listen((data) { | 62 subscription = stream.listen((data) { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 test("singleWhere", () { | 171 test("singleWhere", () { |
172 StreamController c = new StreamController(); | 172 StreamController c = new StreamController(); |
173 Future f = c.stream.singleWhere((x) => (x % 9) == 0); | 173 Future f = c.stream.singleWhere((x) => (x % 9) == 0); |
174 f.then(expectAsync1((v) { Expect.equals(9, v); })); | 174 f.then(expectAsync1((v) { Expect.equals(9, v); })); |
175 sentEvents.replay(c); | 175 sentEvents.replay(c); |
176 }); | 176 }); |
177 | 177 |
178 test("singleWhere 2", () { | 178 test("singleWhere 2", () { |
179 StreamController c = new StreamController(); | 179 StreamController c = new StreamController(); |
180 Future f = c.stream.singleWhere((x) => (x % 3) == 0); // Matches both 9 and
87.. | 180 Future f = c.stream.singleWhere((x) => (x % 3) == 0); // Matches 9 and 87.. |
181 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); | 181 f.catchError(expectAsync1((error) { Expect.isTrue(error is StateError); })); |
182 sentEvents.replay(c); | 182 sentEvents.replay(c); |
183 }); | 183 }); |
184 | 184 |
185 test("first", () { | 185 test("first", () { |
186 StreamController c = new StreamController(); | 186 StreamController c = new StreamController(); |
187 Future f = c.stream.first; | 187 Future f = c.stream.first; |
188 f.then(expectAsync1((v) { Expect.equals(7, v);})); | 188 f.then(expectAsync1((v) { Expect.equals(7, v);})); |
189 sentEvents.replay(c); | 189 sentEvents.replay(c); |
190 }); | 190 }); |
191 | 191 |
192 test("first empty", () { | 192 test("first empty", () { |
193 StreamController c = new StreamController(); | 193 StreamController c = new StreamController(); |
194 Future f = c.stream.first; | 194 Future f = c.stream.first; |
195 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); | 195 f.catchError(expectAsync1((error) { Expect.isTrue(error is StateError); })); |
196 Events emptyEvents = new Events()..close(); | 196 Events emptyEvents = new Events()..close(); |
197 emptyEvents.replay(c); | 197 emptyEvents.replay(c); |
198 }); | 198 }); |
199 | 199 |
200 test("first error", () { | 200 test("first error", () { |
201 StreamController c = new StreamController(); | 201 StreamController c = new StreamController(); |
202 Future f = c.stream.first; | 202 Future f = c.stream.first; |
203 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); | 203 f.catchError(expectAsync1((error) { Expect.equals("error", error); })); |
204 Events errorEvents = new Events()..error("error")..close(); | 204 Events errorEvents = new Events()..error("error")..close(); |
205 errorEvents.replay(c); | 205 errorEvents.replay(c); |
206 }); | 206 }); |
207 | 207 |
208 test("first error 2", () { | 208 test("first error 2", () { |
209 StreamController c = new StreamController(); | 209 StreamController c = new StreamController(); |
210 Future f = c.stream.first; | 210 Future f = c.stream.first; |
211 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); | 211 f.catchError(expectAsync1((error) { Expect.equals("error", error); })); |
212 Events errorEvents = new Events()..error("error")..error("error2")..close(); | 212 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
213 errorEvents.replay(c); | 213 errorEvents.replay(c); |
214 }); | 214 }); |
215 | 215 |
216 test("last", () { | 216 test("last", () { |
217 StreamController c = new StreamController(); | 217 StreamController c = new StreamController(); |
218 Future f = c.stream.last; | 218 Future f = c.stream.last; |
219 f.then(expectAsync1((v) { Expect.equals(87, v);})); | 219 f.then(expectAsync1((v) { Expect.equals(87, v);})); |
220 sentEvents.replay(c); | 220 sentEvents.replay(c); |
221 }); | 221 }); |
222 | 222 |
223 test("last empty", () { | 223 test("last empty", () { |
224 StreamController c = new StreamController(); | 224 StreamController c = new StreamController(); |
225 Future f = c.stream.last; | 225 Future f = c.stream.last; |
226 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); | 226 f.catchError(expectAsync1((error) { Expect.isTrue(error is StateError); })); |
227 Events emptyEvents = new Events()..close(); | 227 Events emptyEvents = new Events()..close(); |
228 emptyEvents.replay(c); | 228 emptyEvents.replay(c); |
229 }); | 229 }); |
230 | 230 |
231 test("last error", () { | 231 test("last error", () { |
232 StreamController c = new StreamController(); | 232 StreamController c = new StreamController(); |
233 Future f = c.stream.last; | 233 Future f = c.stream.last; |
234 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); | 234 f.catchError(expectAsync1((error) { Expect.equals("error", error); })); |
235 Events errorEvents = new Events()..error("error")..close(); | 235 Events errorEvents = new Events()..error("error")..close(); |
236 errorEvents.replay(c); | 236 errorEvents.replay(c); |
237 }); | 237 }); |
238 | 238 |
239 test("last error 2", () { | 239 test("last error 2", () { |
240 StreamController c = new StreamController(); | 240 StreamController c = new StreamController(); |
241 Future f = c.stream.last; | 241 Future f = c.stream.last; |
242 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); | 242 f.catchError(expectAsync1((error) { Expect.equals("error", error); })); |
243 Events errorEvents = new Events()..error("error")..error("error2")..close(); | 243 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
244 errorEvents.replay(c); | 244 errorEvents.replay(c); |
245 }); | 245 }); |
246 | 246 |
247 test("elementAt", () { | 247 test("elementAt", () { |
248 StreamController c = new StreamController(); | 248 StreamController c = new StreamController(); |
249 Future f = c.stream.elementAt(2); | 249 Future f = c.stream.elementAt(2); |
250 f.then(expectAsync1((v) { Expect.equals(13, v);})); | 250 f.then(expectAsync1((v) { Expect.equals(13, v);})); |
251 sentEvents.replay(c); | 251 sentEvents.replay(c); |
252 }); | 252 }); |
253 | 253 |
254 test("elementAt 2", () { | 254 test("elementAt 2", () { |
255 StreamController c = new StreamController(); | 255 StreamController c = new StreamController(); |
256 Future f = c.stream.elementAt(20); | 256 Future f = c.stream.elementAt(20); |
257 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); | 257 f.catchError(expectAsync1((error) { Expect.isTrue(error is StateError); })); |
258 sentEvents.replay(c); | 258 sentEvents.replay(c); |
259 }); | 259 }); |
260 } | 260 } |
261 | 261 |
262 testPause() { | 262 testPause() { |
263 test("pause event-unpause", () { | 263 test("pause event-unpause", () { |
264 StreamController c = new StreamController(); | 264 StreamController c = new StreamController(); |
265 Events actualEvents = new Events.capture(c.stream); | 265 Events actualEvents = new Events.capture(c.stream); |
266 Events expectedEvents = new Events(); | 266 Events expectedEvents = new Events(); |
267 expectedEvents.add(42); | 267 expectedEvents.add(42); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 completer.complete(); | 362 completer.complete(); |
363 Expect.listEquals(expectedEvents.events, actualEvents.events); | 363 Expect.listEquals(expectedEvents.events, actualEvents.events); |
364 expectedEvents..add(43)..add(44)..close(); | 364 expectedEvents..add(43)..add(44)..close(); |
365 actualEvents.onDone(expectAsync0(() { | 365 actualEvents.onDone(expectAsync0(() { |
366 Expect.listEquals(expectedEvents.events, actualEvents.events); | 366 Expect.listEquals(expectedEvents.events, actualEvents.events); |
367 })); | 367 })); |
368 actualEvents.resume(); | 368 actualEvents.resume(); |
369 }); | 369 }); |
370 } | 370 } |
371 | 371 |
| 372 class TestError { const TestError(); } |
| 373 |
372 testRethrow() { | 374 testRethrow() { |
373 AsyncError error = new AsyncError("UNIQUE", "UNIQUE"); | 375 TestError error = const TestError(); |
| 376 |
374 | 377 |
375 testStream(name, streamValueTransform) { | 378 testStream(name, streamValueTransform) { |
376 test("rethrow-$name-value", () { | 379 test("rethrow-$name-value", () { |
377 StreamController c = new StreamController(); | 380 StreamController c = new StreamController(); |
378 Stream s = streamValueTransform(c.stream, (v) { throw error; }); | 381 Stream s = streamValueTransform(c.stream, (v) { throw error; }); |
379 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( | 382 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( |
380 (AsyncError e) { Expect.identical(error, e); })); | 383 (e) { Expect.identical(error, e); })); |
381 c.add(null); | 384 c.add(null); |
382 c.close(); | 385 c.close(); |
383 }); | 386 }); |
384 } | 387 } |
385 | 388 |
386 testStreamError(name, streamErrorTransform) { | 389 testStreamError(name, streamErrorTransform) { |
387 test("rethrow-$name-error", () { | 390 test("rethrow-$name-error", () { |
388 StreamController c = new StreamController(); | 391 StreamController c = new StreamController(); |
389 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); | 392 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); |
390 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( | 393 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( |
391 (AsyncError e) { Expect.identical(error, e); })); | 394 (e) { Expect.identical(error, e); })); |
392 c.addError(null); | 395 c.addError(null); |
393 c.close(); | 396 c.close(); |
394 }); | 397 }); |
395 } | 398 } |
396 | 399 |
397 testFuture(name, streamValueTransform) { | 400 testFuture(name, streamValueTransform) { |
398 test("rethrow-$name-value", () { | 401 test("rethrow-$name-value", () { |
399 StreamController c = new StreamController(); | 402 StreamController c = new StreamController(); |
400 Future f = streamValueTransform(c.stream, (v) { throw error; }); | 403 Future f = streamValueTransform(c.stream, (v) { throw error; }); |
401 f.then((v) { Expect.fail("unreachable"); }, | 404 f.then((v) { Expect.fail("unreachable"); }, |
(...skipping 17 matching lines...) Expand all Loading... |
419 testFuture("fold", (s, act) => s.fold(0, (a,b) => act(b))); | 422 testFuture("fold", (s, act) => s.fold(0, (a,b) => act(b))); |
420 } | 423 } |
421 | 424 |
422 main() { | 425 main() { |
423 testController(); | 426 testController(); |
424 testSingleController(); | 427 testSingleController(); |
425 testExtraMethods(); | 428 testExtraMethods(); |
426 testPause(); | 429 testPause(); |
427 testRethrow(); | 430 testRethrow(); |
428 } | 431 } |
OLD | NEW |