-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
181 lines (154 loc) · 3.89 KB
/
parse_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
package ecp
import (
"reflect"
"testing"
"time"
)
func TestParseSlice(t *testing.T) {
type slices struct {
String []string
Bool []bool
Int []int
Int8 []int8
Int16 []int16
Int32 []int32
Int64 []int64
UInt []uint
UInt8 []uint8
UInt16 []uint16
UInt32 []uint32
UInt64 []uint64
Float32 []float32
Float64 []float64
Times []time.Duration
Unsupported []*string
Pointer *string
NotSlice string
}
var parseSlice = globalEcp.parseSlice
s := &slices{}
t.Run("test duration", func(t *testing.T) {
v := toValue(s).FieldByName("Times")
if err := parseSlice("1h 2m 3d", v); err != nil {
t.Errorf("parse time slice failed: %v", err)
} else if len(s.Times) != 3 || s.Times[0] != time.Hour {
t.Errorf("parse time slice failed: %v", s.Times)
}
})
t.Run("test string", func(t *testing.T) {
v := toValue(s).FieldByName("String")
if err := parseSlice("a b c", v); err != nil {
t.Errorf("parse string slice failed: %s", err)
} else if len(s.String) != 3 || s.String[1] != "b" {
t.Errorf("parse string slice failed: %v", s.String)
}
s = &slices{}
if parseSlice("", v) != nil || len(s.String) != 0 {
t.Errorf("test empty string failed: %v", s.String)
}
})
t.Run("test bool", func(t *testing.T) {
v := toValue(s).FieldByName("Bool")
if err := parseSlice("trUe tRue True", v); err != nil {
t.Errorf("parse bool slice failed: %s", err)
} else if len(s.Bool) != 3 && !s.Bool[1] {
t.Errorf("parse bool slice failed: %v", s.String)
}
if parseSlice("233", v) == nil {
t.Error("should not")
}
})
t.Run("test int", func(t *testing.T) {
var v reflect.Value
x := []string{"Int", "Int8", "Int16", "Int32", "Int64"}
for _, name := range x {
v = toValue(s).FieldByName(name)
if err := parseSlice("1 2 3", v); err != nil {
t.Errorf("parse int slice failed: %s", err)
}
}
switch {
case len(s.Int) != 3:
case len(s.Int8) != 3:
case len(s.Int16) != 3:
case len(s.Int32) != 3:
case len(s.Int64) != 3:
default:
for _, name := range x {
v = toValue(s).FieldByName(name)
if parseSlice("1.1", v) == nil {
t.Error("should not")
}
}
return
}
t.Errorf("parse int slice error")
})
t.Run("test uint", func(t *testing.T) {
var v reflect.Value
x := []string{"UInt", "UInt8", "UInt16", "UInt32", "UInt64"}
for _, name := range x {
v = toValue(s).FieldByName(name)
if err := parseSlice("1 2 3", v); err != nil {
t.Errorf("parse int slice failed: %s", err)
}
}
switch {
case len(s.UInt) != 3:
case len(s.UInt8) != 3:
case len(s.UInt16) != 3:
case len(s.UInt32) != 3:
case len(s.UInt64) != 3:
default:
for _, name := range x {
v = toValue(s).FieldByName(name)
if parseSlice("-1", v) == nil {
t.Error("should not")
}
}
return
}
t.Errorf("parse int slice error")
})
t.Run("test float", func(t *testing.T) {
var v reflect.Value
for _, name := range []string{"Float32", "Float64"} {
v = toValue(s).FieldByName(name)
if err := parseSlice("1.1 2.2 3.3", v); err != nil {
t.Errorf("parse int slice failed: %s", err)
}
}
switch {
case len(s.Float32) != 3:
case len(s.Float64) != 3:
default:
for _, name := range []string{"Float32", "Float64"} {
v = toValue(s).FieldByName(name)
if parseSlice("1,", v) == nil {
t.Error("should not")
}
}
return
}
t.Errorf("parse float slice error")
})
t.Run("test unsupported type", func(t *testing.T) {
v := toValue(s).FieldByName("Unsupported")
if err := parseSlice("a b c", v); err == nil {
t.Errorf("???")
}
})
t.Run("test error", func(t *testing.T) {
pointer := toValue(s).FieldByName("Pointer")
if parseSlice("a b c", pointer) == nil {
t.Errorf("???")
}
ns := toValue(s).FieldByName("NotSlice")
if parseSlice("a b c", ns) == nil {
t.Errorf("???")
}
if parseSlice("a b c", reflect.ValueOf(s.String)) == nil {
t.Errorf("???")
}
})
}