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

Add in matcher #1811

Merged
merged 6 commits into from Jun 5, 2018
Merged

Add in matcher #1811

merged 6 commits into from Jun 5, 2018

Conversation

mhmoudgmal
Copy link
Contributor

@mhmoudgmal mhmoudgmal commented May 26, 2018

Purpose (TL;DR)

Add sinon.match.in matcher

Background (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'))

const expectedPayload = {
    experiment_uuid: 'target1',
    uuid: 'fake-uuid',
    variation: sinon.match.same('control').or(sinon.match.same('group1'))
};

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

  1. Check out this branch
  2. npm install
  3. npm test

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

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, ..])
Copy link
Member

@mroderick mroderick left a 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.

@@ -178,6 +178,18 @@ match.same = function (expectation) {
}, "same(" + valueToString(expectation) + ")");
};

match.in = function (arrayOfExpectations) {
if (!(arrayOfExpectations instanceof Array)) {
Copy link
Member

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.


assert.exception(function () {
sinonMatch.in(arg);
}, {name: "TypeError"});
Copy link
Member

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.

it("returns true", function () {
arrays.forEach(function (array) {
var inMatcher = sinonMatch.in(array);
assert(inMatcher.test(array[0]));
Copy link
Member

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.

});

context("and none of the elements is the same as the actual", function () {
it("returns true", function () {
Copy link
Member

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.
@mhmoudgmal
Copy link
Contributor Author

@mroderick Thanks a lot 👍
I updated the code and added the documentation, please rephrase it if it's not clear.

Copy link
Member

@fearphage fearphage left a 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

assert(sinonMatch.isMatcher(inMatcher));
});

context("when the test is called with non array argument", function () {
Copy link
Member

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.

});
});

context("when the test is called with an array", function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use describe.

];

context("and one of the elements is the same as the actual", function () {
it("returns true", function () {
Copy link
Member

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`*
Copy link
Contributor

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).
@fatso83 fatso83 merged commit 53169e3 into sinonjs:master Jun 5, 2018
@mroderick
Copy link
Member

This has been published to NPM as sinon@5.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants