-
Notifications
You must be signed in to change notification settings - Fork 2
/
contain_matched_error_matcher.go
51 lines (41 loc) · 1.26 KB
/
contain_matched_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
42
43
44
45
46
47
48
49
50
51
package gerrors
import (
multierror "github.com/hashicorp/go-multierror"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
func ContainMatchedError(expected interface{}) types.GomegaMatcher {
return &ContainMatchedErrorMatcher{
Expected: expected,
}
}
type ContainMatchedErrorMatcher struct {
Expected interface{}
}
func (matcher *ContainMatchedErrorMatcher) Match(actual interface{}) (bool, error) {
success, err := matchError(matcher.Expected, actual)
// Matched or it's an incompatible use of the matcher
if success || err != nil {
return success, err
}
merr, ok := actual.(*multierror.Error)
if !ok {
return success, err
}
innerErrors := merr.WrappedErrors()
for _, e := range innerErrors {
success, err = matchError(e, matcher.Expected)
// Early exit if it matches
if err == nil && success {
return success, err
}
}
// Otherwise just return most recent result
return success, err
}
func (matcher *ContainMatchedErrorMatcher) FailureMessage(actual interface{}) string {
return format.Message(actual, "to contain matched error", matcher.Expected)
}
func (matcher *ContainMatchedErrorMatcher) NegatedFailureMessage(actual interface{}) string {
return format.Message(actual, "not to contain matched error", matcher.Expected)
}