-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher_coll.go
90 lines (80 loc) · 2.28 KB
/
matcher_coll.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
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
package bdd
import (
"fmt"
"github.com/onsi/gomega"
"github.com/onsi/gomega/format"
)
// HasLen succeeds if actual has the passed-in length. Actual must be of type
// string, array, map, chan, or slice.
var HasLen = &matcher{
minArgs: 1,
maxArgs: 1,
name: "HasLen",
apply: func(actual interface{}, expected []interface{}) Result {
len, ok := expected[0].(int)
if !ok {
err := fmt.Errorf("expected length to be an int, got: \n %s", format.Object(expected[0], 1))
return Result{Error: err}
}
return resultFromGomega(gomega.HaveLen(len), actual)
},
}
// IsEmpty succeeds if actual is empty. Actual must be of type
// string, array, map, chan, or slice.
var IsEmpty = &matcher{
name: "IsEmpty",
apply: func(actual interface{}, _ []interface{}) Result {
return resultFromGomega(gomega.BeEmpty(), actual)
},
}
// HasElem succeeds if actual contains the passed in element. Actual must be
// an array, slice or map. For maps, HasElem searches through the map's values.
var HasElem = &matcher{
minArgs: 1,
maxArgs: 1,
name: "HasElem",
apply: func(actual interface{}, expected []interface{}) Result {
return resultFromGomega(gomega.ContainElement(expected[0]), actual)
},
}
// HasElems succeeds if actual contains all passed in element. Actual must be
// an array, slice or map. For maps, HasElems searches through the map's values.
var HasElems = &matcher{
minArgs: 1,
maxArgs: 1<<(31) - 1,
name: "HasElems",
apply: func(actual interface{}, expected []interface{}) (r Result) {
for _, val := range expected {
r = resultFromGomega(gomega.ContainElement(val), actual)
if !r.Success {
return
}
}
return
},
}
// Contains succeeds if actual contains all passed in substrings / elements.
// Actual must be an error, string, stringer, array, slice or map.
var Contains = &matcher{
minArgs: 1,
maxArgs: 1<<(31) - 1,
name: "Contains",
apply: func(actual interface{}, expected []interface{}) (r Result) {
var apply func(obtained interface{}, args []interface{}) Result
switch actual.(type) {
case string, fmt.Stringer:
apply = HasSubstr.Apply
case error:
apply = ErrorContains.Apply
default:
apply = HasElem.Apply
}
for _, exp := range expected {
r = apply(actual, []interface{}{exp})
if !r.Success {
return
}
}
return
},
}