Skip to content

Commit 0015430

Browse files
BridgeARtargos
authored andcommitted
assert: align argument names
This makes sure the documented argument names and the ones thrown in errors is aligned with the actual argument name. PR-URL: #22760 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent b1d667b commit 0015430

6 files changed

+72
-80
lines changed

lib/assert.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,12 @@ function expectedException(actual, expected, msg) {
557557
return expected.call({}, actual) === true;
558558
}
559559

560-
function getActual(block) {
561-
if (typeof block !== 'function') {
562-
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block);
560+
function getActual(fn) {
561+
if (typeof fn !== 'function') {
562+
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
563563
}
564564
try {
565-
block();
565+
fn();
566566
} catch (e) {
567567
return e;
568568
}
@@ -579,20 +579,21 @@ function checkIsPromise(obj) {
579579
typeof obj.catch === 'function';
580580
}
581581

582-
async function waitForActual(block) {
582+
async function waitForActual(promiseFn) {
583583
let resultPromise;
584-
if (typeof block === 'function') {
585-
// Return a rejected promise if `block` throws synchronously.
586-
resultPromise = block();
584+
if (typeof promiseFn === 'function') {
585+
// Return a rejected promise if `promiseFn` throws synchronously.
586+
resultPromise = promiseFn();
587587
// Fail in case no promise is returned.
588588
if (!checkIsPromise(resultPromise)) {
589589
throw new ERR_INVALID_RETURN_VALUE('instance of Promise',
590-
'block', resultPromise);
590+
'promiseFn', resultPromise);
591591
}
592-
} else if (checkIsPromise(block)) {
593-
resultPromise = block;
592+
} else if (checkIsPromise(promiseFn)) {
593+
resultPromise = promiseFn;
594594
} else {
595-
throw new ERR_INVALID_ARG_TYPE('block', ['Function', 'Promise'], block);
595+
throw new ERR_INVALID_ARG_TYPE(
596+
'promiseFn', ['Function', 'Promise'], promiseFn);
596597
}
597598

598599
try {
@@ -672,20 +673,20 @@ function expectsNoError(stackStartFn, actual, error, message) {
672673
throw actual;
673674
}
674675

675-
assert.throws = function throws(block, ...args) {
676-
expectsError(throws, getActual(block), ...args);
676+
assert.throws = function throws(promiseFn, ...args) {
677+
expectsError(throws, getActual(promiseFn), ...args);
677678
};
678679

679-
assert.rejects = async function rejects(block, ...args) {
680-
expectsError(rejects, await waitForActual(block), ...args);
680+
assert.rejects = async function rejects(promiseFn, ...args) {
681+
expectsError(rejects, await waitForActual(promiseFn), ...args);
681682
};
682683

683-
assert.doesNotThrow = function doesNotThrow(block, ...args) {
684-
expectsNoError(doesNotThrow, getActual(block), ...args);
684+
assert.doesNotThrow = function doesNotThrow(fn, ...args) {
685+
expectsNoError(doesNotThrow, getActual(fn), ...args);
685686
};
686687

687-
assert.doesNotReject = async function doesNotReject(block, ...args) {
688-
expectsNoError(doesNotReject, await waitForActual(block), ...args);
688+
assert.doesNotReject = async function doesNotReject(fn, ...args) {
689+
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
689690
};
690691

691692
assert.ifError = function ifError(err) {

test/parallel/test-assert-async.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const promises = [];
4141
name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
4242
code: 'ERR_INVALID_RETURN_VALUE',
4343
message: 'Expected instance of Promise to be returned ' +
44-
'from the "block" function but got type undefined.'
44+
'from the "promiseFn" function but got type undefined.'
4545
}));
4646

4747
promise = assert.rejects(Promise.resolve(), common.mustNotCall());
@@ -62,7 +62,7 @@ promises.push(assert.rejects(
6262
assert.rejects('fail', {}),
6363
{
6464
code: 'ERR_INVALID_ARG_TYPE',
65-
message: 'The "block" argument must be one of type ' +
65+
message: 'The "promiseFn" argument must be one of type ' +
6666
'Function or Promise. Received type string'
6767
}
6868
));
@@ -73,7 +73,7 @@ promises.push(assert.rejects(
7373
const promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
7474
promises.push(assert.rejects(promise, {
7575
message: 'Expected instance of Promise to be returned ' +
76-
'from the "block" function but got instance of Map.',
76+
'from the "promiseFn" function but got instance of Map.',
7777
code: 'ERR_INVALID_RETURN_VALUE',
7878
name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
7979
}));
@@ -116,7 +116,7 @@ promises.push(assert.rejects(
116116
assert.doesNotReject(123),
117117
{
118118
code: 'ERR_INVALID_ARG_TYPE',
119-
message: 'The "block" argument must be one of type ' +
119+
message: 'The "promiseFn" argument must be one of type ' +
120120
'Function or Promise. Received type number'
121121
}
122122
));

test/parallel/test-assert.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,15 @@ try {
341341
}
342342

343343
{
344-
// Verify that throws() and doesNotThrow() throw on non-function block.
345-
const testBlockTypeError = (method, block) => {
344+
// Verify that throws() and doesNotThrow() throw on non-functions.
345+
const testBlockTypeError = (method, fn) => {
346346
common.expectsError(
347-
() => method(block),
347+
() => method(fn),
348348
{
349349
code: 'ERR_INVALID_ARG_TYPE',
350350
type: TypeError,
351-
message: 'The "block" argument must be of type Function. Received ' +
352-
`type ${typeof block}`
351+
message: 'The "fn" argument must be of type Function. Received ' +
352+
`type ${typeof fn}`
353353
}
354354
);
355355
};

test/parallel/test-file-write-stream3.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function run_test_3() {
178178

179179
const run_test_4 = common.mustCall(function() {
180180
// Error: start must be >= zero
181-
const block = () => {
181+
const fn = () => {
182182
fs.createWriteStream(filepath, { start: -5, flags: 'r+' });
183183
};
184184
const err = {
@@ -187,7 +187,7 @@ const run_test_4 = common.mustCall(function() {
187187
'It must be >= 0. Received {start: -5}',
188188
type: RangeError
189189
};
190-
common.expectsError(block, err);
190+
common.expectsError(fn, err);
191191
});
192192

193193
run_test_1();

test/parallel/test-net-connect-options-port.js

+38-47
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const net = require('net');
6262
const hints = (dns.ADDRCONFIG | dns.V4MAPPED) + 42;
6363
const hintOptBlocks = doConnect([{ hints }],
6464
() => common.mustNotCall());
65-
for (const block of hintOptBlocks) {
66-
common.expectsError(block, {
65+
for (const fn of hintOptBlocks) {
66+
common.expectsError(fn, {
6767
code: 'ERR_INVALID_OPT_VALUE',
6868
type: TypeError,
6969
message: /The value "\d+" is invalid for option "hints"/
@@ -136,67 +136,59 @@ function doConnect(args, getCb) {
136136
function syncFailToConnect(port, assertErr, optOnly) {
137137
if (!optOnly) {
138138
// connect(port, cb) and connect(port)
139-
const portArgBlocks = doConnect([port], () => common.mustNotCall());
140-
for (const block of portArgBlocks) {
141-
assert.throws(block,
142-
assertErr,
143-
`${block.name}(${port})`);
139+
const portArgFunctions = doConnect([port], () => common.mustNotCall());
140+
for (const fn of portArgFunctions) {
141+
assert.throws(fn, assertErr, `${fn.name}(${port})`);
144142
}
145143

146144
// connect(port, host, cb) and connect(port, host)
147-
const portHostArgBlocks = doConnect([port, 'localhost'],
148-
() => common.mustNotCall());
149-
for (const block of portHostArgBlocks) {
150-
assert.throws(block,
151-
assertErr,
152-
`${block.name}(${port}, 'localhost')`);
145+
const portHostArgFunctions = doConnect([port, 'localhost'],
146+
() => common.mustNotCall());
147+
for (const fn of portHostArgFunctions) {
148+
assert.throws(fn, assertErr, `${fn.name}(${port}, 'localhost')`);
153149
}
154150
}
155151
// connect({port}, cb) and connect({port})
156-
const portOptBlocks = doConnect([{ port }],
157-
() => common.mustNotCall());
158-
for (const block of portOptBlocks) {
159-
assert.throws(block,
160-
assertErr,
161-
`${block.name}({port: ${port}})`);
152+
const portOptFunctions = doConnect([{ port }], () => common.mustNotCall());
153+
for (const fn of portOptFunctions) {
154+
assert.throws(fn, assertErr, `${fn.name}({port: ${port}})`);
162155
}
163156

164157
// connect({port, host}, cb) and connect({port, host})
165-
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }],
166-
() => common.mustNotCall());
167-
for (const block of portHostOptBlocks) {
168-
assert.throws(block,
158+
const portHostOptFunctions = doConnect([{ port: port, host: 'localhost' }],
159+
() => common.mustNotCall());
160+
for (const fn of portHostOptFunctions) {
161+
assert.throws(fn,
169162
assertErr,
170-
`${block.name}({port: ${port}, host: 'localhost'})`);
163+
`${fn.name}({port: ${port}, host: 'localhost'})`);
171164
}
172165
}
173166

174167
function canConnect(port) {
175168
const noop = () => common.mustCall();
176169

177170
// connect(port, cb) and connect(port)
178-
const portArgBlocks = doConnect([port], noop);
179-
for (const block of portArgBlocks) {
180-
block();
171+
const portArgFunctions = doConnect([port], noop);
172+
for (const fn of portArgFunctions) {
173+
fn();
181174
}
182175

183176
// connect(port, host, cb) and connect(port, host)
184-
const portHostArgBlocks = doConnect([port, 'localhost'], noop);
185-
for (const block of portHostArgBlocks) {
186-
block();
177+
const portHostArgFunctions = doConnect([port, 'localhost'], noop);
178+
for (const fn of portHostArgFunctions) {
179+
fn();
187180
}
188181

189182
// connect({port}, cb) and connect({port})
190-
const portOptBlocks = doConnect([{ port }], noop);
191-
for (const block of portOptBlocks) {
192-
block();
183+
const portOptFunctions = doConnect([{ port }], noop);
184+
for (const fn of portOptFunctions) {
185+
fn();
193186
}
194187

195188
// connect({port, host}, cb) and connect({port, host})
196-
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }],
197-
noop);
198-
for (const block of portHostOptBlocks) {
199-
block();
189+
const portHostOptFns = doConnect([{ port, host: 'localhost' }], noop);
190+
for (const fn of portHostOptFns) {
191+
fn();
200192
}
201193
}
202194

@@ -208,21 +200,20 @@ function asyncFailToConnect(port) {
208200

209201
const dont = () => common.mustNotCall();
210202
// connect(port, cb) and connect(port)
211-
const portArgBlocks = doConnect([port], dont);
212-
for (const block of portArgBlocks) {
213-
block().on('error', onError());
203+
const portArgFunctions = doConnect([port], dont);
204+
for (const fn of portArgFunctions) {
205+
fn().on('error', onError());
214206
}
215207

216208
// connect({port}, cb) and connect({port})
217-
const portOptBlocks = doConnect([{ port }], dont);
218-
for (const block of portOptBlocks) {
219-
block().on('error', onError());
209+
const portOptFunctions = doConnect([{ port }], dont);
210+
for (const fn of portOptFunctions) {
211+
fn().on('error', onError());
220212
}
221213

222214
// connect({port, host}, cb) and connect({port, host})
223-
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }],
224-
dont);
225-
for (const block of portHostOptBlocks) {
226-
block().on('error', onError());
215+
const portHostOptFns = doConnect([{ port, host: 'localhost' }], dont);
216+
for (const fn of portHostOptFns) {
217+
fn().on('error', onError());
227218
}
228219
}

test/parallel/test-net-server-listen-options.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ const listenOnPort = [
5454

5555
{
5656
function shouldFailToListen(options) {
57-
const block = () => {
57+
const fn = () => {
5858
net.createServer().listen(options, common.mustNotCall());
5959
};
60-
common.expectsError(block,
60+
common.expectsError(fn,
6161
{
6262
code: 'ERR_INVALID_OPT_VALUE',
6363
type: TypeError,

0 commit comments

Comments
 (0)