Skip to content

Commit

Permalink
Fix unexpected MatchError() behaviour (#375)
Browse files Browse the repository at this point in the history
Resolves #374

Co-authored-by: Marcela Campo <mcampo@pivotal.io>
Co-authored-by: George Blue <gblue@pivotal.io>
Co-authored-by: David Ansari <dansari@pivotal.io>
  • Loading branch information
4 people authored Dec 19, 2019
1 parent 546170b commit 8ae7b2f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
20 changes: 1 addition & 19 deletions matchers/match_error_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err e
actualErr := actual.(error)
expected := matcher.Expected

if isPtrToErrorType(expected) {
return xerrors.As(actualErr, expected), nil
}

if isError(expected) {
return reflect.DeepEqual(actualErr, expected) || xerrors.Is(actualErr, expected.(error)), nil
}
Expand All @@ -46,7 +42,7 @@ func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err e
}

return false, fmt.Errorf(
"MatchError must be passed an error, a pointer to a type that implements error, a string, or a Matcher that can match on strings. Got:\n%s",
"MatchError must be passed an error, a string, or a Matcher that can match on strings. Got:\n%s",
format.Object(expected, 1))
}

Expand All @@ -57,17 +53,3 @@ func (matcher *MatchErrorMatcher) FailureMessage(actual interface{}) (message st
func (matcher *MatchErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to match error", matcher.Expected)
}

func isPtrToErrorType(a interface{}) bool {
if isNil(a) {
return false
}

typeOfA := reflect.TypeOf(a)
if typeOfA.Kind() != reflect.Ptr {
return false
}

errorType := reflect.TypeOf((*error)(nil)).Elem()
return typeOfA.Elem().Implements(errorType)
}
10 changes: 4 additions & 6 deletions matchers/match_error_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ var _ = Describe("MatchErrorMatcher", func() {
})
})

Context("when passed non-nil pointer to a type that implements error", func() {
It("should succeed when any error in the chain matches the passed pointer", func() {
err := xerrors.Errorf("outer error wrapping: %w", CustomError{})

var expectedErrType CustomError
Expect(err).Should(MatchError(&expectedErrType))
Context("when actual an expected are both pointers to an error", func() {
It("should succeed when errors are deeply equal", func() {
err := CustomError{}
Expect(&err).To(MatchError(&err))
})
})

Expand Down

0 comments on commit 8ae7b2f

Please sign in to comment.