Skip to content

Commit

Permalink
[Breaking] make equality functions consistent:
Browse files Browse the repository at this point in the history
 - `is`/`isNot`/`not`/`notStrictEqual`/`notStrictEquals`: use `Object.is` instead of `===`
 - `notEqual`/`notEquals`/`isNotEqual`/`doesNotEqual`/`isInequal`: use `==` instead of `===`

See #495
  • Loading branch information
ljharb committed Feb 21, 2020
1 parent 8f85337 commit 24240e2
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 36 deletions.
26 changes: 20 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ function equal(a, b, msg, extra) {
Test.prototype.equal
= Test.prototype.equals
= Test.prototype.isEqual
= Test.prototype.is
= equal;

function strictEqual(a, b, msg, extra) {
Expand All @@ -449,13 +448,14 @@ function strictEqual(a, b, msg, extra) {
}
Test.prototype.strictEqual
= Test.prototype.strictEquals
= Test.prototype.is
= strictEqual;

function notEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(a !== b, {
this._assert(a != b, {
message: defined(msg, 'should not be equal'),
operator: 'notEqual',
actual: a,
Expand All @@ -465,15 +465,29 @@ function notEqual(a, b, msg, extra) {
}
Test.prototype.notEqual
= Test.prototype.notEquals
= Test.prototype.notStrictEqual
= Test.prototype.notStrictEquals
= Test.prototype.isNotEqual
= Test.prototype.isNot
= Test.prototype.not
= Test.prototype.doesNotEqual
= Test.prototype.isInequal
= notEqual;

function notStrictEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
}
this._assert(!is(a, b), {
message: defined(msg, 'should not be strictly equal'),
operator: 'notStrictEqual',
actual: a,
expected: b,
extra: extra
});
}
Test.prototype.notStrictEqual
= Test.prototype.notStrictEquals
= Test.prototype.isNot
= Test.prototype.not
= notStrictEqual;

function tapeDeepEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
Expand Down
22 changes: 16 additions & 6 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,27 @@ Aliases: `t.ifError()`, `t.ifErr()`, `t.iferror()`

## t.equal(actual, expected, msg)

Assert that `actual === expected` with an optional description of the assertion `msg`.
Assert that `actual == expected` with an optional description of the assertion `msg`.

Aliases: `t.equals()`, `t.isEqual()`, `t.is()`, `t.strictEqual()`,
`t.strictEquals()`
Aliases: `t.equals()`, `t.isEqual()`

## t.strictEqual(actual, expected, msg)

Assert that `Object.is(actual, expected)` with an optional description of the assertion `msg`.

Aliases: `t.is()`, `t.strictEqual()`, `t.strictEquals()`

## t.notEqual(actual, expected, msg)

Assert that `actual !== expected` with an optional description of the assertion `msg`.
Assert that `actual != expected` with an optional description of the assertion `msg`.

Aliases: `t.notEquals()`, `t.isNotEqual()`, `t.doesNotEqual()`, `t.isInequal()`

## t.notStrictEqual(actual, expected, msg)

Assert that `!Object.is(actual, expected)` with an optional description of the assertion `msg`.

Aliases: `t.notEquals()`, `t.notStrictEqual()`, `t.notStrictEquals()`,
`t.isNotEqual()`, `t.isNot()`, `t.not()`, `t.doesNotEqual()`, `t.isInequal()`
Aliases: `t.notStrictEqual()`, `t.notStrictEquals()`, `t.isNot()`, `t.not()`

## t.deepEqual(actual, expected, msg)

Expand Down
126 changes: 112 additions & 14 deletions test/edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,35 @@ tap.test('edge cases', function (tt) {
+ '# zeroes\n'
+ 'ok 1 0 equal to -0\n'
+ 'ok 2 -0 equal to 0\n'
+ 'not ok 3 0 strictEqual to -0\n'
+ 'not ok 3 0 notEqual to -0\n'
+ ' ---\n'
+ ' operator: notEqual\n'
+ ' expected: |-\n'
+ ' -0\n'
+ ' actual: |-\n'
+ ' 0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: 0 notEqual to -0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 4 -0 notEqual to 0\n'
+ ' ---\n'
+ ' operator: notEqual\n'
+ ' expected: |-\n'
+ ' 0\n'
+ ' actual: |-\n'
+ ' -0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: -0 notEqual to 0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 5 0 strictEqual to -0\n'
+ ' ---\n'
+ ' operator: strictEqual\n'
+ ' expected: |-\n'
Expand All @@ -31,7 +59,7 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 4 -0 strictEqual to 0\n'
+ 'not ok 6 -0 strictEqual to 0\n'
+ ' ---\n'
+ ' operator: strictEqual\n'
+ ' expected: |-\n'
Expand All @@ -45,9 +73,39 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'ok 5 0 deepLooseEqual to -0\n'
+ 'ok 6 -0 deepLooseEqual to 0\n'
+ 'not ok 7 0 deepEqual to -0\n'
+ 'ok 7 0 notStrictEqual to -0\n'
+ 'ok 8 -0 notStrictEqual to 0\n'
+ 'ok 9 0 deepLooseEqual to -0\n'
+ 'ok 10 -0 deepLooseEqual to 0\n'
+ 'not ok 11 0 notDeepLooseEqual to -0\n'
+ ' ---\n'
+ ' operator: notDeepLooseEqual\n'
+ ' expected: |-\n'
+ ' -0\n'
+ ' actual: |-\n'
+ ' 0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: 0 notDeepLooseEqual to -0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 12 -0 notDeepLooseEqual to 0\n'
+ ' ---\n'
+ ' operator: notDeepLooseEqual\n'
+ ' expected: |-\n'
+ ' 0\n'
+ ' actual: |-\n'
+ ' -0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: -0 notDeepLooseEqual to 0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 13 0 deepEqual to -0\n'
+ ' ---\n'
+ ' operator: deepEqual\n'
+ ' expected: |-\n'
Expand All @@ -61,7 +119,7 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 8 -0 deepEqual to 0\n'
+ 'not ok 14 -0 deepEqual to 0\n'
+ ' ---\n'
+ ' operator: deepEqual\n'
+ ' expected: |-\n'
Expand All @@ -75,8 +133,10 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'ok 15 0 notDeepEqual to -0\n'
+ 'ok 16 -0 notDeepEqual to 0\n'
+ '# NaNs\n'
+ 'not ok 9 NaN equal to NaN\n'
+ 'not ok 17 NaN equal to NaN\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: NaN\n'
Expand All @@ -88,8 +148,21 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'ok 10 NaN strictEqual to NaN\n'
+ 'not ok 11 NaN deepLooseEqual to NaN\n'
+ 'ok 18 NaN notEqual to NaN\n'
+ 'ok 19 NaN strictEqual to NaN\n'
+ 'not ok 20 NaN notStrictEqual to NaN\n'
+ ' ---\n'
+ ' operator: notStrictEqual\n'
+ ' expected: NaN\n'
+ ' actual: NaN\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: NaN notStrictEqual to NaN\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 21 NaN deepLooseEqual to NaN\n'
+ ' ---\n'
+ ' operator: deepLooseEqual\n'
+ ' expected: NaN\n'
Expand All @@ -101,38 +174,63 @@ tap.test('edge cases', function (tt) {
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'ok 12 NaN deepEqual to NaN\n'
+ '\n1..12\n'
+ '# tests 12\n'
+ '# pass 6\n'
+ '# fail 6\n'
+ 'ok 22 NaN notDeepLooseEqual to NaN\n'
+ 'ok 23 NaN deepEqual to NaN\n'
+ 'not ok 24 NaN notDeepEqual to NaN\n'
+ ' ---\n'
+ ' operator: notDeepEqual\n'
+ ' expected: NaN\n'
+ ' actual: NaN\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: NaN notDeepEqual to NaN\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ '\n1..24\n'
+ '# tests 24\n'
+ '# pass 12\n'
+ '# fail 12\n'
);
}));

test('zeroes', function (t) {
t.equal(0, -0, '0 equal to -0');
t.equal(-0, 0, '-0 equal to 0');
t.notEqual(0, -0, '0 notEqual to -0');
t.notEqual(-0, 0, '-0 notEqual to 0');

t.strictEqual(0, -0, '0 strictEqual to -0');
t.strictEqual(-0, 0, '-0 strictEqual to 0');
t.notStrictEqual(0, -0, '0 notStrictEqual to -0');
t.notStrictEqual(-0, 0, '-0 notStrictEqual to 0');

t.deepLooseEqual(0, -0, '0 deepLooseEqual to -0');
t.deepLooseEqual(-0, 0, '-0 deepLooseEqual to 0');
t.notDeepLooseEqual(0, -0, '0 notDeepLooseEqual to -0');
t.notDeepLooseEqual(-0, 0, '-0 notDeepLooseEqual to 0');

t.deepEqual(0, -0, '0 deepEqual to -0');
t.deepEqual(-0, 0, '-0 deepEqual to 0');
t.notDeepEqual(0, -0, '0 notDeepEqual to -0');
t.notDeepEqual(-0, 0, '-0 notDeepEqual to 0');

t.end();
});

test('NaNs', function (t) {
t.equal(NaN, NaN, 'NaN equal to NaN');
t.notEqual(NaN, NaN, 'NaN notEqual to NaN');

t.strictEqual(NaN, NaN, 'NaN strictEqual to NaN');
t.notStrictEqual(NaN, NaN, 'NaN notStrictEqual to NaN');

t.deepLooseEqual(NaN, NaN, 'NaN deepLooseEqual to NaN');
t.notDeepLooseEqual(NaN, NaN, 'NaN notDeepLooseEqual to NaN');

t.deepEqual(NaN, NaN, 'NaN deepEqual to NaN');
t.notDeepEqual(NaN, NaN, 'NaN notDeepEqual to NaN');

t.end();
});
Expand Down
Loading

0 comments on commit 24240e2

Please sign in to comment.