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: https://github.com/nodejs/node/pull/22760
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
This commit is contained in:
Ruben Bridgewater 2018-09-08 02:30:07 +02:00
parent e4dd213516
commit b5430d782d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
6 changed files with 73 additions and 81 deletions

View File

@ -555,12 +555,12 @@ function expectedException(actual, expected, msg) {
return expected.call({}, actual) === true; return expected.call({}, actual) === true;
} }
function getActual(block) { function getActual(fn) {
if (typeof block !== 'function') { if (typeof fn !== 'function') {
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block); throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
} }
try { try {
block(); fn();
} catch (e) { } catch (e) {
return e; return e;
} }
@ -577,20 +577,21 @@ function checkIsPromise(obj) {
typeof obj.catch === 'function'; typeof obj.catch === 'function';
} }
async function waitForActual(block) { async function waitForActual(promiseFn) {
let resultPromise; let resultPromise;
if (typeof block === 'function') { if (typeof promiseFn === 'function') {
// Return a rejected promise if `block` throws synchronously. // Return a rejected promise if `promiseFn` throws synchronously.
resultPromise = block(); resultPromise = promiseFn();
// Fail in case no promise is returned. // Fail in case no promise is returned.
if (!checkIsPromise(resultPromise)) { if (!checkIsPromise(resultPromise)) {
throw new ERR_INVALID_RETURN_VALUE('instance of Promise', throw new ERR_INVALID_RETURN_VALUE('instance of Promise',
'block', resultPromise); 'promiseFn', resultPromise);
} }
} else if (checkIsPromise(block)) { } else if (checkIsPromise(promiseFn)) {
resultPromise = block; resultPromise = promiseFn;
} else { } else {
throw new ERR_INVALID_ARG_TYPE('block', ['Function', 'Promise'], block); throw new ERR_INVALID_ARG_TYPE(
'promiseFn', ['Function', 'Promise'], promiseFn);
} }
try { try {
@ -676,20 +677,20 @@ function expectsNoError(stackStartFn, actual, error, message) {
throw actual; throw actual;
} }
assert.throws = function throws(block, ...args) { assert.throws = function throws(promiseFn, ...args) {
expectsError(throws, getActual(block), ...args); expectsError(throws, getActual(promiseFn), ...args);
}; };
assert.rejects = async function rejects(block, ...args) { assert.rejects = async function rejects(promiseFn, ...args) {
expectsError(rejects, await waitForActual(block), ...args); expectsError(rejects, await waitForActual(promiseFn), ...args);
}; };
assert.doesNotThrow = function doesNotThrow(block, ...args) { assert.doesNotThrow = function doesNotThrow(fn, ...args) {
expectsNoError(doesNotThrow, getActual(block), ...args); expectsNoError(doesNotThrow, getActual(fn), ...args);
}; };
assert.doesNotReject = async function doesNotReject(block, ...args) { assert.doesNotReject = async function doesNotReject(fn, ...args) {
expectsNoError(doesNotReject, await waitForActual(block), ...args); expectsNoError(doesNotReject, await waitForActual(fn), ...args);
}; };
assert.ifError = function ifError(err) { assert.ifError = function ifError(err) {

View File

@ -41,7 +41,7 @@ const promises = [];
name: 'TypeError [ERR_INVALID_RETURN_VALUE]', name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
code: 'ERR_INVALID_RETURN_VALUE', code: 'ERR_INVALID_RETURN_VALUE',
message: 'Expected instance of Promise to be returned ' + message: 'Expected instance of Promise to be returned ' +
'from the "block" function but got type undefined.' 'from the "promiseFn" function but got type undefined.'
})); }));
promise = assert.rejects(Promise.resolve(), common.mustNotCall()); promise = assert.rejects(Promise.resolve(), common.mustNotCall());
@ -62,7 +62,7 @@ promises.push(assert.rejects(
assert.rejects('fail', {}), assert.rejects('fail', {}),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "block" argument must be one of type ' + message: 'The "promiseFn" argument must be one of type ' +
'Function or Promise. Received type string' 'Function or Promise. Received type string'
} }
)); ));
@ -73,7 +73,7 @@ promises.push(assert.rejects(
const promise = assert.doesNotReject(() => new Map(), common.mustNotCall()); const promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
promises.push(assert.rejects(promise, { promises.push(assert.rejects(promise, {
message: 'Expected instance of Promise to be returned ' + message: 'Expected instance of Promise to be returned ' +
'from the "block" function but got instance of Map.', 'from the "promiseFn" function but got instance of Map.',
code: 'ERR_INVALID_RETURN_VALUE', code: 'ERR_INVALID_RETURN_VALUE',
name: 'TypeError [ERR_INVALID_RETURN_VALUE]' name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
})); }));
@ -116,7 +116,7 @@ promises.push(assert.rejects(
assert.doesNotReject(123), assert.doesNotReject(123),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "block" argument must be one of type ' + message: 'The "promiseFn" argument must be one of type ' +
'Function or Promise. Received type number' 'Function or Promise. Received type number'
} }
)); ));

