array.go
7.9 KB
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
package httpexpect
import (
"reflect"
)
// Array provides methods to inspect attached []interface{} object
// (Go representation of JSON array).
type Array struct {
chain chain
value []interface{}
}
// NewArray returns a new Array given a reporter used to report failures
// and value to be inspected.
//
// Both reporter and value should not be nil. If value is nil, failure is
// reported.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
func NewArray(reporter Reporter, value []interface{}) *Array {
chain := makeChain(reporter)
if value == nil {
chain.fail("expected non-nil array value")
} else {
value, _ = canonArray(&chain, value)
}
return &Array{chain, value}
}
// Raw returns underlying value attached to Array.
// This is the value originally passed to NewArray, converted to canonical form.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// assert.Equal(t, []interface{}{"foo", 123.0}, array.Raw())
func (a *Array) Raw() []interface{} {
return a.value
}
// Path is similar to Value.Path.
func (a *Array) Path(path string) *Value {
return getPath(&a.chain, a.value, path)
}
// Schema is similar to Value.Schema.
func (a *Array) Schema(schema interface{}) *Array {
checkSchema(&a.chain, a.value, schema)
return a
}
// Length returns a new Number object that may be used to inspect array length.
//
// Example:
// array := NewArray(t, []interface{}{1, 2, 3})
// array.Length().Equal(3)
func (a *Array) Length() *Number {
return &Number{a.chain, float64(len(a.value))}
}
// Element returns a new Value object that may be used to inspect array element
// for given index.
//
// If index is out of array bounds, Element reports failure and returns empty
// (but non-nil) value.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.Element(0).String().Equal("foo")
// array.Element(1).Number().Equal(123)
func (a *Array) Element(index int) *Value {
if index < 0 || index >= len(a.value) {
a.chain.fail(
"\narray index out of bounds:\n index %d\n\n bounds [%d; %d)",
index,
0,
len(a.value))
return &Value{a.chain, nil}
}
return &Value{a.chain, a.value[index]}
}
// First returns a new Value object that may be used to inspect first element
// of given array.
//
// If given array is empty, First reports failure and returns empty
// (but non-nil) value.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.First().String().Equal("foo")
func (a *Array) First() *Value {
if len(a.value) < 1 {
a.chain.fail("\narray is empty")
return &Value{a.chain, nil}
}
return &Value{a.chain, a.value[0]}
}
// Last returns a new Value object that may be used to inspect last element
// of given array.
//
// If given array is empty, Last reports failure and returns empty
// (but non-nil) value.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.Last().Number().Equal(123)
func (a *Array) Last() *Value {
if len(a.value) < 1 {
a.chain.fail("\narray is empty")
return &Value{a.chain, nil}
}
return &Value{a.chain, a.value[len(a.value)-1]}
}
// Iter returns a new slice of Values attached to array elements.
//
// Example:
// strings := []interface{}{"foo", "bar"}
// array := NewArray(t, strings)
//
// for n, val := range array.Iter() {
// val.String().Equal(strings[n])
// }
func (a *Array) Iter() []Value {
if a.chain.failed() {
return []Value{}
}
ret := []Value{}
for n := range a.value {
ret = append(ret, Value{a.chain, a.value[n]})
}
return ret
}
// Empty succeeds if array is empty.
//
// Example:
// array := NewArray(t, []interface{}{})
// array.Empty()
func (a *Array) Empty() *Array {
return a.Equal([]interface{}{})
}
// NotEmpty succeeds if array is non-empty.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotEmpty()
func (a *Array) NotEmpty() *Array {
return a.NotEqual([]interface{}{})
}
// Equal succeeds if array is equal to given Go slice.
// Before comparison, both array and value are converted to canonical form.
//
// value should be a slice of any type.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.Equal([]interface{}{"foo", 123})
//
// array := NewArray(t, []interface{}{"foo", "bar"})
// array.Equal([]string{}{"foo", "bar"})
//
// array := NewArray(t, []interface{}{123, 456})
// array.Equal([]int{}{123, 456})
func (a *Array) Equal(value interface{}) *Array {
expected, ok := canonArray(&a.chain, value)
if !ok {
return a
}
if !reflect.DeepEqual(expected, a.value) {
a.chain.fail("\nexpected array equal to:\n%s\n\nbut got:\n%s\n\ndiff:\n%s",
dumpValue(expected),
dumpValue(a.value),
diffValues(expected, a.value))
}
return a
}
// NotEqual succeeds if array is not equal to given Go slice.
// Before comparison, both array and value are converted to canonical form.
//
// value should be a slice of any type.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotEqual([]interface{}{123, "foo"})
func (a *Array) NotEqual(value interface{}) *Array {
expected, ok := canonArray(&a.chain, value)
if !ok {
return a
}
if reflect.DeepEqual(expected, a.value) {
a.chain.fail("\nexpected array not equal to:\n%s",
dumpValue(expected))
}
return a
}
// Elements succeeds if array contains all given elements, in given order, and only
// them. Before comparison, array and all elements are converted to canonical form.
//
// For partial or unordered comparison, see Contains and ContainsOnly.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.Elements("foo", 123)
//
// This calls are equivalent:
// array.Elelems("a", "b")
// array.Equal([]interface{}{"a", "b"})
func (a *Array) Elements(values ...interface{}) *Array {
return a.Equal(values)
}
// Contains succeeds if array contains all given elements (in any order).
// Before comparison, array and all elements are converted to canonical form.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.Contains(123, "foo")
func (a *Array) Contains(values ...interface{}) *Array {
elements, ok := canonArray(&a.chain, values)
if !ok {
return a
}
for _, e := range elements {
if !a.containsElement(e) {
a.chain.fail("\nexpected array containing element:\n%s\n\nbut got:\n%s",
dumpValue(e), dumpValue(a.value))
}
}
return a
}
// NotContains succeeds if array contains none of given elements.
// Before comparison, array and all elements are converted to canonical form.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotContains("bar") // success
// array.NotContains("bar", "foo") // failure (array contains "foo")
func (a *Array) NotContains(values ...interface{}) *Array {
elements, ok := canonArray(&a.chain, values)
if !ok {
return a
}
for _, e := range elements {
if a.containsElement(e) {
a.chain.fail("\nexpected array not containing element:\n%s\n\nbut got:\n%s",
dumpValue(e), dumpValue(a.value))
}
}
return a
}
// ContainsOnly succeeds if array contains all given elements, in any order, and only
// them. Before comparison, array and all elements are converted to canonical form.
//
// Example:
// array := NewArray(t, []interface{}{"foo", 123})
// array.ContainsOnly(123, "foo")
//
// This calls are equivalent:
// array.ContainsOnly("a", "b")
// array.ContainsOnly("b", "a")
func (a *Array) ContainsOnly(values ...interface{}) *Array {
elements, ok := canonArray(&a.chain, values)
if !ok {
return a
}
if len(elements) != len(a.value) {
a.chain.fail("\nexpected array of length == %d:\n%s\n\n"+
"but got array of length %d:\n%s",
len(elements), dumpValue(elements),
len(a.value), dumpValue(a.value))
return a
}
for _, e := range elements {
if !a.containsElement(e) {
a.chain.fail("\nexpected array containing element:\n%s\n\nbut got:\n%s",
dumpValue(e), dumpValue(a.value))
}
}
return a
}
func (a *Array) containsElement(expected interface{}) bool {
for _, e := range a.value {
if reflect.DeepEqual(expected, e) {
return true
}
}
return false
}