ContainElement no longer bails if a passed-in matcher errors
diff --git a/CHANGELOG.md b/CHANGELOG.md index a33a96c..5f2c1ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Bug Fixes: - gexec: `session.Wait` now uses `EventuallyWithOffset` to get the right line number in the failure. +- `ContainElement` no longer bails if a passed-in matcher errors. ## 1.0 (8/2/2014)
diff --git a/matchers/contain_element_matcher.go b/matchers/contain_element_matcher.go index 014a20f..4159335 100644 --- a/matchers/contain_element_matcher.go +++ b/matchers/contain_element_matcher.go
@@ -2,8 +2,9 @@ import ( "fmt" - "github.com/onsi/gomega/format" "reflect" + + "github.com/onsi/gomega/format" ) type ContainElementMatcher struct { @@ -25,6 +26,7 @@ if isMap(actual) { keys = value.MapKeys() } + var lastError error for i := 0; i < value.Len(); i++ { var success bool var err error @@ -34,14 +36,15 @@ success, err = elemMatcher.Match(value.Index(i).Interface()) } if err != nil { - return false, fmt.Errorf("ContainElement's element matcher failed with:\n\t%s", err.Error()) + lastError = err + continue } if success { return true, nil } } - return false, nil + return false, lastError } func (matcher *ContainElementMatcher) FailureMessage(actual interface{}) (message string) {
diff --git a/matchers/contain_element_matcher_test.go b/matchers/contain_element_matcher_test.go index 4d29eeb..38ee518 100644 --- a/matchers/contain_element_matcher_test.go +++ b/matchers/contain_element_matcher_test.go
@@ -35,8 +35,12 @@ Ω(map[string]int{"foo": 1, "bar": 2}).ShouldNot(ContainElement(BeNumerically(">", 2))) }) - It("should fail if the matcher ever fails", func() { - actual := []interface{}{1, 2, "3", 4} + It("should power through even if the matcher ever fails", func() { + Ω([]interface{}{1, 2, "3", 4}).Should(ContainElement(BeNumerically(">=", 3))) + }) + + It("should fail if the matcher fails", func() { + actual := []interface{}{1, 2, "3", "4"} success, err := (&ContainElementMatcher{Element: BeNumerically(">=", 3)}).Match(actual) Ω(success).Should(BeFalse()) Ω(err).Should(HaveOccurred())