2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "../assert/mod.ts";
|
2022-11-29 13:55:38 +00:00
|
|
|
import { ByteSliceStream } from "./byte_slice_stream.ts";
|
|
|
|
|
|
|
|
Deno.test("[streams] ByteSliceStream", async function () {
|
|
|
|
function createStream(start = 0, end = Infinity) {
|
2023-11-10 03:57:52 +00:00
|
|
|
return ReadableStream.from([
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]).pipeThrough(new ByteSliceStream(start, end));
|
2022-11-29 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let chunks = [];
|
|
|
|
|
|
|
|
for await (const chunk of createStream(0, 3)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(0, 1)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(0, 2)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(0, 3)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(1, 3)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(2, 3)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
chunks = [];
|
|
|
|
for await (const chunk of createStream(0, 10)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assertThrows(() => createStream(-1, Infinity));
|
|
|
|
});
|