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

[RFC] HaveEach matcher #520

Closed
thediveo opened this issue Feb 17, 2022 · 6 comments
Closed

[RFC] HaveEach matcher #520

thediveo opened this issue Feb 17, 2022 · 6 comments

Comments

@thediveo
Copy link
Collaborator

thediveo commented Feb 17, 2022

In the hope that I didn't overlooked this functionality in the existing matchers: I would like to propose an Each matcher that checks that all elements of an array or slice match the specified value/matcher. Similar for maps, Each checks that all map values match the specified value/matcher.

Expect(m).To(Each(Equal(42))) // map[string]int
Expect(m).To(Each(Each(Equal(42))) // map[string][]int

border case: Expect([]string{}).To(Each(Equal(""))) is expected to succeed. If not desired, use this idiom:

Expect([]string{}).To(And(Not(BeEmpty()), Each(Equal(""))))

I can work on a PR if you like and after some clean-up and adding the necessary tests 😁.

P.S.: still struggling with the language semantics of Each and Every, but then Javascript used forEach(...) and not forEvery(...) 😉

@thediveo thediveo changed the title [RFC] Every matcher [RFC] Each matcher Feb 18, 2022
@onsi
Copy link
Owner

onsi commented Feb 18, 2022

hey @thediveo this sounds like a useful addition. How about HaveAllElements()? It's wordy but unlikely to collide and I think it reads well:

Expect(m).To(HaveAllElements(Equal(42)))

And paves the way for

Expect(m).To(HaveAnyElement(Equal(42)))

thoughts?

@thediveo
Copy link
Collaborator Author

wouldn't HaveAnyElement() be the same as ContainsElement()?

@onsi
Copy link
Owner

onsi commented Feb 18, 2022

um... (awkward silence)... yes

@thediveo
Copy link
Collaborator Author

Pondering again on HaveAllElements() this appears to me ambiguous in the sense of "have all the following elements". Yet, you raise the import concern of name clashes with Each. What about HaveEach()? Avoids name clashes, keeps with the established Have... pattern, yet avoids the false friend of ContainsElements(). What do you think?

@onsi
Copy link
Owner

onsi commented Feb 18, 2022

Yeah, you're right. HaveEach() works and makes sense to me.

@thediveo thediveo changed the title [RFC] Each matcher [RFC] HaveEach matcher Feb 19, 2022
thediveo added a commit to thediveo/gomega that referenced this issue Feb 28, 2022
- implements new HaveEachMatcher including unit tests
- wires up HaveEach with HaveEachMatcher
- adds HaveEach documentation
onsi pushed a commit that referenced this issue Feb 28, 2022
@thediveo
Copy link
Collaborator Author

thediveo commented Feb 28, 2022

With this merged in #523 and #524 this issue is done. Thank you for discussing the idea and merging!

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

No branches or pull requests

2 participants