-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
reflection_test.go
215 lines (187 loc) · 6.33 KB
/
reflection_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
package caches
import (
"reflect"
"testing"
)
type unsupportedMockStruct struct {
ExportedField string
unexportedField string
ExportedSliceField []string
unexportedSliceField []string
ExportedMapField map[string]string
unexportedMapField map[string]string
}
type supportedMockStruct struct {
ExportedField string
ExportedSliceField []string
ExportedMapField map[string]string
}
func Test_SetPointedValue(t *testing.T) {
src := &struct {
Name string
}{
Name: "Test",
}
dest := &struct {
Name string
}{}
SetPointedValue(dest, src)
if !reflect.DeepEqual(src, dest) {
t.Error("SetPointedValue was expected to point the dest to the source")
}
if dest.Name != src.Name {
t.Errorf("src and dest were expected to have the same name, src.Name `%s`, dest.Name `%s`", src.Name, dest.Name)
}
}
func Test_deepCopy(t *testing.T) {
t.Run("struct", func(t *testing.T) {
t.Run("supported", func(t *testing.T) {
srcStruct := supportedMockStruct{
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
}
dstStruct := supportedMockStruct{}
if err := deepCopy(srcStruct, &dstStruct); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcStruct, dstStruct) {
t.Errorf("deepCopy failed to copy structure: got %+v, want %+v", dstStruct, srcStruct)
}
})
t.Run("unsupported", func(t *testing.T) {
srcStruct := unsupportedMockStruct{
ExportedField: "exported field",
unexportedField: "unexported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
unexportedSliceField: []string{"1st elem of an unexported slice field", "2nd elem of an unexported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
unexportedMapField: map[string]string{
"key1": "unexported map elem",
"key2": "unexported map elem",
},
}
dstStruct := unsupportedMockStruct{}
if err := deepCopy(srcStruct, &dstStruct); err == nil {
t.Error("deepCopy was expected to fail copying an structure with unexported fields")
}
})
})
t.Run("map", func(t *testing.T) {
t.Run("map[string]string", func(t *testing.T) {
srcMap := map[string]string{
"key1": "value1",
"key2": "value2",
}
dstMap := make(map[string]string)
if err := deepCopy(srcMap, &dstMap); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcMap, dstMap) {
t.Errorf("deepCopy failed to copy map: got %+v, want %+v", dstMap, srcMap)
}
})
t.Run("map[string]struct", func(t *testing.T) {
srcMap := map[string]supportedMockStruct{
"key1": {
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
},
"key2": {
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
},
}
dstMap := make(map[string]supportedMockStruct)
if err := deepCopy(srcMap, &dstMap); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcMap, dstMap) {
t.Errorf("deepCopy failed to copy map: got %+v, want %+v", dstMap, srcMap)
}
})
})
t.Run("slice", func(t *testing.T) {
t.Run("[]string", func(t *testing.T) {
srcSlice := []string{"A", "B", "C"}
dstSlice := make([]string, len(srcSlice))
if err := deepCopy(srcSlice, &dstSlice); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcSlice, dstSlice) {
t.Errorf("deepCopy failed to copy slice: got %+v, want %+v", dstSlice, srcSlice)
}
})
t.Run("[]struct", func(t *testing.T) {
srcSlice := []supportedMockStruct{
{
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
}, {
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
}, {
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
},
}
dstSlice := make([]supportedMockStruct, len(srcSlice))
if err := deepCopy(srcSlice, &dstSlice); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcSlice, dstSlice) {
t.Errorf("deepCopy failed to copy slice: got %+v, want %+v", dstSlice, srcSlice)
}
})
})
t.Run("pointer", func(t *testing.T) {
srcStruct := &supportedMockStruct{
ExportedField: "exported field",
ExportedSliceField: []string{"1st elem of an exported slice field", "2nd elem of an exported slice field"},
ExportedMapField: map[string]string{
"key1": "exported map elem",
"key2": "exported map elem",
},
}
dstStruct := &supportedMockStruct{}
if err := deepCopy(srcStruct, dstStruct); err != nil {
t.Errorf("deepCopy returned an unexpected error %+v", err)
}
if !reflect.DeepEqual(srcStruct, dstStruct) {
t.Errorf("deepCopy failed to copy structure: got %+v, want %+v", dstStruct, srcStruct)
}
})
t.Run("mismatched", func(t *testing.T) {
src := "a string"
dst := 123
if err := deepCopy(src, &dst); err == nil {
t.Error("deepCopy did not return an error when provided mismatched types")
}
})
}