mirror-to-gitea/vendor/github.com/onsi/gomega/matchers/consist_of.go
vendor/github.com/onsi/gomega/matchers/consist_of.goRaw
1// untested sections: 3
2
3package matchers
4
5import (
6 "fmt"
7 "reflect"
8
9 "github.com/onsi/gomega/format"
10 "github.com/onsi/gomega/matchers/support/goraph/bipartitegraph"
11)
12
13type ConsistOfMatcher struct {
14 Elements []interface{}
15 missingElements []interface{}
16 extraElements []interface{}
17}
18
19func (matcher *ConsistOfMatcher) Match(actual interface{}) (success bool, err error) {
20 if !isArrayOrSlice(actual) && !isMap(actual) {
21 return false, fmt.Errorf("ConsistOf matcher expects an array/slice/map. Got:\n%s", format.Object(actual, 1))
22 }
23
24 matchers := matchers(matcher.Elements)
25 values := valuesOf(actual)
26
27 bipartiteGraph, err := bipartitegraph.NewBipartiteGraph(values, matchers, neighbours)
28 if err != nil {
29 return false, err
30 }
31
32 edges := bipartiteGraph.LargestMatching()
33 if len(edges) == len(values) && len(edges) == len(matchers) {
34 return true, nil
35 }
36
37 var missingMatchers []interface{}
38 matcher.extraElements, missingMatchers = bipartiteGraph.FreeLeftRight(edges)
39 matcher.missingElements = equalMatchersToElements(missingMatchers)
40
41 return false, nil
42}
43
44func neighbours(value, matcher interface{}) (bool, error) {
45 match, err := matcher.(omegaMatcher).Match(value)
46 return match && err == nil, nil
47}
48
49func equalMatchersToElements(matchers []interface{}) (elements []interface{}) {
50 for _, matcher := range matchers {
51 equalMatcher, ok := matcher.(*EqualMatcher)
52 if ok {
53 matcher = equalMatcher.Expected
54 }
55 elements = append(elements, matcher)
56 }
57 return
58}
59
60func flatten(elems []interface{}) []interface{} {
61 if len(elems) != 1 || !isArrayOrSlice(elems[0]) {
62 return elems
63 }
64
65 value := reflect.ValueOf(elems[0])
66 flattened := make([]interface{}, value.Len())
67 for i := 0; i < value.Len(); i++ {
68 flattened[i] = value.Index(i).Interface()
69 }
70 return flattened
71}
72
73func matchers(expectedElems []interface{}) (matchers []interface{}) {
74 for _, e := range flatten(expectedElems) {
75 matcher, isMatcher := e.(omegaMatcher)
76 if !isMatcher {
77 matcher = &EqualMatcher{Expected: e}
78 }
79 matchers = append(matchers, matcher)
80 }
81 return
82}
83
84func presentable(elems []interface{}) interface{} {
85 elems = flatten(elems)
86
87 if len(elems) == 0 {
88 return []interface{}{}
89 }
90
91 sv := reflect.ValueOf(elems)
92 tt := sv.Index(0).Elem().Type()
93 for i := 1; i < sv.Len(); i++ {
94 if sv.Index(i).Elem().Type() != tt {
95 return elems
96 }
97 }
98
99 ss := reflect.MakeSlice(reflect.SliceOf(tt), sv.Len(), sv.Len())
100 for i := 0; i < sv.Len(); i++ {
101 ss.Index(i).Set(sv.Index(i).Elem())
102 }
103
104 return ss.Interface()
105}
106
107func valuesOf(actual interface{}) []interface{} {
108 value := reflect.ValueOf(actual)
109 values := []interface{}{}
110 if isMap(actual) {
111 keys := value.MapKeys()
112 for i := 0; i < value.Len(); i++ {
113 values = append(values, value.MapIndex(keys[i]).Interface())
114 }
115 } else {
116 for i := 0; i < value.Len(); i++ {
117 values = append(values, value.Index(i).Interface())
118 }
119 }
120
121 return values
122}
123
124func (matcher *ConsistOfMatcher) FailureMessage(actual interface{}) (message string) {
125 message = format.Message(actual, "to consist of", presentable(matcher.Elements))
126 message = appendMissingElements(message, matcher.missingElements)
127 if len(matcher.extraElements) > 0 {
128 message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,
129 format.Object(presentable(matcher.extraElements), 1))
130 }
131 return
132}
133
134func appendMissingElements(message string, missingElements []interface{}) string {
135 if len(missingElements) == 0 {
136 return message
137 }
138 return fmt.Sprintf("%s\nthe missing elements were\n%s", message,
139 format.Object(presentable(missingElements), 1))
140}
141
142func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
143 return format.Message(actual, "not to consist of", presentable(matcher.Elements))
144}