From 89f2010a825ad68318dd086350ce1280110238d1 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 27 Dec 2019 00:29:07 -0800 Subject: [PATCH] [Breaking] equality functions: throw when < 2 arguments are provided Fixes #442. --- lib/test.js | 18 ++++++++++++++++++ test/deep.js | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/test.js b/lib/test.js index 52fc76d0..97247b34 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', diff --git a/test/deep.js b/test/deep.js index 909ebe10..bedc9a24 100644 --- a/test/deep.js +++ b/test/deep.js @@ -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(); +});