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";
|
2019-02-10 18:49:48 -05:00
|
|
|
import { MultiReader, StringReader } from "./readers.ts";
|
|
|
|
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
|
|
|
});
|