-
Notifications
You must be signed in to change notification settings - Fork 10
/
int_slice_test.go
121 lines (92 loc) · 3.75 KB
/
int_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
package meta
import (
"net/url"
"testing"
)
//
// Int
//
type withIntSlice struct {
A Int64Slice
}
var withIntSliceDecoder = NewDecoder(&withIntSlice{})
func TestIntSliceSuccess(t *testing.T) {
var inputs, inputs2 withIntSlice
e := withIntSliceDecoder.DecodeValues(&inputs, url.Values{"a": {"-1,8,3"}})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{-1, 8, 3})
e = withIntSliceDecoder.DecodeValues(&inputs2, url.Values{"a": {"5"}})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs2.A.Val, []int64{5})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":"-2,9,30"}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{-2, 9, 30})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":"6"}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{6})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":[-2,9,"30"]}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{-2, 9, 30})
e = withIntSliceDecoder.DecodeMap(&inputs, map[string]interface{}{"a": []interface{}{-2, 9}})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{-2, 9})
}
func TestIntSliceBlank(t *testing.T) {
var inputs withIntSlice
e := withIntSliceDecoder.DecodeValues(&inputs, url.Values{"a": {""}})
assertEqual(t, e, ErrorHash{"a": ErrBlank})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":""}`))
assertEqual(t, e, ErrorHash{"a": ErrBlank})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":null}`))
assertEqual(t, e, ErrorHash{"a": ErrBlank})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":[]}`))
assertEqual(t, e, ErrorHash{"a": ErrBlank})
}
func TestIntSliceInvalid(t *testing.T) {
var inputs withIntSlice
e := withIntSliceDecoder.DecodeValues(&inputs, url.Values{"a": {"1,b"}})
assertEqual(t, e, ErrorHash{"a": ErrorSlice{nil, ErrInt}})
e = withIntSliceDecoder.DecodeJSON(&inputs, []byte(`{"a":[1, true, false, {}, [0], "9", "bob"]}`))
assertEqual(t, e, ErrorHash{"a": ErrorSlice{nil, ErrInt, ErrInt, ErrInt, ErrInt, nil, ErrInt}})
}
func TestIntSliceMinMax(t *testing.T) {
var inputs struct {
A Int64Slice `meta_min:"-5" meta_max:"11"`
}
e := NewDecoder(&inputs).DecodeValues(&inputs, url.Values{"a": {"-6,4,17"}})
assertEqual(t, e, ErrorHash{"a": ErrorSlice{ErrMin, nil, ErrMax}})
e = NewDecoder(&inputs).DecodeJSON(&inputs, []byte(`{"a":[-1, -6, "-10", 12]}`))
assertEqual(t, e, ErrorHash{"a": ErrorSlice{nil, ErrMin, ErrMin, ErrMax}})
}
func TestIntSliceLength(t *testing.T) {
type boundLengthInput struct {
A Int64Slice `meta_min_length:"2" meta_max_length:"4"`
}
// Valid length
inputs := boundLengthInput{}
e := NewDecoder(&inputs).DecodeValues(&inputs, url.Values{"a": {"0,1,2"}})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{0, 1, 2})
inputs = boundLengthInput{}
e = NewDecoder(&inputs).DecodeJSON(&inputs, []byte(`{"a": [0, 1, 2]}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, []int64{0, 1, 2})
// Too short
inputs = boundLengthInput{}
e = NewDecoder(&inputs).DecodeValues(&inputs, url.Values{"a": {"0"}})
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
assertEqual(t, len(inputs.A.Val), 0)
inputs = boundLengthInput{}
e = NewDecoder(&inputs).DecodeJSON(&inputs, []byte(`{"a": [0]}`))
assertEqual(t, e, ErrorHash{"a": ErrMinLength})
assertEqual(t, len(inputs.A.Val), 0)
// Too long
inputs = boundLengthInput{}
e = NewDecoder(&inputs).DecodeValues(&inputs, url.Values{"a": {"0,1,2,3,4"}})
assertEqual(t, e, ErrorHash{"a": ErrMaxLength})
assertEqual(t, len(inputs.A.Val), 0)
inputs = boundLengthInput{}
e = NewDecoder(&inputs).DecodeJSON(&inputs, []byte(`{"a": [0,1,2,3,4]}`))
assertEqual(t, e, ErrorHash{"a": ErrMaxLength})
assertEqual(t, len(inputs.A.Val), 0)
}