mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
assert: validate required arguments
This validates most `assert` functions to verify that the required arguments are indeed passed to the function. PR-URL: https://github.com/nodejs/node/pull/26641 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
ab3f7afacc
commit
5a3623af74
16
.eslintrc.js
16
.eslintrc.js
@ -210,22 +210,6 @@ module.exports = {
|
||||
selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]',
|
||||
message: 'Use `new` keyword when throwing an `Error`.',
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.length<2]",
|
||||
message: 'assert.notDeepStrictEqual() must be invoked with at least two arguments.',
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.property.name='deepStrictEqual'][arguments.length<2]",
|
||||
message: 'assert.deepStrictEqual() must be invoked with at least two arguments.',
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.property.name='notStrictEqual'][arguments.length<2]",
|
||||
message: 'assert.notStrictEqual() must be invoked with at least two arguments.',
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.property.name='strictEqual'][arguments.length<2]",
|
||||
message: 'assert.strictEqual() must be invoked with at least two arguments.',
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])",
|
||||
message: 'The first argument should be the `actual`, not the `expected` value.',
|
||||
|
@ -25,7 +25,8 @@ const { codes: {
|
||||
ERR_AMBIGUOUS_ARGUMENT,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_ARG_VALUE,
|
||||
ERR_INVALID_RETURN_VALUE
|
||||
ERR_INVALID_RETURN_VALUE,
|
||||
ERR_MISSING_ARGS
|
||||
} } = require('internal/errors');
|
||||
const AssertionError = require('internal/assert/assertion_error');
|
||||
const { openSync, closeSync, readSync } = require('fs');
|
||||
@ -351,6 +352,9 @@ assert.ok = ok;
|
||||
// The equality assertion tests shallow, coercive equality with ==.
|
||||
/* eslint-disable no-restricted-properties */
|
||||
assert.equal = function equal(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
// eslint-disable-next-line eqeqeq
|
||||
if (actual != expected) {
|
||||
innerFail({
|
||||
@ -366,6 +370,9 @@ assert.equal = function equal(actual, expected, message) {
|
||||
// The non-equality assertion tests for whether two objects are not
|
||||
// equal with !=.
|
||||
assert.notEqual = function notEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
// eslint-disable-next-line eqeqeq
|
||||
if (actual == expected) {
|
||||
innerFail({
|
||||
@ -380,6 +387,9 @@ assert.notEqual = function notEqual(actual, expected, message) {
|
||||
|
||||
// The equivalence assertion tests a deep equality relation.
|
||||
assert.deepEqual = function deepEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (!isDeepEqual(actual, expected)) {
|
||||
innerFail({
|
||||
@ -394,6 +404,9 @@ assert.deepEqual = function deepEqual(actual, expected, message) {
|
||||
|
||||
// The non-equivalence assertion tests for any deep inequality.
|
||||
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (isDeepEqual(actual, expected)) {
|
||||
innerFail({
|
||||
@ -408,6 +421,9 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
||||
/* eslint-enable */
|
||||
|
||||
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (!isDeepStrictEqual(actual, expected)) {
|
||||
innerFail({
|
||||
@ -422,6 +438,9 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
||||
|
||||
assert.notDeepStrictEqual = notDeepStrictEqual;
|
||||
function notDeepStrictEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (isDeepStrictEqual(actual, expected)) {
|
||||
innerFail({
|
||||
@ -435,6 +454,9 @@ function notDeepStrictEqual(actual, expected, message) {
|
||||
}
|
||||
|
||||
assert.strictEqual = function strictEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (!Object.is(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
@ -447,6 +469,9 @@ assert.strictEqual = function strictEqual(actual, expected, message) {
|
||||
};
|
||||
|
||||
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('actual', 'expected');
|
||||
}
|
||||
if (Object.is(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
|
@ -560,7 +560,7 @@ assert.throws(
|
||||
'- undefined',
|
||||
].join('\n');
|
||||
assert.throws(
|
||||
() => assert.deepEqual([1, 2, 1]),
|
||||
() => assert.deepEqual([1, 2, 1], undefined),
|
||||
{ message });
|
||||
|
||||
message = [
|
||||
@ -1147,3 +1147,53 @@ assert.throws(
|
||||
}
|
||||
assert(threw);
|
||||
}
|
||||
|
||||
assert.throws(
|
||||
() => a.equal(1),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.deepEqual(/a/),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.notEqual(null),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.notDeepEqual('test'),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.strictEqual({}),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.deepStrictEqual(Symbol()),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.notStrictEqual(5n),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.notDeepStrictEqual(undefined),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.strictEqual(),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => a.deepStrictEqual(),
|
||||
{ code: 'ERR_MISSING_ARGS' }
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user