-
Notifications
You must be signed in to change notification settings - Fork 3
/
map_slice_test.go
291 lines (258 loc) · 5.24 KB
/
map_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
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
package reflectutils_test
import (
"fmt"
"testing"
. "github.com/sunfmin/reflectutils"
)
type mapTest struct {
Name string
Value string
Getter func(db map[string][]map[string]string) string
}
var mapcases = []*mapTest{
{
Name: "users[0].gender",
Value: "male",
Getter: func(db map[string][]map[string]string) string {
return db["users"][0]["gender"]
},
},
{
Name: "users[1].gender",
Value: "female",
Getter: func(db map[string][]map[string]string) string {
return db["users"][1]["gender"]
},
},
{
Name: "companies[].name",
Value: "The Plant",
Getter: func(db map[string][]map[string]string) string {
return db["companies"][0]["name"]
},
},
{
Name: "companies[].name",
Value: "AQ",
Getter: func(db map[string][]map[string]string) string {
return db["companies"][1]["name"]
},
},
}
func TestSetMapAndSlice(t *testing.T) {
var db map[string][]map[string]string
for _, c := range mapcases {
err := Set(&db, c.Name, c.Value)
if err != nil {
t.Error(err)
}
val := c.Getter(db)
if val != c.Value {
t.Errorf("expected is %v, but was %v", c.Value, val)
}
}
}
func TestSlices(t *testing.T) {
var val []string
Set(&val, "[100]", "100")
if val[0] != "" {
t.Error("val[0] is not empty")
}
if val[100] != "100" {
t.Error("val[100] is not 100")
}
}
func TestMapSetNil(t *testing.T) {
m := make(map[string]int)
Set(&m, "", nil)
if m != nil {
t.Errorf("got non-nil (%p), want nil", m)
}
}
func TestSliceSetNil(t *testing.T) {
m := []int{1}
err := Set(&m, "", nil)
if err != nil {
panic(err)
}
if m != nil {
t.Errorf("got non-nil (%p), want nil", m)
}
}
func TestStructSetNil(t *testing.T) {
type S struct {
Val string
}
m := &S{Val: "123"}
err := Set(&m, "", nil)
if err != nil {
panic(err)
}
if m != nil {
t.Errorf("got non-nil (%#+v), want nil", m)
}
}
func TestSetFieldNil(t *testing.T) {
type S struct {
Value []int
}
var s = &S{
Value: []int{1, 2},
}
err := Set(s, "Value", nil)
if err != nil {
panic(err)
}
if s.Value != nil {
panic("s.Value is not nil")
}
}
func TestDelete(t *testing.T) {
p := &Person{
Departments: []*Department{
{
Name: "D1",
},
{
Name: "D2",
},
{
Name: "D3",
},
},
Phones: map[string]string{
"felix": "123",
"john": "456",
},
Projects: []*Project{
{
Name: "Project1",
Members: []*Person{
{
Name: "Felix1",
},
{
Name: "Felix2",
},
},
},
},
}
var strs = []string{"1", "2", "3"}
var map1 = map[string]string{"a": "a1", "b": "b1"}
var map2 = &map1
var p2 = &Person{}
var deleteCases = []struct {
caseName string
obj interface{}
name string
result func(obj interface{}) (v interface{})
expected string
expectedErr string
}{
{
caseName: "Delete map element in object",
obj: p,
name: "Phones[felix]",
result: func(obj interface{}) (v interface{}) {
return obj.(*Person).Phones
},
expected: "map[john:456]",
},
{
caseName: "Delete slice element in object",
obj: p,
name: "Departments[1]",
result: func(obj interface{}) (v interface{}) {
return obj.(*Person).Departments[1]
},
expected: "&{Id:0 Name:D3}",
},
{
caseName: "Delete slice element in object nested",
obj: p,
name: "Projects[0].Members[0]",
result: func(obj interface{}) (v interface{}) {
return obj.(*Person).Projects[0].Members[0].Name + obj.(*Person).Projects[0].Name
},
expected: "Felix2Project1",
},
{
caseName: "Delete slice element in object overflow",
obj: p,
name: "Departments[100]",
result: func(obj interface{}) (v interface{}) {
return obj.(*Person).Departments[1]
},
expected: "&{Id:0 Name:D3}",
},
{
caseName: "Delete struct with error",
obj: p,
name: "Comp21",
expectedErr: "no such field",
},
{
caseName: "Delete struct",
obj: p,
name: "Departments[0].Name",
result: func(obj interface{}) (v interface{}) {
return obj.(*Person).Departments[0].Name
},
expected: "",
},
{
caseName: "Delete with wrong index",
obj: p,
name: "Departments[abc].Name",
expectedErr: "no such field",
},
{
caseName: "Delete slice element",
obj: &strs,
name: "[1]",
result: func(obj interface{}) (v interface{}) {
return strs
},
expected: `[1 3]`,
},
{
caseName: "Delete map element",
obj: &map2,
name: "[a]",
result: func(obj interface{}) (v interface{}) {
return map1
},
expected: `map[b:b1]`,
},
{
caseName: "Delete struct whole",
obj: &p2,
name: "",
result: func(obj interface{}) (v interface{}) {
return p2
},
expected: `<nil>`,
},
}
for _, c := range deleteCases {
t.Run(c.caseName, func(t *testing.T) {
err := Delete(c.obj, c.name)
if c.expectedErr != "" {
if err == nil || err.Error() != c.expectedErr {
t.Errorf("expected error %s, but was %+v", c.expectedErr, err)
}
} else {
if err != nil {
t.Error(err)
}
}
if c.result != nil {
actual := fmt.Sprintf("%+v", c.result(c.obj))
if actual != c.expected {
t.Errorf("expected %s, but was %s", c.expected, actual)
}
}
})
}
}