-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
228 lines (215 loc) · 3.78 KB
/
common_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
package oxygen
import (
"errors"
"math/bits"
"reflect"
"testing"
)
func equal(t *testing.T, exp, got interface{}) {
if !reflect.DeepEqual(exp, got) {
t.Fatalf("Not equal:\nexp: %v\ngot: %v", exp, got)
}
}
func Test_bitSize(t *testing.T) {
var tests = []struct {
name string
reflectKind reflect.Kind
expect int
}{
{
name: "bool",
reflectKind: reflect.Bool,
expect: 0,
},
{
name: "int8",
reflectKind: reflect.Int8,
expect: 8,
},
{
name: "int16",
reflectKind: reflect.Int16,
expect: 16,
},
{
name: "int32",
reflectKind: reflect.Int32,
expect: 32,
},
{
name: "int64",
reflectKind: reflect.Int64,
expect: 64,
},
{
name: "uint8",
reflectKind: reflect.Uint8,
expect: 8,
},
{
name: "uint16",
reflectKind: reflect.Uint16,
expect: 16,
},
{
name: "uint32",
reflectKind: reflect.Uint32,
expect: 32,
},
{
name: "uint64",
reflectKind: reflect.Uint64,
expect: 64,
},
{
name: "int",
reflectKind: reflect.Int,
expect: bits.UintSize,
},
{
name: "uint",
reflectKind: reflect.Uint,
expect: bits.UintSize,
},
{
name: "uintptr",
reflectKind: reflect.Uintptr,
expect: bits.UintSize,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
equal(t, tt.expect, bitSize(tt.reflectKind))
})
}
}
func Test_isEmptyValue(t *testing.T) {
var a any
a = 77
var b any
var tests = []struct {
name string
value any
expect bool
}{
{
name: "no empty bool",
value: true,
expect: false,
},
{
name: "empty bool",
value: false,
expect: true,
},
{
name: "no empty int",
value: 1,
expect: false,
},
{
name: "empty int",
value: 0,
expect: true,
},
{
name: "np empty float",
value: 1.1,
expect: false,
},
{
name: "empty float",
value: 0.0,
expect: true,
},
{
name: "no empty string",
value: "a",
expect: false,
},
{
name: "empty string",
value: "",
expect: true,
},
{
name: "no empty pointer",
value: &struct{}{},
expect: false,
},
{
name: "empty pointer",
value: (*struct{})(nil),
expect: true,
},
{
name: "no empty slice",
value: []int{1},
expect: false,
},
{
name: "empty slice",
value: []int{},
expect: true,
},
{
name: "no empty interface",
value: a,
expect: false,
},
{
name: "empty interface",
value: b,
expect: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
equal(t, tt.expect, isEmptyValue(reflect.ValueOf(tt.value)))
})
}
}
type empty struct{}
func Test_contextSetError(t *testing.T) {
tagName := "tagName"
str := "set/get"
var tests = []struct {
name string
ctx context[empty]
expect error
}{
{
name: "error for structs",
ctx: context[empty]{
structName: "structName",
field: &field[empty]{
name: "fieldName",
typ: reflect.TypeOf(true),
},
err: ErrNotSupportType,
},
expect: errors.New("tagName: cannot set/get Go struct field structName.fieldName of type bool: cannot support type"),
},
{
name: "error for simple types",
ctx: context[empty]{
structName: "",
field: &field[empty]{
typ: reflect.TypeOf(true),
},
err: ErrNotSupportType,
},
expect: errors.New("tagName: cannot set/get Go value of type bool: cannot support type"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.ctx.setError(tagName, str, tt.ctx.err)
if tt.expect != nil {
equal(t, tt.expect.Error(), tt.ctx.err.Error())
return
}
equal(t, nil, tt.ctx.err)
})
}
}