Chromium Code Reviews| Index: test/scan_test.dart |
| diff --git a/test/scan_test.dart b/test/scan_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..643d8e811c68ce10290769d65681a938df909e08 |
| --- /dev/null |
| +++ b/test/scan_test.dart |
| @@ -0,0 +1,76 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// Test stream transformers. |
| +import "dart:async"; |
| + |
| +import "package:async/async.dart"; |
| +import "package:test/test.dart"; |
| + |
| +void main() { |
| + test("addition", () { |
| + var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]); |
| + var scanned = intStream.transform(new Scan<int, int>(0, (a, v) => a + v)); |
| + expect(scanned.toList(), completion([2, 5, 9, 14, 20])); |
| + }); |
| + |
| + 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.
|
| + // Will not work correctly if parameters are swapped. |
| + var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]); |
| + var scanned = intStream.transform(new Scan<int, int>(20, (a, v) => a - v)); |
| + expect(scanned.toList(), completion([18, 15, 11, 6, 0])); |
| + }); |
| + |
| + test("no events", () { |
| + var emptyStream = new Stream<int>.empty(); |
| + var scanned = emptyStream.transform(new Scan<int, int>( |
| + 42, (a, v) => throw "unreachable")); |
| + expect(scanned.toList(), completion(isEmpty)); |
| + }); |
| + |
| + test("types", () { |
| + var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]); |
| + var scan = new Scan<int, String>("", (String a, int v) => "$a$v"); |
| + var scanned = intStream.transform(scan); |
| + expect(scanned is Stream<String>, isTrue); |
| + expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"])); |
| + }); |
| + |
| + test("types 2", () { |
| + var intStream = new Stream<int>.fromIterable([2, 3, 4, 5, 6]); |
| + var scan = new Scan<int, String>("", (String a, int v) => "$a$v"); |
| + var scanned = scan.bind(intStream); |
| + expect(scanned is Stream<String>, isTrue); |
| + expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"])); |
| + }); |
| + |
| + 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.
|
| + var stream = new Stream<String>.fromIterable(["2", "3", "4", "5", "6"]); |
| + var scan = new Scan<String, String>("", (String a, String v) => "$a$v"); |
| + var scanned = stream.transform(scan); |
| + expect(scanned is Stream<String>, isTrue); |
| + expect(scanned.toList(), completion(["2", "23", "234", "2345", "23456"])); |
| + }); |
| + |
| + test("error", () async { |
| + errorStream(int start, int end, int errorAfter) async* { |
| + for (int i = start; i < end; i++) { |
| + yield i; |
| + if (i == errorAfter) { |
| + 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.
|
| + } |
| + } |
| + } |
| + var scanned = errorStream(0, 5, 3) |
| + .transform(new Scan<int, String>("", (a, b) => "$a$b")); |
| + var list = []; |
| + var result = scanned.forEach(list.add); |
| + try { |
| + await result; |
| + } catch (e) { |
| + expect(e, "BAD"); |
| + } |
| + expect(list, ["0", "01", "012", "0123"]); |
| + }); |
| +} |