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

Deep equality check for collection membership #228

Merged
merged 1 commit into from
Jan 30, 2014
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
20 changes: 15 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1245,24 +1245,32 @@ module.exports = function (chai, _) {
);
});

function isSubsetOf(subset, superset) {
function isSubsetOf(subset, superset, cmp) {
return subset.every(function(elem) {
return superset.indexOf(elem) !== -1;
if (!cmp) return superset.indexOf(elem) !== -1;

return superset.some(function(elem2) {
return cmp(elem, elem2);
});
})
}

/**
* ### .members(set)
*
* Asserts that the target is a superset of `set`,
* or that the target and `set` have the same members.
* or that the target and `set` have the same strictly-equal (===) members.
* Alternately, if the `deep` flag is set, set members are compared for deep
* equality.
*
* expect([1, 2, 3]).to.include.members([3, 2]);
* expect([1, 2, 3]).to.not.include.members([3, 2, 8]);
*
* expect([4, 2]).to.have.members([2, 4]);
* expect([5, 2]).to.not.have.members([5, 2, 1]);
*
* expect([{ id: 1 }]).to.deep.include.members([{ id: 1 }]);
*
* @name members
* @param {Array} set
* @param {String} message _optional_
Expand All @@ -1276,9 +1284,11 @@ module.exports = function (chai, _) {
new Assertion(obj).to.be.an('array');
new Assertion(subset).to.be.an('array');

var cmp = flag(this, 'deep') ? _.eql : undefined;

if (flag(this, 'contains')) {
return this.assert(
isSubsetOf(subset, obj)
isSubsetOf(subset, obj, cmp)
, 'expected #{this} to be a superset of #{act}'
, 'expected #{this} to not be a superset of #{act}'
, obj
Expand All @@ -1287,7 +1297,7 @@ module.exports = function (chai, _) {
}

this.assert(
isSubsetOf(obj, subset) && isSubsetOf(subset, obj)
isSubsetOf(obj, subset, cmp) && isSubsetOf(subset, obj, cmp)
, 'expected #{this} to have the same members as #{act}'
, 'expected #{this} to not have the same members as #{act}'
, obj
Expand Down
11 changes: 10 additions & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,15 @@ describe('expect', function () {
expect([5, 4]).not.members([]);
expect([5, 4]).not.members([6, 3]);
expect([5, 4]).not.members([5, 4, 2]);
})
expect([{ id: 1 }]).not.members([{ id: 1 }]);
});

it('deep.members', function() {
expect([{ id: 1 }]).deep.members([{ id: 1 }]);
expect([{ id: 2 }]).not.deep.members([{ id: 1 }]);
err(function(){
expect([{ id: 1 }]).deep.members([{ id: 2 }])
}, "expected [ { id: 1 } ] to have the same members as [ { id: 2 } ]");
});

});