-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_error_matcher.go
41 lines (33 loc) · 1.12 KB
/
match_error_matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package matchers
import (
"fmt"
"github.com/onsi/gomega/format"
"reflect"
)
type MatchErrorMatcher struct {
Expected interface{}
}
func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, message string, err error) {
if isNil(actual) {
return false, "", fmt.Errorf("Expected an error, got nil")
}
if !isError(actual) {
return false, "", fmt.Errorf("Expected an error. Got:\n%s", format.Object(actual, 1))
}
actualErr := actual.(error)
if isString(matcher.Expected) {
if reflect.DeepEqual(actualErr.Error(), matcher.Expected) {
return true, format.Message(actual, "not to match error", matcher.Expected), nil
} else {
return false, format.Message(actual, "to match error", matcher.Expected), nil
}
}
if isError(matcher.Expected) {
if reflect.DeepEqual(actualErr, matcher.Expected) {
return true, format.Message(actual, "not to match error", matcher.Expected), nil
} else {
return false, format.Message(actual, "to match error", matcher.Expected), nil
}
}
return false, "", fmt.Errorf("MatchError must be passed an error or string. Got:\n%s", format.Object(matcher.Expected, 1))
}