initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / have_key_with_value_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "github.com/onsi/gomega/format"
6         "reflect"
7 )
8
9 type HaveKeyWithValueMatcher struct {
10         Key   interface{}
11         Value interface{}
12 }
13
14 func (matcher *HaveKeyWithValueMatcher) Match(actual interface{}) (success bool, err error) {
15         if !isMap(actual) {
16                 return false, fmt.Errorf("HaveKeyWithValue matcher expects a map.  Got:%s", format.Object(actual, 1))
17         }
18
19         keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
20         if !keyIsMatcher {
21                 keyMatcher = &EqualMatcher{Expected: matcher.Key}
22         }
23
24         valueMatcher, valueIsMatcher := matcher.Value.(omegaMatcher)
25         if !valueIsMatcher {
26                 valueMatcher = &EqualMatcher{Expected: matcher.Value}
27         }
28
29         keys := reflect.ValueOf(actual).MapKeys()
30         for i := 0; i < len(keys); i++ {
31                 success, err := keyMatcher.Match(keys[i].Interface())
32                 if err != nil {
33                         return false, fmt.Errorf("HaveKeyWithValue's key matcher failed with:\n%s%s", format.Indent, err.Error())
34                 }
35                 if success {
36                         actualValue := reflect.ValueOf(actual).MapIndex(keys[i])
37                         success, err := valueMatcher.Match(actualValue.Interface())
38                         if err != nil {
39                                 return false, fmt.Errorf("HaveKeyWithValue's value matcher failed with:\n%s%s", format.Indent, err.Error())
40                         }
41                         return success, nil
42                 }
43         }
44
45         return false, nil
46 }
47
48 func (matcher *HaveKeyWithValueMatcher) FailureMessage(actual interface{}) (message string) {
49         str := "to have {key: value}"
50         if _, ok := matcher.Key.(omegaMatcher); ok {
51                 str += " matching"
52         } else if _, ok := matcher.Value.(omegaMatcher); ok {
53                 str += " matching"
54         }
55
56         expect := make(map[interface{}]interface{}, 1)
57         expect[matcher.Key] = matcher.Value
58         return format.Message(actual, str, expect)
59 }
60
61 func (matcher *HaveKeyWithValueMatcher) NegatedFailureMessage(actual interface{}) (message string) {
62         kStr := "not to have key"
63         if _, ok := matcher.Key.(omegaMatcher); ok {
64                 kStr = "not to have key matching"
65         }
66
67         vStr := "or that key's value not be"
68         if _, ok := matcher.Value.(omegaMatcher); ok {
69                 vStr = "or to have that key's value not matching"
70         }
71
72         return format.Message(actual, kStr, matcher.Key, vStr, matcher.Value)
73 }