Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/onsi/gomega/matchers/consist_of.go
2880 views
1
// untested sections: 3
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
"github.com/onsi/gomega/matchers/support/goraph/bipartitegraph"
12
)
13
14
type ConsistOfMatcher struct {
15
Elements []any
16
missingElements []any
17
extraElements []any
18
}
19
20
func (matcher *ConsistOfMatcher) Match(actual any) (success bool, err error) {
21
if !isArrayOrSlice(actual) && !isMap(actual) && !miter.IsIter(actual) {
22
return false, fmt.Errorf("ConsistOf matcher expects an array/slice/map/iter.Seq/iter.Seq2. Got:\n%s", format.Object(actual, 1))
23
}
24
25
matchers := matchers(matcher.Elements)
26
values := valuesOf(actual)
27
28
bipartiteGraph, err := bipartitegraph.NewBipartiteGraph(values, matchers, neighbours)
29
if err != nil {
30
return false, err
31
}
32
33
edges := bipartiteGraph.LargestMatching()
34
if len(edges) == len(values) && len(edges) == len(matchers) {
35
return true, nil
36
}
37
38
var missingMatchers []any
39
matcher.extraElements, missingMatchers = bipartiteGraph.FreeLeftRight(edges)
40
matcher.missingElements = equalMatchersToElements(missingMatchers)
41
42
return false, nil
43
}
44
45
func neighbours(value, matcher any) (bool, error) {
46
match, err := matcher.(omegaMatcher).Match(value)
47
return match && err == nil, nil
48
}
49
50
func equalMatchersToElements(matchers []any) (elements []any) {
51
for _, matcher := range matchers {
52
if equalMatcher, ok := matcher.(*EqualMatcher); ok {
53
elements = append(elements, equalMatcher.Expected)
54
} else if _, ok := matcher.(*BeNilMatcher); ok {
55
elements = append(elements, nil)
56
} else {
57
elements = append(elements, matcher)
58
}
59
}
60
return
61
}
62
63
func flatten(elems []any) []any {
64
if len(elems) != 1 ||
65
!(isArrayOrSlice(elems[0]) ||
66
(miter.IsIter(elems[0]) && !miter.IsSeq2(elems[0]))) {
67
return elems
68
}
69
70
if miter.IsIter(elems[0]) {
71
flattened := []any{}
72
miter.IterateV(elems[0], func(v reflect.Value) bool {
73
flattened = append(flattened, v.Interface())
74
return true
75
})
76
return flattened
77
}
78
79
value := reflect.ValueOf(elems[0])
80
flattened := make([]any, value.Len())
81
for i := 0; i < value.Len(); i++ {
82
flattened[i] = value.Index(i).Interface()
83
}
84
return flattened
85
}
86
87
func matchers(expectedElems []any) (matchers []any) {
88
for _, e := range flatten(expectedElems) {
89
if e == nil {
90
matchers = append(matchers, &BeNilMatcher{})
91
} else if matcher, isMatcher := e.(omegaMatcher); isMatcher {
92
matchers = append(matchers, matcher)
93
} else {
94
matchers = append(matchers, &EqualMatcher{Expected: e})
95
}
96
}
97
return
98
}
99
100
func presentable(elems []any) any {
101
elems = flatten(elems)
102
103
if len(elems) == 0 {
104
return []any{}
105
}
106
107
sv := reflect.ValueOf(elems)
108
firstEl := sv.Index(0)
109
if firstEl.IsNil() {
110
return elems
111
}
112
tt := firstEl.Elem().Type()
113
for i := 1; i < sv.Len(); i++ {
114
el := sv.Index(i)
115
if el.IsNil() || (sv.Index(i).Elem().Type() != tt) {
116
return elems
117
}
118
}
119
120
ss := reflect.MakeSlice(reflect.SliceOf(tt), sv.Len(), sv.Len())
121
for i := 0; i < sv.Len(); i++ {
122
ss.Index(i).Set(sv.Index(i).Elem())
123
}
124
125
return ss.Interface()
126
}
127
128
func valuesOf(actual any) []any {
129
value := reflect.ValueOf(actual)
130
values := []any{}
131
if miter.IsIter(actual) {
132
if miter.IsSeq2(actual) {
133
miter.IterateKV(actual, func(k, v reflect.Value) bool {
134
values = append(values, v.Interface())
135
return true
136
})
137
} else {
138
miter.IterateV(actual, func(v reflect.Value) bool {
139
values = append(values, v.Interface())
140
return true
141
})
142
}
143
} else if isMap(actual) {
144
keys := value.MapKeys()
145
for i := 0; i < value.Len(); i++ {
146
values = append(values, value.MapIndex(keys[i]).Interface())
147
}
148
} else {
149
for i := 0; i < value.Len(); i++ {
150
values = append(values, value.Index(i).Interface())
151
}
152
}
153
154
return values
155
}
156
157
func (matcher *ConsistOfMatcher) FailureMessage(actual any) (message string) {
158
message = format.Message(actual, "to consist of", presentable(matcher.Elements))
159
message = appendMissingElements(message, matcher.missingElements)
160
if len(matcher.extraElements) > 0 {
161
message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,
162
format.Object(presentable(matcher.extraElements), 1))
163
}
164
return
165
}
166
167
func appendMissingElements(message string, missingElements []any) string {
168
if len(missingElements) == 0 {
169
return message
170
}
171
return fmt.Sprintf("%s\nthe missing elements were\n%s", message,
172
format.Object(presentable(missingElements), 1))
173
}
174
175
func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual any) (message string) {
176
return format.Message(actual, "not to consist of", presentable(matcher.Elements))
177
}
178
179