-
-
Notifications
You must be signed in to change notification settings - Fork 699
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
} | ||
|
||
|
@@ -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( | ||
|
@@ -1244,7 +1251,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'); | ||
} | ||
|
||
|
@@ -1264,13 +1271,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( | ||
|
@@ -1340,7 +1354,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'); | ||
} | ||
|
||
|
@@ -1360,13 +1374,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( | ||
|
@@ -1436,7 +1457,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'); | ||
} | ||
|
||
|
@@ -1456,13 +1477,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( | ||
|
@@ -1536,7 +1564,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'); | ||
} | ||
|
||
|
@@ -1556,11 +1584,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( | ||
|
@@ -2013,6 +2048,11 @@ module.exports = function (chai, _) { | |
* expect([1, 2, 3]).to.have.lengthOf(3); | ||
* expect('foo').to.have.lengthOf(3); | ||
* | ||
* When the target is a map or set, asserts that the target's `size` property. | ||
* | ||
* expect(new Set([1, 2])).to.hove.lengthOf(2); | ||
* expect(new Map([['a', 1], ['b', 2]])).to.hove.lengthOf(2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a typo in these examples: "hove" instead of "have". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
* | ||
* 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 | ||
* value, rather than not equal to one of many unexpected values. | ||
|
@@ -2067,17 +2107,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 | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads awkwardly to me. Instead of having this separate line, I suggest updating lines 2045-2046 to read: "Asserts that the target's
length
orsize
is equal to the given numbern
."And then the new examples could go right beneath the "foo" example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!