Skip to content

Commit b4510f7

Browse files
lpincaMylesBorins
authored andcommitted
test: refactor stream-*-constructor-set-methods
- Use `common.mustCall()` to ensure that callbacks are called. - Remove no longer needed variables. - Remove unnecessary `process.on('exit')` usage. PR-URL: #18817 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent f18c801 commit b4510f7

2 files changed

+35
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43

5-
const Transform = require('stream').Transform;
4+
const { strictEqual } = require('assert');
5+
const { Transform } = require('stream');
66

7-
const _transform = common.mustCall(function _transform(d, e, n) {
8-
n();
7+
const _transform = common.mustCall((chunk, _, next) => {
8+
next();
99
});
1010

11-
const _final = common.mustCall(function _final(n) {
12-
n();
11+
const _final = common.mustCall((next) => {
12+
next();
1313
});
1414

15-
const _flush = common.mustCall(function _flush(n) {
16-
n();
15+
const _flush = common.mustCall((next) => {
16+
next();
1717
});
1818

1919
const t = new Transform({
@@ -22,21 +22,19 @@ const t = new Transform({
2222
final: _final
2323
});
2424

25-
const t2 = new Transform({});
25+
strictEqual(t._transform, _transform);
26+
strictEqual(t._flush, _flush);
27+
strictEqual(t._final, _final);
2628

2729
t.end(Buffer.from('blerg'));
2830
t.resume();
2931

32+
const t2 = new Transform({});
33+
3034
common.expectsError(() => {
3135
t2.end(Buffer.from('blerg'));
3236
}, {
3337
type: Error,
3438
code: 'ERR_METHOD_NOT_IMPLEMENTED',
3539
message: 'The _transform method is not implemented'
3640
});
37-
38-
process.on('exit', () => {
39-
assert.strictEqual(t._transform, _transform);
40-
assert.strictEqual(t._flush, _flush);
41-
assert.strictEqual(t._final, _final);
42-
});
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43

5-
const Writable = require('stream').Writable;
4+
const { strictEqual } = require('assert');
5+
const { Writable } = require('stream');
66

7-
let _writeCalled = false;
8-
function _write(d, e, n) {
9-
_writeCalled = true;
10-
}
7+
const _write = common.mustCall((chunk, _, next) => {
8+
next();
9+
});
10+
11+
const _writev = common.mustCall((chunks, next) => {
12+
strictEqual(chunks.length, 2);
13+
next();
14+
});
1115

12-
const w = new Writable({ write: _write });
13-
w.end(Buffer.from('blerg'));
16+
const w = new Writable({ write: _write, writev: _writev });
1417

15-
let _writevCalled = false;
16-
let dLength = 0;
17-
function _writev(d, n) {
18-
dLength = d.length;
19-
_writevCalled = true;
20-
}
18+
strictEqual(w._write, _write);
19+
strictEqual(w._writev, _writev);
2120

22-
const w2 = new Writable({ writev: _writev });
23-
w2.cork();
21+
w.write(Buffer.from('blerg'));
2422

25-
w2.write(Buffer.from('blerg'));
26-
w2.write(Buffer.from('blerg'));
27-
w2.end();
23+
w.cork();
24+
w.write(Buffer.from('blerg'));
25+
w.write(Buffer.from('blerg'));
2826

29-
const w3 = new Writable();
27+
w.end();
3028

31-
w3.on('error', common.expectsError({
29+
const w2 = new Writable();
30+
31+
w2.on('error', common.expectsError({
3232
type: Error,
3333
code: 'ERR_METHOD_NOT_IMPLEMENTED',
3434
message: 'The _write method is not implemented'
3535
}));
3636

37-
w3.end(Buffer.from('blerg'));
38-
39-
process.on('exit', function() {
40-
assert.strictEqual(w._write, _write);
41-
assert(_writeCalled);
42-
assert.strictEqual(w2._writev, _writev);
43-
assert.strictEqual(dLength, 2);
44-
assert(_writevCalled);
45-
});
37+
w2.end(Buffer.from('blerg'));

0 commit comments

Comments
 (0)