std/io/string_reader_test.ts
2024-04-29 11:57:30 +09:00

35 lines
1.1 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { StringReader } from "./string_reader.ts";
Deno.test("ioStringReader", async function () {
const r = new StringReader("abcdef");
const res0 = await r.read(new Uint8Array(6));
assertEquals(res0, 6);
const res1 = await r.read(new Uint8Array(6));
assertEquals(res1, null);
});
Deno.test("ioStringReader", async function () {
const decoder = new TextDecoder();
const r = new StringReader("abcdef");
const buf = new Uint8Array(3);
const res1 = await r.read(buf);
assertEquals(res1, 3);
assertEquals(decoder.decode(buf), "abc");
const res2 = await r.read(buf);
assertEquals(res2, 3);
assertEquals(decoder.decode(buf), "def");
const res3 = await r.read(buf);
assertEquals(res3, null);
assertEquals(decoder.decode(buf), "def");
});
Deno.test("testStringReaderEof", async function () {
const r = new StringReader("abc");
assertEquals(await r.read(new Uint8Array()), 0);
assertEquals(await r.read(new Uint8Array(4)), 3);
assertEquals(await r.read(new Uint8Array(1)), null);
});