-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdecode_test.go
214 lines (182 loc) · 4.51 KB
/
decode_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
package qstring
import (
"errors"
"net/url"
"testing"
"time"
)
type TestStruct struct {
Name string `qstring:"name"`
Do bool
// int fields
Page int `qstring:"page"`
ID int8
Small int16
Med int32
Big int64
// uint fields
UPage uint
UID uint8
USmall uint16
UMed uint32
UBig uint64
// Floats
Float32 float32
Float64 float64
// slice fields
Fields []string `qstring:"fields"`
DoFields []bool `qstring:"dofields"`
Counts []int
IDs []int8
Smalls []int16
Meds []int32
Bigs []int64
// uint fields
UPages []uint
UIDs []uint8
USmalls []uint16
UMeds []uint32
UBigs []uint64
// Floats
Float32s []float32
Float64s []float64
hidden int
Hidden int `qstring:"-"`
}
func TestUnmarshall(t *testing.T) {
var ts TestStruct
query := url.Values{
"name": []string{"SomeName"},
"do": []string{"true"},
"page": []string{"1"},
"id": []string{"12"},
"small": []string{"13"},
"med": []string{"14"},
"big": []string{"15"},
"upage": []string{"2"},
"uid": []string{"16"},
"usmall": []string{"17"},
"umed": []string{"18"},
"ubig": []string{"19"},
"float32": []string{"6000"},
"float64": []string{"7000"},
"fields": []string{"foo", "bar"},
"dofields": []string{"true", "false"},
"counts": []string{"1", "2"},
"ids": []string{"3", "4", "5"},
"smalls": []string{"6", "7", "8"},
"meds": []string{"9", "10", "11"},
"bigs": []string{"12", "13", "14"},
"upages": []string{"2", "3", "4"},
"uids": []string{"5", "6", "7"},
"usmalls": []string{"8", "9", "10"},
"umeds": []string{"9", "10", "11"},
"ubigs": []string{"12", "13", "14"},
"float32s": []string{"6000", "6001", "6002"},
"float64s": []string{"7000", "7001", "7002"},
}
err := Unmarshal(query, &ts)
if err != nil {
t.Fatal(err.Error())
}
if ts.Page != 1 {
t.Errorf("Expected page to be 1, got %d", ts.Page)
}
if len(ts.Fields) != 2 {
t.Errorf("Expected 2 fields, got %d", len(ts.Fields))
}
}
func TestUnmarshalNested(t *testing.T) {
type Paging struct {
Page int
Limit int
}
type Params struct {
Paging Paging
Name string
}
query := url.Values{
"name": []string{"SomeName"},
"page": []string{"1"},
"limit": []string{"50"},
}
params := &Params{}
err := Unmarshal(query, params)
if err != nil {
t.Fatal(err.Error())
}
if params.Paging.Page != 1 {
t.Errorf("Nested Struct Failed to Unmarshal. Expected 1, got %d", params.Paging.Page)
}
}
func TestUnmarshalTime(t *testing.T) {
type Query struct {
Created time.Time
LastUpdated time.Time
}
createdTS := "2006-01-02T15:04:05Z"
updatedTS := "2016-01-02T15:04:05-07:00"
query := url.Values{
"created": []string{createdTS},
"lastupdated": []string{updatedTS},
}
params := &Query{}
err := Unmarshal(query, params)
if err != nil {
t.Fatal(err.Error())
}
if params.Created.Format(time.RFC3339) != createdTS {
t.Errorf("Expected created ts of %s, got %s instead.", createdTS, params.Created.Format(time.RFC3339))
}
if params.LastUpdated.Format(time.RFC3339) != updatedTS {
t.Errorf("Expected update ts of %s, got %s instead.", updatedTS, params.LastUpdated.Format(time.RFC3339))
}
}
func TestUnmarshalInvalidTypes(t *testing.T) {
var err error
var ts *TestStruct
testio := []struct {
inp interface{}
errString string
}{
{inp: nil, errString: "qstring: Unmarshal(nil)"},
{inp: TestStruct{}, errString: "qstring: Unmarshal(non-pointer qstring.TestStruct)"},
{inp: ts, errString: "qstring: Unmarshal(nil *qstring.TestStruct)"},
}
for _, test := range testio {
err = Unmarshal(url.Values{}, test.inp)
if err == nil {
t.Errorf("Expected invalid type error, got success instead")
}
if err.Error() != test.errString {
t.Errorf("Got %q error, expected %q", err.Error(), test.errString)
}
}
}
var errNoNames = errors.New("No Names Provided")
type MarshalInterfaceTest struct {
Names []string
}
func (u *MarshalInterfaceTest) UnmarshalQuery(v url.Values) error {
var ok bool
if u.Names, ok = v["names"]; ok {
return nil
}
return errNoNames
}
func TestUnmarshaller(t *testing.T) {
testIO := []struct {
inp url.Values
expected interface{}
}{
{url.Values{"names": []string{"foo", "bar"}}, nil},
{make(url.Values), errNoNames},
}
s := &MarshalInterfaceTest{Names: []string{}}
for _, test := range testIO {
err := Unmarshal(test.inp, s)
if err != test.expected {
t.Errorf("Expected Unmarshaller to return %s, but got %s instead", test.expected, err)
}
}
}