1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/io/readers_test.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

const { copy } = Deno;
2019-03-06 16:39:50 -05:00
import { test } from "../testing/mod.ts";
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";
import { decode } from "../strings/strings.ts";
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 { nread, eof } = await r.read(new Uint8Array(6));
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);
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);
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);
assertEquals(n, 4);
assertEquals(w.toString(), "abcd");
2019-02-10 18:49:48 -05:00
await copy(w, r);
assertEquals(w.toString(), "abcdef");
2019-02-10 18:49:48 -05:00
});