diff --git a/matchers/have_exact_elements.go b/matchers/have_exact_elements.go index dca5b9446..5a236d7d6 100644 --- a/matchers/have_exact_elements.go +++ b/matchers/have_exact_elements.go @@ -30,15 +30,18 @@ func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool lenMatchers := len(matchers) lenValues := len(values) + success = true for i := 0; i < lenMatchers || i < lenValues; i++ { if i >= lenMatchers { matcher.extraIndex = i + success = false continue } if i >= lenValues { matcher.missingIndex = i + success = false return } @@ -49,15 +52,17 @@ func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool index: i, failure: err.Error(), }) + success = false } else if !match { matcher.mismatchFailures = append(matcher.mismatchFailures, mismatchFailure{ index: i, failure: elemMatcher.FailureMessage(values[i]), }) + success = false } } - return matcher.missingIndex+matcher.extraIndex+len(matcher.mismatchFailures) == 0, nil + return success, nil } func (matcher *HaveExactElementsMatcher) FailureMessage(actual interface{}) (message string) { diff --git a/matchers/have_exact_elements_test.go b/matchers/have_exact_elements_test.go index f9719ce9f..66e146815 100644 --- a/matchers/have_exact_elements_test.go +++ b/matchers/have_exact_elements_test.go @@ -76,6 +76,16 @@ var _ = Describe("HaveExactElements", func() { }) }) + When("passed nil", func() { + It("should fail correctly", func() { + failures := InterceptGomegaFailures(func() { + var expected []any + Expect([]string{"one"}).Should(HaveExactElements(expected...)) + }) + Expect(failures).Should(HaveLen(1)) + }) + }) + Describe("Failure Message", func() { When("actual contains extra elements", func() { It("should print the starting index of the extra elements", func() {