-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeepcopy_map_test.go
105 lines (89 loc) · 1.6 KB
/
deepcopy_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
package deepcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
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).Do()
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).Do()
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).Do()
return testCase{}
}(),
func() testCase {
src := map[string]mVal{
"1": {ID: 1, Name: "name:1"},
}
Copy(new(int), &src).Do()
return testCase{}
}(),
// key相同,value不同
func() testCase {
dst := map[int]string{
1: "hello",
}
src := map[int]int{
1: 1,
}
Copy(&dst, &src).Do()
return testCase{}
}(),
// key不同,value不同
func() testCase {
dst := map[string]int64{
"hello": 3,
}
src := map[int]int64{
1: 64,
}
Copy(&dst, &src).Do()
return testCase{}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}