2020-02-11 11:24:27 -05:00
|
|
|
const { copy, test } = Deno;
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2020-06-01 18:37:59 -04:00
|
|
|
import { LimitedReader, MultiReader, StringReader } from "./readers.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
import { StringWriter } from "./writers.ts";
|
|
|
|
import { copyN } from "./ioutil.ts";
|
2020-04-01 15:23:39 -04:00
|
|
|
import { decode } from "../encoding/utf8.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("ioStringReader", async function (): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new StringReader("abcdef");
|
2019-07-07 15:20:41 -04:00
|
|
|
const res0 = await r.read(new Uint8Array(6));
|
|
|
|
assertEquals(res0, 6);
|
|
|
|
const res1 = await r.read(new Uint8Array(6));
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(res1, null);
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("ioStringReader", async function (): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new StringReader("abcdef");
|
|
|
|
const buf = new Uint8Array(3);
|
2019-10-05 12:02:34 -04:00
|
|
|
const res1 = await r.read(buf);
|
2019-07-07 15:20:41 -04:00
|
|
|
assertEquals(res1, 3);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decode(buf), "abc");
|
2019-10-05 12:02:34 -04:00
|
|
|
const res2 = await r.read(buf);
|
2019-07-07 15:20:41 -04:00
|
|
|
assertEquals(res2, 3);
|
|
|
|
assertEquals(decode(buf), "def");
|
2019-10-05 12:02:34 -04:00
|
|
|
const res3 = await r.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(res3, null);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decode(buf), "def");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("ioMultiReader", async function (): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new MultiReader(new StringReader("abc"), new StringReader("def"));
|
|
|
|
const w = new StringWriter();
|
2020-04-26 16:26:02 -04:00
|
|
|
const n = await copyN(r, w, 4);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 4);
|
|
|
|
assertEquals(w.toString(), "abcd");
|
2020-04-24 18:09:14 -04:00
|
|
|
await copy(r, w);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(w.toString(), "abcdef");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
2020-06-01 18:37:59 -04:00
|
|
|
|
|
|
|
test("ioLimitedReader", async function (): Promise<void> {
|
|
|
|
let sr = new StringReader("abc");
|
|
|
|
let r = new LimitedReader(sr, 2);
|
|
|
|
let buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "ab");
|
|
|
|
assertEquals(decode(await Deno.readAll(sr)), "c");
|
|
|
|
sr = new StringReader("abc");
|
|
|
|
r = new LimitedReader(sr, 3);
|
|
|
|
buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "abc");
|
|
|
|
assertEquals((await Deno.readAll(r)).length, 0);
|
|
|
|
sr = new StringReader("abc");
|
|
|
|
r = new LimitedReader(sr, 4);
|
|
|
|
buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "abc");
|
|
|
|
assertEquals((await Deno.readAll(r)).length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("ioLimitedReader", async function (): Promise<void> {
|
|
|
|
const rb = new StringReader("abc");
|
|
|
|
const wb = new StringWriter();
|
|
|
|
await Deno.copy(new LimitedReader(rb, -1), wb);
|
|
|
|
assertEquals(wb.toString(), "");
|
|
|
|
});
|