Skip to content

Commit

Permalink
[Breaking] strictEqual: bring -0/0, and NaN into line with `a…
Browse files Browse the repository at this point in the history
…ssert`

Fixes #495.
  • Loading branch information
ljharb committed Feb 20, 2020
1 parent 15ea7d1 commit f6812f3
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var trim = require('string.prototype.trim');
var bind = require('function-bind');
var forEach = require('for-each');
var inspect = require('object-inspect');
var is = require('object-is');
var isEnumerable = bind.call(Function.call, Object.prototype.propertyIsEnumerable);
var toLowerCase = bind.call(Function.call, String.prototype.toLowerCase);
var isProto = bind.call(Function.call, Object.prototype.isPrototypeOf);
Expand Down Expand Up @@ -432,10 +433,24 @@ Test.prototype.equal
= Test.prototype.equals
= Test.prototype.isEqual
= Test.prototype.is
= Test.prototype.strictEqual
= Test.prototype.strictEquals
= equal;

function strictEqual(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 be strictly equal'),
operator: 'strictEqual',
actual: a,
expected: b,
extra: extra
});
}
Test.prototype.strictEqual
= Test.prototype.strictEquals
= strictEqual;

function notEqual(a, b, msg, extra) {
if (arguments.length < 2) {
throw new TypeError('two arguments must be provided to compare');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"is-regex": "^1.0.5",
"minimist": "^1.2.0",
"object-inspect": "^1.7.0",
"object-is": "^1.0.2",
"object.assign": "^4.1.0",
"resolve": "^1.15.1",
"resumer": "^0.0.0",
Expand Down
139 changes: 139 additions & 0 deletions test/edge-cases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'use strict';

var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');

var stripFullStack = require('./common').stripFullStack;

tap.test('edge cases', function (tt) {
tt.plan(1);

var test = tape.createHarness();
test.createStream().pipe(concat(function (body) {
tt.equal(
stripFullStack(body.toString('utf8')),
'TAP version 13\n'
+ '# zeroes\n'
+ 'ok 1 0 equal to -0\n'
+ 'ok 2 -0 equal to 0\n'
+ 'not ok 3 0 strictEqual to -0\n'
+ ' ---\n'
+ ' operator: strictEqual\n'
+ ' expected: |-\n'
+ ' -0\n'
+ ' actual: |-\n'
+ ' 0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: 0 strictEqual to -0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 4 -0 strictEqual to 0\n'
+ ' ---\n'
+ ' operator: strictEqual\n'
+ ' expected: |-\n'
+ ' 0\n'
+ ' actual: |-\n'
+ ' -0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: -0 strictEqual to 0\n'
+ ' [... stack stripped ...]\n'
+ ' 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'
+ ' ---\n'
+ ' operator: deepEqual\n'
+ ' expected: |-\n'
+ ' -0\n'
+ ' actual: |-\n'
+ ' 0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: 0 deepEqual to -0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ 'not ok 8 -0 deepEqual to 0\n'
+ ' ---\n'
+ ' operator: deepEqual\n'
+ ' expected: |-\n'
+ ' 0\n'
+ ' actual: |-\n'
+ ' -0\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: -0 deepEqual to 0\n'
+ ' [... stack stripped ...]\n'
+ ' at Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' [... stack stripped ...]\n'
+ ' ...\n'
+ '# NaNs\n'
+ 'not ok 9 NaN equal to NaN\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: NaN\n'
+ ' actual: NaN\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: NaN equal to NaN\n'
+ ' [... stack stripped ...]\n'
+ ' 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'
+ ' ---\n'
+ ' operator: deepLooseEqual\n'
+ ' expected: NaN\n'
+ ' actual: NaN\n'
+ ' at: Test.<anonymous> ($TEST/edge-cases.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: NaN deepLooseEqual to NaN\n'
+ ' [... stack stripped ...]\n'
+ ' 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'
);
}));

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

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

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

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

t.end();
});

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

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

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

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

t.end();
});
});

0 comments on commit f6812f3

Please sign in to comment.