Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compare inherited properties not prototypes #22

Merged
merged 2 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ function getGeneratorEntries(generator) {
return accumulator;
}

/*!
* Gets all own and inherited enumerable keys from a target.
*
* @param {Object} target
* @returns {Array} an array of own and inherited enumerable keys from the target.
*/
function getEnumerableKeys(target) {
var keys = [];
for (var key in target) {
keys.push(key);
}
return keys;
}

/*!
* Determines if two objects have matching values, given a set of keys. Defers to deepEqual for the equality check of
* each key. If any value of the given key is not equal, the function will return false (early).
Expand Down Expand Up @@ -391,12 +405,8 @@ function keysEqual(leftHandOperand, rightHandOperand, keys, options) {
*/

function objectEqual(leftHandOperand, rightHandOperand, options) {
if (Object.getPrototypeOf(leftHandOperand) !== Object.getPrototypeOf(rightHandOperand)) {
return false;
}

var leftHandKeys = Object.keys(leftHandOperand);
var rightHandKeys = Object.keys(rightHandOperand);
var leftHandKeys = getEnumerableKeys(leftHandOperand);
var rightHandKeys = getEnumerableKeys(rightHandOperand);
if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) {
leftHandKeys.sort();
rightHandKeys.sort();
Expand Down
30 changes: 28 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,29 @@ describe('Generic', function () {
assert(eql(new BaseA(1), new BaseA(1)), 'eql(new BaseA(1), new BaseA(1))');
});

it('returns true given two class instances with deeply equal bases', function () {
function BaseA() {}
function BaseB() {}
BaseA.prototype.foo = { a: 1 };
BaseB.prototype.foo = { a: 1 };
assert(eql(new BaseA(), new BaseB()) === true,
'eql(new <base with .prototype.foo = { a: 1 }>, new <base with .prototype.foo = { a: 1 }>) === true');
});

it('returns false given two class instances with different properties', function () {
function BaseA(prop) {
this.prop = prop;
}
assert(eql(new BaseA(1), new BaseA(2)) === false, 'eql(new BaseA(1), new BaseA(2)) === false');
});

it('returns false given two different empty class instances', function () {
it('returns false given two class instances with deeply unequal bases', function () {
function BaseA() {}
function BaseB() {}
assert(eql(new BaseA(), new BaseB()) === false, 'eql(new BaseA(), new BaseB()) === false');
BaseA.prototype.foo = { a: 1 };
BaseB.prototype.foo = { a: 2 };
assert(eql(new BaseA(), new BaseB()) === false,
'eql(new <base with .prototype.foo = { a: 1 }>, new <base with .prototype.foo = { a: 2 }>) === false');
});

});
Expand Down Expand Up @@ -283,6 +295,13 @@ describe('Generic', function () {
'eql({ foo: 1, bar: objectC }, { foo: 1, bar: objectC }) === true');
});

it('returns true with objects with deeply equal prototypes', function () {
var objectA = Object.create({ foo: { a: 1 } });
var objectB = Object.create({ foo: { a: 1 } });
assert(eql(objectA, objectB) === true,
'eql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 1 } })) === true');
});

it('returns false with objects containing different literals', function () {
assert(eql({ foo: 1, bar: 1 }, { foo: 1, bar: 2 }) === false,
'eql({ foo: 1, bar: 2 }, { foo: 1, bar: 2 }) === false');
Expand All @@ -306,6 +325,13 @@ describe('Generic', function () {
'eql({ foo: 1, bar: -> }, { foo: 1, bar: <- }) === true');
});

it('returns false with objects with deeply unequal prototypes', function () {
var objectA = Object.create({ foo: { a: 1 } });
var objectB = Object.create({ foo: { a: 2 } });
assert(eql(objectA, objectB) === false,
'eql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 2 } })) === false');
});

});

describe('functions', function () {
Expand Down