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

HaveExactElements() doesn't work properly inside ContainElement or collection matchers #647

Closed
eiiches opened this issue Mar 6, 2023 · 2 comments · Fixed by #648
Closed

Comments

@eiiches
Copy link
Contributor

eiiches commented Mar 6, 2023

Hi,

I've encountered a weird behavior when using HaveExactElements matcher. See this example:

actual := [][]bool{
	{true},
	{false},
}
g.Expect(actual).To(ContainElement( // this fails, even though it should pass
	HaveExactElements(Equal(false)),
))

I expect this assertion to pass, because actual contains an element []bool{false} which matches HaveExactElements(Equal(false)). But the assertion fails instead.

I believe the culprit is that HaveExactElementsMatcher leaves some internal state once its Match() returns false, which never gets cleaned and causes subsequent calls to always return false. Fwiw, cleaning the state at the beginning of the Match() fixed the issue for me.

 func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool, err error) {
+ 	matcher.missingIndex = 0
+ 	matcher.extraIndex = 0
+ 	matcher.mismatchFailures = nil
+ 
 	if isMap(actual) {

Version: 1.27.2

@onsi
Copy link
Owner

onsi commented Mar 6, 2023

hey good catch. would you be up for submitting a pull request?

@eiiches
Copy link
Contributor Author

eiiches commented Mar 6, 2023

Sure!

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 a pull request may close this issue.

2 participants