initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / match_error_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "github.com/onsi/gomega/format"
6         "reflect"
7 )
8
9 type MatchErrorMatcher struct {
10         Expected interface{}
11 }
12
13 func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err error) {
14         if isNil(actual) {
15                 return false, fmt.Errorf("Expected an error, got nil")
16         }
17
18         if !isError(actual) {
19                 return false, fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
20         }
21
22         actualErr := actual.(error)
23
24         if isString(matcher.Expected) {
25                 return reflect.DeepEqual(actualErr.Error(), matcher.Expected), nil
26         }
27
28         if isError(matcher.Expected) {
29                 return reflect.DeepEqual(actualErr, matcher.Expected), nil
30         }
31
32         var subMatcher omegaMatcher
33         var hasSubMatcher bool
34         if matcher.Expected != nil {
35                 subMatcher, hasSubMatcher = (matcher.Expected).(omegaMatcher)
36                 if hasSubMatcher {
37                         return subMatcher.Match(actualErr.Error())
38                 }
39         }
40
41         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))
42 }
43
44 func (matcher *MatchErrorMatcher) FailureMessage(actual interface{}) (message string) {
45         return format.Message(actual, "to match error", matcher.Expected)
46 }
47
48 func (matcher *MatchErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
49         return format.Message(actual, "not to match error", matcher.Expected)
50 }