OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 library stream_group_by_test; | 5 library stream_group_by_test; |
6 | 6 |
7 import "dart:async"; | 7 import "dart:async"; |
8 | 8 |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
11 | 11 |
12 int len(x) => x.length; | 12 int len(x) => x.length; |
13 String wrap(x) => "[$x]"; | 13 String wrap(x) => "[$x]"; |
14 | 14 |
15 void main() { | 15 void main() { |
16 asyncStart(); | 16 asyncStart(); |
17 // groupBy. | 17 // groupBy. |
18 test("splits", () async { | 18 test("splits", () async { |
19 var grouped = stringStream.groupBy<int>(len); | 19 var grouped = stringStream.groupBy<int>(len); |
20 var byLength = <int, Future<List<String>>>{}; | 20 var byLength = <int, Future<List<String>>>{}; |
21 await for (StreamGroup<int, String> group in grouped) { | 21 await for (GroupedEvents<int, String> group in grouped) { |
22 byLength[group.key] = group.values.toList(); | 22 byLength[group.key] = group.values.toList(); |
23 } | 23 } |
24 Expect.listEquals([1, 2, 4, 3], byLength.keys.toList()); | 24 Expect.listEquals([1, 2, 4, 3], byLength.keys.toList()); |
25 expectCompletes(byLength[1], ["a", "b"]); | 25 expectCompletes(byLength[1], ["a", "b"]); |
26 expectCompletes(byLength[2], ["ab"]); | 26 expectCompletes(byLength[2], ["ab"]); |
27 expectCompletes(byLength[3], ["abe", "lea"]); | 27 expectCompletes(byLength[3], ["abe", "lea"]); |
28 expectCompletes(byLength[4], ["abel", "bell", "able", "abba"]); | 28 expectCompletes(byLength[4], ["abel", "bell", "able", "abba"]); |
29 }); | 29 }); |
30 | 30 |
31 test("empty", () async { | 31 test("empty", () async { |
32 var grouped = emptyStream.groupBy<int>(len); | 32 var grouped = emptyStream.groupBy<int>(len); |
33 var byLength = <int, Future<List<String>>>{}; | 33 var byLength = <int, Future<List<String>>>{}; |
34 await for (StreamGroup<int, String> group in grouped) { | 34 await for (GroupedEvents<int, String> group in grouped) { |
35 byLength[group.key] = group.values.toList(); | 35 byLength[group.key] = group.values.toList(); |
36 } | 36 } |
37 Expect.isTrue(byLength.isEmpty); | 37 Expect.isTrue(byLength.isEmpty); |
38 }); | 38 }); |
39 | 39 |
40 test("single group", () async { | 40 test("single group", () async { |
41 var grouped = repeatStream(5, "x").groupBy<int>(len); | 41 var grouped = repeatStream(5, "x").groupBy<int>(len); |
42 var byLength = <int, Future<List<String>>>{}; | 42 var byLength = <int, Future<List<String>>>{}; |
43 await for (StreamGroup<int, String> group in grouped) { | 43 await for (GroupedEvents<int, String> group in grouped) { |
44 byLength[group.key] = group.values.toList(); | 44 byLength[group.key] = group.values.toList(); |
45 } | 45 } |
46 Expect.listEquals([1], byLength.keys.toList()); | 46 Expect.listEquals([1], byLength.keys.toList()); |
47 expectCompletes(byLength[1], ["x", "x", "x", "x", "x"]); | 47 expectCompletes(byLength[1], ["x", "x", "x", "x", "x"]); |
48 }); | 48 }); |
49 | 49 |
50 test("with error", () async { | 50 test("with error", () async { |
51 var grouped = stringErrorStream(3).groupBy<int>(len); | 51 var grouped = stringErrorStream(3).groupBy<int>(len); |
52 var byLength = <int, Future<List<String>>>{}; | 52 var byLength = <int, Future<List<String>>>{}; |
53 bool caught = false; | 53 bool caught = false; |
54 try { | 54 try { |
55 await for (StreamGroup<int, String> group in grouped) { | 55 await for (GroupedEvents<int, String> group in grouped) { |
56 byLength[group.key] = group.values.toList(); | 56 byLength[group.key] = group.values.toList(); |
57 } | 57 } |
58 } catch (e) { | 58 } catch (e) { |
59 Expect.equals("BAD", e); | 59 Expect.equals("BAD", e); |
60 caught = true; | 60 caught = true; |
61 } | 61 } |
62 Expect.isTrue(caught); | 62 Expect.isTrue(caught); |
63 Expect.listEquals([1, 2, 4], byLength.keys.toList()); | 63 Expect.listEquals([1, 2, 4], byLength.keys.toList()); |
64 expectCompletes(byLength[1], ["a", "b"]); | 64 expectCompletes(byLength[1], ["a", "b"]); |
65 expectCompletes(byLength[2], ["ab"]); | 65 expectCompletes(byLength[2], ["ab"]); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 yield start++; | 310 yield start++; |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 Stream timerStream(int count, Duration interval) async* { | 314 Stream timerStream(int count, Duration interval) async* { |
315 for (int i = 0; i < count; i++) { | 315 for (int i = 0; i < count; i++) { |
316 await new Future.delayed(interval); | 316 await new Future.delayed(interval); |
317 yield i; | 317 yield i; |
318 } | 318 } |
319 } | 319 } |
OLD | NEW |