-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepcopy_test.go
246 lines (213 loc) · 3.82 KB
/
deepcopy_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
package deepcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
type srcStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
Method int
}
type srcStruct1 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
II []int
UU []uint
SS []string
V srcStruct2
VV []srcStruct2
M1 map[int]string
M2 *map[int8]int8
M3 map[int]int
M4 map[[3]int]*srcStruct2
P2V *int
V2P int
S2A []string
MatchedX string `copy:"Matched"`
UnmatchedX string `copy:"UnmatchedX"`
}
type dstStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
MethodVal int
}
func (d *dstStruct2) CopyMethod(v int) error {
d.MethodVal = v * 2
return nil
}
type dstStruct1 struct {
S StrT
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B *bool
II []int
UU []uint
SS []*StrT
V dstStruct2
VV []dstStruct2
M1 map[int]string
M2 map[int8]int8
M3 *map[int]IntT
M4 map[[3]int]*dstStruct2
P2V int
V2P *int
S2A [3]string
MatchedY string `copy:"Matched"`
UnmatchedY string `copy:"UnmatchedY"`
}
var (
srcStructA = srcStruct2{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,
Method: 1234,
}
srcStructB = srcStruct2{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,
Method: 4321,
}
srcStruct = srcStruct1{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,
II: []int{},
UU: nil,
SS: []string{"string1", "string2", "", "string4", "string5"},
V: srcStructA,
VV: []srcStruct2{srcStructA, srcStructB, srcStructA, srcStructB, srcStructA},
M1: map[int]string{1: "11", 2: "22", 3: "33"},
M2: nil,
M3: map[int]int{7: 77, 8: 88, 9: 99},
//nolint:gofmt
M4: map[[3]int]*srcStruct2{
[3]int{1, 1, 1}: &srcStructA,
[3]int{2, 2, 2}: &srcStructB,
},
P2V: nil,
V2P: 0,
S2A: nil,
MatchedX: "hahaha",
UnmatchedX: "hihihi",
}
)
func Test_Copy(t *testing.T) {
var dst dstStruct1
err := Copy(&dst, srcStruct)
assert.Nil(t, err)
// TODO: need verification here
}
func Test_ClearCache(t *testing.T) {
ClearCache()
assert.Equal(t, 0, len(copierCacheMap))
}
func Test_ConfigOption(t *testing.T) {
ctx := defaultContext()
CopyBetweenPtrAndValue(false)(ctx)
assert.Equal(t, false, ctx.CopyBetweenPtrAndValue)
CopyBetweenPtrAndValue(true)(ctx)
assert.Equal(t, true, ctx.CopyBetweenPtrAndValue)
CopyBetweenStructFieldAndMethod(false)(ctx)
assert.Equal(t, false, ctx.CopyBetweenStructFieldAndMethod)
CopyBetweenStructFieldAndMethod(true)(ctx)
assert.Equal(t, true, ctx.CopyBetweenStructFieldAndMethod)
IgnoreNonCopyableTypes(false)(ctx)
assert.Equal(t, false, ctx.IgnoreNonCopyableTypes)
IgnoreNonCopyableTypes(true)(ctx)
assert.Equal(t, true, ctx.IgnoreNonCopyableTypes)
UseGlobalCache(false)(ctx)
assert.Equal(t, false, ctx.UseGlobalCache)
UseGlobalCache(true)(ctx)
assert.Equal(t, true, ctx.UseGlobalCache)
}
func Test_SetDefaultTagName(t *testing.T) {
assert.Equal(t, DefaultTagName, defaultTagName)
// Invalid one
SetDefaultTagName(" abc")
assert.Equal(t, DefaultTagName, defaultTagName)
// Valid one
SetDefaultTagName("abc")
assert.Equal(t, "abc", defaultTagName)
// Restore the tag
SetDefaultTagName(DefaultTagName)
}