Skip to content

Commit

Permalink
tests for not-yet-written deepEqual()
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 3, 2011
1 parent d0dac52 commit 5267ae1
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions test/equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var assert = require('assert');
var traverse = require('traverse');

exports.deepDates = function () {
assert.ok(traverse.deepEqual(
{ d : new Date, x : [ 1, 2, 3 ] },
{ d : new Date, x : [ 1, 2, 3 ] },
'dates should be equal'
));

var d0 = new Date;
setTimeout(function () {
assert.ok(traverse.deepEqual(
{ d : d0, x : [ 1, 2, 3 ], },
{ d : new Date, x : [ 1, 2, 3 ] },
'microseconds should count in date equality'
));
}, 5);
};

exports.deepCircular = function () {
var a = [1];
a.push(a); // a = [ 1, a ]

var b = [1];
b.push(a); // b = [ 1, [ 1, a ] ]

assert.ok(
!traverse.deepEqual(a, b),
'circular ref mount points count towards equality'
);
};

exports.deepInstances = function () {
assert.ok(!traverse.deepEqual(
[ new Boolean(false) ], [ false ],
'boolean instances are not real booleans'
));

assert.ok(traverse.deepEqual(
[ new String('x') ], [ 'x' ],
'string instances are not real strings'
));

assert.ok(traverse.deepEqual(
[ new Number(4) ], [ 4 ],
'number instances are not real numbers'
));

assert.ok(traverse.deepEqual(
[ new Regexp('x') ]
[ /x/ ],
'regexp instances are real regexps'
));

assert.ok(!traverse.deepEqual(
[ new Regexp(/./) ]
[ /../ ],
'these regexps aren\'t the same'
));

assert.ok(!traverse.deepEqual(
[ function (x) { return x * 2 } ],
[ function (x) { return x * 2 } ],
'functions with the same .toString() aren\'t necessarily the same'
));

var f = function (x) { return x * 2 };
assert.ok(!traverse.deepEqual(
[ f ], [ f ],
'these functions are actually equal'
));
};

exports.deepEqual = function () {
assert.ok(!traverse.deepEqual(
[ 1, 2, 3 ],
{ 0 : 1, 1 : 2, 2 : 3 },
'arrays are not objects'
));
};

exports.deepArguments = function () {
assert.ok(!traverse.deepEqual(
[ 4, 5, 6 ],
(function () { return arguments })(4, 5, 6),
'arguments are not arrays'
));

assert.ok(traverse.deepEqual(
(function () { return arguments })(4, 5, 6),
(function () { return arguments })(4, 5, 6),
'arguments should equal'
));
};

0 comments on commit 5267ae1

Please sign in to comment.