View File

@ -348,15 +348,15 @@ try {
} }
{ {
// Verify that throws() and doesNotThrow() throw on non-function block. // Verify that throws() and doesNotThrow() throw on non-functions.
const testBlockTypeError = (method, block) => { const testBlockTypeError = (method, fn) => {
common.expectsError( common.expectsError(
() => method(block), () => method(fn),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "block" argument must be of type Function. Received ' + message: 'The "fn" argument must be of type Function. Received ' +
`type ${typeof block}` `type ${typeof fn}`
} }
); );
}; };

View File

@ -178,7 +178,7 @@ function run_test_3() {
const run_test_4 = common.mustCall(function() { const run_test_4 = common.mustCall(function() {
// Error: start must be >= zero // Error: start must be >= zero
const block = () => { const fn = () => {
fs.createWriteStream(filepath, { start: -5, flags: 'r+' }); fs.createWriteStream(filepath, { start: -5, flags: 'r+' });
}; };
const err = { const err = {
@ -187,7 +187,7 @@ const run_test_4 = common.mustCall(function() {
'It must be >= 0. Received {start: -5}', 'It must be >= 0. Received {start: -5}',
type: RangeError type: RangeError
}; };
common.expectsError(block, err); common.expectsError(fn, err);
}); });
run_test_1(); run_test_1();

View File

@ -62,8 +62,8 @@ const net = require('net');
const hints = (dns.ADDRCONFIG | dns.V4MAPPED) + 42; const hints = (dns.ADDRCONFIG | dns.V4MAPPED) + 42;
const hintOptBlocks = doConnect([{ hints }], const hintOptBlocks = doConnect([{ hints }],
() => common.mustNotCall()); () => common.mustNotCall());
for (const block of hintOptBlocks) { for (const fn of hintOptBlocks) {
common.expectsError(block, { common.expectsError(fn, {
code: 'ERR_INVALID_OPT_VALUE', code: 'ERR_INVALID_OPT_VALUE',
type: TypeError, type: TypeError,
message: /The value "\d+" is invalid for option "hints"/ message: /The value "\d+" is invalid for option "hints"/
@ -136,38 +136,31 @@ function doConnect(args, getCb) {
function syncFailToConnect(port, assertErr, optOnly) { function syncFailToConnect(port, assertErr, optOnly) {
if (!optOnly) { if (!optOnly) {
// connect(port, cb) and connect(port) // connect(port, cb) and connect(port)
const portArgBlocks = doConnect([port], () => common.mustNotCall()); const portArgFunctions = doConnect([port], () => common.mustNotCall());
for (const block of portArgBlocks) { for (const fn of portArgFunctions) {
assert.throws(block, assert.throws(fn, assertErr, `${fn.name}(${port})`);
assertErr,
`${block.name}(${port})`);
} }
// connect(port, host, cb) and connect(port, host) // connect(port, host, cb) and connect(port, host)
const portHostArgBlocks = doConnect([port, 'localhost'], const portHostArgFunctions = doConnect([port, 'localhost'],
() => common.mustNotCall()); () => common.mustNotCall());
for (const block of portHostArgBlocks) { for (const fn of portHostArgFunctions) {
assert.throws(block, assert.throws(fn, assertErr, `${fn.name}(${port}, 'localhost')`);
assertErr,
`${block.name}(${port}, 'localhost')`);
} }
} }
// connect({port}, cb) and connect({port}) // connect({port}, cb) and connect({port})
const portOptBlocks = doConnect([{ port }], const portOptFunctions = doConnect([{ port }], () => common.mustNotCall());
() => common.mustNotCall()); for (const fn of portOptFunctions) {
for (const block of portOptBlocks) { assert.throws(fn, assertErr, `${fn.name}({port: ${port}})`);
assert.throws(block,
assertErr,
`${block.name}({port: ${port}})`);
} }
// connect({port, host}, cb) and connect({port, host}) // connect({port, host}, cb) and connect({port, host})
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }], const portHostOptFunctions = doConnect([{ port: port, host: 'localhost' }],
() => common.mustNotCall()); () => common.mustNotCall());
for (const block of portHostOptBlocks) { for (const fn of portHostOptFunctions) {
assert.throws(block, assert.throws(fn,
assertErr, assertErr,
`${block.name}({port: ${port}, host: 'localhost'})`); `${fn.name}({port: ${port}, host: 'localhost'})`);
} }
} }
@ -175,28 +168,27 @@ function canConnect(port) {
const noop = () => common.mustCall(); const noop = () => common.mustCall();
// connect(port, cb) and connect(port) // connect(port, cb) and connect(port)
const portArgBlocks = doConnect([port], noop); const portArgFunctions = doConnect([port], noop);
for (const block of portArgBlocks) { for (const fn of portArgFunctions) {
block(); fn();
} }
// connect(port, host, cb) and connect(port, host) // connect(port, host, cb) and connect(port, host)
const portHostArgBlocks = doConnect([port, 'localhost'], noop); const portHostArgFunctions = doConnect([port, 'localhost'], noop);
for (const block of portHostArgBlocks) { for (const fn of portHostArgFunctions) {
block(); fn();
} }
// connect({port}, cb) and connect({port}) // connect({port}, cb) and connect({port})
const portOptBlocks = doConnect([{ port }], noop); const portOptFunctions = doConnect([{ port }], noop);
for (const block of portOptBlocks) { for (const fn of portOptFunctions) {
block(); fn();
} }
// connect({port, host}, cb) and connect({port, host}) // connect({port, host}, cb) and connect({port, host})
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }], const portHostOptFns = doConnect([{ port, host: 'localhost' }], noop);
noop); for (const fn of portHostOptFns) {
for (const block of portHostOptBlocks) { fn();
block();
} }
} }
@ -208,21 +200,20 @@ function asyncFailToConnect(port) {
const dont = () => common.mustNotCall(); const dont = () => common.mustNotCall();
// connect(port, cb) and connect(port) // connect(port, cb) and connect(port)
const portArgBlocks = doConnect([port], dont); const portArgFunctions = doConnect([port], dont);
for (const block of portArgBlocks) { for (const fn of portArgFunctions) {
block().on('error', onError()); fn().on('error', onError());
} }
// connect({port}, cb) and connect({port}) // connect({port}, cb) and connect({port})
const portOptBlocks = doConnect([{ port }], dont); const portOptFunctions = doConnect([{ port }], dont);
for (const block of portOptBlocks) { for (const fn of portOptFunctions) {
block().on('error', onError()); fn().on('error', onError());
} }
// connect({port, host}, cb) and connect({port, host}) // connect({port, host}, cb) and connect({port, host})
const portHostOptBlocks = doConnect([{ port: port, host: 'localhost' }], const portHostOptFns = doConnect([{ port, host: 'localhost' }], dont);
dont); for (const fn of portHostOptFns) {
for (const block of portHostOptBlocks) { fn().on('error', onError());
block().on('error', onError());
} }
} }

View File

@ -54,20 +54,20 @@ const listenOnPort = [
{ {
function shouldFailToListen(options) { function shouldFailToListen(options) {
const block = () => { const fn = () => {
net.createServer().listen(options, common.mustNotCall()); net.createServer().listen(options, common.mustNotCall());
}; };
if (typeof options === 'object' && if (typeof options === 'object' &&
!(('port' in options) || ('path' in options))) { !(('port' in options) || ('path' in options))) {
common.expectsError(block, common.expectsError(fn,
{ {
code: 'ERR_INVALID_ARG_VALUE', code: 'ERR_INVALID_ARG_VALUE',
type: TypeError, type: TypeError,
message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/, message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
}); });
} else { } else {
common.expectsError(block, common.expectsError(fn,
{ {
code: 'ERR_INVALID_OPT_VALUE', code: 'ERR_INVALID_OPT_VALUE',
type: TypeError, type: TypeError,