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 in matcher #1811
Add in matcher #1811
Conversation
Allows to match a value to one of the elements of an array. And it can be useful when the the value is not known, but could be one of few possibilities provided as an array. Usage: sinon.match.in([p1, p2, p3, ..])
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.
Thank you for contributing to Sinon.
I only have a few comments for this pull request.
Remember to add documentation for the new feature in docs/release-source
.
lib/sinon/match.js
Outdated
@@ -178,6 +178,18 @@ match.same = function (expectation) { | |||
}, "same(" + valueToString(expectation) + ")"); | |||
}; | |||
|
|||
match.in = function (arrayOfExpectations) { | |||
if (!(arrayOfExpectations instanceof Array)) { |
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.
Please use Array.isArray
to accurately detect instances of Array.
See http://web.mit.edu/jwalden/www/isArray.html for a detailed explanation.
test/match-test.js
Outdated
|
||
assert.exception(function () { | ||
sinonMatch.in(arg); | ||
}, {name: "TypeError"}); |
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.
It would be good to also test for the message, so we can be sure that it's actually expected error. Otherwise, another error might go undetected by this test for a long time.
test/match-test.js
Outdated
it("returns true", function () { | ||
arrays.forEach(function (array) { | ||
var inMatcher = sinonMatch.in(array); | ||
assert(inMatcher.test(array[0])); |
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.
Referee's assert
checks for truthy, not true
.
You'll want to use assert.isTrue
to test for true
.
test/match-test.js
Outdated
}); | ||
|
||
context("and none of the elements is the same as the actual", function () { | ||
it("returns true", function () { |
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.
Copy-pasta, this should read it("returns false", function () {
Assert for the exception message. Correct an example description. Assert for true instead of truthy.
@mroderick Thanks a lot 👍 |
9155895
to
722465e
Compare
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.
Just a few simplifications for the tests
test/match-test.js
Outdated
assert(sinonMatch.isMatcher(inMatcher)); | ||
}); | ||
|
||
context("when the test is called with non array argument", function () { |
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.
2 things here:
- Use
describe
instead when necessary - Only nest tests when there is additional/unique setup
In this instance, the nesting is not warranted. Just change the label/name on the test below.
test/match-test.js
Outdated
}); | ||
}); | ||
|
||
context("when the test is called with an array", function () { |
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.
Use describe
.
test/match-test.js
Outdated
]; | ||
|
||
context("and one of the elements is the same as the actual", function () { | ||
it("returns true", function () { |
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.
Rename the tests instead of nesting them here and on the following tests.
#### `sinon.match.in(array)` | ||
|
||
Requires the value to be in the `array`. | ||
|
||
*Since `sinon@2.0.0`* |
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.
The line about Sinon 2 belongs to the symbol matcher above. I'll fix it in my browser, if my crappy Moto G5 can take the load 😁
Was the odd man out. - wasn't done for any of the other matchers. Use the change log for history instead (or got blame).
This has been published to NPM as |
Purpose (TL;DR)
Add
sinon.match.in
matcherBackground (Problem in detail)
I had a case where I wanted to match a computed value (a value that I don't know exactly what it is), but I knew the possibilities of that value.
so I had to do something like this:
sinon.match.same('something').or(sinon.match.same('something_else'))
I know also there is array.contains matcher.. but the case above, that payload is used in different places where I check if a function called with it, and I thought it would be much easier if I have something like this:
sinon.match.in([v1, v2, v3])
Solution
sinon.match.in matcher.
Allows to match a value to one of the elements of an array.
And it can be useful when the the value is not known,
but could be one of few possibilities provided as an array.
Usage:
sinon.match.in([p1, p2, p3, ..])
How to verify - mandatory
npm install
npm test
Checklist for author
npm run lint
passes