std/streams/buffer_test.ts
2024-04-29 11:57:30 +09:00

39 lines
1.3 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "@std/assert";
import { Buffer } from "./buffer.ts";
Deno.test("Buffer handles write and read", async function () {
const buf = new Buffer();
const writer = buf.writable.getWriter();
const reader = buf.readable.getReader({ mode: "byob" });
const data = new Uint8Array([4, 21, 45, 19]);
await writer.write(data);
const read = await reader.read(new Uint8Array(4));
assertEquals(read.value, data);
});
Deno.test("Buffer handles read empty", async function () {
const buf = new Buffer();
const reader = buf.readable.getReader({ mode: "byob" });
const read = await reader.read(new Uint8Array(5));
assert(read.done);
assertEquals(read.value!.byteLength, 0);
});
Deno.test("Buffer handles write and get bytes", async function () {
const buf = new Buffer();
const writer = buf.writable.getWriter();
const data = new Uint8Array([4, 21, 45, 19]);
await writer.write(data);
assertEquals(buf.bytes(), data);
});
Deno.test("Buffer handles truncate", async function () {
const buf = new Buffer();
const writer = buf.writable.getWriter();
await writer.write(new Uint8Array([4, 21, 45, 19]));
buf.truncate(3);
assertEquals(buf.bytes(), new Uint8Array([4, 21, 45]));
});