-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatchers.go
245 lines (204 loc) · 4.1 KB
/
matchers.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package matchers
import (
"errors"
"fmt"
"reflect"
"strings"
"testing"
)
type Matcher interface {
Match(interface{}) bool
String() string
}
type Mortal interface {
Fatal(args ...interface{})
}
type Protect struct {
*testing.T
}
func (t Protect) Fatal(args ...interface{}) {
t.Error(args...)
}
type EqualTo struct {
V interface{}
}
func (m EqualTo) Match(i interface{}) bool {
return reflect.DeepEqual(m.V, i)
}
func (m EqualTo) String() string {
return fmt.Sprintf("equal to %v(%v)", reflect.TypeOf(m.V), m.V)
}
type Is struct {
V interface{}
}
func (m Is) matcher() Matcher {
switch m.V.(type) {
case Matcher:
return m.V.(Matcher)
}
return &EqualTo{m.V}
}
func (m Is) Match(i interface{}) bool {
return m.matcher().Match(i)
}
func (m Is) String() string {
return fmt.Sprintf("is %v", m.matcher())
}
type TypeOf struct {
V interface{}
}
func (m TypeOf) Match(i interface{}) bool {
return reflect.TypeOf(m.V) == reflect.TypeOf(i)
}
func (m TypeOf) String() string {
return fmt.Sprintf("type %v", reflect.TypeOf(m.V))
}
type Not struct {
V interface{}
}
func (m Not) Match(i interface{}) bool {
return !Is{m.V}.Match(i)
}
func (m Not) String() string {
return fmt.Sprintf("not %v", m.V)
}
type AllOf []Matcher
func (all AllOf) Match(v interface{}) bool {
for _, m := range all {
if !m.Match(v) {
return false
}
}
return true
}
func (all AllOf) String() string {
s := []string{}
for _, m := range all {
s = append(s, fmt.Sprintf("%v", m))
}
return strings.Join(s, ", and ")
}
type AnyOf []Matcher
func (any AnyOf) Match(v interface{}) bool {
for _, m := range any {
if m.Match(v) {
return true
}
}
return false
}
func (any AnyOf) String() string {
s := ""
for i, m := range any {
s += fmt.Sprintf("%v", m)
if i < len(any)-1 {
s += ", or "
}
}
return s
}
type ElementsAre []interface{}
func (self ElementsAre) Match(vs interface{}) bool {
if reflect.TypeOf(vs).Kind() == reflect.Slice {
return self.match(reflect.ValueOf(vs))
}
return false
}
func (self ElementsAre) matcher(i int) Matcher {
switch self[i].(type) {
case Matcher:
return self[i].(Matcher)
}
return &EqualTo{self[i]}
}
func (self ElementsAre) match(vs reflect.Value) bool {
if len(self) != vs.Len() {
return false
}
m := make(map[int]bool)
for i := range self {
for j := 0; j < vs.Len(); j++ {
if self.matcher(i).Match(vs.Index(j).Interface()) {
m[i] = true
}
}
}
return len(m) == len(self)
}
func (self ElementsAre) String() string {
s := []string{}
for _, m := range self {
s = append(s, fmt.Sprintf("%v", m))
}
return "elements are: [" + strings.Join(s, ", ") + "]"
}
type Contains []interface{}
func (self Contains) Match(vs interface{}) bool {
if reflect.TypeOf(vs).Kind() == reflect.Slice {
return self.match(reflect.ValueOf(vs))
}
return false
}
func (self Contains) matcher(i int) Matcher {
switch self[i].(type) {
case Matcher:
return self[i].(Matcher)
}
return &EqualTo{self[i]}
}
func (self Contains) match(vs reflect.Value) bool {
for i := range self {
match := false
for j := 0; j < vs.Len(); j++ {
if self.matcher(i).Match(vs.Index(j).Interface()) {
match = true
}
}
if !match {
return false
}
}
return true
}
func (self Contains) String() string {
s := []string{}
for _, m := range self {
s = append(s, fmt.Sprintf("%v", m))
}
return "contains: [" + strings.Join(s, ", ") + "]"
}
type Fails struct {
}
func (m Fails) Match(i interface{}) bool {
err := i.(Expect).Confirm()
return err != nil
}
func (m Fails) String() string {
return fmt.Sprint("fails")
}
type Expect struct {
I interface{}
M Matcher
}
func (m Expect) String() string {
return fmt.Sprintf("%v %v", m.I, m.M)
}
func (e Expect) Confirm() (err error) {
msg := fmt.Sprintf("%v(%v) %v", reflect.TypeOf(e.I), e.I, e.M)
defer func() {
e := recover()
if e != nil {
err = errors.New(fmt.Sprintf("%s, but was error: %v", msg, e))
}
}()
if !e.M.Match(e.I) {
return errors.New(msg)
}
return nil
}
func AssertThat(t Mortal, i interface{}, m Matcher) {
err := Expect{i, m}.Confirm()
if err != nil {
t.Fatal(fmt.Sprintf("expect that: %v", err))
}
}