...
1 package matchers
2
3 import (
4 "fmt"
5 "reflect"
6
7 "github.com/onsi/gomega/format"
8 )
9
10 type BeKeyOfMatcher struct {
11 Map interface{}
12 }
13
14 func (matcher *BeKeyOfMatcher) Match(actual interface{}) (success bool, err error) {
15 if !isMap(matcher.Map) {
16 return false, fmt.Errorf("BeKeyOf matcher needs expected to be a map type")
17 }
18
19 if reflect.TypeOf(actual) == nil {
20 return false, fmt.Errorf("BeKeyOf matcher expects actual to be typed")
21 }
22
23 var lastError error
24 for _, key := range reflect.ValueOf(matcher.Map).MapKeys() {
25 matcher := &EqualMatcher{Expected: key.Interface()}
26 success, err := matcher.Match(actual)
27 if err != nil {
28 lastError = err
29 continue
30 }
31 if success {
32 return true, nil
33 }
34 }
35
36 return false, lastError
37 }
38
39 func (matcher *BeKeyOfMatcher) FailureMessage(actual interface{}) (message string) {
40 return format.Message(actual, "to be a key of", presentable(valuesOf(matcher.Map)))
41 }
42
43 func (matcher *BeKeyOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
44 return format.Message(actual, "not to be a key of", presentable(valuesOf(matcher.Map)))
45 }
46
View as plain text