Allow MatchError to take a submatcher
diff --git a/matchers/match_error_matcher.go b/matchers/match_error_matcher.go index ad00fef..03cdf04 100644 --- a/matchers/match_error_matcher.go +++ b/matchers/match_error_matcher.go
@@ -29,7 +29,16 @@ return reflect.DeepEqual(actualErr, matcher.Expected), nil } - return false, fmt.Errorf("MatchError must be passed an error or string. Got:\n%s", format.Object(matcher.Expected, 1)) + var subMatcher omegaMatcher + var hasSubMatcher bool + if matcher.Expected != nil { + subMatcher, hasSubMatcher = (matcher.Expected).(omegaMatcher) + if hasSubMatcher { + return subMatcher.Match(actualErr.Error()) + } + } + + return false, fmt.Errorf("MatchError must be passed an error, string, or Matcher that can match on strings. Got:\n%s", format.Object(matcher.Expected, 1)) } func (matcher *MatchErrorMatcher) FailureMessage(actual interface{}) (message string) {
diff --git a/matchers/match_error_matcher_test.go b/matchers/match_error_matcher_test.go index b331e2f..338b512 100644 --- a/matchers/match_error_matcher_test.go +++ b/matchers/match_error_matcher_test.go
@@ -41,6 +41,19 @@ Ω(customErr).Should(MatchError("an error")) }) + Context("when passed a matcher", func() { + It("should pass if the matcher passes against the error string", func() { + err := errors.New("error 123 abc") + + Ω(err).Should(MatchError(MatchRegexp(`\d{3}`))) + }) + + It("should fail if the matcher fails against the error string", func() { + err := errors.New("no digits") + Ω(err).ShouldNot(MatchError(MatchRegexp(`\d`))) + }) + }) + It("should fail when passed anything else", func() { actualErr := errors.New("an error") _, err := (&MatchErrorMatcher{