Skip to content

Commit

Permalink
Set support in same members (#1583)
Browse files Browse the repository at this point in the history
* Implement `iterator` assertion

* Move JSDoc to it's function

* Add support for Sets in `members` assertion

* Add `sameMembers` test for `Set`

* Update tests

* Implement `iterable` assertion

* Change `iterable` implementation to a property

* Make changes after merging

* Add more tests for members equality
  • Loading branch information
koddsson committed Feb 12, 2024
1 parent f224339 commit 1ba37b5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
9 changes: 6 additions & 3 deletions lib/chai/core/assertions.js
Expand Up @@ -3037,7 +3037,9 @@ Assertion.addMethod('closeTo', closeTo);
Assertion.addMethod('approximately', closeTo);

// Note: Duplicates are ignored if testing for inclusion instead of sameness.
function isSubsetOf(subset, superset, cmp, contains, ordered) {
function isSubsetOf(_subset, _superset, cmp, contains, ordered) {
let superset = Array.from(_superset);
let subset = Array.from(_subset);
if (!contains) {
if (subset.length !== superset.length) return false;
superset = superset.slice();
Expand Down Expand Up @@ -3139,8 +3141,8 @@ Assertion.addMethod('members', function (subset, msg) {
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
new Assertion(subset, flagMsg, ssfi, true).to.be.an('array');
new Assertion(obj, flagMsg, ssfi, true).to.be.iterable;
new Assertion(subset, flagMsg, ssfi, true).to.be.iterable;

var contains = flag(this, 'contains');
var ordered = flag(this, 'ordered');
Expand Down Expand Up @@ -3175,6 +3177,7 @@ Assertion.addMethod('members', function (subset, msg) {
* Asserts that the target is an iterable, which means that it has a iterator.
*
* expect([1, 2]).to.be.iterable;
* expect("foobar").to.be.iterable;
*
* Add `.not` earlier in the chain to negate `.iterable`.
*
Expand Down
6 changes: 4 additions & 2 deletions test/assert.js
Expand Up @@ -1909,6 +1909,8 @@ describe('assert', function () {
assert.sameMembers([4, 2], [4, 2]);
assert.sameMembers([4, 2, 2], [4, 2, 2]);

assert.sameMembers(new Set([1,2,3]), new Set([3,2,1]));

err(function() {
assert.sameMembers([], [1, 2], 'blah');
}, 'blah: expected [] to have the same members as [ 1, 2 ]');
Expand All @@ -1919,11 +1921,11 @@ describe('assert', function () {

err(function () {
assert.sameMembers({}, [], 'blah');
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');

err(function () {
assert.sameMembers([], {}, 'blah');
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');
});

it('notSameMembers', function() {
Expand Down
13 changes: 8 additions & 5 deletions test/expect.js
Expand Up @@ -3397,16 +3397,19 @@ describe('expect', function () {
});

it('same.members', function() {
expect([5, 4]).to.have.same.members([4, 5]);
expect([5, 4]).to.have.same.members([5, 4]);
expect([5, 4]).to.have.same.members([4, 5]);
expect([5, 4, 4]).to.have.same.members([5, 4, 4]);
expect(new Set([5, 4])).to.have.same.members([4, 5]);

expect([5, 4]).to.not.have.same.members([]);
expect([5, 4]).to.not.have.same.members([6, 3]);
expect([5, 4]).to.not.have.same.members([5, 4, 2]);
expect([5, 4]).to.not.have.same.members([5, 4, 4]);
expect([5, 4, 4]).to.not.have.same.members([5, 4]);
expect([5, 4, 4]).to.not.have.same.members([5, 4, 3]);
expect([5, 4, 3]).to.not.have.same.members([5, 4, 4]);
expect(new Set([5, 4])).to.not.have.same.members([4]);
});

it('members', function() {
Expand Down Expand Up @@ -3436,19 +3439,19 @@ describe('expect', function () {

err(function () {
expect({}).members([], 'blah');
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');

err(function () {
expect({}, 'blah').members([]);
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');

err(function () {
expect([]).members({}, 'blah');
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');

err(function () {
expect([], 'blah').members({});
}, 'blah: expected {} to be an array');
}, 'blah: expected {} to be an iterable');
});

it('deep.members', function() {
Expand Down
9 changes: 6 additions & 3 deletions test/should.js
Expand Up @@ -2815,32 +2815,35 @@ describe('should', function() {

err(function() {
'foo'.should.include.members([12], 'blah');
}, "blah: expected 'foo' to be an array");
}, "blah: expected 'foo' to be a superset of [ 12 ]");

err(function() {
[1, 2, 3].should.include.members('o', 'blah');
}, "blah: expected 'o' to be an array");
}, "blah: expected [ 1, 2, 3 ] to be a superset of 'o'");
});

it('memberEquals', function() {
[1, 2, 3].should.have.same.members([3, 2, 1]);
[5, 4].should.have.same.members([5, 4]);
[5, 4, 4].should.have.same.members([5, 4, 4]);
[].should.have.same.members([]);
(new Set([])).should.have.same.members(new Set([]));
(new Set([1,2,3])).should.have.same.members(new Set([3,2,1]));

[5, 4].should.not.have.same.members([5, 4, 4]);
[5, 4, 4].should.not.have.same.members([5, 4]);
[5, 4, 4].should.not.have.same.members([5, 4, 3]);
[5, 4, 3].should.not.have.same.members([5, 4, 4]);
[{a: 1}].should.not.have.same.members([{a: 1}]);
(new Set([1,2,3])).should.not.have.same.members(new Set([2,1]));

err(function() {
[1, 2, 3].should.have.same.members([], 'blah');
}, 'blah: expected [ 1, 2, 3 ] to have the same members as []');

err(function() {
[1, 2, 3].should.have.same.members(4, 'blah');
}, 'blah: expected 4 to be an array');
}, 'blah: expected 4 to be an iterable');
});

it('deep.members', function() {
Expand Down

0 comments on commit 1ba37b5

Please sign in to comment.