-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflect_test.go
199 lines (172 loc) · 4.41 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
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
package utils
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
type TestReflect struct {
Foo string
Map map[string]string
}
func TestAssign(t *testing.T) {
values := make(map[string]interface{})
values["Foo"] = "bar"
values["Map"] = map[string]string{"go": "pher"}
values["Ja"] = "va"
test := new(TestReflect)
Assign(test, values)
if test.Foo != "bar" {
t.Errorf("Invalid dume got %s instead of %s", test.Foo, "bar")
}
if test.Map == nil {
t.Error("Missing value for Map")
}
if v, ok := test.Map["go"]; ok {
if v != "pher" {
t.Errorf("Invalid dume got %s instead of %s", v, "pher")
}
} else {
t.Error("Missing value for map key \"go\"")
}
return
}
func TestToInterfaceArray(t *testing.T) {
ToInterfaceArray([]int{1, 2, 3, 4, 5, 6})
}
func TestAssignStringString(t *testing.T) {
type TestStruct struct {
Foo string
}
obj := &TestStruct{}
elem := reflect.ValueOf(obj).Elem().Field(0)
err := AssignStringString(elem, "bar")
require.NoError(t, err)
require.Equal(t, "bar", obj.Foo)
}
func TestAssignBoolString(t *testing.T) {
type TestStruct struct {
Foo bool
}
obj := &TestStruct{}
elem := reflect.ValueOf(obj).Elem().Field(0)
err := AssignBoolString(elem, "true")
require.NoError(t, err)
require.True(t, obj.Foo)
err = AssignBoolString(elem, "false")
require.NoError(t, err)
require.False(t, obj.Foo)
err = AssignBoolString(elem, "blah")
require.Error(t, err)
}
func TestAssignIntString(t *testing.T) {
type TestStruct struct {
Foo int
}
obj := &TestStruct{}
elem := reflect.ValueOf(obj).Elem().Field(0)
err := AssignIntString(elem, "42")
require.NoError(t, err)
require.Equal(t, 42, obj.Foo)
}
func TestAssignUintString(t *testing.T) {
type TestStruct struct {
Foo uint
}
obj := &TestStruct{}
elem := reflect.ValueOf(obj).Elem().Field(0)
err := AssignUintString(elem, "42")
require.NoError(t, err)
require.Equal(t, uint(42), obj.Foo)
}
func TestAssignFloatString(t *testing.T) {
type TestStruct struct {
Foo float64
}
obj := &TestStruct{}
elem := reflect.ValueOf(obj).Elem().Field(0)
err := AssignFloatString(elem, "42.26")
require.NoError(t, err)
require.Equal(t, 42.26, obj.Foo)
}
func TestAssignSliceValues(t *testing.T) {
type TestStruct struct {
Slice []string
}
obj := &TestStruct{}
field := reflect.ValueOf(obj).Elem().Field(0)
err := AssignJsonSliceString(field, "[\"foo\",\"bar\",\"baz\"]")
require.NoError(t, err)
require.EqualValues(t, []string{"foo", "bar", "baz"}, obj.Slice)
}
func TestAssignSliceIntValues(t *testing.T) {
type TestStruct struct {
Slice []int
}
obj := &TestStruct{}
field := reflect.ValueOf(obj).Elem().Field(0)
err := AssignJsonSliceString(field, "[0,1,2]")
require.NoError(t, err)
require.EqualValues(t, []int{0, 1, 2}, obj.Slice)
}
func TestAssignMapValuesNilMap(t *testing.T) {
type TestStruct struct {
Map map[string]string
}
obj := &TestStruct{}
field := reflect.ValueOf(obj).Elem().Field(0)
err := AssignJsonMapString(field, "{\"foo\":\"bar\"}")
require.NoError(t, err)
require.EqualValues(t, map[string]string{"foo": "bar"}, obj.Map)
}
func TestAssignMapValues(t *testing.T) {
type TestStruct struct {
Map map[string]string
}
obj := &TestStruct{Map: map[string]string{"foo": "baz", "plip": "plop"}}
field := reflect.ValueOf(obj).Elem().Field(0)
err := AssignJsonMapString(field, "{\"foo\":\"bar\"}")
require.NoError(t, err)
require.EqualValues(t, map[string]string{"foo": "bar", "plip": "plop"}, obj.Map)
}
func TestAssignStrings(t *testing.T) {
type TestStruct struct {
Bool bool
String string
Int int
Uint uint
Float float64
Slice []interface{}
Map map[string]interface{}
Nope struct{}
}
getter := func(name string) (value string, ok bool) {
switch name {
case "Bool":
return "true", true
case "String":
return "string", true
case "Int":
return "42", true
case "Uint":
return "42", true
case "Float":
return "42.26", true
case "Slice":
return "[\"foo\",\"bar\"]", true
case "Map":
return "{\"foo\":\"bar\"}", true
default:
return "", false
}
}
obj := &TestStruct{}
err := AssignStrings(obj, getter)
require.NoError(t, err)
require.True(t, obj.Bool)
require.Equal(t, "string", obj.String)
require.Equal(t, 42, obj.Int)
require.Equal(t, uint(42), obj.Uint)
require.Equal(t, 42.26, obj.Float)
require.EqualValues(t, []interface{}{"foo", "bar"}, obj.Slice)
require.EqualValues(t, map[string]interface{}{"foo": "bar"}, obj.Map)
}