-
-
Notifications
You must be signed in to change notification settings - Fork 693
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
Add ordered flag for members assertions #728
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -92,6 +92,23 @@ module.exports = function (chai, _) { | |
flag(this, 'deep', true); | ||
}); | ||
|
||
/** | ||
* ### .ordered | ||
* | ||
* Sets the `ordered` flag, later used by the `members` | ||
* assertions. | ||
* | ||
* expect(foo).to.have.ordered.members([bar, baz]); | ||
* | ||
* @name ordered | ||
* @namespace BDD | ||
* @api public | ||
*/ | ||
|
||
Assertion.addProperty('ordered', function () { | ||
flag(this, 'ordered', true); | ||
}); | ||
|
||
/** | ||
* ### .any | ||
* | ||
|
@@ -1547,8 +1564,9 @@ module.exports = function (chai, _) { | |
Assertion.addMethod('closeTo', closeTo); | ||
Assertion.addMethod('approximately', closeTo); | ||
|
||
function isSubsetOf(subset, superset, cmp) { | ||
return subset.every(function(elem) { | ||
function isSubsetOf(subset, superset, cmp, ordered) { | ||
return subset.every(function(elem, idx) { | ||
if (ordered) return cmp ? cmp(elem, superset[idx]) : elem === superset[idx]; | ||
if (!cmp) return superset.indexOf(elem) !== -1; | ||
|
||
return superset.some(function(elem2) { | ||
|
@@ -1560,18 +1578,43 @@ module.exports = function (chai, _) { | |
/** | ||
* ### .members(set) | ||
* | ||
* Asserts that the target is a superset of `set`, | ||
* or that the target and `set` have the same strictly-equal (===) members. | ||
* Alternately, if the `deep` flag is set, set members are compared for deep | ||
* equality. | ||
* Asserts that the target and `set` have the same members. By default, | ||
* members are compared using strict equality (===), and order doesn't | ||
* matter. | ||
* | ||
* expect([1, 2, 3]).to.have.members([2, 1, 3]); | ||
* expect([1, 2, 3]).to.not.have.members([2, 1]); | ||
* expect([1, 2, 3]).to.not.have.members([5, 1, 3]); | ||
* | ||
* If the `contains` flag is set via `.include` or `.contain`, instead asserts | ||
* that the target is a superset of `set`. | ||
* | ||
* expect([1, 2, 3]).to.include.members([2, 1]); | ||
* expect([1, 2, 3]).to.not.include.members([5, 1]); | ||
* | ||
* expect([1, 2, 3]).to.contain.members([2, 1]); | ||
* expect([1, 2, 3]).to.not.contain.members([5, 1]); | ||
* | ||
* expect([1, 2, 3]).to.include.members([3, 2]); | ||
* expect([1, 2, 3]).to.not.include.members([3, 2, 8]); | ||
* If the `deep` flag is set, members are instead compared for deep equality. | ||
* | ||
* expect([4, 2]).to.have.members([2, 4]); | ||
* expect([5, 2]).to.not.have.members([5, 2, 1]); | ||
* expect([{ a: 1 }, { b: 2 }, { c: 3 }]).to.have.deep.members([{ b: 2 }, { a: 1 }, { c: 3 }]); | ||
* expect([{ a: 1 }, { b: 2 }, { c: 3 }]).to.not.have.deep.members([{ b: 2 }, { a: 1 }, { f: 5 }]); | ||
* | ||
* expect([{ id: 1 }]).to.deep.include.members([{ id: 1 }]); | ||
* If the `ordered` flag is set, members must instead appear in the same | ||
* order. | ||
* | ||
* expect([1, 2, 3]).to.have.ordered.members([1, 2, 3]); | ||
* expect([1, 2, 3]).to.not.have.ordered.members([2, 1, 3]); | ||
* | ||
* Any of the flags can be combined. | ||
* | ||
* expect([{ a: 1 }, { b: 2 }, { c: 3 }]).to.have.deep.ordered.members([{ a: 1 }, { b: 2 }, { c: 3 }]); | ||
* | ||
* If both the `contains` and `ordered` flag are set, the ordering begins | ||
* with the first element in the target. | ||
* | ||
* expect([1, 2, 3]).to.include.ordered.members([1, 2]); | ||
* expect([1, 2, 3]).to.not.include.ordered.members([2, 3]); | ||
* | ||
* @name members | ||
* @param {Array} set | ||
|
@@ -1587,24 +1630,32 @@ module.exports = function (chai, _) { | |
new Assertion(obj).to.be.an('array'); | ||
new Assertion(subset).to.be.an('array'); | ||
|
||
var cmp = flag(this, 'deep') ? _.eql : undefined; | ||
var ordered = flag(this, 'ordered'); | ||
|
||
var failMsg, failNegateMsg, lengthCheck; | ||
|
||
if (flag(this, 'contains')) { | ||
return this.assert( | ||
isSubsetOf(subset, obj, cmp) | ||
, 'expected #{this} to be a superset of #{exp}' | ||
, 'expected #{this} to not be a superset of #{exp}' | ||
, subset | ||
, obj | ||
); | ||
lengthCheck = true; | ||
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. Nice work using |
||
|
||
var subject = ordered ? 'an ordered superset' : 'a superset'; | ||
failMsg = 'expected #{this} to be ' + subject + ' of #{exp}'; | ||
failNegateMsg = 'expected #{this} to not be ' + subject + ' of #{exp}'; | ||
} else { | ||
lengthCheck = obj.length === subset.length; | ||
|
||
var subject = ordered ? 'ordered members' : 'members'; | ||
failMsg = 'expected #{this} to have the same ' + subject + ' as #{exp}'; | ||
failNegateMsg = 'expected #{this} to not have the same ' + subject + ' as #{exp}'; | ||
} | ||
|
||
var cmp = flag(this, 'deep') ? _.eql : undefined; | ||
|
||
this.assert( | ||
isSubsetOf(obj, subset, cmp) && isSubsetOf(subset, obj, cmp) | ||
, 'expected #{this} to have the same members as #{exp}' | ||
, 'expected #{this} to not have the same members as #{exp}' | ||
, subset | ||
, obj | ||
lengthCheck && isSubsetOf(subset, obj, cmp, ordered) | ||
, failMsg | ||
, failNegateMsg | ||
, subset | ||
, obj | ||
); | ||
}); | ||
|
||
|
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.
I'll leave this here just to make other reviewer's job easier.
This line is related to the behaviour we talked about at this post.