-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparser_test.go
More file actions
409 lines (348 loc) · 9.09 KB
/
parser_test.go
File metadata and controls
409 lines (348 loc) · 9.09 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package urlquery
import (
"errors"
"reflect"
"strconv"
"strings"
"testing"
)
type testParseChild struct {
Description string `query:"desc"`
Long uint16 `query:",vip"`
Height int `query:"-"`
}
type testParseInfo struct {
Id int
Name string `query:"name"`
Child testParseChild `query:"child"`
ChildPtr *testParseChild `query:"childPtr"`
Children []testParseChild `query:"children"`
Params map[byte]int8
status bool
UintPtr uintptr
Tags []int16 `query:"tags"`
Int64 int64
Uint uint
Uint32 uint32
Float32 float32
Float64 float64
Bool bool
Inter interface{} `query:"inter"`
}
type errorQueryEncoder struct {
times int
errorAt int
}
func (q *errorQueryEncoder) Escape(s string) string {
return s
}
func (q *errorQueryEncoder) UnEscape(s string) (string, error) {
q.times++
if q.times >= q.errorAt {
return "", errors.New("failed")
}
return s, nil
}
func TestParser_Unmarshal_DuplicateCall(t *testing.T) {
parser := NewParser()
d1 := "desc=bb&Long=200"
v1 := &testParseChild{}
_ = parser.Unmarshal([]byte(d1), v1)
d2 := "desc=a&Long=100"
v2 := &testParseChild{}
err := parser.Unmarshal([]byte(d2), v2)
if err != nil {
t.Error(err)
}
if v2.Description != "a" || v2.Long != 100 {
t.Error("failed to Unmarshal duplicate call")
}
}
func TestParser_Unmarshal_NestedStructure(t *testing.T) {
var data = "Id=1&name=test&child[desc]=c1&child[Long]=10&childPtr[Long]=2&childPtr[Description]=b" +
"&children[0][desc]=d1&children[1][Long]=12&children[5][desc]=d5&children[5][Long]=50&desc=rtt" +
"&Params[120]=1&Params[121]=2&status=1&UintPtr=300&tags[]=1&tags[]=2&Int64=64&Uint=22&Uint32=5&Float32=1.3" +
"&Float64=5.64&Bool=0&inter=ss"
data = encodeSquareBracket(data)
v := &testParseInfo{}
err := Unmarshal([]byte(data), v)
if err != nil {
t.Error(err)
}
if v.Id != 1 {
t.Error("Id wrong")
}
if v.Name != "test" {
t.Error("Name wrong")
}
if v.Child.Description != "c1" || v.Child.Long != 10 || v.Child.Height != 0 {
t.Error("Child wrong")
}
if v.ChildPtr == nil || v.ChildPtr.Description != "" || v.ChildPtr.Long != 2 || v.ChildPtr.Height != 0 {
t.Error("ChildPtr wrong")
}
if len(v.Children) != 6 {
t.Error("Children's length is wrong")
}
if v.Children[0].Description != "d1" {
t.Error("Children[0] wrong")
}
if v.Children[1].Description != "" || v.Children[1].Long != 12 {
t.Error("Children[1] wrong")
}
if v.Children[2].Description != "" || v.Children[3].Description != "" || v.Children[4].Description != "" {
t.Error("Children[2,3,4] wrong")
}
if v.Children[5].Description != "d5" || v.Children[5].Long != 50 || v.Children[5].Height != 0 {
t.Error("Children[5] wrong")
}
if len(v.Params) != 2 || v.Params[120] != 1 || v.Params[121] != 2 {
t.Error("Params wrong")
}
if v.status != false {
t.Error("status wrong")
}
if v.UintPtr != uintptr(300) {
t.Error("UintPtr wrong")
}
if len(v.Tags) != 2 {
t.Error("Tags wrong")
}
}
func TestParser_Unmarshal_Map(t *testing.T) {
var m map[string]string
data := "id=1&name=ab&arr[0]=6d"
data = encodeSquareBracket(data)
err := Unmarshal([]byte(data), &m)
if err != nil {
t.Error(err)
}
if len(m) != 2 {
t.Error("length is wrong")
}
if v1, ok1 := m["id"]; v1 != "1" || !ok1 {
t.Error("map[id] is wrong")
}
if v2, ok2 := m["name"]; v2 != "ab" || !ok2 {
t.Error("map[iname] is wrong")
}
if _, ok3 := m["arr%5B0%5D"]; ok3 {
t.Error("map[arr%5B0%5D] should not be exist")
}
}
func TestParser_Unmarshal_Slice(t *testing.T) {
var slice []int
slice = make([]int, 0)
data := "1=20&3=30"
err := Unmarshal([]byte(data), &slice)
if err != nil {
t.Error(err)
}
if len(slice) != 4 {
t.Error("failed to Unmarshal slice")
}
}
func TestParser_Unmarshal_Array(t *testing.T) {
var arr [5]int
data := "1=20&3=30"
err := Unmarshal([]byte(data), &arr)
if err != nil {
t.Error(err)
}
if arr[1] != 20 || arr[3] != 30 || arr[0] != 0 {
t.Error("failed to Unmarshal array")
}
}
func TestParser_Unmarshal_Array_Failed(t *testing.T) {
var arr [5]int
data := "1=20&3=s"
err := Unmarshal([]byte(data), &arr)
if err == nil {
t.Error("dont return error")
}
}
type testParserPoint struct {
X, Y int
}
type testParserCircle struct {
testParserPoint
R int
}
func TestParser_Unmarshal_AnonymousFields(t *testing.T) {
v := &testParserCircle{}
data := "X=12&Y=13&R=1"
err := Unmarshal([]byte(data), &v)
if err != nil {
t.Error(err)
}
if v.X != 12 || v.Y != 13 || v.R != 1 {
t.Error("failed to Unmarshal anonymous fields")
}
}
type testFormat struct {
Id uint64
B rune `query:"b"`
}
func TestParser_Unmarshal_UnmatchedDataFormat(t *testing.T) {
var data = "Id=1&b=a"
data = encodeSquareBracket(data)
v := &testFormat{}
err := Unmarshal([]byte(data), v)
if err == nil {
t.Error("error should not be ignored")
}
if _, ok := err.(ErrTranslated); !ok {
t.Errorf("error type is unexpected. %v", err)
}
}
func TestParser_Unmarshal_UnhandledType(t *testing.T) {
var data = "Id=1&b=a"
data = encodeSquareBracket(data)
v := &map[interface{}]string{}
err := Unmarshal([]byte(data), v)
if err == nil {
t.Error("error should not be ignored")
}
if _, ok := err.(ErrInvalidMapKeyType); !ok {
t.Errorf("error type is unexpected. %v", err)
}
}
type TestUnhandled struct {
Id int
Params map[string]testFormat
}
func TestParser_Unmarshal_UnhandledType2(t *testing.T) {
var data = "Id=1&b=a"
data = encodeSquareBracket(data)
v := &TestUnhandled{}
parser := NewParser(WithQueryEncoder(defaultQueryEncoder))
err := parser.Unmarshal([]byte(data), v)
if err == nil {
t.Error("error should not be ignored")
}
if _, ok := err.(ErrInvalidMapKeyType); !ok {
t.Errorf("error type is unexpected. %v", err)
}
}
func TestParser_init(t *testing.T) {
query := &errorQueryEncoder{errorAt: 1}
parser := NewParser(WithQueryEncoder(query))
parser.resetQueryEncoder()
var data = "Id=1&b=a"
err := parser.init([]byte(data))
if err == nil || err.Error() != "failed" {
t.Error("init error")
}
}
func TestParser_Unmarshal_InitError(t *testing.T) {
query := &errorQueryEncoder{errorAt: 2}
parser := NewParser(WithQueryEncoder(query))
v := &TestUnhandled{}
var data = "Id=1&b=a"
err := parser.Unmarshal([]byte(data), v)
if err == nil || err.Error() != "failed" {
t.Error("init error")
}
}
func TestParser_Unmarshal_NonPointer(t *testing.T) {
parser := NewParser()
var data = "Id=1&b=a"
v := TestUnhandled{}
err := parser.Unmarshal([]byte(data), v)
if _, ok := err.(ErrInvalidUnmarshalError); !ok {
t.Error("unmatched error")
}
}
func TestParser_Unmarshal_MapKey_DecodeError(t *testing.T) {
parser := NewParser()
parser.RegisterDecodeFunc(reflect.String, nil)
var data = "Id=1&b=2"
v := &map[string]int{}
err := parser.Unmarshal([]byte(data), v)
if _, ok := err.(ErrUnhandledType); !ok {
t.Error("unmatched error")
}
}
func TestParser_Unmarshal_MapValue_DecodeError(t *testing.T) {
parser := NewParser()
parser.RegisterDecodeFunc(reflect.Int, nil)
var data = "Id=1&b=2"
v := &map[string]int{}
err := parser.Unmarshal([]byte(data), v)
if _, ok := err.(ErrUnhandledType); !ok {
t.Error("unmatched error")
}
}
func TestParser_RegisterDecodeFunc(t *testing.T) {
parser := NewParser()
parser.RegisterDecodeFunc(reflect.String, func(s string) (reflect.Value, error) {
return reflect.ValueOf("11"), nil
})
f := parser.getDecodeFunc(reflect.String)
v, _ := f("bb")
if v.String() != "11" {
t.Error("failed to RegisterDecodeFunc")
}
}
func TestParser_lookupForSlice(t *testing.T) {
var data = "Tags[s]=1&Tags[]=2"
data = encodeSquareBracket(data)
v := &struct {
Tags []int
}{}
err := Unmarshal([]byte(data), v)
if _, ok := err.(*strconv.NumError); !ok {
t.Error("dont failed for wrong slice data")
}
}
func TestParser_SliceEmpty(t *testing.T) {
var data = ""
data = encodeSquareBracket(data)
v := &struct {
Tags []int
}{}
_ = Unmarshal([]byte(data), v)
if len(v.Tags) != 0 {
t.Error("not empty slice")
}
}
func TestParser_decode_UnhandledType(t *testing.T) {
parser := NewParser()
parser.RegisterDecodeFunc(reflect.String, nil)
_, err := parser.decode(reflect.TypeOf(""), "s")
if _, ok := err.(ErrUnhandledType); !ok {
t.Error("unmatched error")
}
}
func TestParser_parseForMap_CanSet(t *testing.T) {
var x = 3.4
v := reflect.ValueOf(x)
parser := NewParser()
parser.parseForMap(v, "")
}
func TestParser_parseForSlice_CanSet(t *testing.T) {
var x = 3.4
v := reflect.ValueOf(x)
parser := NewParser()
parser.parseForSlice(v, "")
}
//mock multi-layer nested structure,
//BenchmarkUnmarshal-4 208219 14873 ns/op
func BenchmarkUnmarshal(b *testing.B) {
var data = "Id=1&name=test&child[desc]=c1&child[Long]=10&childPtr[Long]=2&childPtr[Description]=b" +
"&children[0][desc]=d1&children[1][Long]=12&children[5][desc]=d5&children[5][Long]=50&desc=rtt" +
"&Params[120]=1&Params[121]=2&status=1&UintPtr=300"
data = encodeSquareBracket(data)
for i := 0; i < b.N; i++ {
v := &testParseInfo{}
err := Unmarshal([]byte(data), v)
if err != nil {
b.Error(err)
}
}
}
func encodeSquareBracket(data string) string {
data = strings.ReplaceAll(data, "[", "%5B")
data = strings.ReplaceAll(data, "]", "%5D")
return data
}