-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_copier_test.go
211 lines (187 loc) · 4.31 KB
/
base_copier_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
package deepcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Copy_ptr2ptr(t *testing.T) {
t.Run("#1: int-ptr -> int-ptr", func(t *testing.T) {
var s *int = ptrOf(111)
var d *int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, *d)
assert.True(t, d != s)
})
t.Run("#2: int-ptr -> int-ptr (dst has set)", func(t *testing.T) {
var s *int = ptrOf(111)
var d *int = ptrOf(222)
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, *d)
assert.True(t, d != s)
})
t.Run("#3: nil int-ptr -> int-ptr", func(t *testing.T) {
var s *int
var d *int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Nil(t, d)
})
t.Run("#4: nil int-ptr -> int-ptr (dst has set)", func(t *testing.T) {
var s *int
var d *int = ptrOf(111)
err := Copy(&d, s)
assert.Nil(t, err)
assert.Nil(t, d)
})
}
func Test_Copy_ptr2ptr_error(t *testing.T) {
t.Run("#1: int-ptr -> struct-ptr (error)", func(t *testing.T) {
type DD struct {
I int
}
var s *int = ptrOf(111)
var d *DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
}
func Test_Copy_ptr2value(t *testing.T) {
t.Run("#1: int-ptr -> int", func(t *testing.T) {
var s *int = ptrOf(111)
var d int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, d)
})
t.Run("#2: int-ptr -> int (dst has set)", func(t *testing.T) {
var s *int = ptrOf(111)
var d int = 222
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, d)
})
t.Run("#3: nil int-ptr -> int", func(t *testing.T) {
var s *int
var d int = 111
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 0, d)
})
}
func Test_Copy_ptr2value_error(t *testing.T) {
t.Run("#1: int-ptr -> struct (error)", func(t *testing.T) {
type DD struct {
I int
}
var s *int = ptrOf(111)
var d DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#2: ptr -> value not allow", func(t *testing.T) {
var s *int = ptrOf(111)
var d int
err := Copy(&d, s, func(ctx *Context) {
ctx.CopyBetweenPtrAndValue = false
})
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
}
func Test_Copy_value2ptr(t *testing.T) {
t.Run("#1: int -> int-ptr", func(t *testing.T) {
var s int = 111
var d *int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, *d)
})
t.Run("#2: int -> int-ptr (dst has set)", func(t *testing.T) {
var s int = 111
var d *int = ptrOf(222)
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, *d)
})
}
func Test_Copy_value2ptr_error(t *testing.T) {
t.Run("#1: int -> struct-ptr (error)", func(t *testing.T) {
type DD struct {
I int
}
var s int = 111
var d *DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#2: value -> ptr not allow", func(t *testing.T) {
var s int = 111
var d *int
err := Copy(&d, s, func(ctx *Context) {
ctx.CopyBetweenPtrAndValue = false
})
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
}
func Test_Copy_basicTypes(t *testing.T) {
t.Run("#1: int -> int", func(t *testing.T) {
var s int = 111
var d int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, d)
})
t.Run("#2: int -> IntT", func(t *testing.T) {
var s int = 111
var d IntT
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, IntT(111), d)
})
t.Run("#3: int -> float64", func(t *testing.T) {
var s int = 111
var d float64
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, float64(111), d)
})
t.Run("#4: float64 -> int", func(t *testing.T) {
var s float64 = 111.111
var d int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 111, d)
})
t.Run("#5: string -> StrT", func(t *testing.T) {
var s string = "abc"
var d StrT
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, StrT("abc"), d)
})
t.Run("#6: func -> func", func(t *testing.T) {
var s = func(i int) int {
return i * 2
}
var d func(int) int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, 2, d(1))
})
}
func Test_Copy_basicTypes_error(t *testing.T) {
t.Run("#1: nil dst", func(t *testing.T) {
var s int = 111
var d *int
err := Copy(d, s)
assert.ErrorIs(t, err, ErrValueInvalid)
})
t.Run("#2: func -> func (unmatched)", func(t *testing.T) {
var s = func(i int) int {
return i * 2
}
var d func(int) float32
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
}