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 'dart:async'; | 8 import 'dart:async'; |
9 import 'event_helper.dart'; | 9 import 'event_helper.dart'; |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 sentEvents = new Events() | 46 sentEvents = new Events() |
47 ..add("a string")..add(42)..add("another string")..close(); | 47 ..add("a string")..add(42)..add("another string")..close(); |
48 actualEvents = new Events.capture(c.stream.where((v) => v is String)); | 48 actualEvents = new Events.capture(c.stream.where((v) => v is String)); |
49 sentEvents.replay(c); | 49 sentEvents.replay(c); |
50 Expect.listEquals(expectedEvents.events, actualEvents.events); | 50 Expect.listEquals(expectedEvents.events, actualEvents.events); |
51 | 51 |
52 // Test map. | 52 // Test map. |
53 c = new StreamController.broadcast(); | 53 c = new StreamController.broadcast(); |
54 expectedEvents = new Events()..add("abab")..error("error")..close(); | 54 expectedEvents = new Events()..add("abab")..error("error")..close(); |
55 sentEvents = new Events()..add("ab")..error("error")..close(); | 55 sentEvents = new Events()..add("ab")..error("error")..close(); |
56 actualEvents = new Events.capture(c.stream.mappedBy((v) => "$v$v")); | 56 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); |
57 sentEvents.replay(c); | 57 sentEvents.replay(c); |
58 Expect.listEquals(expectedEvents.events, actualEvents.events); | 58 Expect.listEquals(expectedEvents.events, actualEvents.events); |
59 | 59 |
60 // Test handleError. | 60 // Test handleError. |
61 c = new StreamController.broadcast(); | 61 c = new StreamController.broadcast(); |
62 expectedEvents = new Events()..add("ab")..error("[foo]"); | 62 expectedEvents = new Events()..add("ab")..error("[foo]"); |
63 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 63 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
64 actualEvents = new Events.capture(c.stream.handleError((v) { | 64 actualEvents = new Events.capture(c.stream.handleError((v) { |
65 if (v.error is String) { | 65 if (v.error is String) { |
66 throw new AsyncError("[${v.error}]", | 66 throw new AsyncError("[${v.error}]", |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 c = new StreamController.broadcast(); | 107 c = new StreamController.broadcast(); |
108 sentEvents = new Events()..add(42) | 108 sentEvents = new Events()..add(42) |
109 ..add("snugglefluffy") | 109 ..add("snugglefluffy") |
110 ..add(7) | 110 ..add(7) |
111 ..add("42") | 111 ..add("42") |
112 ..error("not FormatException") // Unsubscribes. | 112 ..error("not FormatException") // Unsubscribes. |
113 ..close(); | 113 ..close(); |
114 expectedEvents = new Events()..add(42)..error("not FormatException"); | 114 expectedEvents = new Events()..add(42)..error("not FormatException"); |
115 actualEvents = new Events.capture( | 115 actualEvents = new Events.capture( |
116 c.stream.where((v) => v is String) | 116 c.stream.where((v) => v is String) |
117 .mappedBy((v) => int.parse(v)) | 117 .map((v) => int.parse(v)) |
118 .handleError((v) { | 118 .handleError((v) { |
119 if (v.error is! FormatException) throw v; | 119 if (v.error is! FormatException) throw v; |
120 }) | 120 }) |
121 .where((v) => v > 10), | 121 .where((v) => v > 10), |
122 unsubscribeOnError: true); | 122 unsubscribeOnError: true); |
123 sentEvents.replay(c); | 123 sentEvents.replay(c); |
124 Expect.listEquals(expectedEvents.events, actualEvents.events); | 124 Expect.listEquals(expectedEvents.events, actualEvents.events); |
125 | 125 |
126 // Test subscription changes while firing. | 126 // Test subscription changes while firing. |
127 c = new StreamController.broadcast(); | 127 c = new StreamController.broadcast(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 sentEvents = new Events() | 187 sentEvents = new Events() |
188 ..add("a string")..add(42)..add("another string")..close(); | 188 ..add("a string")..add(42)..add("another string")..close(); |
189 actualEvents = new Events.capture(c.stream.where((v) => v is String)); | 189 actualEvents = new Events.capture(c.stream.where((v) => v is String)); |
190 sentEvents.replay(c); | 190 sentEvents.replay(c); |
191 Expect.listEquals(expectedEvents.events, actualEvents.events); | 191 Expect.listEquals(expectedEvents.events, actualEvents.events); |
192 | 192 |
193 // Test map. | 193 // Test map. |
194 c = new StreamController(); | 194 c = new StreamController(); |
195 expectedEvents = new Events()..add("abab")..error("error")..close(); | 195 expectedEvents = new Events()..add("abab")..error("error")..close(); |
196 sentEvents = new Events()..add("ab")..error("error")..close(); | 196 sentEvents = new Events()..add("ab")..error("error")..close(); |
197 actualEvents = new Events.capture(c.stream.mappedBy((v) => "$v$v")); | 197 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); |
198 sentEvents.replay(c); | 198 sentEvents.replay(c); |
199 Expect.listEquals(expectedEvents.events, actualEvents.events); | 199 Expect.listEquals(expectedEvents.events, actualEvents.events); |
200 | 200 |
201 // Test handleError. | 201 // Test handleError. |
202 c = new StreamController(); | 202 c = new StreamController(); |
203 expectedEvents = new Events()..add("ab")..error("[foo]"); | 203 expectedEvents = new Events()..add("ab")..error("[foo]"); |
204 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 204 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
205 actualEvents = new Events.capture(c.stream.handleError((v) { | 205 actualEvents = new Events.capture(c.stream.handleError((v) { |
206 if (v.error is String) { | 206 if (v.error is String) { |
207 throw new AsyncError("[${v.error}]", | 207 throw new AsyncError("[${v.error}]", |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 c = new StreamController(); | 296 c = new StreamController(); |
297 sentEvents = new Events()..add(42) | 297 sentEvents = new Events()..add(42) |
298 ..add("snugglefluffy") | 298 ..add("snugglefluffy") |
299 ..add(7) | 299 ..add(7) |
300 ..add("42") | 300 ..add("42") |
301 ..error("not FormatException") // Unsubscribes. | 301 ..error("not FormatException") // Unsubscribes. |
302 ..close(); | 302 ..close(); |
303 expectedEvents = new Events()..add(42)..error("not FormatException"); | 303 expectedEvents = new Events()..add(42)..error("not FormatException"); |
304 actualEvents = new Events.capture( | 304 actualEvents = new Events.capture( |
305 c.stream.where((v) => v is String) | 305 c.stream.where((v) => v is String) |
306 .mappedBy((v) => int.parse(v)) | 306 .map((v) => int.parse(v)) |
307 .handleError((v) { | 307 .handleError((v) { |
308 if (v.error is! FormatException) throw v; | 308 if (v.error is! FormatException) throw v; |
309 }) | 309 }) |
310 .where((v) => v > 10), | 310 .where((v) => v > 10), |
311 unsubscribeOnError: true); | 311 unsubscribeOnError: true); |
312 sentEvents.replay(c); | 312 sentEvents.replay(c); |
313 Expect.listEquals(expectedEvents.events, actualEvents.events); | 313 Expect.listEquals(expectedEvents.events, actualEvents.events); |
314 | 314 |
315 // Test that only one subscription is allowed. | 315 // Test that only one subscription is allowed. |
316 c = new StreamController(); | 316 c = new StreamController(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); | 390 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); |
391 sentEvents.replay(c); | 391 sentEvents.replay(c); |
392 Expect.listEquals(expectedEvents.events, actualEvents.events); | 392 Expect.listEquals(expectedEvents.events, actualEvents.events); |
393 } | 393 } |
394 | 394 |
395 main() { | 395 main() { |
396 testMultiController(); | 396 testMultiController(); |
397 testSingleController(); | 397 testSingleController(); |
398 testExtraMethods(); | 398 testExtraMethods(); |
399 } | 399 } |
OLD | NEW |