deno.land / std@0.224.0 / streams / text_line_stream_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { TextLineStream } from "./text_line_stream.ts";import { assertEquals } from "../assert/mod.ts";
Deno.test("TextLineStream parses simple input", async () => { 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());
assertEquals(await Array.fromAsync(stream), [ "qwertzuiopasd", "mnbvcxylk\rjhgfds", "apoiuzt\rqwr\r09ei\rqwrjiowqr", "rewq0987", "", "654321", "rewq0987", "", "654321\r", ]);
const stream2 = ReadableStream.from("rewq0987\r\n\r\n654321\n") .pipeThrough(new TextLineStream());
assertEquals(await Array.fromAsync(stream2), [ "rewq0987", "", "654321", ]);});
Deno.test("TextLineStream parses with `allowCR` enabled", async () => { 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 })); assertEquals(await Array.fromAsync(stream), [ "qwertzuiopasd", "mnbvcxylk", "jhgfds", "apoiuzt", "qwr", "09ei", "qwrjiowqr", "rewq0987", "", "654321", "rewq0987", "", "654321", ]);
const stream2 = ReadableStream.from("rewq0987\r\n\r\n654321\n") .pipeThrough(new TextLineStream());
assertEquals(await Array.fromAsync(stream2), [ "rewq0987", "", "654321", ]);});
Deno.test("TextLineStream parses large chunks", async () => { const totalLines = 20_000; const stream = ReadableStream.from("\n".repeat(totalLines)) .pipeThrough(new TextLineStream()); const lines = await Array.fromAsync(stream);
assertEquals(lines.length, totalLines); assertEquals(lines, Array.from({ length: totalLines }).fill(""));});
Deno.test("TextLineStream parses no final empty chunk with terminal newline", async () => { const stream = ReadableStream.from([ "abc\n", "def\nghi\njk", "l\nmn", "o\np", "qr", "\nstu\nvwx\n", "yz\n", ]).pipeThrough(new TextLineStream());
assertEquals(await Array.fromAsync(stream), [ "abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz", ]);});
Deno.test("TextLineStream parses no final empty chunk without terminal newline", async () => { const stream = ReadableStream.from([ "abc\n", "def\nghi\njk", "l\nmn", "o\np", "qr", "\nstu\nvwx\n", "yz", ]).pipeThrough(new TextLineStream());
assertEquals(await Array.fromAsync(stream), [ "abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz", ]);});
Version Info