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-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));
|
|
|
|
assertEquals(res1, Deno.EOF);
|
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-07-07 15:20:41 -04:00
|
|
|
assertEquals(res1, 3);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decode(buf), "abc");
|
2019-02-10 18:49:48 -05:00
|
|
|
let res2 = await r.read(buf);
|
2019-07-07 15:20:41 -04:00
|
|
|
assertEquals(res2, 3);
|
|
|
|
assertEquals(decode(buf), "def");
|
|
|
|
let res3 = await r.read(buf);
|
|
|
|
assertEquals(res3, Deno.EOF);
|
2019-03-06 19:42:24 -05:00
|
|
|
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
|
|
|
});
|