forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
237 lines (200 loc) · 5.81 KB
/
slice_test.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
package meta
import (
"net/url"
"testing"
)
type withSliceString struct {
A []String `meta_required:"true"`
B []*String
}
var withSliceStringDecoder = NewDecoder(&withSliceString{})
func TestSliceStringSuccess(t *testing.T) {
var inputs withSliceString
e := withSliceStringDecoder.DecodeValues(&inputs, url.Values{
"a.0": {"z"},
"a.1": {"y"},
"a.2": {"x"},
"b.0": {"w"},
})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, len(inputs.A), 3)
assertEqual(t, len(inputs.B), 1)
if len(inputs.A) == 3 {
assertEqual(t, inputs.A[0].Val, "z")
assertEqual(t, inputs.A[1].Val, "y")
assertEqual(t, inputs.A[2].Val, "x")
}
if len(inputs.B) == 1 {
assertEqual(t, inputs.B[0].Val, "w")
}
}
func TestSliceStringSuccessMultiSource(t *testing.T) {
var inputs withSliceString
e := withSliceStringDecoder.Decode(&inputs, url.Values{
"a.0": {"z"},
"a.1": {"y"},
"a.2": {"x"},
"b.0": {"w"},
}, []byte(`{"a":["z1", "y1"]}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, len(inputs.A), 3)
assertEqual(t, len(inputs.B), 1)
if len(inputs.A) == 3 {
assertEqual(t, inputs.A[0].Val, "z1")
assertEqual(t, inputs.A[1].Val, "y1")
assertEqual(t, inputs.A[2].Val, "x")
}
if len(inputs.B) == 1 {
assertEqual(t, inputs.B[0].Val, "w")
}
}
// by default, if no items are present, then the slice will be set to nil
func TestSliceStringNoItems(t *testing.T) {
var inputs withSliceString
e := withSliceStringDecoder.DecodeValues(&inputs, url.Values{})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, len(inputs.A), 0)
assertEqual(t, len(inputs.B), 0)
assertEqual(t, inputs.A, []String(nil))
assertEqual(t, inputs.B, []*String(nil))
}
// TODO: make a test where it's a []OptionalString, and pass in blank values. Should they be included in the array?
// errors in an element.
func TestSliceStringItemBlank(t *testing.T) {
var inputs withSliceString
e := withSliceStringDecoder.DecodeValues(&inputs, url.Values{
"a.0": {""},
"a.1": {"z"},
"a.2": {""},
"a.3": {"y"},
})
assertEqual(t, e, ErrorHash{"a": ErrorSlice{ErrBlank, nil, ErrBlank, nil}})
}
type withSliceOfHashes struct {
A []struct {
A String `meta_required:"true"`
B String
}
B []*struct {
Z String
}
}
var withSliceOfHashesDecoder = NewDecoder(&withSliceOfHashes{})
func TestSliceOfHashesSuccess(t *testing.T) {
var inputs withSliceOfHashes
e := withSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{
"a.0.a": {"Z"},
"a.0.b": {"Y"},
"a.1.a": {"X"},
"a.1.b": {"W"},
"a.2.a": {"V"},
"b.0.z": {""},
"b.1.z": {"U"},
})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, len(inputs.A), 3)
assertEqual(t, len(inputs.B), 2)
if len(inputs.A) == 3 {
assertEqual(t, inputs.A[0].A.Val, "Z")
assertEqual(t, inputs.A[0].B.Val, "Y")
assertEqual(t, inputs.A[0].B.Present, true)
assertEqual(t, inputs.A[1].A.Val, "X")
assertEqual(t, inputs.A[1].B.Val, "W")
assertEqual(t, inputs.A[1].B.Present, true)
assertEqual(t, inputs.A[2].A.Val, "V")
assertEqual(t, inputs.A[2].B.Val, "")
assertEqual(t, inputs.A[2].B.Present, false)
}
if len(inputs.B) == 2 {
// NOTE: it is conceivable that we'd change the spec:
// if no value is present in a a hash that is in an array, then it's as if the item wasn't passed. So len(inputs.B) == 1 in this case.
assertEqual(t, inputs.B[0].Z.Val, "")
assertEqual(t, inputs.B[0].Z.Present, false)
assertEqual(t, inputs.B[1].Z.Val, "U")
assertEqual(t, inputs.B[1].Z.Present, true)
}
}
func TestSliceOfHashesError(t *testing.T) {
var inputs withSliceOfHashes
e := withSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{
"a.0.a": {""}, // blank
"a.0.b": {"Y"},
// a.1.a: required
"a.1.b": {"W"},
"a.2.a": {"V"}, // ok
})
assertEqual(t, e, ErrorHash{"a": ErrorSlice{ErrorHash{"a": ErrBlank}, ErrorHash{"a": ErrRequired}, nil}})
}
func TestSliceOfHashesLength(t *testing.T) {
type withRequiredSliceOfHashes struct {
A []struct {
Z String
} `meta_min_length:"2" meta_max_length:"4"`
}
withRequiredSliceOfHashesDecoder := NewDecoder(&withRequiredSliceOfHashes{})
// Valid length
inputs := withRequiredSliceOfHashes{}
e := withRequiredSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{
"a.0.z": {"A"},
"a.1.z": {"B"},
"a.2.z": {"C"},
})
assertEqual(t, e, ErrorHash(nil))
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeJSON(&inputs, []byte(`{
"a": [
{"z": "A"},
{"z": "B"},
{"z": "C"}
]
}`))
assertEqual(t, e, ErrorHash(nil))
// Too short
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{
"a.0.z": {"A"},
})
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeJSON(&inputs, []byte(`{
"a": [
{"z": "A"}
]
}`))
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeJSON(&inputs, []byte(`{
"a": [
{"z": "A"},
]
}`)) // comma
assertEqual(t, e, ErrorHash{"error": ErrMalformed})
// Too short
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{})
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeJSON(&inputs, []byte(`{}`))
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
// Too long
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeValues(&inputs, url.Values{
"a.0.z": {"A"},
"a.1.z": {"B"},
"a.2.z": {"C"},
"a.3.z": {"D"},
"a.4.z": {"E"},
})
assertEqual(t, e, ErrorHash{"a": ErrMaxLength})
inputs = withRequiredSliceOfHashes{}
e = withRequiredSliceOfHashesDecoder.DecodeJSON(&inputs, []byte(`{
"a": [
{"z": "A"},
{"z": "B"},
{"z": "C"},
{"z": "D"},
{"z": "E"}
]
}`))
assertEqual(t, e, ErrorHash{"a": ErrMaxLength})
}