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

Side by Side Diff: test/byte_collection_test.dart

Issue 2649233006: Add `byteCollector` stream transformer and `collectBytes` function. (Closed)
Patch Set: Created 3 years, 10 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
« lib/src/byte_collector.dart ('K') | « pubspec.yaml ('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 "dart:typed_data";
7
8 import "package:test/test.dart";
9 import "package:async/async.dart" show byteCollector, collectBytes, Result;
10
11 void main() {
12 group("collectBytes", () {
13 test("simple list and overflow", () {
14 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.
15 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.
16 expect(result, completion(expected));
17 });
18
19 test("no events", () {
20 Future<Uint8List> result = collectBytes(streamOf([]));
21 var expected = new Uint8List(0);
22 expect(result, completion(expected));
23 });
24
25 test("empty events", () {
26 Future<Uint8List> result = collectBytes(streamOf([[], []]));
27 var expected = new Uint8List(0);
28 expect(result, completion(expected));
29 });
30
31 test("error event", () {
32 Future<Uint8List> result = collectBytes(streamOf(
33 new Iterable.generate(3, (n) => n == 2 ? throw "badness" : [n])));
34 expect(result, throwsA("badness"));
35 });
36 });
37
38 group ("byteCollector", () {
39 test("simple list and overflow", () {
40 var stream = streamOf([[0], [1], [2], [256]]);
41 Stream<Uint8List> result = stream.transform(byteCollector);
42 var expected = new Uint8List.fromList([0, 1, 2, 0]);
43 expect(result.single, completion(expected));
44 });
45
46 test("no events", () {
47 Stream<Uint8List> result = streamOf([]).transform(byteCollector);
48 var expected = new Uint8List(0);
49 expect(result.single, completion(expected));
50 });
51
52 test("empty events", () {
53 Stream<Uint8List> result = streamOf([[], []]).transform(byteCollector);
54 var expected = new Uint8List(0);
55 expect(result.single, completion(expected));
56 });
57
58 test("error event", () async {
59 var stream = streamOf(
60 new Iterable.generate(3, (n) => n == 2 ? throw "badness" : [n]));
61 Stream<Uint8List> result = stream.transform(byteCollector);
62 var expected = new Uint8List.fromList([0, 1]);
63 var results = await Result.captureStream(result).toList();
64 expect(results[0].asFuture, throwsA("badness"));
65 expect(results[1].asFuture, completion(expected));
66 expect(results.length, 2);
67 });
68
69 test("multiple error events", () async {
70 var stream = () async* {
71 yield [0];
72 yield* new Future.error("badness1").asStream();
73 yield [2];
74 yield* new Future.error("badness3").asStream();
75 yield [4];
76 }();
77 Stream<Uint8List> result = stream.transform(byteCollector);
78 var expected = new Uint8List.fromList([0, 2, 4]);
79 var results = await Result.captureStream(result).toList();
80 expect(results[0].asFuture, throwsA("badness1"));
81 expect(results[1].asFuture, throwsA("badness3"));
82 expect(results[2].asFuture, completion(expected));
83 expect(results.length, 3);
84 });
85
86
87
nweiz 2017/01/25 23:00:40 Extra empty lines.
Lasse Reichstein Nielsen 2017/01/26 09:43:06 Done.
88 });
89
90
91
92
93
94 }
95
96 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
97 for (var list in lists) yield list;
98 }
99
100
101
OLDNEW
« 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