forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 3
/
match.go
327 lines (284 loc) · 7.42 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
package httpexpect
import (
"errors"
"reflect"
)
// Match provides methods to inspect attached regexp match results.
type Match struct {
chain *chain
submatches []string
names map[string]int
}
// NewMatch returns a new Match instance.
//
// If reporter is nil, the function panics.
// Both submatches and names 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.Index(0).IsEqual("http://example.com/users/john")
// m.Index(1).IsEqual("example.com")
// m.Index(2).IsEqual("john")
//
// m.Name("host").IsEqual("example.com")
// m.Name("user").IsEqual("john")
func NewMatch(reporter Reporter, submatches []string, names []string) *Match {
return newMatch(newChainWithDefaults("Match()", reporter), submatches, names)
}
// 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, submatches []string, names []string) *Match {
return newMatch(newChainWithConfig("Match()", config.withDefaults()), submatches, names)
}
func newMatch(parent *chain, matchList []string, nameList []string) *Match {
m := &Match{parent.clone(), nil, nil}
if matchList != nil {
m.submatches = matchList
} else {
m.submatches = []string{}
}
m.names = map[string]int{}
for n, name := range nameList {
if name != "" {
m.names[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.submatches
}
// 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.submatches)))
}
// Index 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, Index 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.Index(0).IsEqual("http://example.com/users/john")
// m.Index(1).IsEqual("example.com")
// m.Index(2).IsEqual("john")
func (m *Match) Index(index int) *String {
opChain := m.chain.enter("Index(%d)", index)
defer opChain.leave()
if opChain.failed() {
return newString(opChain, "")
}
if index < 0 || index >= len(m.submatches) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Actual: &AssertionValue{index},
Expected: &AssertionValue{AssertionRange{
Min: 0,
Max: len(m.submatches) - 1,
}},
Errors: []error{
errors.New("expected: valid sub-match index"),
},
})
return newString(opChain, "")
}
return newString(opChain, m.submatches[index])
}
// Name returns a new String instance with submatch for given name.
//
// If there is no submatch with given name, Name 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.Name("host").IsEqual("example.com")
// m.Name("user").IsEqual("john")
func (m *Match) Name(name string) *String {
opChain := m.chain.enter("Name(%q)", name)
defer opChain.leave()
if opChain.failed() {
return newString(opChain, "")
}
index, ok := m.names[name]
if !ok {
nameList := make([]interface{}, 0, len(m.names))
for n := range m.names {
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.submatches[index])
}
// 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.submatches) == 0) {
opChain.fail(AssertionFailure{
Type: AssertEmpty,
Actual: &AssertionValue{m.submatches},
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.submatches) != 0) {
opChain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{m.submatches},
Errors: []error{
errors.New("expected: non-empty sub-match list"),
},
})
}
return m
}
// Deprecated: use IsEmpty instead.
func (m *Match) Empty() *Match {
return m.IsEmpty()
}
// Values 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.Values("example.com", "john")
func (m *Match) Values(values ...string) *Match {
opChain := m.chain.enter("Values()")
defer opChain.leave()
if opChain.failed() {
return m
}
if values == nil {
values = []string{}
}
if !reflect.DeepEqual(values, m.getValues()) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{m.submatches},
Expected: &AssertionValue{values},
Errors: []error{
errors.New("expected: sub-match lists are equal"),
},
})
}
return m
}
// NotValues 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.NotValues("example.com", "bob")
func (m *Match) NotValues(values ...string) *Match {
opChain := m.chain.enter("NotValues()")
defer opChain.leave()
if values == nil {
values = []string{}
}
if reflect.DeepEqual(values, m.getValues()) {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{m.submatches},
Expected: &AssertionValue{values},
Errors: []error{
errors.New("expected: sub-match lists are non-equal"),
},
})
}
return m
}
func (m *Match) getValues() []string {
if len(m.submatches) > 1 {
return m.submatches[1:]
}
return []string{}
}