aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/consist_of.go
blob: e8ef0dee1f42fb3ca72c22cc71251572c113ca38 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// untested sections: 3

package matchers

import (
	"fmt"
	"reflect"

	"github.com/onsi/gomega/format"
	"github.com/onsi/gomega/matchers/support/goraph/bipartitegraph"
)

type ConsistOfMatcher struct {
	Elements        []interface{}
	missingElements []interface{}
	extraElements   []interface{}
}

func (matcher *ConsistOfMatcher) Match(actual interface{}) (success bool, err error) {
	if !isArrayOrSlice(actual) && !isMap(actual) {
		return false, fmt.Errorf("ConsistOf matcher expects an array/slice/map.  Got:\n%s", format.Object(actual, 1))
	}

	matchers := matchers(matcher.Elements)
	values := valuesOf(actual)

	bipartiteGraph, err := bipartitegraph.NewBipartiteGraph(values, matchers, neighbours)
	if err != nil {
		return false, err
	}

	edges := bipartiteGraph.LargestMatching()
	if len(edges) == len(values) && len(edges) == len(matchers) {
		return true, nil
	}

	var missingMatchers []interface{}
	matcher.extraElements, missingMatchers = bipartiteGraph.FreeLeftRight(edges)
	matcher.missingElements = equalMatchersToElements(missingMatchers)

	return false, nil
}

func neighbours(value, matcher interface{}) (bool, error) {
	match, err := matcher.(omegaMatcher).Match(value)
	return match && err == nil, nil
}

func equalMatchersToElements(matchers []interface{}) (elements []interface{}) {
	for _, matcher := range matchers {
		equalMatcher, ok := matcher.(*EqualMatcher)
		if ok {
			matcher = equalMatcher.Expected
		}
		elements = append(elements, matcher)
	}
	return
}

func flatten(elems []interface{}) []interface{} {
	if len(elems) != 1 || !isArrayOrSlice(elems[0]) {
		return elems
	}

	value := reflect.ValueOf(elems[0])
	flattened := make([]interface{}, value.Len())
	for i := 0; i < value.Len(); i++ {
		flattened[i] = value.Index(i).Interface()
	}
	return flattened
}

func matchers(expectedElems []interface{}) (matchers []interface{}) {
	for _, e := range flatten(expectedElems) {
		matcher, isMatcher := e.(omegaMatcher)
		if !isMatcher {
			matcher = &EqualMatcher{Expected: e}
		}
		matchers = append(matchers, matcher)
	}
	return
}

func presentable(elems []interface{}) interface{} {
	elems = flatten(elems)

	if len(elems) == 0 {
		return []interface{}{}
	}

	sv := reflect.ValueOf(elems)
	tt := sv.Index(0).Elem().Type()
	for i := 1; i < sv.Len(); i++ {
		if sv.Index(i).Elem().Type() != tt {
			return elems
		}
	}

	ss := reflect.MakeSlice(reflect.SliceOf(tt), sv.Len(), sv.Len())
	for i := 0; i < sv.Len(); i++ {
		ss.Index(i).Set(sv.Index(i).Elem())
	}

	return ss.Interface()
}

func valuesOf(actual interface{}) []interface{} {
	value := reflect.ValueOf(actual)
	values := []interface{}{}
	if isMap(actual) {
		keys := value.MapKeys()
		for i := 0; i < value.Len(); i++ {
			values = append(values, value.MapIndex(keys[i]).Interface())
		}
	} else {
		for i := 0; i < value.Len(); i++ {
			values = append(values, value.Index(i).Interface())
		}
	}

	return values
}

func (matcher *ConsistOfMatcher) FailureMessage(actual interface{}) (message string) {
	message = format.Message(actual, "to consist of", presentable(matcher.Elements))
	message = appendMissingElements(message, matcher.missingElements)
	if len(matcher.extraElements) > 0 {
		message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,
			format.Object(presentable(matcher.extraElements), 1))
	}
	return
}

func appendMissingElements(message string, missingElements []interface{}) string {
	if len(missingElements) == 0 {
		return message
	}
	return fmt.Sprintf("%s\nthe missing elements were\n%s", message,
		format.Object(presentable(missingElements), 1))
}

func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "not to consist of", presentable(matcher.Elements))
}