Skip to content

Commit

Permalink
Merge pull request #7 from teppeis/es6-object-keys
Browse files Browse the repository at this point in the history
Fix deepEqual for ES6 Object.keys
  • Loading branch information
defunctzombie committed Dec 24, 2014
2 parents d08b0bf + fd1cd62 commit 98c05e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
16 changes: 7 additions & 9 deletions assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ function objEquiv(a, b) {
return false;
// an identical 'prototype' property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
// if one is a primitive, the other must be same
if (util.isPrimitive(a) || util.isPrimitive(b)) {
return a === b;
}
var aIsArgs = isArguments(a),
bIsArgs = isArguments(b);
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
Expand All @@ -236,13 +238,9 @@ function objEquiv(a, b) {
b = pSlice.call(b);
return _deepEqual(a, b);
}
try {
var ka = objectKeys(a),
kb = objectKeys(b),
key, i;
} catch (e) {//happens when one is a string literal and the other isn't
return false;
}
var ka = objectKeys(a),
kb = objectKeys(b),
key, i;
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length != kb.length)
Expand Down
20 changes: 18 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,25 @@ test('assert.deepEqual - instances', function () {
nameBuilder2.prototype = Object;
nb2 = new nameBuilder2('Ryan', 'Dahl');
assert.throws(makeBlock(assert.deepEqual, nb1, nb2), assert.AssertionError);
});

test('assert.deepEqual - ES6 primitives', function () {
assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError);
assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError);
assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError);
assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError);
assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError);
assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError);
if (typeof Symbol === 'symbol') {
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError);
}
});

// String literal + object blew up my implementation...
assert.throws(makeBlock(assert.deepEqual, 'a', {}), assert.AssertionError);
test('assert.deepEqual - object wrappers', function () {
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a']));
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'}));
assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {}));
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
});

function thrower(errorConstructor) {
Expand Down

0 comments on commit 98c05e5

Please sign in to comment.