Skip to content

Commit

Permalink
Implement iterable assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jan 27, 2024
1 parent e08d796 commit 3acc615
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
29 changes: 19 additions & 10 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ function an (type, msg) {
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
} else if (type === 'iterable') {
this.assert(
typeof obj !== 'string' && obj[Symbol.iterator]
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
} else {
this.assert(
type === detectedType
Expand Down Expand Up @@ -3136,26 +3142,29 @@ function isSubsetOf(_subset, _superset, cmp, contains, ordered) {
* @api public
*/

Assertion.addMethod('members', function (subset, msg) {
Assertion.addMethod('iterable', function(msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (!['Array', 'Set'].includes(_.type(obj))) {
throw new AssertionError(
`${flagMsg}: expected ${_.inspect(obj)} to be an iterator`,
undefined,
ssfi
);
}
if (!['Array', 'Set'].includes(_.type(subset))) {
if (typeof obj === 'string' || !obj[Symbol.iterator]) {
throw new AssertionError(
`${flagMsg}: expected ${_.inspect(subset)} to be an iterator`,
`${flagMsg}: expected ${_.inspect(obj)} to be an iterable`,
undefined,
ssfi
);
}
});

Assertion.addMethod('members', function (subset, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

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

var contains = flag(this, 'contains');
var ordered = flag(this, 'ordered');
Expand Down
4 changes: 2 additions & 2 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1981,11 +1981,11 @@ describe('assert', function () {

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

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

it('notSameMembers', function() {
Expand Down
8 changes: 4 additions & 4 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3508,19 +3508,19 @@ describe('expect', function () {

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

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

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

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

it('deep.members', function() {
Expand Down
6 changes: 3 additions & 3 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -2820,11 +2820,11 @@ describe('should', function() {

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

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

it('memberEquals', function() {
Expand All @@ -2845,7 +2845,7 @@ describe('should', function() {

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

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

0 comments on commit 3acc615

Please sign in to comment.