Skip to content

Commit cfa4614

Browse files
committed
stream: add tests to validate possible stream arg scenarios
1 parent c5da09b commit cfa4614

1 file changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { Readable, Writable, PassThrough, pipeline } = require('stream');
6+
const { ReadableStream, WritableStream, TransformStream } = require('stream/web');
7+
8+
class NonReadableNodeStream extends Writable {
9+
_write(chunk, encoding, callback) {
10+
callback();
11+
}
12+
}
13+
14+
class NonWritableNodeStream extends Readable {
15+
_read() {
16+
this.push('x');
17+
this.push(null);
18+
}
19+
}
20+
21+
function assertInvalidArg(fn, name) {
22+
assert.throws(fn, (err) => {
23+
assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
24+
assert.strictEqual(err.message.includes(`The "${name}"`), true);
25+
return true;
26+
});
27+
}
28+
29+
// Non-readable Node stream
30+
{
31+
assertInvalidArg(() => {
32+
pipeline(
33+
new NonReadableNodeStream(), // source
34+
new PassThrough(),
35+
common.mustNotCall(),
36+
);
37+
}, 'source');
38+
39+
assertInvalidArg(() => {
40+
pipeline(
41+
Readable.from(['x']),
42+
new PassThrough(),
43+
new NonReadableNodeStream(), // transform
44+
new PassThrough(),
45+
common.mustNotCall(),
46+
);
47+
}, 'transform[1]');
48+
49+
pipeline(
50+
Readable.from(['x']),
51+
new NonReadableNodeStream(), // destination
52+
common.mustSucceed(),
53+
);
54+
}
55+
56+
// Non-writable Node stream
57+
{
58+
pipeline(
59+
new NonWritableNodeStream(), // source
60+
new Writable({
61+
write(chunk, encoding, callback) {
62+
callback();
63+
},
64+
}),
65+
common.mustSucceed(),
66+
);
67+
68+
assertInvalidArg(() => {
69+
pipeline(
70+
Readable.from(['x']),
71+
new NonWritableNodeStream(), // transform
72+
new PassThrough(),
73+
common.mustNotCall(),
74+
);
75+
}, 'transform[0]');
76+
77+
assertInvalidArg(() => {
78+
pipeline(
79+
Readable.from(['x']),
80+
new NonWritableNodeStream(), // destination
81+
common.mustNotCall(),
82+
);
83+
}, 'destination');
84+
}
85+
86+
// ReadableStream
87+
{
88+
const chunks = [];
89+
pipeline(
90+
new ReadableStream({ // source
91+
start(controller) {
92+
controller.enqueue('x');
93+
controller.close();
94+
},
95+
}),
96+
new Writable({
97+
write(chunk, encoding, callback) {
98+
chunks.push(chunk.toString());
99+
callback();
100+
},
101+
}),
102+
common.mustSucceed(() => {
103+
assert.deepStrictEqual(chunks, ['x']);
104+
}),
105+
);
106+
107+
assertInvalidArg(() => {
108+
pipeline(
109+
Readable.from(['x']),
110+
new ReadableStream({ // transform
111+
start(controller) {
112+
controller.enqueue('x');
113+
controller.close();
114+
},
115+
}),
116+
new PassThrough(),
117+
common.mustNotCall(),
118+
);
119+
}, 'transform[0]');
120+
121+
assertInvalidArg(() => {
122+
pipeline(
123+
Readable.from(['x']),
124+
new ReadableStream({ // destination
125+
start(controller) {
126+
controller.enqueue('x');
127+
controller.close();
128+
},
129+
}),
130+
common.mustNotCall(),
131+
);
132+
}, 'destination');
133+
}
134+
135+
// TransformStream
136+
{
137+
const source = new TransformStream();
138+
const writer = source.writable.getWriter();
139+
const seen = [];
140+
pipeline(
141+
source, // source
142+
new Writable({
143+
write(chunk, encoding, callback) {
144+
seen.push(chunk.toString());
145+
callback();
146+
},
147+
}),
148+
common.mustSucceed(() => {
149+
assert.deepStrictEqual(seen, ['x']);
150+
}),
151+
);
152+
153+
writer.write('x').then(common.mustCall(() => writer.close()));
154+
155+
const transformed = [];
156+
pipeline(
157+
Readable.from(['x']),
158+
new TransformStream({ // transform
159+
transform(chunk, controller) {
160+
controller.enqueue(chunk.toString().toUpperCase());
161+
},
162+
}),
163+
new Writable({
164+
write(chunk, encoding, callback) {
165+
transformed.push(chunk.toString());
166+
callback();
167+
},
168+
}),
169+
common.mustSucceed(() => {
170+
assert.deepStrictEqual(transformed, ['X']);
171+
}),
172+
);
173+
174+
assert.doesNotThrow(() => {
175+
pipeline(
176+
Readable.from(['x']),
177+
new TransformStream({ // destination
178+
transform() {},
179+
}),
180+
() => {},
181+
);
182+
});
183+
}
184+
185+
// WritableStream
186+
{
187+
assertInvalidArg(() => {
188+
pipeline(
189+
new WritableStream({ // source
190+
write() {},
191+
}),
192+
new PassThrough(),
193+
common.mustNotCall(),
194+
);
195+
}, 'source');
196+
197+
assertInvalidArg(() => {
198+
pipeline(
199+
Readable.from(['x']),
200+
new WritableStream({ // transform
201+
write() {},
202+
}),
203+
new PassThrough(),
204+
common.mustNotCall(),
205+
);
206+
}, 'transform[0]');
207+
208+
const values = [];
209+
pipeline(
210+
Readable.from(['x']),
211+
new WritableStream({ // destination
212+
write(chunk) {
213+
values.push(chunk.toString());
214+
},
215+
}),
216+
common.mustSucceed(() => {
217+
assert.deepStrictEqual(values, ['x']);
218+
}),
219+
);
220+
}

0 commit comments

Comments
 (0)