Skip to content

Commit 1a4e27f

Browse files
authored
Feature/525 return findings for ContainElement (#527)
* allow ContainElement to optionally return matched elements * adds ContainElement documentation about findings pointer
1 parent 1f2e714 commit 1a4e27f

File tree

4 files changed

+356
-56
lines changed

4 files changed

+356
-56
lines changed

docs/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,13 @@ succeeds if the capacity of `ACTUAL` is `INT`. `ACTUAL` must be of type `array`,
886886
Ω(ACTUAL).Should(ContainElement(ELEMENT))
887887
```
888888

889+
or
890+
891+
```go
892+
Ω(ACTUAL).Should(ContainElement(ELEMENT, <Pointer>))
893+
```
894+
895+
889896
succeeds if `ACTUAL` contains an element that equals `ELEMENT`. `ACTUAL` must be an `array`, `slice`, or `map` -- anything else is an error. For `map`s `ContainElement` searches through the map's values (not keys!).
890897

891898
By default `ContainElement()` uses the `Equal()` matcher under the hood to assert equality between `ACTUAL`'s elements and `ELEMENT`. You can change this, however, by passing `ContainElement` a `GomegaMatcher`. For example, to check that a slice of strings has an element that matches a substring:
@@ -894,6 +901,29 @@ By default `ContainElement()` uses the `Equal()` matcher under the hood to asser
894901
Ω([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))
895902
```
896903

904+
In addition, there are occasions when you need to grab (all) matching contained elements, for instance, to make several assertions against the matching contained elements. To do this, you can ask the `ContainElement` matcher for the matching contained elements by passing it a pointer to a variable of the appropriate type. If multiple matching contained elements are expected, then a pointer to either a slice or a map should be passed (but not a pointer to an array), otherwise a pointer to a scalar (non-slice, non-map):
905+
906+
```go
907+
var findings []string
908+
Ω([]string{"foo", "foobar", "bar"}).Should(ContainElement(ContainSubstring("foo"), &findings))
909+
910+
var finding string
911+
Ω([]string{"foo", "foobar", "bar"}).Should(ContainElement("foobar", &finding))
912+
```
913+
914+
The `ContainElement` matcher will fail with a descriptive error message in case of multiple matches when the pointer references a scalar type.
915+
916+
In case of maps, the matching contained elements will be returned with their keys in the map referenced by the pointer.
917+
918+
```go
919+
var findings map[int]string
920+
Ω(map[int]string{
921+
1: "bar",
922+
2: "foobar",
923+
3: "foo",
924+
}).Should(ContainElement(ContainSubstring("foo"), &findings))
925+
```
926+
897927
#### ContainElements(element ...interface{})
898928

899929
```go

matchers.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,26 @@ func BeZero() types.GomegaMatcher {
256256
return &matchers.BeZeroMatcher{}
257257
}
258258

259-
//ContainElement succeeds if actual contains the passed in element.
260-
//By default ContainElement() uses Equal() to perform the match, however a
261-
//matcher can be passed in instead:
259+
//ContainElement succeeds if actual contains the passed in element. By default
260+
//ContainElement() uses Equal() to perform the match, however a matcher can be
261+
//passed in instead:
262262
// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))
263263
//
264-
//Actual must be an array, slice or map.
265-
//For maps, ContainElement searches through the map's values.
266-
func ContainElement(element interface{}) types.GomegaMatcher {
264+
//Actual must be an array, slice or map. For maps, ContainElement searches
265+
//through the map's values.
266+
//
267+
//If you want to have a copy of the matching element(s) found you can pass a
268+
//pointer to a variable of the appropriate type. If the variable isn't a slice
269+
//or map, then exactly one match will be expected and returned. If the variable
270+
//is a slice or map, then at least one match is expected and all matches will be
271+
//stored in the variable.
272+
//
273+
// var findings []string
274+
// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings)))
275+
func ContainElement(element interface{}, result ...interface{}) types.GomegaMatcher {
267276
return &matchers.ContainElementMatcher{
268277
Element: element,
278+
Result: result,
269279
}
270280
}
271281

matchers/contain_element_matcher.go

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package matchers
44

55
import (
6+
"errors"
67
"fmt"
78
"reflect"
89

@@ -11,44 +12,157 @@ import (
1112

1213
type ContainElementMatcher struct {
1314
Element interface{}
15+
Result []interface{}
1416
}
1517

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

23+
var actualT reflect.Type
24+
var result reflect.Value
25+
switch l := len(matcher.Result); {
26+
case l > 1:
27+
return false, errors.New("ContainElement matcher expects at most a single optional pointer to store its findings at")
28+
case l == 1:
29+
if reflect.ValueOf(matcher.Result[0]).Kind() != reflect.Ptr {
30+
return false, fmt.Errorf("ContainElement matcher expects a non-nil pointer to store its findings at. Got\n%s",
31+
format.Object(matcher.Result[0], 1))
32+
}
33+
actualT = reflect.TypeOf(actual)
34+
resultReference := matcher.Result[0]
35+
result = reflect.ValueOf(resultReference).Elem() // what ResultReference points to, to stash away our findings
36+
switch result.Kind() {
37+
case reflect.Array:
38+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
39+
reflect.SliceOf(actualT.Elem()).String(), result.Type().String())
40+
case reflect.Slice:
41+
if !isArrayOrSlice(actual) {
42+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
43+
reflect.MapOf(actualT.Key(), actualT.Elem()).String(), result.Type().String())
44+
}
45+
if !actualT.Elem().AssignableTo(result.Type().Elem()) {
46+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
47+
actualT.String(), result.Type().String())
48+
}
49+
case reflect.Map:
50+
if !isMap(actual) {
51+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
52+
actualT.String(), result.Type().String())
53+
}
54+
if !actualT.AssignableTo(result.Type()) {
55+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
56+
actualT.String(), result.Type().String())
57+
}
58+
default:
59+
if !actualT.Elem().AssignableTo(result.Type()) {
60+
return false, fmt.Errorf("ContainElement cannot return findings. Need *%s, got *%s",
61+
actualT.Elem().String(), result.Type().String())
62+
}
63+
}
64+
}
65+
2166
elemMatcher, elementIsMatcher := matcher.Element.(omegaMatcher)
2267
if !elementIsMatcher {
2368
elemMatcher = &EqualMatcher{Expected: matcher.Element}
2469
}
2570

2671
value := reflect.ValueOf(actual)
2772
var valueAt func(int) interface{}
73+
74+
var getFindings func() reflect.Value
75+
var foundAt func(int)
76+
2877
if isMap(actual) {
2978
keys := value.MapKeys()
3079
valueAt = func(i int) interface{} {
3180
return value.MapIndex(keys[i]).Interface()
3281
}
82+
if result.Kind() != reflect.Invalid {
83+
fm := reflect.MakeMap(actualT)
84+
getFindings = func() reflect.Value {
85+
return fm
86+
}
87+
foundAt = func(i int) {
88+
fm.SetMapIndex(keys[i], value.MapIndex(keys[i]))
89+
}
90+
}
3391
} else {
3492
valueAt = func(i int) interface{} {
3593
return value.Index(i).Interface()
3694
}
95+
if result.Kind() != reflect.Invalid {
96+
var f reflect.Value
97+
if result.Kind() == reflect.Slice {
98+
f = reflect.MakeSlice(result.Type(), 0, 0)
99+
} else {
100+
f = reflect.MakeSlice(reflect.SliceOf(result.Type()), 0, 0)
101+
}
102+
getFindings = func() reflect.Value {
103+
return f
104+
}
105+
foundAt = func(i int) {
106+
f = reflect.Append(f, value.Index(i))
107+
}
108+
}
37109
}
38110

39111
var lastError error
40112
for i := 0; i < value.Len(); i++ {
41-
success, err := elemMatcher.Match(valueAt(i))
113+
elem := valueAt(i)
114+
success, err := elemMatcher.Match(elem)
42115
if err != nil {
43116
lastError = err
44117
continue
45118
}
46119
if success {
47-
return true, nil
120+
if result.Kind() == reflect.Invalid {
121+
return true, nil
122+
}
123+
foundAt(i)
48124
}
49125
}
50126

51-
return false, lastError
127+
// when the expectation isn't interested in the findings except for success
128+
// or non-success, then we're done here and return the last matcher error
129+
// seen, if any, as well as non-success.
130+
if result.Kind() == reflect.Invalid {
131+
return false, lastError
132+
}
133+
134+
// pick up any findings the test is interested in as it specified a non-nil
135+
// result reference. However, the expection always is that there are at
136+
// least one or multiple findings. So, if a result is expected, but we had
137+
// no findings, then this is an error.
138+
findings := getFindings()
139+
if findings.Len() == 0 {
140+
return false, lastError
141+
}
142+
143+
// there's just a single finding and the result is neither a slice nor a map
144+
// (so it's a scalar): pick the one and only finding and return it in the
145+
// place the reference points to.
146+
if findings.Len() == 1 && !isArrayOrSlice(result.Interface()) && !isMap(result.Interface()) {
147+
if isMap(actual) {
148+
miter := findings.MapRange()
149+
miter.Next()
150+
result.Set(miter.Value())
151+
} else {
152+
result.Set(findings.Index(0))
153+
}
154+
return true, nil
155+
}
156+
157+
// at least one or even multiple findings and a the result references a
158+
// slice or a map, so all we need to do is to store our findings where the
159+
// reference points to.
160+
if !findings.Type().AssignableTo(result.Type()) {
161+
return false, fmt.Errorf("ContainElement cannot return multiple findings. Need *%s, got *%s",
162+
findings.Type().String(), result.Type().String())
163+
}
164+
result.Set(findings)
165+
return true, nil
52166
}
53167

54168
func (matcher *ContainElementMatcher) FailureMessage(actual interface{}) (message string) {

0 commit comments

Comments
 (0)