...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package gomock
16
17 import (
18 "fmt"
19 "reflect"
20 "strings"
21 )
22
23
24
25 type Matcher interface {
26
27 Matches(x interface{}) bool
28
29
30 String() string
31 }
32
33
34
35
36 func WantFormatter(s fmt.Stringer, m Matcher) Matcher {
37 type matcher interface {
38 Matches(x interface{}) bool
39 }
40
41 return struct {
42 matcher
43 fmt.Stringer
44 }{
45 matcher: m,
46 Stringer: s,
47 }
48 }
49
50
51
52
53 type StringerFunc func() string
54
55
56 func (f StringerFunc) String() string {
57 return f()
58 }
59
60
61
62
63 type GotFormatter interface {
64
65
66 Got(got interface{}) string
67 }
68
69
70
71
72 type GotFormatterFunc func(got interface{}) string
73
74
75 func (f GotFormatterFunc) Got(got interface{}) string {
76 return f(got)
77 }
78
79
80 func GotFormatterAdapter(s GotFormatter, m Matcher) Matcher {
81 return struct {
82 GotFormatter
83 Matcher
84 }{
85 GotFormatter: s,
86 Matcher: m,
87 }
88 }
89
90 type anyMatcher struct{}
91
92 func (anyMatcher) Matches(interface{}) bool {
93 return true
94 }
95
96 func (anyMatcher) String() string {
97 return "is anything"
98 }
99
100 type eqMatcher struct {
101 x interface{}
102 }
103
104 func (e eqMatcher) Matches(x interface{}) bool {
105
106 if e.x == nil || x == nil {
107 return reflect.DeepEqual(e.x, x)
108 }
109
110
111 x1Val := reflect.ValueOf(e.x)
112 x2Val := reflect.ValueOf(x)
113
114 if x1Val.Type().AssignableTo(x2Val.Type()) {
115 x1ValConverted := x1Val.Convert(x2Val.Type())
116 return reflect.DeepEqual(x1ValConverted.Interface(), x2Val.Interface())
117 }
118
119 return false
120 }
121
122 func (e eqMatcher) String() string {
123 return fmt.Sprintf("is equal to %v (%T)", e.x, e.x)
124 }
125
126 type nilMatcher struct{}
127
128 func (nilMatcher) Matches(x interface{}) bool {
129 if x == nil {
130 return true
131 }
132
133 v := reflect.ValueOf(x)
134 switch v.Kind() {
135 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
136 reflect.Ptr, reflect.Slice:
137 return v.IsNil()
138 }
139
140 return false
141 }
142
143 func (nilMatcher) String() string {
144 return "is nil"
145 }
146
147 type notMatcher struct {
148 m Matcher
149 }
150
151 func (n notMatcher) Matches(x interface{}) bool {
152 return !n.m.Matches(x)
153 }
154
155 func (n notMatcher) String() string {
156 return "not(" + n.m.String() + ")"
157 }
158
159 type assignableToTypeOfMatcher struct {
160 targetType reflect.Type
161 }
162
163 func (m assignableToTypeOfMatcher) Matches(x interface{}) bool {
164 return reflect.TypeOf(x).AssignableTo(m.targetType)
165 }
166
167 func (m assignableToTypeOfMatcher) String() string {
168 return "is assignable to " + m.targetType.Name()
169 }
170
171 type allMatcher struct {
172 matchers []Matcher
173 }
174
175 func (am allMatcher) Matches(x interface{}) bool {
176 for _, m := range am.matchers {
177 if !m.Matches(x) {
178 return false
179 }
180 }
181 return true
182 }
183
184 func (am allMatcher) String() string {
185 ss := make([]string, 0, len(am.matchers))
186 for _, matcher := range am.matchers {
187 ss = append(ss, matcher.String())
188 }
189 return strings.Join(ss, "; ")
190 }
191
192 type lenMatcher struct {
193 i int
194 }
195
196 func (m lenMatcher) Matches(x interface{}) bool {
197 v := reflect.ValueOf(x)
198 switch v.Kind() {
199 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
200 return v.Len() == m.i
201 default:
202 return false
203 }
204 }
205
206 func (m lenMatcher) String() string {
207 return fmt.Sprintf("has length %d", m.i)
208 }
209
210 type inAnyOrderMatcher struct {
211 x interface{}
212 }
213
214 func (m inAnyOrderMatcher) Matches(x interface{}) bool {
215 given, ok := m.prepareValue(x)
216 if !ok {
217 return false
218 }
219 wanted, ok := m.prepareValue(m.x)
220 if !ok {
221 return false
222 }
223
224 if given.Len() != wanted.Len() {
225 return false
226 }
227
228 usedFromGiven := make([]bool, given.Len())
229 foundFromWanted := make([]bool, wanted.Len())
230 for i := 0; i < wanted.Len(); i++ {
231 wantedMatcher := Eq(wanted.Index(i).Interface())
232 for j := 0; j < given.Len(); j++ {
233 if usedFromGiven[j] {
234 continue
235 }
236 if wantedMatcher.Matches(given.Index(j).Interface()) {
237 foundFromWanted[i] = true
238 usedFromGiven[j] = true
239 break
240 }
241 }
242 }
243
244 missingFromWanted := 0
245 for _, found := range foundFromWanted {
246 if !found {
247 missingFromWanted++
248 }
249 }
250 extraInGiven := 0
251 for _, used := range usedFromGiven {
252 if !used {
253 extraInGiven++
254 }
255 }
256
257 return extraInGiven == 0 && missingFromWanted == 0
258 }
259
260 func (m inAnyOrderMatcher) prepareValue(x interface{}) (reflect.Value, bool) {
261 xValue := reflect.ValueOf(x)
262 switch xValue.Kind() {
263 case reflect.Slice, reflect.Array:
264 return xValue, true
265 default:
266 return reflect.Value{}, false
267 }
268 }
269
270 func (m inAnyOrderMatcher) String() string {
271 return fmt.Sprintf("has the same elements as %v", m.x)
272 }
273
274
275
276
277
278 func All(ms ...Matcher) Matcher { return allMatcher{ms} }
279
280
281 func Any() Matcher { return anyMatcher{} }
282
283
284
285
286
287
288 func Eq(x interface{}) Matcher { return eqMatcher{x} }
289
290
291
292 func Len(i int) Matcher {
293 return lenMatcher{i}
294 }
295
296
297
298
299
300
301
302
303 func Nil() Matcher { return nilMatcher{} }
304
305
306
307
308
309
310 func Not(x interface{}) Matcher {
311 if m, ok := x.(Matcher); ok {
312 return notMatcher{m}
313 }
314 return notMatcher{Eq(x)}
315 }
316
317
318
319
320
321
322
323
324
325
326
327 func AssignableToTypeOf(x interface{}) Matcher {
328 if xt, ok := x.(reflect.Type); ok {
329 return assignableToTypeOfMatcher{xt}
330 }
331 return assignableToTypeOfMatcher{reflect.TypeOf(x)}
332 }
333
334
335
336
337
338
339 func InAnyOrder(x interface{}) Matcher {
340 return inAnyOrderMatcher{x}
341 }
342
View as plain text