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

Side by Side Diff: tests/lib/async/stream_distinct_test.dart

Issue 2885993005: Fix Stream.distinct. (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « sdk/lib/async/stream_pipe.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import 'dart:async';
6 import "package:expect/expect.dart";
7 import "package:async_helper/async_helper.dart";
8
9 class A {
10 const A();
11 }
12
13 class B extends A {
14 const B();
15 }
16
17 main() {
18 asyncStart();
19 // Correct behavior.
20 for (var eq in [null, (a, b) => a == b]) {
21 checkStream(mkSingleStream, eq, "single");
22 checkBroadcastStream(mkBroadcastStream, eq, "broadcast");
23 checkBroadcastStream(
24 () => mkSingleStream().asBroadcastStream(), eq, "asBroadcast");
25 }
26
27 // Regression test. Multiple listens on the same broadcast distinct stream.
28 var stream = mkBroadcastStream().distinct();
29 expectStream(stream, [1, 2, 3, 2], "broadcast.distinct#1");
30 expectStream(stream, [1, 2, 3, 2], "broadcast.distinct#2");
31
32 // Doesn't ignore equality.
33 expectStream(
34 new Stream.fromIterable([1, 2, 1, 3, 3]).distinct((a, b) => false),
35 [1, 2, 1, 3, 3],
36 "kFalse");
37 expectStream(
38 new Stream.fromIterable([1, 2, 1, 3, 3]).distinct((a, b) => true),
39 [1],
40 "kTrue");
41 expectStream(
42 new Stream.fromIterable([1, 2, 1, 3, 3]).distinct((a, b) => a != b),
43 [1, 1],
44 "neq");
45 expectStream(
46 new Stream.fromIterable([1, 2, 1, 3, 3]).distinct((a, b) => 2 == b),
47 [1, 1, 3, 3],
48 "is2");
49 // Forwards errors as errors.
50 expectStream(
51 new Stream.fromIterable([1, "E1", 2, "E2", 2, 3])
52 .map((v) => (v is String) ? (throw v) : v) // Make strings errors.
53 .distinct()
54 .transform(reifyErrors),
55 [1, "[E1]", 2, "[E2]", 3],
56 "errors");
57 // Equality throwing acts like error.
58 expectStream(
59 new Stream.fromIterable([1, "E1", 1, 2, "E2", 3])
60 .distinct((a, b) => (b is String) ? (throw b) : (a == b))
61 .transform(reifyErrors),
62 [1, "[E1]", 2, "[E2]", 3],
63 "eq-throws");
64 // Operator== throwing acts like error.
65 expectStream(
66 new Stream.fromIterable([1, 1, 2, 2, 1, 3])
67 .map((v) => new T(v))
68 .distinct()
69 .transform(reifyErrors)
70 .map((v) => v is T ? v.value : "$v"),
71 [1, "[2]", "[2]", 3],
72 "==-throws");
73 asyncEnd();
74 }
75
76 checkStream(mkStream, eq, name) {
77 expectStream(mkStream().distinct(eq), [1, 2, 3, 2], "$name.distinct");
78 expectStream(mkStream().expand((e) => [e, e]).distinct(eq), [1, 2, 3, 2],
79 "$name.expand.distinct");
80 expectStream(mkStream().where((x) => x != 3).distinct(eq), [1, 2],
81 "$name.where.distinct");
82 }
83
84 checkBroadcastStream(mkStream, eq, name) {
85 var stream = mkStream();
86 // Run all the tests, multiple times each.
87 checkStream(() => stream, eq, "$name#1");
88 checkStream(() => stream, eq, "$name#2");
89 }
90
91 mkSingleStream() async* {
92 yield 1;
93 yield 2;
94 yield 3;
95 yield 2;
96 }
97
98 mkBroadcastStream() {
99 var c = new StreamController.broadcast();
100 c.onListen = () {
101 c.addStream(mkSingleStream()).whenComplete(c.close);
102 };
103 return c.stream;
104 }
105
106 expectStream(stream, list, [name]) {
107 asyncStart();
108 return stream.toList().then((events) {
109 Expect.listEquals(list, events, name);
110 asyncEnd();
111 });
112 }
113
114 // Class where operator== throws.
115 class T {
116 final int value;
117 T(this.value);
118 int get hashCode => value.hashCode;
119 bool operator ==(Object other) =>
120 other is T && ((other.value == 2) ? throw 2 : (value == other.value));
121 }
122
123 final reifyErrors =
124 new StreamTransformer.fromHandlers(handleError: (e, s, sink) {
125 sink.add("[$e]");
126 });
OLDNEW
« no previous file with comments | « sdk/lib/async/stream_pipe.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698