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

Implement InList and NotInList methods #250

Closed
gavv opened this issue Jan 28, 2023 · 8 comments · Fixed by #256 or #267
Closed

Implement InList and NotInList methods #250

gavv opened this issue Jan 28, 2023 · 8 comments · Fixed by #256 or #267
Assignees
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Milestone

Comments

@gavv
Copy link
Owner

gavv commented Jan 28, 2023

Some matcher structs (Number, DateTime) have InRange method that checks that the value belongs to given range [min; max].

Another convenient thing would be InList method, that checks that the value equals to one of the given values a, b, c, ....

For example:

array.Element(1).String().InList("green", "red")

We need to add InList and counterpart NotInList methods to the following structs:

  • Value
  • Object
  • Array
  • String
  • Number
  • Boolean (just for completeness)
  • Duration
  • DateTime

The new methods can use assertions of type AssertBelongs and AssertNotBelongs. Please place these methods after InRange, or after Equal* if there is no InRange for this struct.

Each method should have documentation comment with code example and be covered by a unit test. It would be also nice to demonstrate usage of InRange and InList in _examples/fruits_test.go.

@gavv gavv added feature New feature or request help wanted Contributions are welcome good first issue Good for newcomers labels Jan 28, 2023
@fahimbagar
Copy link
Contributor

Hi @gavv, I have created PR for this issue: #256

Could you check it? and let me know how it goes

PS.

  1. Since AssertionList rejects a single slice as a param, single array param in InList will behave like IsEqual and NotInList will behave like NotEqual. Added in 649ee8c

    if len(lst) == 1 && reflect.ValueOf(lst[0]).Kind() == reflect.Slice {
    return errors.New(
    "AssertionList should contain a list of values," +
    " but it contains a single element which itself is a list")
    }

  2. I'll update _examples/fruits_test.go if this PR is merged. Or do you have an alternative without modifying go.mod under _examples?

@gavv
Copy link
Owner Author

gavv commented Jan 30, 2023

Thanks for pr! Will take a look shortly.

Re: PS

  1. Let's just remove this check from assertion validation. It was useful euristics to catch bugs, but it's not working well with InList.

  2. Yes, let's update examples separately, thanks.

@fahimbagar
Copy link
Contributor

Addressed in 40c6e50.

Sure, thanks for your awesome repo as well 💯

@gavv gavv linked a pull request Feb 1, 2023 that will close this issue
8 tasks
@gavv gavv closed this as completed in #256 Feb 1, 2023
@gavv gavv reopened this Feb 1, 2023
@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Reopening after auto-close because we also need to update example.

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Created follow-up issue: #261

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

I've tagged v2.11.0, which includes InList.

@fahimbagar
Copy link
Contributor

@gavv, thanks for reviewing the PR.

I'll add an example along with this issue #251 if that's okay for you.

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Great, thanks!! Please leave a comment there so that I can assign it to you.

@gavv gavv closed this as completed in #267 Feb 4, 2023
@gavv gavv added this to the v2 milestone Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Projects
None yet
2 participants