-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
218 lines (207 loc) · 4.19 KB
/
examples_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
package deepcopy
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
"testing"
"time"
)
// Example 1
type Ex1StructA struct {
Foo string
}
type Ex1StructB struct {
Foo string
}
var (
ex1objA = Ex1StructA{Foo: "leia"}
ex1objB = Ex1StructB{}
)
// Example 2
type Ex2StructA struct {
Zak bool
Yo uint32
}
type Ex2StructB struct {
Zak bool
Yo int64
}
type Ex2StructC struct {
Foo *Ex2StructA
}
type Ex2StructD struct {
Foo *Ex2StructB
}
var (
ex2objC = Ex2StructC{
Foo: &Ex2StructA{
Zak: true,
Yo: uint32(65),
},
}
ex2objD = Ex2StructD{}
)
// Example 3
type Ex3StructA struct {
Foodx string
}
type Ex3StructB struct {
Food Ex3StructA
}
type Ex3StructC struct {
Foo string
Bar int
Zak bool
Yo *bool
Sel []*Ex3StructB
Mar map[string]string
Frey map[uint]float64
Rand *time.Time
Dot *timestamppb.Timestamp
}
var (
yo = true
timeNow = time.Now()
timestampNow = timestamppb.New(timeNow)
ex3objC = Ex3StructC{
Foo: "hello there!",
Bar: 34234,
Zak: false,
Yo: &yo,
Sel: []*Ex3StructB{
&Ex3StructB{
Food: Ex3StructA{
Foodx: "nested string here",
},
},
&Ex3StructB{
Food: Ex3StructA{
Foodx: "second nested string",
},
},
},
Mar: map[string]string{
"first": "f i r s t",
"second": "the second one",
},
Frey: map[uint]float64{
uint(4): 8.2343,
uint(5): 23.4322,
uint(6): 883423.0,
},
Rand: &timeNow,
Dot: timestampNow,
}
)
// Example 4
var (
ex4String = "42"
ex4EmptyInt int32
ex4Int = int32(42)
)
// Example 5
var (
ex5String = "true"
ex5EmptyBool bool
ex5Bool = true
)
// Example 6
var (
ex6String = "6.4"
ex6EmptyFloat64 float64
ex6Float64 = 6.4
)
func TestDeepCopyExamples(t *testing.T) {
testCases := []struct {
name string
input interface{}
outputPtr interface{}
expectedOutputPtr interface{}
expectedError error
}{
/* **** Case 1: Struct Conversion **** */
{
name: "Example 1: struct conversion",
input: ex1objA,
outputPtr: &ex1objB,
expectedOutputPtr: &Ex1StructB{Foo: "leia"},
},
{
name: "Example 2: struct conversion, nested fields",
input: ex2objC,
outputPtr: &ex2objD,
expectedOutputPtr: &Ex2StructD{
Foo: &Ex2StructB{
Zak: true,
Yo: int64(65),
},
},
},
/* **** Case 2: Identical Copy **** */
{
name: "Example 3: identical copy, nested fields",
input: ex3objC,
outputPtr: &Ex3StructC{},
expectedOutputPtr: &Ex3StructC{
Foo: "hello there!",
Bar: 34234,
Zak: false,
Yo: &yo,
Sel: []*Ex3StructB{
&Ex3StructB{
Food: Ex3StructA{
Foodx: "nested string here",
},
},
&Ex3StructB{
Food: Ex3StructA{
Foodx: "second nested string",
},
},
},
Mar: map[string]string{
"first": "f i r s t",
"second": "the second one",
},
Frey: map[uint]float64{
uint(4): 8.2343,
uint(5): 23.4322,
uint(6): 883423.0,
},
Rand: &timeNow,
Dot: timestampNow,
},
},
/* **** Case 3: General Type Casting **** */
{
name: "Example 4: type casting, string to int",
input: ex4String, // "42"
outputPtr: &ex4EmptyInt, // empty int var
expectedOutputPtr: &ex4Int, // 42 (int)
},
{
name: "Example 5: type casting, string to bool",
input: ex5String, // "true"
outputPtr: &ex5EmptyBool, // empty bool
expectedOutputPtr: &ex5Bool, // true
},
{
name: "Example 5: type casting, string to float64",
input: ex6String, // "6.4"
outputPtr: &ex6EmptyFloat64, // empty float64
expectedOutputPtr: &ex6Float64, // 6.4 (float64)
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := DeepCopy(tc.input, tc.outputPtr)
if tc.expectedError != nil {
require.Error(t, err)
assert.Equal(t, tc.expectedError.Error(), err.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedOutputPtr, tc.outputPtr)
}
})
}
}