/
readers_test.ts
66 lines (61 loc) · 2.23 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { LimitedReader, MultiReader, StringReader } from "./readers.ts";
import { StringWriter } from "./writers.ts";
import { copyN } from "./util.ts";
import { copy } from "../streams/copy.ts";
import { readAll } from "../streams/read_all.ts";
Deno.test("ioStringReader", async function () {
const r = new StringReader("abcdef");
const res0 = await r.read(new Uint8Array(6));
assertEquals(res0, 6);
const res1 = await r.read(new Uint8Array(6));
assertEquals(res1, null);
});
Deno.test("ioStringReader", async function () {
const decoder = new TextDecoder();
const r = new StringReader("abcdef");
const buf = new Uint8Array(3);
const res1 = await r.read(buf);
assertEquals(res1, 3);
assertEquals(decoder.decode(buf), "abc");
const res2 = await r.read(buf);
assertEquals(res2, 3);
assertEquals(decoder.decode(buf), "def");
const res3 = await r.read(buf);
assertEquals(res3, null);
assertEquals(decoder.decode(buf), "def");
});
Deno.test("ioMultiReader", async function () {
const r = new MultiReader([new StringReader("abc"), new StringReader("def")]);
const w = new StringWriter();
const n = await copyN(r, w, 4);
assertEquals(n, 4);
assertEquals(w.toString(), "abcd");
await copy(r, w);
assertEquals(w.toString(), "abcdef");
});
Deno.test("ioLimitedReader", async function () {
const decoder = new TextDecoder();
let sr = new StringReader("abc");
let r = new LimitedReader(sr, 2);
let buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "ab");
assertEquals(decoder.decode(await readAll(sr)), "c");
sr = new StringReader("abc");
r = new LimitedReader(sr, 3);
buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "abc");
assertEquals((await readAll(r)).length, 0);
sr = new StringReader("abc");
r = new LimitedReader(sr, 4);
buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "abc");
assertEquals((await readAll(r)).length, 0);
});
Deno.test("ioLimitedReader", async function () {
const rb = new StringReader("abc");
const wb = new StringWriter();
await copy(new LimitedReader(rb, -1), wb);
assertEquals(wb.toString(), "");
});