-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_test.go
297 lines (268 loc) · 7.78 KB
/
struct_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
292
293
294
295
296
297
package gouse
import (
"fmt"
"reflect"
"testing"
)
type Person struct {
Name string
Age int
Address string
}
type Job struct {
Title string
}
type Developer struct {
Name string
Age int
Address string
Title string
}
func TestMergeStruct(t *testing.T) {
tests := []struct {
name string
input []interface{}
output interface{}
}{
{
name: "Merge Person and Job",
input: []interface{}{Person{Name: "John", Age: 25, Address: "123 Main St"}, Job{Title: "Developer"}},
output: map[string]interface{}{"Name": "John", "Age": 25, "Address": "123 Main St", "Title": "Developer"},
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := MergeStruct(test.input...)
fmt.Println(result)
fmt.Println(result.(map[string]interface{})["Name"])
if !reflect.DeepEqual(result, test.output) {
t.Errorf("Expected %+v, but got %+v", test.output, result)
}
})
}
}
func TestRemoveStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
fieldsToRemove []string
expectedResult interface{}
}{
{
name: "Remove Address field",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
fieldsToRemove: []string{"Address"},
expectedResult: struct {
Name string
Age int
}{Name: "John", Age: 25},
},
{
name: "Remove multiple fields",
inputStruct: Person{Name: "Jane", Age: 30, Address: "456 Side St"},
fieldsToRemove: []string{"Age", "Address"},
expectedResult: struct{ Name string }{Name: "Jane"},
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := RemoveStruct(test.inputStruct, test.fieldsToRemove...)
expectedResult := reflect.ValueOf(test.expectedResult).Interface()
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected %+v, but got %+v", expectedResult, result)
}
})
}
}
func TestAddStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
newFields map[string]interface{}
expectedResult interface{}
}{
{
name: "Add Email field",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
newFields: map[string]interface{}{"Email": "[email protected]"},
expectedResult: map[string]interface{}{"Name": "John", "Age": 25, "Address": "123 Main St", "Email": "[email protected]"},
},
{
name: "Add multiple fields",
inputStruct: Person{Name: "Jane", Age: 30, Address: "456 Side St"},
newFields: map[string]interface{}{"Email": "[email protected]", "Phone": "555-1234"},
expectedResult: map[string]interface{}{"Name": "Jane", "Age": 30, "Address": "456 Side St", "Email": "[email protected]", "Phone": "555-1234"},
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := AddStruct(test.inputStruct, test.newFields)
// Convert result and expected result to maps for comparison
resultMap := make(map[string]interface{})
for _, field := range reflect.VisibleFields(reflect.TypeOf(result)) {
resultMap[field.Name] = reflect.ValueOf(result).FieldByName(field.Name).Interface()
}
expectedResultMap := test.expectedResult.(map[string]interface{})
if !reflect.DeepEqual(resultMap, expectedResultMap) {
t.Errorf("Expected %+v, but got %+v", expectedResultMap, resultMap)
}
})
}
}
func TestSetStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
fieldName string
value interface{}
expectedResult interface{}
}{
{
name: "Set Name field",
inputStruct: &Person{Name: "John", Age: 25, Address: "123 Main St"},
fieldName: "Name",
value: "Jane",
expectedResult: &Person{Name: "Jane", Age: 25, Address: "123 Main St"},
},
{
name: "Set Age field",
inputStruct: &Person{Name: "Bob", Age: 30, Address: "456 Side St"},
fieldName: "Age",
value: 35,
expectedResult: &Person{Name: "Bob", Age: 35, Address: "456 Side St"},
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
SetStruct(test.inputStruct, test.fieldName, test.value)
result := test.inputStruct
if !reflect.DeepEqual(result, test.expectedResult) {
t.Errorf("Expected %+v, but got %+v", test.expectedResult, result)
}
})
}
}
func TestGetStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
fieldName string
expectedResult interface{}
}{
{
name: "Get Name field",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
fieldName: "Name",
expectedResult: "John",
},
{
name: "Get Age field",
inputStruct: Person{Name: "Bob", Age: 30, Address: "456 Side St"},
fieldName: "Age",
expectedResult: 30,
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := GetStruct(test.inputStruct, test.fieldName)
if !reflect.DeepEqual(result, test.expectedResult) {
t.Errorf("Expected %+v, but got %+v", test.expectedResult, result)
}
})
}
}
func TestCloneStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
expectedResult interface{}
}{
{
name: "Clone Person struct",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
expectedResult: Person{Name: "John", Age: 25, Address: "123 Main St"},
},
{
name: "Clone with different values",
inputStruct: Person{Name: "Alice", Age: 30, Address: "456 Side St"},
expectedResult: Person{Name: "Alice", Age: 30, Address: "456 Side St"},
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := CloneStruct(test.inputStruct)
expectedResult := test.expectedResult
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected %+v, but got %+v", expectedResult, result)
}
})
}
}
func TestHasInStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
fieldName string
expectedResult bool
}{
{
name: "Has Name field",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
fieldName: "Name",
expectedResult: true,
},
{
name: "Does not have Email field",
inputStruct: Person{Name: "Alice", Age: 30, Address: "456 Side St"},
fieldName: "Email",
expectedResult: false,
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := HasInStruct(test.inputStruct, test.fieldName)
expectedResult := test.expectedResult
if result != expectedResult {
t.Errorf("Expected %v, but got %v", expectedResult, result)
}
})
}
}
func TestHasEmptyInStruct(t *testing.T) {
tests := []struct {
name string
inputStruct interface{}
fieldName string
expectedResult bool
}{
{
name: "Name field is not empty",
inputStruct: Person{Name: "John", Age: 25, Address: "123 Main St"},
fieldName: "Name",
expectedResult: false,
},
{
name: "Email field is empty",
inputStruct: Person{Name: "Alice", Age: 30, Address: "456 Side St"},
fieldName: "Email",
expectedResult: true,
},
// Add more test cases as needed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := HasEmptyInStruct(test.inputStruct, test.fieldName)
expectedResult := test.expectedResult
if result != expectedResult {
t.Errorf("Expected %v, but got %v", expectedResult, result)
}
})
}
}