Skip to content

Commit 1ba37b5

Browse files
author
Kristján Oddsson
authored
Set support in same members (#1583)
* 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
1 parent f224339 commit 1ba37b5

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

Diff for: lib/chai/core/assertions.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,9 @@ Assertion.addMethod('closeTo', closeTo);
30373037
Assertion.addMethod('approximately', closeTo);
30383038

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

3142-
new Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
3143-
new Assertion(subset, flagMsg, ssfi, true).to.be.an('array');
3144+
new Assertion(obj, flagMsg, ssfi, true).to.be.iterable;
3145+
new Assertion(subset, flagMsg, ssfi, true).to.be.iterable;
31443146

31453147
var contains = flag(this, 'contains');
31463148
var ordered = flag(this, 'ordered');
@@ -3175,6 +3177,7 @@ Assertion.addMethod('members', function (subset, msg) {
31753177
* Asserts that the target is an iterable, which means that it has a iterator.
31763178
*
31773179
* expect([1, 2]).to.be.iterable;
3180+
* expect("foobar").to.be.iterable;
31783181
*
31793182
* Add `.not` earlier in the chain to negate `.iterable`.
31803183
*

Diff for: test/assert.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,8 @@ describe('assert', function () {
19091909
assert.sameMembers([4, 2], [4, 2]);
19101910
assert.sameMembers([4, 2, 2], [4, 2, 2]);
19111911

1912+
assert.sameMembers(new Set([1,2,3]), new Set([3,2,1]));
1913+
19121914
err(function() {
19131915
assert.sameMembers([], [1, 2], 'blah');
19141916
}, 'blah: expected [] to have the same members as [ 1, 2 ]');
@@ -1919,11 +1921,11 @@ describe('assert', function () {
19191921

19201922
err(function () {
19211923
assert.sameMembers({}, [], 'blah');
1922-
}, 'blah: expected {} to be an array');
1924+
}, 'blah: expected {} to be an iterable');
19231925

19241926
err(function () {
19251927
assert.sameMembers([], {}, 'blah');
1926-
}, 'blah: expected {} to be an array');
1928+
}, 'blah: expected {} to be an iterable');
19271929
});
19281930

19291931
it('notSameMembers', function() {

Diff for: test/expect.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -3397,16 +3397,19 @@ describe('expect', function () {
33973397
});
33983398

33993399
it('same.members', function() {
3400-
expect([5, 4]).to.have.same.members([4, 5]);
34013400
expect([5, 4]).to.have.same.members([5, 4]);
3401+
expect([5, 4]).to.have.same.members([4, 5]);
34023402
expect([5, 4, 4]).to.have.same.members([5, 4, 4]);
3403+
expect(new Set([5, 4])).to.have.same.members([4, 5]);
3404+
34033405
expect([5, 4]).to.not.have.same.members([]);
34043406
expect([5, 4]).to.not.have.same.members([6, 3]);
34053407
expect([5, 4]).to.not.have.same.members([5, 4, 2]);
34063408
expect([5, 4]).to.not.have.same.members([5, 4, 4]);
34073409
expect([5, 4, 4]).to.not.have.same.members([5, 4]);
34083410
expect([5, 4, 4]).to.not.have.same.members([5, 4, 3]);
34093411
expect([5, 4, 3]).to.not.have.same.members([5, 4, 4]);
3412+
expect(new Set([5, 4])).to.not.have.same.members([4]);
34103413
});
34113414

34123415
it('members', function() {
@@ -3436,19 +3439,19 @@ describe('expect', function () {
34363439

34373440
err(function () {
34383441
expect({}).members([], 'blah');
3439-
}, 'blah: expected {} to be an array');
3442+
}, 'blah: expected {} to be an iterable');
34403443

34413444
err(function () {
34423445
expect({}, 'blah').members([]);
3443-
}, 'blah: expected {} to be an array');
3446+
}, 'blah: expected {} to be an iterable');
34443447

34453448
err(function () {
34463449
expect([]).members({}, 'blah');
3447-
}, 'blah: expected {} to be an array');
3450+
}, 'blah: expected {} to be an iterable');
34483451

34493452
err(function () {
34503453
expect([], 'blah').members({});
3451-
}, 'blah: expected {} to be an array');
3454+
}, 'blah: expected {} to be an iterable');
34523455
});
34533456

34543457
it('deep.members', function() {

Diff for: test/should.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2815,32 +2815,35 @@ describe('should', function() {
28152815

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

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

28252825
it('memberEquals', function() {
28262826
[1, 2, 3].should.have.same.members([3, 2, 1]);
28272827
[5, 4].should.have.same.members([5, 4]);
28282828
[5, 4, 4].should.have.same.members([5, 4, 4]);
28292829
[].should.have.same.members([]);
2830+
(new Set([])).should.have.same.members(new Set([]));
2831+
(new Set([1,2,3])).should.have.same.members(new Set([3,2,1]));
28302832

28312833
[5, 4].should.not.have.same.members([5, 4, 4]);
28322834
[5, 4, 4].should.not.have.same.members([5, 4]);
28332835
[5, 4, 4].should.not.have.same.members([5, 4, 3]);
28342836
[5, 4, 3].should.not.have.same.members([5, 4, 4]);
28352837
[{a: 1}].should.not.have.same.members([{a: 1}]);
2838+
(new Set([1,2,3])).should.not.have.same.members(new Set([2,1]));
28362839

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

28412844
err(function() {
28422845
[1, 2, 3].should.have.same.members(4, 'blah');
2843-
}, 'blah: expected 4 to be an array');
2846+
}, 'blah: expected 4 to be an iterable');
28442847
});
28452848

28462849
it('deep.members', function() {

0 commit comments

Comments
 (0)