-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
116 lines (108 loc) · 2.05 KB
/
decoder_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
package data
import (
"reflect"
"strings"
"testing"
"time"
"github.com/huandu/go-assert"
)
func TestDecode(t *testing.T) {
cases := []struct {
Fields []string
Data Data
AllValue interface{}
Value interface{}
}{
{ // 空数据
nil,
emptyData,
(*AllValue)(nil),
(*AllValue)(nil),
},
{ // 简单数据
nil,
Make(RawData{
"int": 123,
}),
&AllValue{
Int: 123,
SquashType: &SquashType{},
},
&AllValue{
Int: 123,
SquashType: &SquashType{},
},
},
{ // 数字兼容性测试
nil,
Make(RawData{
"int": uint(345),
"uint32": int64(567),
"uint64": 123456.,
"duration": "13m20s",
}),
&AllValue{
Int: 345,
Duration: 13*time.Minute + 20*time.Second,
SquashType: &SquashType{
Uint32: 567,
Uint64: 123456,
},
},
&AllValue{
Int: 345,
Duration: 13*time.Minute + 20*time.Second,
SquashType: &SquashType{
Uint32: 567,
Uint64: 123456,
},
},
},
{ // 测试所有类型
nil,
fullData,
allValues,
allValues,
},
{ // 测试 query
[]string{"sub_type", "int64"},
fullData,
allValues,
int64(-64),
},
{ // 测试错误的 query
[]string{"fake", "query"},
fullData,
allValues,
float64(0),
},
}
a := assert.New(t)
dec := &Decoder{
TagName: "test",
}
for i, c := range cases {
a.Use(&i, &c)
query := strings.Join(c.Fields, ".")
avt := reflect.ValueOf(c.AllValue).Type()
vt := reflect.ValueOf(c.Value).Type()
{
expected := c.AllValue
actual := reflect.New(avt).Elem()
a.NilError(dec.Decode(c.Data, actual.Addr().Interface()))
a.Equal(expected, actual.Interface())
}
{
expected := c.Value
actual := reflect.New(vt).Elem()
a.NilError(dec.DecodeField(c.Data, c.Fields, actual.Addr().Interface()))
a.Equal(expected, actual.Interface())
}
{
expected := c.Value
actual := reflect.New(vt).Elem()
a.NilError(dec.DecodeQuery(c.Data, query, actual.Addr().Interface()))
a.Equal(expected, actual.Interface())
}
}
}