forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
350 lines (303 loc) · 8.43 KB
/
match.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package httpexpect
import (
"errors"
"reflect"
)
// Match provides methods to inspect attached regexp match results.
type Match struct {
chain *chain
submatchValues []string
submatchNames map[string]int
}
// NewMatch returns a new Match instance.
//
// If reporter is nil, the function panics.
// Both submatchValues and submatchNames may be nil.
//
// Example:
//
// s := "http://example.com/users/john"
// r := regexp.MustCompile(`http://(?P<host>.+)/users/(?P<user>.+)`)
//
// m := NewMatch(t, r.FindStringSubmatch(s), r.SubexpNames())
//
// m.NotEmpty()
// m.Length().IsEqual(3)
//
// m.Submatch(0).IsEqual("http://example.com/users/john")
// m.Submatch(1).IsEqual("example.com")
// m.Submatch(2).IsEqual("john")
//
// m.NamedSubmatch("host").IsEqual("example.com")
// m.NamedSubmatch("user").IsEqual("john")
func NewMatch(reporter Reporter, submatchValues []string, submatchNames []string) *Match {
return newMatch(
newChainWithDefaults("Match()", reporter), submatchValues, submatchNames)
}
// NewMatchC returns a new Match instance with config.
//
// Requirements for config are same as for WithConfig function.
// Both submatches and names may be nil.
//
// See NewMatch for usage example.
func NewMatchC(config Config, submatchValues []string, submatchNames []string) *Match {
return newMatch(
newChainWithConfig("Match()", config.withDefaults()), submatchValues, submatchNames)
}
func newMatch(parent *chain, submatchValues []string, submatchNames []string) *Match {
m := &Match{parent.clone(), nil, nil}
if submatchValues != nil {
m.submatchValues = make([]string, len(submatchValues))
copy(m.submatchValues, submatchValues)
} else {
m.submatchValues = []string{}
}
m.submatchNames = map[string]int{}
for n, name := range submatchNames {
if name != "" {
m.submatchNames[name] = n
}
}
return m
}
// Raw returns underlying submatches attached to Match.
// This is the value originally passed to NewMatch.
//
// Example:
//
// m := NewMatch(t, submatches, names)
// assert.Equal(t, submatches, m.Raw())
func (m *Match) Raw() []string {
return m.submatchValues
}
// Alias is similar to Value.Alias.
func (m *Match) Alias(name string) *Match {
opChain := m.chain.enter("Alias(%q)", name)
defer opChain.leave()
m.chain.setAlias(name)
return m
}
// Length returns a new Number instance with number of submatches.
//
// Example:
//
// m := NewMatch(t, submatches, names)
// m.Length().IsEqual(len(submatches))
func (m *Match) Length() *Number {
opChain := m.chain.enter("Length()")
defer opChain.leave()
if opChain.failed() {
return newNumber(opChain, 0)
}
return newNumber(opChain, float64(len(m.submatchValues)))
}
// Submatch returns a new String instance with submatch for given index.
//
// Note that submatch with index 0 contains the whole match. If index is out
// of bounds, Submatch reports failure and returns empty (but non-nil) instance.
//
// Example:
//
// s := "http://example.com/users/john"
//
// r := regexp.MustCompile(`http://(.+)/users/(.+)`)
// m := NewMatch(t, r.FindStringSubmatch(s), nil)
//
// m.Submatch(0).IsEqual("http://example.com/users/john")
// m.Submatch(1).IsEqual("example.com")
// m.Submatch(2).IsEqual("john")
func (m *Match) Submatch(index int) *String {
opChain := m.chain.enter("Submatch(%d)", index)
defer opChain.leave()
if opChain.failed() {
return newString(opChain, "")
}
if index < 0 || index >= len(m.submatchValues) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Actual: &AssertionValue{index},
Expected: &AssertionValue{AssertionRange{
Min: 0,
Max: len(m.submatchValues) - 1,
}},
Errors: []error{
errors.New("expected: valid sub-match index"),
},
})
return newString(opChain, "")
}
return newString(opChain, m.submatchValues[index])
}
// NamedSubmatch returns a new String instance with submatch for given name.
//
// If there is no submatch with given name, NamedSubmatch reports failure and returns
// empty (but non-nil) instance.
//
// Example:
//
// s := "http://example.com/users/john"
//
// r := regexp.MustCompile(`http://(?P<host>.+)/users/(?P<user>.+)`)
// m := NewMatch(t, r.FindStringSubmatch(s), r.SubexpNames())
//
// m.NamedSubmatch("host").IsEqual("example.com")
// m.NamedSubmatch("user").IsEqual("john")
func (m *Match) NamedSubmatch(name string) *String {
opChain := m.chain.enter("NamedSubmatch(%q)", name)
defer opChain.leave()
if opChain.failed() {
return newString(opChain, "")
}
index, ok := m.submatchNames[name]
if !ok {
nameList := make([]interface{}, 0, len(m.submatchNames))
for n := range m.submatchNames {
nameList = append(nameList, n)
}
opChain.fail(AssertionFailure{
Type: AssertBelongs,
Actual: &AssertionValue{name},
Expected: &AssertionValue{AssertionList(nameList)},
Errors: []error{
errors.New("expected: existing sub-match name"),
},
})
return newString(opChain, "")
}
return newString(opChain, m.submatchValues[index])
}
// Deprecated: use Submatch instead.
func (m *Match) Index(index int) *String {
return m.Submatch(index)
}
// Deprecated: use Submatch instead.
func (m *Match) Name(name string) *String {
return m.NamedSubmatch(name)
}
// IsEmpty succeeds if submatches array is empty.
//
// Example:
//
// m := NewMatch(t, submatches, names)
// m.IsEmpty()
func (m *Match) IsEmpty() *Match {
opChain := m.chain.enter("IsEmpty()")
defer opChain.leave()
if opChain.failed() {
return m
}
if !(len(m.submatchValues) == 0) {
opChain.fail(AssertionFailure{
Type: AssertEmpty,
Actual: &AssertionValue{m.submatchValues},
Errors: []error{
errors.New("expected: empty sub-match list"),
},
})
}
return m
}
// NotEmpty succeeds if submatches array is non-empty.
//
// Example:
//
// m := NewMatch(t, submatches, names)
// m.NotEmpty()
func (m *Match) NotEmpty() *Match {
opChain := m.chain.enter("NotEmpty()")
defer opChain.leave()
if opChain.failed() {
return m
}
if !(len(m.submatchValues) != 0) {
opChain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{m.submatchValues},
Errors: []error{
errors.New("expected: non-empty sub-match list"),
},
})
}
return m
}
// Deprecated: use IsEmpty instead.
func (m *Match) Empty() *Match {
return m.IsEmpty()
}
// HasSubmatches succeeds if submatches array, starting from index 1, is equal to
// given array.
//
// Note that submatch with index 0 contains the whole match and is not
// included into this check.
//
// Example:
//
// s := "http://example.com/users/john"
// r := regexp.MustCompile(`http://(.+)/users/(.+)`)
// m := NewMatch(t, r.FindStringSubmatch(s), nil)
// m.HasSubmatches("example.com", "john")
func (m *Match) HasSubmatches(submatchValues ...string) *Match {
opChain := m.chain.enter("HasSubmatches()")
defer opChain.leave()
if opChain.failed() {
return m
}
if submatchValues == nil {
submatchValues = []string{}
}
if !reflect.DeepEqual(submatchValues, m.getValues()) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{m.submatchValues},
Expected: &AssertionValue{submatchValues},
Errors: []error{
errors.New("expected: sub-match lists are equal"),
},
})
}
return m
}
// NotHasSubmatches succeeds if submatches array, starting from index 1, is not
// equal to given array.
//
// Note that submatch with index 0 contains the whole match and is not
// included into this check.
//
// Example:
//
// s := "http://example.com/users/john"
// r := regexp.MustCompile(`http://(.+)/users/(.+)`)
// m := NewMatch(t, r.FindStringSubmatch(s), nil)
// m.NotHasSubmatches("example.com", "bob")
func (m *Match) NotHasSubmatches(submatchValues ...string) *Match {
opChain := m.chain.enter("NotHasSubmatches()")
defer opChain.leave()
if submatchValues == nil {
submatchValues = []string{}
}
if reflect.DeepEqual(submatchValues, m.getValues()) {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{m.submatchValues},
Expected: &AssertionValue{submatchValues},
Errors: []error{
errors.New("expected: sub-match lists are non-equal"),
},
})
}
return m
}
// Deprecated: use HasSubmatches instead.
func (m *Match) Values(submatchValues ...string) *Match {
return m.HasSubmatches(submatchValues...)
}
// Deprecated: use NotHasSubmatches instead.
func (m *Match) NotValues(submatchValues ...string) *Match {
return m.NotHasSubmatches(submatchValues...)
}
func (m *Match) getValues() []string {
if len(m.submatchValues) > 1 {
return m.submatchValues[1:]
}
return []string{}
}