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

26 lines
743 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { readerFromIterable } from "./reader_from_iterable.ts";
Deno.test("readerFromIterable()", async function () {
const reader = readerFromIterable((function* () {
const encoder = new TextEncoder();
for (const string of ["hello", "deno", "foo"]) {
yield encoder.encode(string);
}
})());
const readStrings = [];
const decoder = new TextDecoder();
const p = new Uint8Array(4);
while (true) {
const n = await reader.read(p);
if (n === null) {
break;
}
readStrings.push(decoder.decode(p.slice(0, n)));
}
assertEquals(readStrings, ["hell", "o", "deno", "foo"]);
});