From 096f3925a4dba582508d4019fa118ef8cc1ee0a7 Mon Sep 17 00:00:00 2001 From: Onsi Fakhouri Date: Tue, 6 Jun 2023 22:06:58 -0600 Subject: [PATCH] HaveExactElement should not call FailureMessage if a submatcher returned an error Fixes #668 --- matchers/have_exact_elements.go | 7 ++++++- matchers/have_exact_elements_test.go | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/matchers/have_exact_elements.go b/matchers/have_exact_elements.go index 7cce776c1..dca5b9446 100644 --- a/matchers/have_exact_elements.go +++ b/matchers/have_exact_elements.go @@ -44,7 +44,12 @@ func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool elemMatcher := matchers[i].(omegaMatcher) match, err := elemMatcher.Match(values[i]) - if err != nil || !match { + if err != nil { + matcher.mismatchFailures = append(matcher.mismatchFailures, mismatchFailure{ + index: i, + failure: err.Error(), + }) + } else if !match { matcher.mismatchFailures = append(matcher.mismatchFailures, mismatchFailure{ index: i, failure: elemMatcher.FailureMessage(values[i]), diff --git a/matchers/have_exact_elements_test.go b/matchers/have_exact_elements_test.go index 734a425f2..f9719ce9f 100644 --- a/matchers/have_exact_elements_test.go +++ b/matchers/have_exact_elements_test.go @@ -58,6 +58,14 @@ var _ = Describe("HaveExactElements", func() { Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements(BeFalse(), "bar", "baz")) Expect([]interface{}{"foo", "bar", false}).Should(HaveExactElements(ContainSubstring("foo"), "bar", BeFalse())) }) + + It("should include the error message, not the failure message", func() { + failures := InterceptGomegaFailures(func() { + Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements("foo", BeFalse(), "bar")) + }) + Ω(failures[0]).ShouldNot(ContainSubstring("to be false")) + Ω(failures[0]).Should(ContainSubstring("1: Expected a boolean. Got:\n : bar")) + }) }) })