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

.lengthOf for Maps and Sets #1131

Merged
merged 2 commits into from Feb 11, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
146 changes: 97 additions & 49 deletions lib/chai/core/assertions.js
Expand Up @@ -1102,8 +1102,8 @@ module.exports = function (chai, _) {
* expect(2).to.equal(2); // Recommended
* expect(2).to.be.above(1); // Not recommended
*
* Add `.lengthOf` earlier in the chain to assert that the value of the
* target's `length` property is greater than the given number `n`.
* Add `.lengthOf` earlier in the chain to assert that the target's `length`
* or `size` is greater than the given number `n`.
*
* expect('foo').to.have.lengthOf(3); // Recommended
* expect('foo').to.have.lengthOf.above(2); // Not recommended
Expand Down Expand Up @@ -1147,7 +1147,7 @@ module.exports = function (chai, _) {
, errorMessage
, shouldThrow = true;

if (doLength) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

Expand All @@ -1167,13 +1167,20 @@ module.exports = function (chai, _) {
}

if (doLength) {
var len = obj.length;
var descriptor = 'length'
, itemsCount;
if (objType === 'map' || objType === 'set') {
descriptor = 'size';
itemsCount = obj.size;
} else {
itemsCount = obj.length;
}
this.assert(
len > n
, 'expected #{this} to have a length above #{exp} but got #{act}'
, 'expected #{this} to not have a length above #{exp}'
itemsCount > n
, 'expected #{this} to have a ' + descriptor + ' above #{exp} but got #{act}'
, 'expected #{this} to not have a ' + descriptor + ' above #{exp}'
, n
, len
, itemsCount
);
} else {
this.assert(
Expand All @@ -1200,9 +1207,8 @@ module.exports = function (chai, _) {
* expect(2).to.be.at.least(1); // Not recommended
* expect(2).to.be.at.least(2); // Not recommended
*
* Add `.lengthOf` earlier in the chain to assert that the value of the
* target's `length` property is greater than or equal to the given number
* `n`.
* Add `.lengthOf` earlier in the chain to assert that the target's `length`
* or `size` is greater than or equal to the given number `n`.
*
* expect('foo').to.have.lengthOf(3); // Recommended
* expect('foo').to.have.lengthOf.at.least(2); // Not recommended
Expand Down Expand Up @@ -1244,7 +1250,7 @@ module.exports = function (chai, _) {
, errorMessage
, shouldThrow = true;

if (doLength) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

Expand All @@ -1264,13 +1270,20 @@ module.exports = function (chai, _) {
}

if (doLength) {
var len = obj.length;
var descriptor = 'length'
, itemsCount;
if (objType === 'map' || objType === 'set') {
descriptor = 'size';
itemsCount = obj.size;
} else {
itemsCount = obj.length;
}
this.assert(
len >= n
, 'expected #{this} to have a length at least #{exp} but got #{act}'
, 'expected #{this} to have a length below #{exp}'
itemsCount >= n
, 'expected #{this} to have a ' + descriptor + ' at least #{exp} but got #{act}'
, 'expected #{this} to have a ' + descriptor + ' below #{exp}'
, n
, len
, itemsCount
);
} else {
this.assert(
Expand All @@ -1295,8 +1308,8 @@ module.exports = function (chai, _) {
* expect(1).to.equal(1); // Recommended
* expect(1).to.be.below(2); // Not recommended
*
* Add `.lengthOf` earlier in the chain to assert that the value of the
* target's `length` property is less than the given number `n`.
* Add `.lengthOf` earlier in the chain to assert that the target's `length`
* or `size` is less than the given number `n`.
*
* expect('foo').to.have.lengthOf(3); // Recommended
* expect('foo').to.have.lengthOf.below(4); // Not recommended
Expand Down Expand Up @@ -1340,7 +1353,7 @@ module.exports = function (chai, _) {
, errorMessage
, shouldThrow = true;

if (doLength) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

Expand All @@ -1360,13 +1373,20 @@ module.exports = function (chai, _) {
}

if (doLength) {
var len = obj.length;
var descriptor = 'length'
, itemsCount;
if (objType === 'map' || objType === 'set') {
descriptor = 'size';
itemsCount = obj.size;
} else {
itemsCount = obj.length;
}
this.assert(
len < n
, 'expected #{this} to have a length below #{exp} but got #{act}'
, 'expected #{this} to not have a length below #{exp}'
itemsCount < n
, 'expected #{this} to have a ' + descriptor + ' below #{exp} but got #{act}'
, 'expected #{this} to not have a ' + descriptor + ' below #{exp}'
, n
, len
, itemsCount
);
} else {
this.assert(
Expand All @@ -1393,8 +1413,8 @@ module.exports = function (chai, _) {
* expect(1).to.be.at.most(2); // Not recommended
* expect(1).to.be.at.most(1); // Not recommended
*
* Add `.lengthOf` earlier in the chain to assert that the value of the
* target's `length` property is less than or equal to the given number `n`.
* Add `.lengthOf` earlier in the chain to assert that the target's `length`
* or `size` is less than or equal to the given number `n`.
*
* expect('foo').to.have.lengthOf(3); // Recommended
* expect('foo').to.have.lengthOf.at.most(4); // Not recommended
Expand Down Expand Up @@ -1436,7 +1456,7 @@ module.exports = function (chai, _) {
, errorMessage
, shouldThrow = true;

if (doLength) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

Expand All @@ -1456,13 +1476,20 @@ module.exports = function (chai, _) {
}

if (doLength) {
var len = obj.length;
var descriptor = 'length'
, itemsCount;
if (objType === 'map' || objType === 'set') {
descriptor = 'size';
itemsCount = obj.size;
} else {
itemsCount = obj.length;
}
this.assert(
len <= n
, 'expected #{this} to have a length at most #{exp} but got #{act}'
, 'expected #{this} to have a length above #{exp}'
itemsCount <= n
, 'expected #{this} to have a ' + descriptor + ' at most #{exp} but got #{act}'
, 'expected #{this} to have a ' + descriptor + ' above #{exp}'
, n
, len
, itemsCount
);
} else {
this.assert(
Expand Down Expand Up @@ -1490,9 +1517,9 @@ module.exports = function (chai, _) {
* expect(2).to.be.within(2, 3); // Not recommended
* expect(2).to.be.within(1, 2); // Not recommended
*
* Add `.lengthOf` earlier in the chain to assert that the value of the
* target's `length` property is greater than or equal to the given number
* `start`, and less than or equal to the given number `finish`.
* Add `.lengthOf` earlier in the chain to assert that the target's `length`
* or `size` is greater than or equal to the given number `start`, and less
* than or equal to the given number `finish`.
*
* expect('foo').to.have.lengthOf(3); // Recommended
* expect('foo').to.have.lengthOf.within(2, 4); // Not recommended
Expand Down Expand Up @@ -1536,7 +1563,7 @@ module.exports = function (chai, _) {
? start.toUTCString() + '..' + finish.toUTCString()
: start + '..' + finish;

if (doLength) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

Expand All @@ -1556,11 +1583,18 @@ module.exports = function (chai, _) {
}

if (doLength) {
var len = obj.length;
var descriptor = 'length'
, itemsCount;
if (objType === 'map' || objType === 'set') {
descriptor = 'size';
itemsCount = obj.size;
} else {
itemsCount = obj.length;
}
this.assert(
len >= start && len <= finish
, 'expected #{this} to have a length within ' + range
, 'expected #{this} to not have a length within ' + range
itemsCount >= start && itemsCount <= finish
, 'expected #{this} to have a ' + descriptor + ' within ' + range
, 'expected #{this} to not have a ' + descriptor + ' within ' + range
);
} else {
this.assert(
Expand Down Expand Up @@ -2007,11 +2041,13 @@ module.exports = function (chai, _) {
/**
* ### .lengthOf(n[, msg])
*
* Asserts that the target's `length` property is equal to the given number
* Asserts that the target's `length` or `size` is equal to the given number
* `n`.
*
* expect([1, 2, 3]).to.have.lengthOf(3);
* expect('foo').to.have.lengthOf(3);
* expect(new Set([1, 2, 3])).to.have.lengthOf(3);
* expect(new Map([['a', 1], ['b', 2], ['c', 3]])).to.have.lengthOf(3);
*
* Add `.not` earlier in the chain to negate `.lengthOf`. However, it's often
* best to assert that the target's `length` property is equal to its expected
Expand Down Expand Up @@ -2067,17 +2103,29 @@ module.exports = function (chai, _) {
function assertLength (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, objType = _.type(obj).toLowerCase()
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
var len = obj.length;
, ssfi = flag(this, 'ssfi')
, descriptor = 'length'
, itemsCount;

switch (objType) {
case 'map':
case 'set':
descriptor = 'size';
itemsCount = obj.size;
break;
default:
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
itemsCount = obj.length;
}

this.assert(
len == n
, 'expected #{this} to have a length of #{exp} but got #{act}'
, 'expected #{this} to not have a length of #{act}'
itemsCount == n
, 'expected #{this} to have a ' + descriptor + ' of #{exp} but got #{act}'
, 'expected #{this} to not have a ' + descriptor + ' of #{act}'
, n
, len
, itemsCount
);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/chai/interface/assert.js
Expand Up @@ -1664,10 +1664,12 @@ module.exports = function (chai, util) {
/**
* ### .lengthOf(object, length, [message])
*
* Asserts that `object` has a `length` property with the expected value.
* Asserts that `object` has a `length` or `size` with the expected value.
*
* assert.lengthOf([1,2,3], 3, 'array has length of 3');
* assert.lengthOf('foobar', 6, 'string has length of 6');
* assert.lengthOf(new Set([1,2,3]), 3, 'set has size of 3');
* assert.lengthOf(new Map([['a',1],['b',2],['c',3]]), 3, 'map has size of 3');
*
* @name lengthOf
* @param {Mixed} object
Expand Down
28 changes: 28 additions & 0 deletions test/assert.js
Expand Up @@ -1388,6 +1388,34 @@ describe('assert', function () {
err(function () {
assert.lengthOf(1, 5);
}, "expected 1 to have property \'length\'");

if (typeof Map === 'function') {
assert.lengthOf(new Map, 0);

var map = new Map;
map.set('a', 1);
map.set('b', 2);

assert.lengthOf(map, 2);

err(function(){
assert.lengthOf(map, 3, 'blah');
}, "blah: expected {} to have a size of 3 but got 2");
}

if (typeof Set === 'function') {
assert.lengthOf(new Set, 0);

var set = new Set;
set.add(1);
set.add(2);

assert.lengthOf(set, 2);

err(function(){
assert.lengthOf(set, 3, 'blah');
}, "blah: expected {} to have a size of 3 but got 2");
}
});

it('match', function () {
Expand Down