-
Notifications
You must be signed in to change notification settings - Fork 4
/
xreflect_test.go
354 lines (323 loc) · 6.14 KB
/
xreflect_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package xreflect
import (
"errors"
"io"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type (
Heart interface{}
Person struct {
Name string `json:"name"`
Age int `json:"age"`
PtrPerson *Person `json:"ptr_person,omitempty"`
// private field
phone string `json:"phone"`
// anonymous fields
string
int
*Person
Country
Heart // interface
inner
}
inner struct {
innerString string
}
Country struct {
ID int
Name string
City City
PtrCity *City
}
City struct {
ID int
PtrTown *Town
Town Town
}
Town struct {
Int int
Str string
Bool bool
Strs []string
}
)
func newCountry() Country {
town := Town{
Int: 0,
Str: "Str",
Bool: false,
Strs: []string{"Str"},
}
city := City{
ID: 0,
Town: town,
PtrTown: &town,
}
country := Country{
ID: 0,
Name: "A country",
City: city,
PtrCity: &city,
}
return country
}
func TestNewInstance(t *testing.T) {
s := "1"
tests := []struct {
name string
value interface{}
want interface{}
}{
{"int", int(1), 0},
{"float", float32(1), float32(0)},
{"complex", complex(1, 1), complex(0, 0)},
{"string", "1", ""},
{"struct", Country{ID: 1}, Country{}},
{"struct ptr", &Country{ID: 1}, &Country{}},
{"[]string", []string{"1"}, []string{}},
{"[]*string", []*string{&s}, []*string{}},
{"1 array", [1]string{}, [1]string{}},
{"2 array", [2]string{}, [2]string{}},
{"map[string]string", make(map[string]string), make(map[string]string)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, NewInstance(tt.value), "NewInstance(%v)", tt.value)
})
}
// test chan
ci1 := make(chan int)
ci2 := NewInstance(ci1).(chan int)
assert.Equal(t, 0, cap(ci1))
assert.Equal(t, 0, cap(ci2))
go func() {
assert.Equal(t, 1, <-ci2)
}()
ci2 <- 1
ci3 := make(chan int, 3)
ci4 := NewInstance(ci3).(chan int)
assert.Equal(t, 3, cap(ci4))
assert.Equal(t, 0, len(ci4))
}
func TestGetType(t *testing.T) {
testCases := []struct {
name string
obj interface{}
expected reflect.Type
}{
{
name: "Testing with reflect.Type",
obj: reflect.TypeOf("test"),
expected: reflect.TypeOf("test"),
},
{
name: "Testing with reflect.Value",
obj: reflect.ValueOf(10),
expected: reflect.TypeOf(10),
},
{
name: "Testing with other types",
obj: "test",
expected: reflect.TypeOf("test"),
},
{
name: "Testing with nil",
obj: nil,
expected: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Type(tc.obj)
if result != tc.expected {
t.Errorf("Expected type %v, got %v", tc.expected, result)
}
})
}
}
func TestGetTypePenetrateElem(t *testing.T) {
var i3 ***int
i0 := 1
i1 := &i0
i2 := &i1
i3 = &i2
testCases := []struct {
name string
obj interface{}
expected reflect.Type
}{
{
name: "***int and *int",
obj: i3,
expected: reflect.TypeOf(i1).Elem(),
},
{
name: "***int and int",
obj: i3,
expected: reflect.TypeOf(i0),
},
{
name: "Testing with reflect.Value",
obj: reflect.ValueOf(10),
expected: reflect.TypeOf(10),
},
{
name: "Testing with other types",
obj: "test",
expected: reflect.TypeOf("test"),
},
{
name: "Testing with nil",
obj: nil,
expected: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := TypePenetrateElem(tc.obj)
if result != tc.expected {
t.Errorf("Expected type %v, got %v", tc.expected, result)
}
})
}
}
func TestGetValue(t *testing.T) {
ii := &[]int{1, 2, 3}
testCases := []struct {
name string
input interface{}
expected reflect.Value
}{
{name: "Int",
input: 42,
expected: reflect.ValueOf(42),
},
{name: "String",
input: "hello",
expected: reflect.ValueOf("hello"),
},
{name: "&[]int{1, 2, 3}",
input: ii,
expected: reflect.ValueOf(ii).Elem(),
},
{name: "reflect.Value",
input: reflect.ValueOf(1),
expected: reflect.ValueOf(1),
},
{name: "Nil",
input: nil,
expected: reflect.Value{},
},
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Value(tc.input)
if actual != tc.expected {
t.Errorf("Expected reflect value %v, but got %v", tc.expected, actual)
}
})
}
}
func TestGetValuePenetrateElem(t *testing.T) {
var i3 ***int
i0 := 1
i1 := &i0
i2 := &i1
i3 = &i2
testCases := []struct {
name string
input interface{}
expected reflect.Value
}{
{"nil", nil, reflect.Value{}},
{"***int", i3, reflect.ValueOf(i1).Elem()},
{"int", i0, reflect.ValueOf(i0)},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := ValuePenetrateElem(tc.input)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("Expected %v, but got %v", tc.expected, actual)
}
})
}
}
func F() {
}
func TestGetPkgPath(t *testing.T) {
tests := []struct {
name string
obj interface{}
want string
}{
{
name: "struct",
obj: &Person{},
want: "github.com/morrisxyang/xreflect",
},
{
name: "[]string{}",
obj: []string{},
want: "",
},
{
name: "Func",
obj: F,
want: "",
},
{
name: "assert.NoError",
obj: assert.NoError,
want: "",
},
{
name: "io.EOF",
obj: io.EOF,
want: "errors",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetPkgPath(tt.obj), "GetPkgPath(%v)", tt.obj)
})
}
}
func TestImplements(t *testing.T) {
type error interface {
Error() string
}
type args struct {
obj interface{}
in interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Nil",
args: args{
obj: nil,
in: nil,
},
want: false,
},
{
name: "error",
args: args{
obj: errors.New(""),
in: (*error)(nil),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, Implements(tt.args.obj, tt.args.in), "Implements(%v, %v)", tt.args.obj, tt.args.in)
})
}
}