Skip to content

Commit

Permalink
[Breaking] equality functions: throw when < 2 arguments are provided
Browse files Browse the repository at this point in the history
Fixes #442.
  • Loading branch information
ljharb committed Dec 27, 2019
1 parent 0e713a2 commit 89f2010
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ Test.prototype.error
= error;

function equal(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(a === b, {
message: defined(msg, 'should be equal'),
operator: 'equal',
Expand All @@ -422,6 +425,9 @@ Test.prototype.equal
= equal;

function notEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(a !== b, {
message: defined(msg, 'should not be equal'),
operator: 'notEqual',
Expand All @@ -442,6 +448,9 @@ Test.prototype.notEqual
= notEqual;

function tapeDeepEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(deepEqual(a, b, { strict: true }), {
message: defined(msg, 'should be equivalent'),
operator: 'deepEqual',
Expand All @@ -457,6 +466,9 @@ Test.prototype.deepEqual
= tapeDeepEqual;

function deepLooseEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(deepEqual(a, b), {
message: defined(msg, 'should be equivalent'),
operator: 'deepLooseEqual',
Expand All @@ -471,6 +483,9 @@ Test.prototype.deepLooseEqual
= deepLooseEqual;

function notDeepEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(!deepEqual(a, b, { strict: true }), {
message: defined(msg, 'should not be equivalent'),
operator: 'notDeepEqual',
Expand All @@ -491,6 +506,9 @@ Test.prototype.notDeepEqual
= notDeepEqual;

function notDeepLooseEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(!deepEqual(a, b), {
message: defined(msg, 'should be equivalent'),
operator: 'notDeepLooseEqual',
Expand Down
18 changes: 18 additions & 0 deletions test/deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ test('deep loose equal', function (t) {
);
t.end();
});

test('requires 2 arguments', function (t) {
var err = /^TypeError: two arguments must be provided/;
t.throws(function () { t.deepEqual(); }, err, 'deepEqual: no args');
t.throws(function () { t.deepEqual(undefined); }, err, 'deepEqual: one arg');
t.throws(function () { t.deepLooseEqual(); }, err, 'deepLooseEqual: no args');
t.throws(function () { t.deepLooseEqual(undefined); }, err, 'deepLooseEqual: one arg');
t.throws(function () { t.notDeepEqual(); }, err, 'notDeepEqual: no args');
t.throws(function () { t.notDeepEqual(undefined); }, err, 'notDeepEqual: one arg');
t.throws(function () { t.notDeepLooseEqual(); }, err, 'notDeepLooseEqual: no args');
t.throws(function () { t.notDeepLooseEqual(undefined); }, err, 'notDeepLooseEqual: one arg');
t.throws(function () { t.equal(); }, err, 'equal: no args');
t.throws(function () { t.equal(undefined); }, err, 'equal: one arg');
t.throws(function () { t.notEqual(); }, err, 'notEqual: no args');
t.throws(function () { t.notEqual(undefined); }, err, 'notEqual: one arg');

t.end();
});

0 comments on commit 89f2010

Please sign in to comment.