forked from jinzhu/copier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copier_different_type_test.go
152 lines (127 loc) · 2.79 KB
/
copier_different_type_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
package copier_test
import (
"testing"
"github.com/jinzhu/copier"
)
type TypeStruct1 struct {
Field1 string
Field2 string
Field3 TypeStruct2
Field4 *TypeStruct2
Field5 []*TypeStruct2
Field6 []TypeStruct2
Field7 []*TypeStruct2
Field8 []TypeStruct2
Field9 []string
}
type TypeStruct2 struct {
Field1 int
Field2 string
Field3 []TypeStruct2
Field4 *TypeStruct2
Field5 *TypeStruct2
Field9 string
}
type TypeStruct3 struct {
Field1 interface{}
Field2 string
Field3 TypeStruct4
Field4 *TypeStruct4
Field5 []*TypeStruct4
Field6 []*TypeStruct4
Field7 []TypeStruct4
Field8 []TypeStruct4
}
type TypeStruct4 struct {
field1 int
Field2 string
}
func (t *TypeStruct4) Field1(i int) {
t.field1 = i
}
func TestCopyDifferentFieldType(t *testing.T) {
ts := &TypeStruct1{
Field1: "str1",
Field2: "str2",
}
ts2 := &TypeStruct2{}
copier.Copy(ts2, ts)
if ts2.Field2 != ts.Field2 || ts2.Field1 != 0 {
t.Errorf("Should be able to copy from ts to ts2")
}
}
func TestCopyDifferentTypeMethod(t *testing.T) {
ts := &TypeStruct1{
Field1: "str1",
Field2: "str2",
}
ts4 := &TypeStruct4{}
copier.Copy(ts4, ts)
if ts4.Field2 != ts.Field2 || ts4.field1 != 0 {
t.Errorf("Should be able to copy from ts to ts4")
}
}
func TestAssignableType(t *testing.T) {
ts := &TypeStruct1{
Field1: "str1",
Field2: "str2",
Field3: TypeStruct2{
Field1: 666,
Field2: "str2",
},
Field4: &TypeStruct2{
Field1: 666,
Field2: "str2",
},
Field5: []*TypeStruct2{
{
Field1: 666,
Field2: "str2",
},
},
Field6: []TypeStruct2{
{
Field1: 666,
Field2: "str2",
},
},
Field7: []*TypeStruct2{
{
Field1: 666,
Field2: "str2",
},
},
}
ts3 := &TypeStruct3{}
copier.Copy(&ts3, &ts)
if v, ok := ts3.Field1.(string); !ok {
t.Error("Assign to interface{} type did not succeed")
} else if v != "str1" {
t.Error("String haven't been copied correctly")
}
if ts3.Field2 != ts.Field2 {
t.Errorf("Field2 should be copied")
}
checkType2WithType4(ts.Field3, ts3.Field3, t, "Field3")
checkType2WithType4(*ts.Field4, *ts3.Field4, t, "Field4")
if len(ts3.Field5) != len(ts.Field5) {
t.Fatalf("fields not equal, got %v, expects: %v", len(ts3.Field5), len(ts.Field5))
}
for idx, f := range ts.Field5 {
checkType2WithType4(*f, *(ts3.Field5[idx]), t, "Field5")
}
for idx, f := range ts.Field6 {
checkType2WithType4(f, *(ts3.Field6[idx]), t, "Field6")
}
for idx, f := range ts.Field7 {
checkType2WithType4(*f, ts3.Field7[idx], t, "Field7")
}
for idx, f := range ts.Field8 {
checkType2WithType4(f, ts3.Field8[idx], t, "Field8")
}
}
func checkType2WithType4(t2 TypeStruct2, t4 TypeStruct4, t *testing.T, testCase string) {
if t2.Field1 != t4.field1 || t2.Field2 != t4.Field2 {
t.Errorf("%v: type struct 4 and type struct 2 is not equal", testCase)
}
}