2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-10-12 09:51:48 +00:00
|
|
|
|
2022-11-29 13:55:38 +00:00
|
|
|
import { TextLineStream } from "./text_line_stream.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2021-10-12 09:51:48 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("TextLineStream parses simple input", async () => {
|
2023-10-29 03:59:42 +00:00
|
|
|
const stream = ReadableStream.from([
|
|
|
|
"qwertzu",
|
|
|
|
"iopasd\r\nmnbvc",
|
|
|
|
"xylk\rjhgfds\napoiuzt\r",
|
|
|
|
"qwr\r09ei\rqwrjiowqr\r",
|
|
|
|
"\nrewq0987\n\n654321",
|
|
|
|
"\nrewq0987\r\n\r\n654321\r",
|
|
|
|
]).pipeThrough(new TextLineStream());
|
|
|
|
|
2023-11-03 00:58:51 +00:00
|
|
|
assertEquals(await Array.fromAsync(stream), [
|
2022-05-29 14:32:14 +00:00
|
|
|
"qwertzuiopasd",
|
|
|
|
"mnbvcxylk\rjhgfds",
|
2022-06-29 08:09:42 +00:00
|
|
|
"apoiuzt\rqwr\r09ei\rqwrjiowqr",
|
2022-05-29 14:32:14 +00:00
|
|
|
"rewq0987",
|
|
|
|
"",
|
|
|
|
"654321",
|
|
|
|
"rewq0987",
|
|
|
|
"",
|
2022-06-29 08:09:42 +00:00
|
|
|
"654321\r",
|
2022-05-29 14:32:14 +00:00
|
|
|
]);
|
2023-11-03 01:08:06 +00:00
|
|
|
|
2024-08-06 09:21:57 +00:00
|
|
|
const stream2 = ReadableStream.from(["rewq0987\r\n\r\n654321\n"])
|
2023-11-03 01:08:06 +00:00
|
|
|
.pipeThrough(new TextLineStream());
|
|
|
|
|
|
|
|
assertEquals(await Array.fromAsync(stream2), [
|
|
|
|
"rewq0987",
|
|
|
|
"",
|
|
|
|
"654321",
|
|
|
|
]);
|
2022-05-29 14:32:14 +00:00
|
|
|
});
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("TextLineStream parses with `allowCR` enabled", async () => {
|
2023-10-29 03:59:42 +00:00
|
|
|
const stream = ReadableStream.from([
|
|
|
|
"qwertzu",
|
|
|
|
"iopasd\r\nmnbvc",
|
|
|
|
"xylk\rjhgfds\napoiuzt\r",
|
|
|
|
"qwr\r09ei\rqwrjiowqr\r",
|
|
|
|
"\nrewq0987\n\n654321",
|
|
|
|
"\nrewq0987\r\n\r\n654321\r",
|
|
|
|
]).pipeThrough(new TextLineStream({ allowCR: true }));
|
2023-11-03 00:58:51 +00:00
|
|
|
assertEquals(await Array.fromAsync(stream), [
|
2022-05-25 08:30:21 +00:00
|
|
|
"qwertzuiopasd",
|
|
|
|
"mnbvcxylk",
|
|
|
|
"jhgfds",
|
|
|
|
"apoiuzt",
|
|
|
|
"qwr",
|
|
|
|
"09ei",
|
|
|
|
"qwrjiowqr",
|
|
|
|
"rewq0987",
|
|
|
|
"",
|
|
|
|
"654321",
|
|
|
|
"rewq0987",
|
|
|
|
"",
|
|
|
|
"654321",
|
|
|
|
]);
|
2023-11-03 01:08:06 +00:00
|
|
|
|
2024-08-06 09:21:57 +00:00
|
|
|
const stream2 = ReadableStream.from(["rewq0987\r\n\r\n654321\n"])
|
2023-11-03 01:08:06 +00:00
|
|
|
.pipeThrough(new TextLineStream());
|
|
|
|
|
|
|
|
assertEquals(await Array.fromAsync(stream2), [
|
|
|
|
"rewq0987",
|
|
|
|
"",
|
|
|
|
"654321",
|
|
|
|
]);
|
2022-03-02 10:42:06 +00:00
|
|
|
});
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("TextLineStream parses large chunks", async () => {
|
2023-10-29 03:59:42 +00:00
|
|
|
const totalLines = 20_000;
|
2024-08-06 09:21:57 +00:00
|
|
|
const stream = ReadableStream.from(["\n".repeat(totalLines)])
|
2023-10-29 03:59:42 +00:00
|
|
|
.pipeThrough(new TextLineStream());
|
2023-11-03 00:58:51 +00:00
|
|
|
const lines = await Array.fromAsync(stream);
|
2022-06-29 08:09:42 +00:00
|
|
|
|
2023-10-29 03:59:42 +00:00
|
|
|
assertEquals(lines.length, totalLines);
|
|
|
|
assertEquals(lines, Array.from({ length: totalLines }).fill(""));
|
2022-06-29 08:09:42 +00:00
|
|
|
});
|
2023-01-14 01:18:40 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("TextLineStream parses no final empty chunk with terminal newline", async () => {
|
2023-10-29 03:59:42 +00:00
|
|
|
const stream = ReadableStream.from([
|
|
|
|
"abc\n",
|
|
|
|
"def\nghi\njk",
|
|
|
|
"l\nmn",
|
|
|
|
"o\np",
|
|
|
|
"qr",
|
|
|
|
"\nstu\nvwx\n",
|
|
|
|
"yz\n",
|
|
|
|
]).pipeThrough(new TextLineStream());
|
|
|
|
|
2023-11-03 00:58:51 +00:00
|
|
|
assertEquals(await Array.fromAsync(stream), [
|
2023-10-29 03:59:42 +00:00
|
|
|
"abc",
|
|
|
|
"def",
|
|
|
|
"ghi",
|
|
|
|
"jkl",
|
|
|
|
"mno",
|
|
|
|
"pqr",
|
|
|
|
"stu",
|
|
|
|
"vwx",
|
|
|
|
"yz",
|
|
|
|
]);
|
|
|
|
});
|
2023-01-14 01:18:40 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("TextLineStream parses no final empty chunk without terminal newline", async () => {
|
2023-10-29 03:59:42 +00:00
|
|
|
const stream = ReadableStream.from([
|
|
|
|
"abc\n",
|
|
|
|
"def\nghi\njk",
|
|
|
|
"l\nmn",
|
|
|
|
"o\np",
|
|
|
|
"qr",
|
|
|
|
"\nstu\nvwx\n",
|
|
|
|
"yz",
|
|
|
|
]).pipeThrough(new TextLineStream());
|
|
|
|
|
2023-11-03 00:58:51 +00:00
|
|
|
assertEquals(await Array.fromAsync(stream), [
|
2023-10-29 03:59:42 +00:00
|
|
|
"abc",
|
|
|
|
"def",
|
|
|
|
"ghi",
|
|
|
|
"jkl",
|
|
|
|
"mno",
|
|
|
|
"pqr",
|
|
|
|
"stu",
|
|
|
|
"vwx",
|
|
|
|
"yz",
|
|
|
|
]);
|
|
|
|
});
|