Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: test/group_by_test.dart

Issue 1648963002: Add reactive-inspired stream transformers: Base URL: https://github.com/dart-lang/async@master
Patch Set: Restructure failes and add more tests. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 // Test stream transformers.
6 import "dart:async";
7
8 import "package:async/async.dart";
9 import "package:test/test.dart";
10
11 void main() {
12 test("splits", () async {
13 var stream = new Stream.fromIterable(
14 ["a", "ab", "b", "abel", "abe", "bell", "able", "abba", "lea"]);
15 var grouped = stream
16 .transform(new GroupBy<String, int, String>(key: (x) => x.length));
17 var byLength = {};
18 await for (Group<int, String> group in grouped) {
19 byLength[group.key] = group.values.toList();
20 }
21 expect(byLength.keys.toList(), [1, 2, 4, 3]);
22 expect(byLength[1], completion(["a", "b"]));
23 expect(byLength[2], completion(["ab"]));
24 expect(byLength[3], completion(["abe", "lea"]));
25 expect(byLength[4], completion(["abel", "bell", "able", "abba"]));
26 });
27
28 test("value transforms", () async {
29 var stream = new Stream.fromIterable(
30 ["a", "ab", "b", "abel", "abe", "bell", "able", "abba", "lea"]);
31 var grouped = stream.transform(new GroupBy<String, int, String>(
32 key: (x) => x.length, value: (s) => "<$s>"));
33 var byLength = {};
34 await for (Group<int, String> group in grouped) {
35 byLength[group.key] = group.values.toList();
36 }
37 expect(byLength.keys.toList(), [1, 2, 4, 3]);
38 expect(byLength[1], completion(["<a>", "<b>"]));
39 expect(byLength[2], completion(["<ab>"]));
40 expect(byLength[3], completion(["<abe>", "<lea>"]));
41 expect(byLength[4], completion(["<abel>", "<bell>", "<able>", "<abba>"]));
42 });
43
44 test("empty source", () async {
45 var emptyStream = new Stream<int>.empty();
46 var grouped = emptyStream.transform(new GroupBy<int, int, String>(
47 key: (_) => throw "key unreachable",
48 value: (_) => throw "value unreachable"));
49 expect(grouped.toList(), completion(isEmpty));
50 });
51
52 test("single group", () async {
53 var stream = new Stream<String>.fromIterable(["a", "b", "c", "d", "e"]);
54 var grouped = stream
55 .transform(new GroupBy<String, int, String>(key: (x) => x.length));
56 var byLength = {};
57 await for (Group<int, String> group in grouped) {
58 byLength[group.key] = group.values.toList();
59 }
60 expect(byLength.keys, [1]);
61 expect(byLength[1], completion(["a", "b", "c", "d", "e"]));
62 });
63
64 test("source error", () async {
65 var stream = new Stream<String>.fromIterable(
66 ["a", "ab", "b", "abel", "X", "abe", "bell", "able", "abba", "lea"]);
67 var errorStream = stream.map((v) => v == "X" ? throw "BAD" : v);
68 var grouped = errorStream
69 .transform(new GroupBy<String, int, String>(key: (x) => x.length));
70 var byLength = {};
71 int errorCount = 0;
72 await for (Result<Group<int, String>> result in
73 Result.captureStream(grouped)) {
74 if (result.isValue) {
75 var group = result.asValue.value;
76 byLength[group.key] = group.values.toList();
77 } else {
78 errorCount++;
79 expect(result.asError.error, "BAD");
80 }
81 }
82 expect(errorCount, 1);
83 // Doesn't stop at the error event.
84 expect(byLength.keys.toList(), [1, 2, 4, 3]);
85 expect(byLength[1], completion(["a", "b"]));
86 expect(byLength[2], completion(["ab"]));
87 expect(byLength[3], completion(["abe", "lea"]));
88 expect(byLength[4], completion(["abel", "bell", "able", "abba"]));
89 });
90
91 test("key error", () async {
92 var stream = new Stream<String>.fromIterable(
93 ["a", "ab", "b", "abel", "X", "abe", "bell", "able", "abba", "lea"]);
94 badKey(x) => x == "X" ? throw "BAD" : x.length;
95 var grouped =
96 stream.transform(new GroupBy<String, int, String>(key: badKey));
97 var byLength = {};
98 int errorCount = 0;
99 await for (Result<Group<int, String>> result in
100 Result.captureStream(grouped)) {
101 if (result.isValue) {
102 var group = result.asValue.value;
103 byLength[group.key] = group.values.toList();
104 } else {
105 errorCount++;
106 expect(result.asError.error, "BAD");
107 }
108 }
109 expect(errorCount, 1);
110 // Doesn't stop at the error event.
111 expect(byLength.keys.toList(), [1, 2, 4, 3]);
112 expect(byLength[1], completion(["a", "b"]));
113 expect(byLength[2], completion(["ab"]));
114 expect(byLength[3], completion(["abe", "lea"]));
115 expect(byLength[4], completion(["abel", "bell", "able", "abba"]));
116 });
117
118 test("value error", () async {
119 var stream = new Stream<String>.fromIterable(
120 ["a", "ab", "b", "abel", "XXX", "abe", "bell", "able", "abba", "lea"]);
121 badValue(x) => x == "XXX" ? throw "BAD" : x;
122 var grouped = stream.transform(new GroupBy<String, int, String>(
123 key: (x) => x.length, value: badValue));
124 var byLength = {};
125 await for (Group<int, String> group in grouped) {
126 byLength[group.key] = Result.captureStream(group.values).toList();
127 }
128 // Doesn't stop at the error event.
129 expect(byLength.keys.toList(), [1, 2, 4, 3]);
130 Result r(v) => new Result<String>.value(v);
131 expect(byLength[1], completion([r("a"), r("b")]));
132 expect(byLength[2], completion([r("ab")]));
133 /// The length-3 stream starts with an error "BAD" and then two values.
134 expect(byLength[3].then((v) => v.first.isError),
135 completion(true));
136 expect(byLength[3].then((v) => v.first.asError.error),
137 completion("BAD"));
138 expect(byLength[3].then((v) => v.sublist(1)),
139 completion([r("abe"), r("lea")]));
140 expect(byLength[4],
141 completion([r("abel"), r("bell"), r("able"), r("abba")]));
142 });
143 }
nweiz 2016/03/01 02:10:04 Test pausing and canceling behavior as well.
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Acknowledged.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698