Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/onsi/gomega/matchers/have_key_matcher.go
2880 views
1
// untested sections: 6
2
3
package matchers
4
5
import (
6
"fmt"
7
"reflect"
8
9
"github.com/onsi/gomega/format"
10
"github.com/onsi/gomega/matchers/internal/miter"
11
)
12
13
type HaveKeyMatcher struct {
14
Key any
15
}
16
17
func (matcher *HaveKeyMatcher) Match(actual any) (success bool, err error) {
18
if !isMap(actual) && !miter.IsSeq2(actual) {
19
return false, fmt.Errorf("HaveKey matcher expects a map/iter.Seq2. Got:%s", format.Object(actual, 1))
20
}
21
22
keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
23
if !keyIsMatcher {
24
keyMatcher = &EqualMatcher{Expected: matcher.Key}
25
}
26
27
if miter.IsSeq2(actual) {
28
var success bool
29
var err error
30
miter.IterateKV(actual, func(k, v reflect.Value) bool {
31
success, err = keyMatcher.Match(k.Interface())
32
if err != nil {
33
err = fmt.Errorf("HaveKey's key matcher failed with:\n%s%s", format.Indent, err.Error())
34
return false
35
}
36
return !success
37
})
38
return success, err
39
}
40
41
keys := reflect.ValueOf(actual).MapKeys()
42
for i := 0; i < len(keys); i++ {
43
success, err := keyMatcher.Match(keys[i].Interface())
44
if err != nil {
45
return false, fmt.Errorf("HaveKey's key matcher failed with:\n%s%s", format.Indent, err.Error())
46
}
47
if success {
48
return true, nil
49
}
50
}
51
52
return false, nil
53
}
54
55
func (matcher *HaveKeyMatcher) FailureMessage(actual any) (message string) {
56
switch matcher.Key.(type) {
57
case omegaMatcher:
58
return format.Message(actual, "to have key matching", matcher.Key)
59
default:
60
return format.Message(actual, "to have key", matcher.Key)
61
}
62
}
63
64
func (matcher *HaveKeyMatcher) NegatedFailureMessage(actual any) (message string) {
65
switch matcher.Key.(type) {
66
case omegaMatcher:
67
return format.Message(actual, "not to have key matching", matcher.Key)
68
default:
69
return format.Message(actual, "not to have key", matcher.Key)
70
}
71
}
72
73