Repository navigation
Expand file tree
/
Copy pathtypeutil_test.go
More file actions
378 lines (267 loc) · 6.01 KB
/
Copy pathtypeutil_test.go
File metadata and controls
378 lines (267 loc) · 6.01 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package typeutil
import (
"math"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
func TestIsNil(t *testing.T) {
t.Parallel()
t.Run("not nil", func(t *testing.T) {
t.Parallel()
got := IsNil("string")
require.False(t, got)
})
t.Run("nil value", func(t *testing.T) {
t.Parallel()
got := IsNil(nil)
require.True(t, got)
})
t.Run("nil chan", func(t *testing.T) {
t.Parallel()
var nilChan chan int
got := IsNil(nilChan)
require.True(t, got)
})
t.Run("nil func", func(t *testing.T) {
t.Parallel()
var nilFunc func()
got := IsNil(nilFunc)
require.True(t, got)
})
t.Run("nil pointer to any", func(t *testing.T) {
t.Parallel()
var nilPtrToAny *any
got := IsNil(nilPtrToAny)
require.True(t, got)
})
t.Run("nil map", func(t *testing.T) {
t.Parallel()
var nilMap map[int]int
got := IsNil(nilMap)
require.True(t, got)
})
t.Run("nil slice", func(t *testing.T) {
t.Parallel()
var nilSlice []int
got := IsNil(nilSlice)
require.True(t, got)
})
t.Run("nil pointer", func(t *testing.T) {
t.Parallel()
var nilPointer *int
got := IsNil(nilPointer)
require.True(t, got)
})
t.Run("nil unsafe pointer", func(t *testing.T) {
t.Parallel()
var nilUnsafe unsafe.Pointer
got := IsNil(nilUnsafe)
require.True(t, got)
})
// Headline case from the package doc: a nil concrete pointer stored in a
// non-nil interface. IsNil sees through it, whereas i == nil would be false.
t.Run("nil struct pointer wrapped in interface", func(t *testing.T) {
t.Parallel()
type myStruct struct{}
var p *myStruct
got := IsNil(any(p))
require.True(t, got)
})
t.Run("non-nil nilable kinds return false", func(t *testing.T) {
t.Parallel()
n := 1
ch := make(chan int)
tests := []struct {
name string
value any
}{
{name: "non-nil pointer", value: &n},
{name: "non-nil map", value: map[int]int{}},
{name: "non-nil slice", value: []int{}},
{name: "non-nil func", value: func() {}},
{name: "non-nil chan", value: ch},
{name: "non-nil unsafe pointer", value: unsafe.Pointer(&n)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
require.False(t, IsNil(tt.value))
})
}
})
}
func TestIsZero(t *testing.T) {
t.Parallel()
t.Run("not empty string", func(t *testing.T) {
t.Parallel()
got := IsZero("string")
require.False(t, got)
})
t.Run("empty string", func(t *testing.T) {
t.Parallel()
var emptyString string
got := IsZero(emptyString)
require.True(t, got)
})
t.Run("nil chan", func(t *testing.T) {
t.Parallel()
var nilChan chan int
got := IsZero(nilChan)
require.True(t, got)
})
t.Run("nil func", func(t *testing.T) {
t.Parallel()
var nilFunc func()
got := IsZero(nilFunc)
require.True(t, got)
})
t.Run("nil pointer to any", func(t *testing.T) {
t.Parallel()
var nilPtrToAny *any
got := IsZero(nilPtrToAny)
require.True(t, got)
})
t.Run("nil map", func(t *testing.T) {
t.Parallel()
var nilMap map[int]int
got := IsZero(nilMap)
require.True(t, got)
})
t.Run("nil slice", func(t *testing.T) {
t.Parallel()
var nilSlice []int
got := IsZero(nilSlice)
require.True(t, got)
})
t.Run("nil pointer", func(t *testing.T) {
t.Parallel()
var nilPointer *int
got := IsZero(nilPointer)
require.True(t, got)
})
t.Run("non-zero int", func(t *testing.T) {
t.Parallel()
require.False(t, IsZero(1))
})
t.Run("non-nil pointer", func(t *testing.T) {
t.Parallel()
n := 0
require.False(t, IsZero(&n))
})
t.Run("non-nil slice", func(t *testing.T) {
t.Parallel()
require.False(t, IsZero([]int{}))
})
t.Run("non-zero struct", func(t *testing.T) {
t.Parallel()
require.False(t, IsZero(struct{ A int }{A: 1}))
})
t.Run("true bool", func(t *testing.T) {
t.Parallel()
require.False(t, IsZero(true))
})
t.Run("negative zero float is zero", func(t *testing.T) {
t.Parallel()
require.True(t, IsZero(math.Copysign(0, -1)))
})
t.Run("NaN is not zero", func(t *testing.T) {
t.Parallel()
require.False(t, IsZero(math.NaN()))
})
}
func TestZero(t *testing.T) {
t.Parallel()
t.Run("string", func(t *testing.T) {
t.Parallel()
v := "test"
got := Zero(v)
require.Empty(t, got)
})
t.Run("slice", func(t *testing.T) {
t.Parallel()
var nilSlice []int
v := []int{1, 2, 3}
got := Zero(v)
require.Equal(t, nilSlice, got)
})
}
func TestValue(t *testing.T) {
t.Parallel()
var nilPtr *int
got := Value(nilPtr)
require.Equal(t, 0, got)
tests := []struct {
name string
value any
}{
{
name: "int",
value: 1,
},
{
name: "string",
value: "test",
},
{
name: "slice",
value: []int{1, 2},
},
{
name: "map",
value: map[string]string{"one": "alpha", "two": "beta"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := Value(&tt.value)
require.Equal(t, tt.value, got)
})
}
}
func TestBoolToInt(t *testing.T) {
t.Parallel()
t.Run("true", func(t *testing.T) {
t.Parallel()
got := BoolToInt(true)
require.Equal(t, 1, got)
})
t.Run("false", func(t *testing.T) {
t.Parallel()
got := BoolToInt(false)
require.Equal(t, 0, got)
})
}
func TestBoolToNum(t *testing.T) {
t.Parallel()
t.Run("int true", func(t *testing.T) {
t.Parallel()
require.Equal(t, 1, BoolToNum[int](true))
})
t.Run("int false", func(t *testing.T) {
t.Parallel()
require.Equal(t, 0, BoolToNum[int](false))
})
t.Run("int8 true", func(t *testing.T) {
t.Parallel()
require.Equal(t, int8(1), BoolToNum[int8](true))
})
t.Run("uint64 false", func(t *testing.T) {
t.Parallel()
require.Equal(t, uint64(0), BoolToNum[uint64](false))
})
t.Run("float64 true", func(t *testing.T) {
t.Parallel()
require.InDelta(t, 1.0, BoolToNum[float64](true), 1e-9)
})
t.Run("float64 false", func(t *testing.T) {
t.Parallel()
require.InDelta(t, 0.0, BoolToNum[float64](false), 1e-9)
})
t.Run("custom numeric type", func(t *testing.T) {
t.Parallel()
type myInt int
require.Equal(t, myInt(1), BoolToNum[myInt](true))
})
}