-
Notifications
You must be signed in to change notification settings - Fork 581
/
delimiter_stream_test.ts
97 lines (92 loc) · 3.37 KB
/
delimiter_stream_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { DelimiterStream } from "./delimiter_stream.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { readableStreamFromIterable } from "./readable_stream_from_iterable.ts";
/**
* Verify that a transform stream produces the expected output data.
* @param transform The transform stream to test
* @param inputs Source input data
* @param outputs Expected output data
*/
async function testTransformStream<T, U>(
transform: TransformStream<T, U>,
inputs: Iterable<T> | AsyncIterable<T>,
outputs: Iterable<U> | AsyncIterable<U>,
) {
const reader = readableStreamFromIterable(inputs)
.pipeThrough(transform)
.getReader();
for await (const output of outputs) {
const { value, done } = await reader.read();
assertEquals(value, output);
assertEquals(done, false);
}
const f = await reader.read();
assert(f.done, `stream not done, value was: ${f.value}`);
}
Deno.test("[streams] DelimiterStream, discard", async () => {
const crlf = new TextEncoder().encode("CRLF");
const delimStream = new DelimiterStream(crlf, { disposition: "discard" });
const inputs = [
"qwertzu", // no delimiter
"iopasdCRLFmnbvc", // one delimiter in the middle
"xylkjhCRLFgfdsapCRLFoiuzt", // two separate delimiters
"euoiCRLFCRLFaueiou", // two consecutive delimiters
"rewq098765432CR", // split delimiter (1/2)
"LF349012i491290", // split delimiter (2/2)
].map((s) => new TextEncoder().encode(s));
const outputs = [
"qwertzuiopasd",
"mnbvcxylkjh",
"gfdsap",
"oiuzteuoi",
"",
"aueiourewq098765432",
"349012i491290",
].map((s) => new TextEncoder().encode(s));
await testTransformStream(delimStream, inputs, outputs);
});
Deno.test("[streams] DelimiterStream, suffix", async () => {
const crlf = new TextEncoder().encode("CRLF");
const delimStream = new DelimiterStream(crlf, { disposition: "suffix" });
const inputs = [
"qwertzu", // no delimiter
"iopasdCRLFmnbvc", // one delimiter in the middle
"xylkjhCRLFgfdsapCRLFoiuzt", // two separate delimiters
"euoiCRLFCRLFaueiou", // two consecutive delimiters
"rewq098765432CR", // split delimiter (1/2)
"LF349012i491290", // split delimiter (2/2)
].map((s) => new TextEncoder().encode(s));
const outputs = [
"qwertzuiopasdCRLF",
"mnbvcxylkjhCRLF",
"gfdsapCRLF",
"oiuzteuoiCRLF",
"CRLF",
"aueiourewq098765432CRLF",
"349012i491290",
].map((s) => new TextEncoder().encode(s));
await testTransformStream(delimStream, inputs, outputs);
});
Deno.test("[streams] DelimiterStream, prefix", async () => {
const crlf = new TextEncoder().encode("CRLF");
const delimStream = new DelimiterStream(crlf, { disposition: "prefix" });
const inputs = [
"qwertzu", // no delimiter
"iopasdCRLFmnbvc", // one delimiter in the middle
"xylkjhCRLFgfdsapCRLFoiuzt", // two separate delimiters
"euoiCRLFCRLFaueiou", // two consecutive delimiters
"rewq098765432CR", // split delimiter (1/2)
"LF349012i491290", // split delimiter (2/2)
].map((s) => new TextEncoder().encode(s));
const outputs = [
"qwertzuiopasd",
"CRLFmnbvcxylkjh",
"CRLFgfdsap",
"CRLFoiuzteuoi",
"CRLF",
"CRLFaueiourewq098765432",
"CRLF349012i491290",
].map((s) => new TextEncoder().encode(s));
await testTransformStream(delimStream, inputs, outputs);
});