Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library stream_group_by_test; | |
| 6 | |
| 7 import "dart:async"; | |
| 8 | |
| 9 import "package:expect/expect.dart"; | |
| 10 import "package:async_helper/async_helper.dart"; | |
| 11 | |
| 12 int len(x) => x.length; | |
| 13 String wrap(x) => "[$x]"; | |
| 14 | |
| 15 void main() { | |
| 16 asyncStart(); | |
| 17 // groupBy. | |
| 18 test("splits", () async { | |
| 19 var grouped = stringStream.groupBy<int>(len); | |
| 20 var byLength = {}; | |
| 21 await for (StreamGroup<int, String> group in grouped) { | |
| 22 byLength[group.key] = group.values.toList(); | |
| 23 } | |
| 24 Expect.listEquals([1, 2, 4, 3], byLength.keys.toList()); | |
| 25 expectCompletes(byLength[1], ["a", "b"]); | |
| 26 expectCompletes(byLength[2], ["ab"]); | |
| 27 expectCompletes(byLength[3], ["abe", "lea"]); | |
| 28 expectCompletes(byLength[4], ["abel", "bell", "able", "abba"]); | |
| 29 }); | |
| 30 | |
| 31 test("empty", () async { | |
| 32 var grouped = emptyStream.groupBy<int>(len); | |
| 33 var byLength = {}; | |
| 34 await for (StreamGroup<int, String> group in grouped) { | |
| 35 byLength[group.key] = group.values.toList(); | |
| 36 } | |
| 37 Expect.isTrue(byLength.isEmpty); | |
| 38 }); | |
| 39 | |
| 40 test("single group", () async { | |
| 41 var grouped = repeatStream(5, "x").groupBy<int>(len); | |
| 42 var byLength = {}; | |
| 43 await for (StreamGroup<int, String> group in grouped) { | |
| 44 byLength[group.key] = group.values.toList(); | |
| 45 } | |
| 46 Expect.listEquals([1], byLength.keys.toList()); | |
| 47 expectCompletes(byLength[1], ["x", "x", "x", "x", "x"]); | |
| 48 }); | |
| 49 | |
| 50 test("with error", () async { | |
| 51 var grouped = stringErrorStream(3).groupBy<int>(len); | |
| 52 var byLength = {}; | |
| 53 bool caught = false; | |
| 54 try { | |
| 55 await for (StreamGroup<int, String> group in grouped) { | |
| 56 byLength[group.key] = group.values.toList(); | |
| 57 } | |
| 58 } catch (e) { | |
| 59 Expect.equals("BAD", e); | |
| 60 caught = true; | |
| 61 } | |
| 62 Expect.isTrue(caught); | |
| 63 Expect.listEquals([1, 2, 4], byLength.keys.toList()); | |
| 64 expectCompletes(byLength[1], ["a", "b"]); | |
| 65 expectCompletes(byLength[2], ["ab"]); | |
| 66 expectCompletes(byLength[4], ["abel"]); | |
| 67 }); | |
| 68 | |
| 69 // groupValuesBy | |
|
floitsch
2017/05/02 12:23:53
Nit. finish with ".".
Lasse Reichstein Nielsen
2017/05/04 11:05:20
Every test below here removed. Problem solved!
| |
| 70 test("splits", () async { | |
|
floitsch
2017/05/02 12:23:53
There is already a test called "splits".
Lasse Reichstein Nielsen
2017/05/04 11:05:20
Acknowledged.
| |
| 71 var grouped = stringStream.groupValuesBy<int, String>(len, wrap); | |
| 72 var byLength = {}; | |
| 73 await for (StreamGroup<int, String> group in grouped) { | |
| 74 byLength[group.key] = group.values.toList(); | |
| 75 } | |
| 76 Expect.listEquals([1, 2, 4, 3], byLength.keys.toList()); | |
| 77 expectCompletes(byLength[1], ["[a]", "[b]"]); | |
| 78 expectCompletes(byLength[2], ["[ab]"]); | |
| 79 expectCompletes(byLength[3], ["[abe]", "[lea]"]); | |
| 80 expectCompletes(byLength[4], ["[abel]", "[bell]", "[able]", "[abba]"]); | |
| 81 }); | |
| 82 | |
| 83 test("empty", () async { | |
| 84 var grouped = emptyStream.groupValuesBy<int>(len, wrap); | |
| 85 var byLength = {}; | |
| 86 await for (StreamGroup<int, String> group in grouped) { | |
| 87 byLength[group.key] = group.values.toList(); | |
| 88 } | |
| 89 Expect.isTrue(byLength.isEmpty); | |
| 90 }); | |
| 91 | |
| 92 test("single group", () async { | |
| 93 var grouped = repeatStream(5, "x").groupValuesBy<int>(len, wrap); | |
| 94 var byLength = {}; | |
| 95 await for (StreamGroup<int, String> group in grouped) { | |
| 96 byLength[group.key] = group.values.toList(); | |
| 97 } | |
| 98 Expect.listEquals([1], byLength.keys.toList()); | |
| 99 expectCompletes(byLength[1], ["[x]", "[x]", "[x]", "[x]", "[x]"]); | |
| 100 }); | |
| 101 | |
| 102 test("with error", () async { | |
| 103 var grouped = stringErrorStream(3).groupValuesBy<int>(len, wrap); | |
| 104 var byLength = {}; | |
| 105 bool caught = false; | |
| 106 try { | |
| 107 await for (StreamGroup<int, String> group in grouped) { | |
| 108 byLength[group.key] = group.values.toList(); | |
| 109 } | |
| 110 } catch (e) { | |
| 111 Expect.equals("BAD", e); | |
| 112 caught = true; | |
| 113 } | |
| 114 Expect.isTrue(caught); | |
| 115 Expect.listEquals([1, 2, 4], byLength.keys.toList()); | |
| 116 expectCompletes(byLength[1], ["[a]", "[b]"]); | |
| 117 expectCompletes(byLength[2], ["[ab]"]); | |
| 118 expectCompletes(byLength[4], ["[abel]"]); | |
| 119 }); | |
| 120 | |
| 121 asyncEnd(); | |
| 122 } | |
| 123 | |
| 124 expectCompletes(future, result) { | |
| 125 asyncStart(); | |
| 126 future.then((v) { | |
| 127 if (result is List) { | |
| 128 Expect.listEquals(result, v); | |
| 129 } else { | |
| 130 Expect.equals(v, result); | |
| 131 } | |
| 132 asyncEnd(); | |
| 133 }, onError: (e, s) { | |
| 134 Expect.fail("$e\n$s"); | |
| 135 }); | |
| 136 } | |
| 137 | |
| 138 void test(name, func) { | |
| 139 asyncStart(); | |
| 140 func().then((_) { | |
| 141 asyncEnd(); | |
| 142 }, onError: (e, s) { | |
| 143 Expect.fail("$name: $e\n$s"); | |
| 144 }); | |
| 145 } | |
| 146 | |
| 147 var strings = const [ | |
| 148 "a", | |
| 149 "ab", | |
| 150 "b", | |
| 151 "abel", | |
| 152 "abe", | |
| 153 "bell", | |
| 154 "able", | |
| 155 "abba", | |
| 156 "lea" | |
| 157 ]; | |
| 158 | |
| 159 Stream<String> get stringStream async* { | |
| 160 for (var string in strings) { | |
| 161 yield string; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 Stream get emptyStream async* {} | |
| 166 | |
| 167 Stream repeatStream(int count, value) async* { | |
| 168 for (var i = 0; i < count; i++) { | |
| 169 yield value; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 // Just some valid stack trace. | |
| 174 var stack = StackTrace.current; | |
| 175 | |
| 176 Stream<String> stringErrorStream(int errorAfter) async* { | |
| 177 for (int i = 0; i < strings.length; i++) { | |
| 178 yield strings[i]; | |
| 179 if (i == errorAfter) { | |
| 180 // Emit error, but continue afterwards. | |
| 181 yield* new Future.error("BAD", stack).asStream(); | |
| 182 } | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 Stream intStream(int count, [int start = 0]) async* { | |
| 187 for (int i = 0; i < count; i++) { | |
| 188 yield start++; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 Stream timerStream(int count, Duration interval) async* { | |
| 193 for (int i = 0; i < count; i++) { | |
| 194 await new Future.delayed(interval); | |
| 195 yield i; | |
| 196 } | |
| 197 } | |
| OLD | NEW |