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

Side by Side Diff: test/scan_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("addition", () {
13 var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]);
14 var scanned = intStream.transform(new Scan<int, int>(0, (a, v) => a + v));
15 expect(scanned.toList(), completion([2, 5, 9, 14, 20]));
16 });
17
18 test("subtraction", () {
nweiz 2016/03/01 02:10:04 Given this, I think testing addition is probably r
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Acknowledged.
19 // Will not work correctly if parameters are swapped.
20 var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]);
21 var scanned = intStream.transform(new Scan<int, int>(20, (a, v) => a - v));
22 expect(scanned.toList(), completion([18, 15, 11, 6, 0]));
23 });
24
25 test("no events", () {
26 var emptyStream = new Stream<int>.empty();
27 var scanned = emptyStream.transform(new Scan<int, int>(
28 42, (a, v) => throw "unreachable"));
29 expect(scanned.toList(), completion(isEmpty));
30 });
31
32 test("types", () {
33 var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]);
34 var scan = new Scan<int, String>("", (String a, int v) => "$a$v");
35 var scanned = intStream.transform(scan);
36 expect(scanned is Stream<String>, isTrue);
37 expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"]));
38 });
39
40 test("types 2", () {
41 var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]);
42 var scan = new Scan<int, String>("", (String a, int v) => "$a$v");
43 var scanned = scan.bind(intStream);
44 expect(scanned is Stream<String>, isTrue);
45 expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"]));
46 });
47
48 test("types 3", () {
nweiz 2016/03/01 02:10:04 Explain what these tests are actually asserting.
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Acknowledged.
49 var stream = new Stream<String>.fromIterable(["2", "3", "4", "5", "6"]);
50 var scan = new Scan<String, String>("", (String a, String v) => "$a$v");
51 var scanned = stream.transform(scan);
52 expect(scanned is Stream<String>, isTrue);
53 expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"]));
54 });
55
56 test("error", () async {
57 errorStream(int start, int end, int errorAfter) async* {
58 for (int i = start; i < end; i++) {
59 yield i;
60 if (i == errorAfter) {
61 yield* () async* { throw "BAD"; }(); // Stream with error.
nweiz 2016/03/01 02:10:04 I think it's cleaner to yield Future.error.
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Probably true. I got tired of writing "new Future.
62 }
63 }
64 }
65 var scanned = errorStream(0, 5, 3)
66 .transform(new Scan<int, String>("", (a, b) => "$a$b"));
67 var list = [];
68 var result = scanned.forEach(list.add);
69 try {
70 await result;
71 } catch (e) {
72 expect(e, "BAD");
73 }
74 expect(list, ["0", "01", "012", "0123"]);
75 });
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698