mirror-to-gitea/vendor/github.com/onsi/gomega/matchers/match_error_matcher.go
vendor/github.com/onsi/gomega/matchers/match_error_matcher.goRaw
1package matchers
2
3import (
4 "errors"
5 "fmt"
6 "reflect"
7
8 "github.com/onsi/gomega/format"
9)
10
11type MatchErrorMatcher struct {
12 Expected interface{}
13}
14
15func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err error) {
16 if isNil(actual) {
17 return false, fmt.Errorf("Expected an error, got nil")
18 }
19
20 if !isError(actual) {
21 return false, fmt.Errorf("Expected an error. Got:\n%s", format.Object(actual, 1))
22 }
23
24 actualErr := actual.(error)
25 expected := matcher.Expected
26
27 if isError(expected) {
28 return reflect.DeepEqual(actualErr, expected) || errors.Is(actualErr, expected.(error)), nil
29 }
30
31 if isString(expected) {
32 return actualErr.Error() == expected, nil
33 }
34
35 var subMatcher omegaMatcher
36 var hasSubMatcher bool
37 if expected != nil {
38 subMatcher, hasSubMatcher = (expected).(omegaMatcher)
39 if hasSubMatcher {
40 return subMatcher.Match(actualErr.Error())
41 }
42 }
43
44 return false, fmt.Errorf(
45 "MatchError must be passed an error, a string, or a Matcher that can match on strings. Got:\n%s",
46 format.Object(expected, 1))
47}
48
49func (matcher *MatchErrorMatcher) FailureMessage(actual interface{}) (message string) {
50 return format.Message(actual, "to match error", matcher.Expected)
51}
52
53func (matcher *MatchErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
54 return format.Message(actual, "not to match error", matcher.Expected)
55}