2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2020-06-01 18:37:59 -04:00
|
|
|
import { LimitedReader, MultiReader, StringReader } from "./readers.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
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-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
await Deno.copy(r, w);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(w.toString(), "abcdef");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
2020-06-01 18:37:59 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("ioLimitedReader", async function (): Promise<void> {
|
2020-06-01 18:37:59 -04:00
|
|
|
let sr = new StringReader("abc");
|
|
|
|
let r = new LimitedReader(sr, 2);
|
|
|
|
let buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "ab");
|
|
|
|
assertEquals(decode(await Deno.readAll(sr)), "c");
|
|
|
|
sr = new StringReader("abc");
|
|
|
|
r = new LimitedReader(sr, 3);
|
|
|
|
buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "abc");
|
|
|
|
assertEquals((await Deno.readAll(r)).length, 0);
|
|
|
|
sr = new StringReader("abc");
|
|
|
|
r = new LimitedReader(sr, 4);
|
|
|
|
buffer = await Deno.readAll(r);
|
|
|
|
assertEquals(decode(buffer), "abc");
|
|
|
|
assertEquals((await Deno.readAll(r)).length, 0);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("ioLimitedReader", async function (): Promise<void> {
|
2020-06-01 18:37:59 -04:00
|
|
|
const rb = new StringReader("abc");
|
|
|
|
const wb = new StringWriter();
|
|
|
|
await Deno.copy(new LimitedReader(rb, -1), wb);
|
|
|
|
assertEquals(wb.toString(), "");
|
|
|
|
});
|