OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.streams.streambuffer_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 import 'package:test/test.dart'; |
| 19 import 'package:quiver/streams.dart'; |
| 20 |
| 21 void main() { |
| 22 group("StreamBuffer", () { |
| 23 test("returns orderly overlaps", () { |
| 24 StreamBuffer<int> buf = new StreamBuffer(); |
| 25 new Stream.fromIterable([[1], [2, 3, 4], [5, 6, 7, 8]]).pipe(buf); |
| 26 return Future.wait([buf.read(2), buf.read(4), buf.read(2)]).then((vals) { |
| 27 expect(vals[0], equals([1, 2])); |
| 28 expect(vals[1], equals([3, 4, 5, 6])); |
| 29 expect(vals[2], equals([7, 8])); |
| 30 }).then((_) { |
| 31 buf.close(); |
| 32 }); |
| 33 }); |
| 34 |
| 35 test("respects pausing of stream", () { |
| 36 StreamBuffer<int> buf = new StreamBuffer()..limit = 2; |
| 37 new Stream.fromIterable([[1], [2], [3], [4]]).pipe(buf); |
| 38 return buf.read(2).then((val) { |
| 39 expect(val, [1, 2]); |
| 40 }).then((_) { |
| 41 return buf.read(2); |
| 42 }).then((val) { |
| 43 expect(val, [3, 4]); |
| 44 }); |
| 45 }); |
| 46 |
| 47 test("throws when reading too much", () { |
| 48 StreamBuffer<int> buf = new StreamBuffer()..limit = 1; |
| 49 new Stream.fromIterable([[1], [2]]).pipe(buf); |
| 50 try { |
| 51 buf.read(2); |
| 52 } catch (e) { |
| 53 expect(e, isArgumentError); |
| 54 return; |
| 55 } |
| 56 fail("error not thrown when reading more data than buffer limit"); |
| 57 }); |
| 58 |
| 59 test("allows patching of streams", () { |
| 60 StreamBuffer<int> buf = new StreamBuffer(); |
| 61 new Stream.fromIterable([1, 2]).pipe(buf).then((_) { |
| 62 return new Stream.fromIterable([3, 4]).pipe(buf); |
| 63 }); |
| 64 return Future.wait([buf.read(1), buf.read(2), buf.read(1)]).then((vals) { |
| 65 expect(vals[0], equals([1])); |
| 66 expect(vals[1], equals([2, 3])); |
| 67 expect(vals[2], equals([4])); |
| 68 }); |
| 69 }); |
| 70 |
| 71 test("underflows when asked to", () { |
| 72 StreamBuffer<int> buf = new StreamBuffer(throwOnError: true); |
| 73 var error; |
| 74 var future = buf.read(4).then((bytes) { |
| 75 fail("should not have gotten bytes: $bytes"); |
| 76 }).catchError((e) { |
| 77 error = e; |
| 78 }).then((_) { |
| 79 expect(error is UnderflowError, isTrue, |
| 80 reason: "!UnderflowError: $error"); |
| 81 }); |
| 82 new Stream.fromIterable([1, 2, 3]).pipe(buf); |
| 83 return future; |
| 84 }); |
| 85 }); |
| 86 } |
OLD | NEW |