initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / have_key_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "github.com/onsi/gomega/format"
6         "reflect"
7 )
8
9 type HaveKeyMatcher struct {
10         Key interface{}
11 }
12
13 func (matcher *HaveKeyMatcher) Match(actual interface{}) (success bool, err error) {
14         if !isMap(actual) {
15                 return false, fmt.Errorf("HaveKey matcher expects a map.  Got:%s", format.Object(actual, 1))
16         }
17
18         keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
19         if !keyIsMatcher {
20                 keyMatcher = &EqualMatcher{Expected: matcher.Key}
21         }
22
23         keys := reflect.ValueOf(actual).MapKeys()
24         for i := 0; i < len(keys); i++ {
25                 success, err := keyMatcher.Match(keys[i].Interface())
26                 if err != nil {
27                         return false, fmt.Errorf("HaveKey's key matcher failed with:\n%s%s", format.Indent, err.Error())
28                 }
29                 if success {
30                         return true, nil
31                 }
32         }
33
34         return false, nil
35 }
36
37 func (matcher *HaveKeyMatcher) FailureMessage(actual interface{}) (message string) {
38         switch matcher.Key.(type) {
39         case omegaMatcher:
40                 return format.Message(actual, "to have key matching", matcher.Key)
41         default:
42                 return format.Message(actual, "to have key", matcher.Key)
43         }
44 }
45
46 func (matcher *HaveKeyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
47         switch matcher.Key.(type) {
48         case omegaMatcher:
49                 return format.Message(actual, "not to have key matching", matcher.Key)
50         default:
51                 return format.Message(actual, "not to have key", matcher.Key)
52         }
53 }