-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
_readers_test.ts
27 lines (26 loc) · 975 Bytes
/
_readers_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { streamReader } from "./_readers.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { group } from "./_test_util.ts";
import { decode, encode } from "./_util.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
group("streamReader", ({ test }) => {
test("basic", async () => {
const stream = new ReadableStream<Uint8Array>({
start(ctrl) {
ctrl.enqueue(encode("Go "));
ctrl.enqueue(encode("To "));
ctrl.enqueue(encode("-> [deno.land]"));
ctrl.close();
},
});
const sr = streamReader(stream);
const buf = new Uint8Array(3);
const dest = new Buffer();
let result: null | number = 0;
while ((result = await sr.read(buf)) !== null) {
await dest.write(buf.subarray(0, result));
}
assertEquals(decode(dest.bytes()), "Go To -> [deno.land]");
});
});