-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpcopy_map_test.go
189 lines (162 loc) · 2.91 KB
/
pcopy_map_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
// Copyright [2020-2023] [guonaihong]
package pcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
type test_MapWithMap_Item struct {
A string
B string
}
type test_MapWithMap_Dst struct {
A map[string]string
B map[string]map[string]string
C map[string]test_MapWithMap_Item
}
type test_MapWithMap_Src test_MapWithMap_Dst
var local_MapWithMap_Src = test_MapWithMap_Src{
A: map[string]string{
"1": "1",
"2": "2",
},
B: map[string]map[string]string{
"1": {
"1": "1",
"2": "2",
},
"2": {
"1": "1",
"2": "2",
},
},
C: map[string]test_MapWithMap_Item{
"1": {
A: "1",
B: "2",
},
"2": {
A: "1",
B: "2",
},
},
}
func Test_MapWithMap(t *testing.T) {
err := Preheat(&test_MapWithMap_Dst{}, &test_MapWithMap_Src{})
assert.NoError(t, err)
d := test_MapWithMap_Dst{}
err = Copy(&d, &local_MapWithMap_Src, WithUsePreheat())
assert.NoError(t, err)
// Copy(&d, &local_MapWithMap_Src)
assert.Equal(t, d, test_MapWithMap_Dst(local_MapWithMap_Src))
}
func Test_MapToMap2(t *testing.T) {
type dst struct {
A map[int]int
B map[string]string
}
type src struct {
B map[string]string
A map[int]int
}
var d dst
b := map[string]string{
"testA": "testA",
"testB": "testB",
}
a := map[int]int{
1: 1,
2: 2,
}
s := src{
B: b,
A: a,
}
err := Preheat(&dst{}, &src{})
assert.NoError(t, err)
Copy(&d, &s, WithUsePreheat())
assert.Equal(t, d, dst{A: a, B: b})
}
func Test_MapToMap(t *testing.T) {
type dst struct {
A map[int]int
B map[string]string
}
type src struct {
B map[string]string
A map[int]int
}
var d dst
b := map[string]string{
"testA": "testA",
"testB": "testB",
}
a := map[int]int{
1: 1,
2: 2,
}
s := src{
B: b,
A: a,
}
Copy(&d, &s)
assert.Equal(t, d, dst{A: a, B: b})
}
func Test_Map_Special(t *testing.T) {
type mVal struct {
ID int
Name string
}
for _, tc := range []testCase{
// map里面的值是结构体
func() testCase {
src := map[string]mVal{
"1": {ID: 1, Name: "name:1"},
"2": {ID: 2, Name: "name:2"},
"3": {ID: 3, Name: "name:3"},
}
var dst map[string]mVal
Copy(&dst, &src)
return testCase{got: dst, need: src}
}(),
// dst的地址不是指针,没有发生panic
func() testCase {
src := map[string]mVal{
"1": {ID: 1, Name: "name:1"},
}
var dst map[string]mVal
Copy(&dst, &src)
return testCase{}
}(),
func() testCase {
src := map[string]mVal{
"1": {ID: 1, Name: "name:1"},
}
Copy(new(int), &src)
return testCase{}
}(),
// key相同,value不同
func() testCase {
dst := map[int]string{
1: "hello",
}
src := map[int]int{
1: 1,
}
Copy(&dst, &src)
return testCase{}
}(),
// key不同,value不同
func() testCase {
dst := map[string]int64{
"hello": 3,
}
src := map[int]int64{
1: 64,
}
Copy(&dst, &src)
return testCase{}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}