-
Notifications
You must be signed in to change notification settings - Fork 14
/
util_test.go
317 lines (299 loc) · 5.71 KB
/
util_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
package jsonserialization
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsCompatible(t *testing.T) {
cases := []struct {
Title string
InputVal interface{}
InputType reflect.Type
Expected interface{}
}{
{
Title: "Valid",
InputVal: int(5),
InputType: reflect.TypeOf(int8(0)),
Expected: true,
},
{
Title: "Incompatible",
InputVal: "I am a string",
InputType: reflect.TypeOf(int8(0)),
Expected: false,
},
}
for _, test := range cases {
t.Run(test.Title, func(t *testing.T) {
isComp := isCompatible(test.InputVal, test.InputType)
assert.Equal(t, test.Expected, isComp)
})
}
}
func TestIsNil(t *testing.T) {
tests := []struct {
Title string
Input interface{}
Expected bool
}{
{"nil value", nil, true},
{"non-nil int", 42, false},
{"nil pointer", (*int)(nil), true},
{"non-nil pointer", new(int), false},
{"nil interface", interface{}(nil), true},
{"non-nil interface", interface{}(42), false},
{"nil slice", ([]int)(nil), true},
{"non-nil slice", []int{1, 2, 3}, false},
{"nil map", (map[string]int)(nil), true},
{"non-nil map", map[string]int{"a": 1}, false},
{"nil chan", (chan int)(nil), true},
{"non-nil chan", make(chan int), false},
{"nil func", (func())(nil), true},
{"non-nil func", func() {}, false},
{"nested nil pointer", (**int)(nil), true},
{"nested non-nil pointer", func() **int { var i int; p := &i; return &p }(), false},
}
for _, tt := range tests {
t.Run(tt.Title, func(t *testing.T) {
got := isNil(tt.Input)
assert.Equal(t, tt.Expected, got)
})
}
}
func TestAs(t *testing.T) {
cases := []struct {
Title string
InputVal []interface{}
Expected interface{}
Error error
}{
{
Title: "Number",
InputVal: []interface{}{
int8(1),
int16(0),
},
Expected: int16(1),
Error: nil,
},
{
Title: "Incompatible",
InputVal: []interface{}{
"I am a string",
int8(0),
},
Expected: int8(0),
Error: errors.New("value 'I am a string' is not compatible with type int8"),
},
{
Title: "Untyped Nil - In",
InputVal: []interface{}{
nil,
int8(0),
},
Expected: int8(0),
Error: nil,
},
{
Title: "Typed Nil - In",
InputVal: []interface{}{
(*string)(nil),
int8(0),
},
Expected: int8(0),
Error: nil,
},
{
Title: "Untyped Nil - Out",
InputVal: []interface{}{
int8(1),
nil,
},
Expected: nil,
Error: errors.New("out is not pointer or is nil"),
},
{
Title: "Typed Nil - Out",
InputVal: []interface{}{
int8(0),
(*int8)(nil),
},
Expected: (*int8)(nil),
Error: errors.New("out is not pointer or is nil"),
},
}
for _, test := range cases {
t.Run(test.Title, func(t *testing.T) {
in := test.InputVal[0]
out := test.InputVal[1]
err := as(in, &out)
assert.Equal(t, test.Error, err)
assert.Equal(t, test.Expected, out)
})
}
}
func TestIsNumericType(t *testing.T) {
cases := []struct {
Title string
InputVal interface{}
Expected bool
}{
{
Title: "Int",
InputVal: int(1),
Expected: true,
},
{
Title: "int8",
InputVal: int8(1),
Expected: true,
},
{
Title: "uint8",
InputVal: uint8(1),
Expected: true,
},
{
Title: "int16",
InputVal: int16(1),
Expected: true,
},
{
Title: "uint16",
InputVal: uint16(1),
Expected: true,
},
{
Title: "int32",
InputVal: int32(1),
Expected: true,
},
{
Title: "uint32",
InputVal: uint32(1),
Expected: true,
},
{
Title: "int64",
InputVal: int64(1),
Expected: true,
},
{
Title: "uint64",
InputVal: uint64(1),
Expected: true,
},
{
Title: "float32",
InputVal: float32(1.1),
Expected: true,
},
{
Title: "float64",
InputVal: float64(1.1),
Expected: true,
},
{
Title: "string",
InputVal: "1.1",
Expected: false,
},
{
Title: "bool",
InputVal: true,
Expected: false,
},
{
Title: "Untyped Nil",
InputVal: nil,
Expected: false,
},
{
Title: "Typed Nil",
InputVal: (*int)(nil),
Expected: false,
},
{
Title: "Interface",
InputVal: interface{}(int(1)),
Expected: true,
},
}
for _, test := range cases {
t.Run(test.Title, func(t *testing.T) {
isNumber := isNumericType(test.InputVal)
assert.Equal(t, test.Expected, isNumber)
})
}
}
func TestIsCompatibleInt(t *testing.T) {
cases := []struct {
Title string
InputVal interface{}
InputType reflect.Type
Expected bool
}{
{
Title: "Valid",
InputVal: 1,
InputType: reflect.TypeOf(float64(0)),
Expected: true,
},
{
Title: "Too Big",
InputVal: 300,
InputType: reflect.TypeOf(int8(0)),
Expected: false,
},
{
Title: "String",
InputVal: "1",
InputType: reflect.TypeOf(int8(0)),
Expected: false,
},
{
Title: "Nested Int",
InputVal: interface{}(int64(1)),
InputType: reflect.TypeOf(int8(0)),
Expected: true,
},
{
Title: "Bool",
InputVal: true,
InputType: reflect.TypeOf(int8(0)),
Expected: false,
},
}
for _, test := range cases {
t.Run(test.Title, func(t *testing.T) {
isNumber := isCompatibleInt(test.InputVal, test.InputType)
assert.Equal(t, test.Expected, isNumber)
})
}
}
func TestHasDecimal(t *testing.T) {
cases := []struct {
Title string
InputVal float64
Expected bool
}{
{
Title: "Yes",
InputVal: 1.000005,
Expected: true,
},
{
Title: "No",
InputVal: 1.0,
Expected: false,
},
}
for _, test := range cases {
t.Run(test.Title, func(t *testing.T) {
hasDecimal := hasDecimalPlace(test.InputVal)
assert.Equal(t, test.Expected, hasDecimal)
})
}
}