2019-02-26 00:35:50 -05:00
|
|
|
const { copy } = Deno;
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
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";
|
2019-05-26 19:58:31 -04:00
|
|
|
import { decode } from "../strings/mod.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ioStringReader(): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new StringReader("abcdef");
|
2019-02-19 12:38:19 -05:00
|
|
|
const { nread, eof } = await r.read(new Uint8Array(6));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(nread, 6);
|
|
|
|
assertEquals(eof, true);
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ioStringReader(): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new StringReader("abcdef");
|
|
|
|
const buf = new Uint8Array(3);
|
|
|
|
let res1 = await r.read(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(res1.nread, 3);
|
|
|
|
assertEquals(res1.eof, false);
|
|
|
|
assertEquals(decode(buf), "abc");
|
2019-02-10 18:49:48 -05:00
|
|
|
let res2 = await r.read(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(res2.nread, 3);
|
|
|
|
assertEquals(res2.eof, true);
|
|
|
|
assertEquals(decode(buf), "def");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ioMultiReader(): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const r = new MultiReader(new StringReader("abc"), new StringReader("def"));
|
|
|
|
const w = new StringWriter();
|
|
|
|
const n = await copyN(w, r, 4);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 4);
|
|
|
|
assertEquals(w.toString(), "abcd");
|
2019-02-10 18:49:48 -05:00
|
|
|
await copy(w, r);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(w.toString(), "abcdef");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|