/
consist_of.go
52 lines (45 loc) · 1.18 KB
/
consist_of.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package matchers
import (
"encoding/json"
"github.com/onsi/gomega/matchers"
"github.com/samber/lo"
)
type ConsistOfMatcher struct {
matchers.ConsistOfMatcher
}
func ConsistOf(elements ...interface{}) GossMatcher {
return &ConsistOfMatcher{
matchers.ConsistOfMatcher{
Elements: elements,
},
}
}
func (m *ConsistOfMatcher) FailureResult(actual interface{}) MatcherResult {
missingElements := getUnexported(m, "missingElements")
extraElements := getUnexported(m, "extraElements")
missingEl, ok := missingElements.([]interface{})
var foundElements any
if ok {
foundElements, _ = lo.Difference(m.Elements, missingEl)
}
return MatcherResult{
Actual: actual,
Message: "to consist of",
Expected: m.Elements,
MissingElements: missingElements,
ExtraElements: extraElements,
FoundElements: foundElements,
}
}
func (m *ConsistOfMatcher) NegatedFailureResult(actual interface{}) MatcherResult {
return MatcherResult{
Actual: actual,
Message: "not to consist of",
Expected: m.Elements,
}
}
func (m *ConsistOfMatcher) MarshalJSON() ([]byte, error) {
j := make(map[string]interface{})
j["consist-of"] = m.Elements
return json.Marshal(j)
}