-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect_test.go
103 lines (95 loc) · 2.11 KB
/
reflect_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
package yc_lockbox_unpack
import (
"net"
"net/url"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnpackText(t *testing.T) {
tests := []struct {
typ reflect.Type
value string
expected interface{}
}{
{
typ: reflect.TypeOf(true),
value: "True",
expected: true,
},
{
typ: reflect.TypeOf(1),
value: " 1_234_567",
expected: 1234567,
},
{
typ: reflect.TypeOf(uint(1)),
value: "1_234_567 ",
expected: uint(1234567),
},
{
typ: reflect.TypeOf(float32(1.0)),
value: "1.23456",
expected: float32(1.23456),
},
{
typ: reflect.TypeOf(net.IP{}),
value: "192.0.2.1",
expected: net.IPv4(192, 0, 2, 1),
},
{
typ: reflect.TypeOf(ptrStr("string")),
value: "stringster",
expected: ptrStr("stringster"),
},
}
var err error
var value reflect.Value
for _, test := range tests {
t.Run(test.typ.String(), func(t *testing.T) {
value = reflect.New(test.typ).Elem()
err = unpackText(value, test.value)
if assert.NoError(t, err) {
expected := reflect.ValueOf(test.expected)
assert.Equal(t, expected.Type(), value.Type())
assert.Equal(t, expected.Interface(), value.Interface())
}
})
}
}
func TestUnpackBinary(t *testing.T) {
tests := []struct {
typ reflect.Type
value []byte
expected interface{}
}{
{
typ: reflect.TypeOf([]byte{}),
value: []byte("bytes"),
expected: []byte("bytes"),
},
{
typ: reflect.TypeOf(""),
value: []byte("string"),
expected: "string",
},
{
typ: reflect.TypeOf(&url.URL{}),
value: []byte("http://localhost:8080"),
expected: &url.URL{Scheme: "http", Host: "localhost:8080"},
},
}
var err error
var value reflect.Value
for _, test := range tests {
t.Run(test.typ.String(), func(t *testing.T) {
value = reflect.New(test.typ).Elem()
err = unpackBinary(value, test.value)
if assert.NoError(t, err) {
expected := reflect.ValueOf(test.expected)
assert.Equal(t, expected.Type(), value.Type())
assert.Equal(t, expected.Interface(), value.Interface())
}
})
}
}