onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.16k stars 281 forks source link

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

Closed eiiches closed 1 year ago

eiiches commented 1 year ago

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 commented 1 year ago

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

eiiches commented 1 year ago

Sure!