Skip to content

Commit

Permalink
Fix HaveExactElements to work inside ContainElement or other collecti…
Browse files Browse the repository at this point in the history
…on matchers (#648)
  • Loading branch information
eiiches committed Mar 6, 2023
1 parent dbd4cb5 commit 636757e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions matchers/have_exact_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type HaveExactElementsMatcher struct {
}

func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool, err error) {
matcher.resetState()

if isMap(actual) {
return false, fmt.Errorf("error")
}
Expand Down Expand Up @@ -73,3 +75,9 @@ func (matcher *HaveExactElementsMatcher) FailureMessage(actual interface{}) (mes
func (matcher *HaveExactElementsMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to contain elements", presentable(matcher.Elements))
}

func (matcher *HaveExactElementsMatcher) resetState() {
matcher.mismatchFailures = nil
matcher.missingIndex = 0
matcher.extraIndex = 0
}
10 changes: 10 additions & 0 deletions matchers/have_exact_elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ to equal
})
})
})

When("matcher instance is reused", func() {
// This is a regression test for https://github.com/onsi/gomega/issues/647.
// Matcher instance may be reused, if placed inside ContainElement() or other collection matchers.
It("should work properly", func() {
matchSingleFalse := HaveExactElements(Equal(false))
Expect([]bool{true}).ShouldNot(matchSingleFalse)
Expect([]bool{false}).Should(matchSingleFalse)
})
})
})

0 comments on commit 636757e

Please sign in to comment.