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

Unified Diff: test/byte_collection_test.dart

Issue 2649233006: Add `byteCollector` stream transformer and `collectBytes` function. (Closed)
Patch Set: Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« lib/src/byte_collector.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/byte_collection_test.dart
diff --git a/test/byte_collection_test.dart b/test/byte_collection_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6afc6997c6f6f76bdb1a6f9621060bd8d4b1ce0b
--- /dev/null
+++ b/test/byte_collection_test.dart
@@ -0,0 +1,101 @@
+// Copyright (c) 2017, 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.
+
+import "dart:async";
+import "dart:typed_data";
+
+import "package:test/test.dart";
+import "package:async/async.dart" show byteCollector, collectBytes, Result;
+
+void main() {
+ group("collectBytes", () {
+ test("simple list and overflow", () {
+ Future<Uint8List> result = collectBytes(streamOf([[0], [1], [2], [256]]));
nweiz 2017/01/25 23:00:40 "var", also below.
Lasse Reichstein Nielsen 2017/01/26 09:43:06 Done.
+ var expected = new Uint8List.fromList([0, 1, 2, 0]);
nweiz 2017/01/25 23:00:40 Just compare against a plain list—test doesn't val
Lasse Reichstein Nielsen 2017/01/26 09:43:06 Done.
+ expect(result, completion(expected));
+ });
+
+ test("no events", () {
+ Future<Uint8List> result = collectBytes(streamOf([]));
+ var expected = new Uint8List(0);
+ expect(result, completion(expected));
+ });
+
+ test("empty events", () {
+ Future<Uint8List> result = collectBytes(streamOf([[], []]));
+ var expected = new Uint8List(0);
+ expect(result, completion(expected));
+ });
+
+ test("error event", () {
+ Future<Uint8List> result = collectBytes(streamOf(
+ new Iterable.generate(3, (n) => n == 2 ? throw "badness" : [n])));
+ expect(result, throwsA("badness"));
+ });
+ });
+
+ group ("byteCollector", () {
+ test("simple list and overflow", () {
+ var stream = streamOf([[0], [1], [2], [256]]);
+ Stream<Uint8List> result = stream.transform(byteCollector);
+ var expected = new Uint8List.fromList([0, 1, 2, 0]);
+ expect(result.single, completion(expected));
+ });
+
+ test("no events", () {
+ Stream<Uint8List> result = streamOf([]).transform(byteCollector);
+ var expected = new Uint8List(0);
+ expect(result.single, completion(expected));
+ });
+
+ test("empty events", () {
+ Stream<Uint8List> result = streamOf([[], []]).transform(byteCollector);
+ var expected = new Uint8List(0);
+ expect(result.single, completion(expected));
+ });
+
+ test("error event", () async {
+ var stream = streamOf(
+ new Iterable.generate(3, (n) => n == 2 ? throw "badness" : [n]));
+ Stream<Uint8List> result = stream.transform(byteCollector);
+ var expected = new Uint8List.fromList([0, 1]);
+ var results = await Result.captureStream(result).toList();
+ expect(results[0].asFuture, throwsA("badness"));
+ expect(results[1].asFuture, completion(expected));
+ expect(results.length, 2);
+ });
+
+ test("multiple error events", () async {
+ var stream = () async* {
+ yield [0];
+ yield* new Future.error("badness1").asStream();
+ yield [2];
+ yield* new Future.error("badness3").asStream();
+ yield [4];
+ }();
+ Stream<Uint8List> result = stream.transform(byteCollector);
+ var expected = new Uint8List.fromList([0, 2, 4]);
+ var results = await Result.captureStream(result).toList();
+ expect(results[0].asFuture, throwsA("badness1"));
+ expect(results[1].asFuture, throwsA("badness3"));
+ expect(results[2].asFuture, completion(expected));
+ expect(results.length, 3);
+ });
+
+
+
nweiz 2017/01/25 23:00:40 Extra empty lines.
Lasse Reichstein Nielsen 2017/01/26 09:43:06 Done.
+ });
+
+
+
+
+
+}
+
+streamOf(Iterable<List<int>> lists) async* {
nweiz 2017/01/25 23:00:40 Why not just use new Stream.fromIterable()?
Lasse Reichstein Nielsen 2017/01/26 09:43:06 Too easy! I had something more complicated here or
+ for (var list in lists) yield list;
+}
+
+
+
« lib/src/byte_collector.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698