1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/io/readers_test.ts
2019-02-10 18:49:48 -05:00

36 lines
1.1 KiB
TypeScript

import { assert, test } from "../testing/mod.ts";
import { MultiReader, StringReader } from "./readers.ts";
import { StringWriter } from "./writers.ts";
import { copy } from "deno";
import { copyN } from "./ioutil.ts";
import { decode } from "../strings/strings.ts";
test(async function ioStringReader() {
const r = new StringReader("abcdef");
const { nread, eof } = await r.read(new Uint8Array(6));
assert.equal(nread, 6);
assert.equal(eof, true);
});
test(async function ioStringReader() {
const r = new StringReader("abcdef");
const buf = new Uint8Array(3);
let res1 = await r.read(buf);
assert.equal(res1.nread, 3);
assert.equal(res1.eof, false);
assert.equal(decode(buf), "abc");
let res2 = await r.read(buf);
assert.equal(res2.nread, 3);
assert.equal(res2.eof, true);
assert.equal(decode(buf), "def");
});
test(async function ioMultiReader() {
const r = new MultiReader(new StringReader("abc"), new StringReader("def"));
const w = new StringWriter();
const n = await copyN(w, r, 4);
assert.equal(n, 4);
assert.equal(w.toString(), "abcd");
await copy(w, r);
assert.equal(w.toString(), "abcdef");
});