Skip to content

Commit

Permalink
assert: validate required arguments
Browse files Browse the repository at this point in the history
This validates most `assert` functions to verify that the required
arguments are indeed passed to the function.

PR-URL: nodejs#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>
  • Loading branch information
BridgeAR committed Mar 21, 2019
1 parent ab3f7af commit 5a3623a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
16 changes: 0 additions & 16 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
27 changes: 26 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -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,
Expand Down
52 changes: 51 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ assert.throws(
'- undefined',
].join('\n');
assert.throws(
() => assert.deepEqual([1, 2, 1]),
() => assert.deepEqual([1, 2, 1], undefined),
{ message });

message = [
Expand Down Expand Up @@ -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' }
);

0 comments on commit 5a3623a

Please sign in to comment.