forked from jinzhu/copier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopier_test.go
342 lines (288 loc) · 9.72 KB
/
copier_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
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
package copier_test
import (
"errors"
"reflect"
"testing"
"time"
"github.com/jinzhu/copier"
)
type User struct {
Name string
Birthday *time.Time
Nickname string
Role string
Age int32
FakeAge *int32
Notes []string
flags []byte
}
func (user User) DoubleAge() int32 {
return 2 * user.Age
}
type Employee struct {
Name string
Birthday *time.Time
Nickname *string
Age int64
FakeAge int
EmployeID int64
DoubleAge int32
SuperRule string
Notes []string
flags []byte
}
func (employee *Employee) Role(role string) {
employee.SuperRule = "Super " + role
}
func checkEmployee(employee Employee, user User, t *testing.T, testCase string) {
if employee.Name != user.Name {
t.Errorf("%v: Name haven't been copied correctly.", testCase)
}
if employee.Nickname == nil || *employee.Nickname != user.Nickname {
t.Errorf("%v: NickName haven't been copied correctly.", testCase)
}
if employee.Birthday == nil && user.Birthday != nil {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if employee.Birthday != nil && user.Birthday == nil {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if employee.Birthday != nil && user.Birthday != nil &&
!employee.Birthday.Equal(*(user.Birthday)) {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if employee.Age != int64(user.Age) {
t.Errorf("%v: Age haven't been copied correctly.", testCase)
}
if user.FakeAge != nil && employee.FakeAge != int(*user.FakeAge) {
t.Errorf("%v: FakeAge haven't been copied correctly.", testCase)
}
if employee.DoubleAge != user.DoubleAge() {
t.Errorf("%v: Copy from method doesn't work", testCase)
}
if employee.SuperRule != "Super "+user.Role {
t.Errorf("%v: Copy to method doesn't work", testCase)
}
if !reflect.DeepEqual(employee.Notes, user.Notes) {
t.Errorf("%v: Copy from slice doen't work", testCase)
}
}
func TestCopySameStructWithPointerField(t *testing.T) {
var fakeAge int32 = 12
var currentTime time.Time = time.Now()
user := &User{Birthday: ¤tTime, Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
newUser := &User{}
copier.Copy(newUser, user)
if user.Birthday == newUser.Birthday {
t.Errorf("TestCopySameStructWithPointerField: copy Birthday failed since they need to have different address")
}
if user.FakeAge == newUser.FakeAge {
t.Errorf("TestCopySameStructWithPointerField: copy FakeAge failed since they need to have different address")
}
}
func checkEmployee2(employee Employee, user *User, t *testing.T, testCase string) {
if user == nil {
if employee.Name != "" || employee.Nickname != nil || employee.Birthday != nil || employee.Age != 0 ||
employee.DoubleAge != 0 || employee.FakeAge != 0 || employee.SuperRule != "" || employee.Notes != nil {
t.Errorf("%v : employee should be empty", testCase)
}
return
}
checkEmployee(employee, *user, t, testCase)
}
func TestCopyStruct(t *testing.T) {
var fakeAge int32 = 12
user := User{Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
employee := Employee{}
if err := copier.Copy(employee, &user); err == nil {
t.Errorf("Copy to unaddressable value should get error")
}
copier.Copy(&employee, &user)
checkEmployee(employee, user, t, "Copy From Ptr To Ptr")
employee2 := Employee{}
copier.Copy(&employee2, user)
checkEmployee(employee2, user, t, "Copy From Struct To Ptr")
employee3 := Employee{}
ptrToUser := &user
copier.Copy(&employee3, &ptrToUser)
checkEmployee(employee3, user, t, "Copy From Double Ptr To Ptr")
employee4 := &Employee{}
copier.Copy(&employee4, user)
checkEmployee(*employee4, user, t, "Copy From Ptr To Double Ptr")
}
func TestCopyFromStructToSlice(t *testing.T) {
user := User{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}
employees := []Employee{}
if err := copier.Copy(employees, &user); err != nil && len(employees) != 0 {
t.Errorf("Copy to unaddressable value should get error")
}
if copier.Copy(&employees, &user); len(employees) != 1 {
t.Errorf("Should only have one elem when copy struct to slice")
} else {
checkEmployee(employees[0], user, t, "Copy From Struct To Slice Ptr")
}
employees2 := &[]Employee{}
if copier.Copy(&employees2, user); len(*employees2) != 1 {
t.Errorf("Should only have one elem when copy struct to slice")
} else {
checkEmployee((*employees2)[0], user, t, "Copy From Struct To Double Slice Ptr")
}
employees3 := []*Employee{}
if copier.Copy(&employees3, user); len(employees3) != 1 {
t.Errorf("Should only have one elem when copy struct to slice")
} else {
checkEmployee(*(employees3[0]), user, t, "Copy From Struct To Ptr Slice Ptr")
}
employees4 := &[]*Employee{}
if copier.Copy(&employees4, user); len(*employees4) != 1 {
t.Errorf("Should only have one elem when copy struct to slice")
} else {
checkEmployee(*((*employees4)[0]), user, t, "Copy From Struct To Double Ptr Slice Ptr")
}
}
func TestCopyFromSliceToSlice(t *testing.T) {
users := []User{User{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}, User{Name: "Jinzhu2", Age: 22, Role: "Dev", Notes: []string{"hello world", "hello"}}}
employees := []Employee{}
if copier.Copy(&employees, users); len(employees) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee(employees[0], users[0], t, "Copy From Slice To Slice Ptr @ 1")
checkEmployee(employees[1], users[1], t, "Copy From Slice To Slice Ptr @ 2")
}
employees2 := &[]Employee{}
if copier.Copy(&employees2, &users); len(*employees2) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee((*employees2)[0], users[0], t, "Copy From Slice Ptr To Double Slice Ptr @ 1")
checkEmployee((*employees2)[1], users[1], t, "Copy From Slice Ptr To Double Slice Ptr @ 2")
}
employees3 := []*Employee{}
if copier.Copy(&employees3, users); len(employees3) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee(*(employees3[0]), users[0], t, "Copy From Slice To Ptr Slice Ptr @ 1")
checkEmployee(*(employees3[1]), users[1], t, "Copy From Slice To Ptr Slice Ptr @ 2")
}
employees4 := &[]*Employee{}
if copier.Copy(&employees4, users); len(*employees4) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee(*((*employees4)[0]), users[0], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 1")
checkEmployee(*((*employees4)[1]), users[1], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 2")
}
}
func TestCopyFromSliceToSlice2(t *testing.T) {
users := []*User{{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}, nil}
employees := []Employee{}
if copier.Copy(&employees, users); len(employees) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee2(employees[0], users[0], t, "Copy From Slice To Slice Ptr @ 1")
checkEmployee2(employees[1], users[1], t, "Copy From Slice To Slice Ptr @ 2")
}
employees2 := &[]Employee{}
if copier.Copy(&employees2, &users); len(*employees2) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee2((*employees2)[0], users[0], t, "Copy From Slice Ptr To Double Slice Ptr @ 1")
checkEmployee2((*employees2)[1], users[1], t, "Copy From Slice Ptr To Double Slice Ptr @ 2")
}
employees3 := []*Employee{}
if copier.Copy(&employees3, users); len(employees3) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee2(*(employees3[0]), users[0], t, "Copy From Slice To Ptr Slice Ptr @ 1")
checkEmployee2(*(employees3[1]), users[1], t, "Copy From Slice To Ptr Slice Ptr @ 2")
}
employees4 := &[]*Employee{}
if copier.Copy(&employees4, users); len(*employees4) != 2 {
t.Errorf("Should have two elems when copy slice to slice")
} else {
checkEmployee2(*((*employees4)[0]), users[0], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 1")
checkEmployee2(*((*employees4)[1]), users[1], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 2")
}
}
func TestEmbeddedAndBase(t *testing.T) {
type Base struct {
BaseField1 int
BaseField2 int
User *User
}
type Embed struct {
EmbedField1 int
EmbedField2 int
Base
}
base := Base{}
embeded := Embed{}
embeded.BaseField1 = 1
embeded.BaseField2 = 2
embeded.EmbedField1 = 3
embeded.EmbedField2 = 4
user:=User{
Name:"testName",
}
embeded.User=&user
copier.Copy(&base, &embeded)
if base.BaseField1 != 1 || base.User.Name!="testName"{
t.Error("Embedded fields not copied")
}
base.BaseField1=11
base.BaseField2=12
user1:=User{
Name:"testName1",
}
base.User=&user1
copier.Copy(&embeded,&base)
if embeded.BaseField1 != 11 || embeded.User.Name!="testName1" {
t.Error("base fields not copied")
}
}
type structSameName1 struct {
A string
B int64
C time.Time
}
type structSameName2 struct {
A string
B time.Time
C int64
}
func TestCopyFieldsWithSameNameButDifferentTypes(t *testing.T) {
obj1 := structSameName1{A: "123", B: 2, C: time.Now()}
obj2 := &structSameName2{}
err := copier.Copy(obj2, &obj1)
if err != nil {
t.Error("Should not raise error")
}
if obj2.A != obj1.A {
t.Errorf("Field A should be copied")
}
}
type ScannerValue struct {
V int
}
func (s *ScannerValue) Scan(src interface{}) error {
return errors.New("I failed")
}
type ScannerStruct struct {
V *ScannerValue
}
type ScannerStructTo struct {
V *ScannerValue
}
func TestScanner(t *testing.T) {
s := &ScannerStruct{
V: &ScannerValue{
V: 12,
},
}
s2 := &ScannerStructTo{}
err := copier.Copy(s2, s)
if err != nil {
t.Error("Should not raise error")
}
if s.V.V != s2.V.V {
t.Errorf("Field V should be copied")
}
}