-
Notifications
You must be signed in to change notification settings - Fork 4
/
conv_test.go
407 lines (343 loc) · 9.18 KB
/
conv_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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package conv
import (
"testing"
"github.com/issue9/assert/v4"
)
func TestMustBool(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.True(MustBool("on", false))
a.True(MustBool(true, false))
a.True(MustBool("123", false))
a.True(MustBool(123, false))
a.True(MustBool(-123, false))
a.True(MustBool(-1.23, false))
a.False(MustBool("off", true))
a.False(MustBool(false, true))
a.True(MustBool("1"))
a.True(MustBool("true"))
a.False(MustBool("0"))
a.False(MustBool("false"))
// 不可解析
a.False(MustBool("str", false))
a.True(MustBool(";adf", true))
a.Panic(func() {
MustBool("str")
})
}
func TestMustInt(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustInt("123", 456), 123)
a.Equal(MustInt(true, 5), 1)
a.Equal(MustInt(false, 5), 0)
a.Equal(MustInt(123, 456), 123)
a.Equal(MustInt(uint(8), 9), 8)
a.Equal(MustInt(uint64(33), 99), 33)
a.Equal(MustInt(int64(-123), 99), -123)
a.Equal(MustInt(1.23, 99), 1)
a.Equal(MustInt(-1.23, 99), -1)
a.Equal(MustInt([]byte("123"), 44), 123)
a.Equal(MustInt("1"), 1)
// 不可解析
a.Equal(MustInt(";sdf", 45), 45)
a.Equal(MustInt("str", 45), 45)
a.Panic(func() {
MustInt(";str")
})
}
func TestMustInt64(t *testing.T) {
// 可解析
a := assert.New(t, false)
a.Equal(MustInt64("123", 456), int64(123))
a.Equal(MustInt64(true, 5), int64(1))
a.Equal(MustInt64(false, 5), int64(0))
a.Equal(MustInt64(123, 456), int64(123))
a.Equal(MustInt64(uint(8), 9), int64(8))
a.Equal(MustInt64(uint64(33), 99), int64(33))
a.Equal(MustInt64(int64(-123), 99), int64(-123))
a.Equal(MustInt64(1.23, 99), int64(1))
a.Equal(MustInt64(-1.23, 99), int64(-1))
a.Equal(MustInt64([]byte("123"), 44), int64(123))
a.Equal(MustInt64([]byte("1607957811"), 44), int64(1607957811))
a.Equal(MustInt64("1"), 1)
// 不可解析
a.Equal(MustInt64(";sdf", 45), int64(45))
a.Equal(MustInt64("str", 45), 45)
a.Panic(func() {
MustInt64("str")
})
}
func TestMustUint32(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustUint32("123", 456), uint32(123))
a.Equal(MustUint32(true, 5), uint32(1))
a.Equal(MustUint32(false, 5), uint32(0))
a.Equal(MustUint32(123, 456), uint32(123))
a.Equal(MustUint32(uint(8), 9), uint32(8))
a.Equal(MustUint32(uint64(33), 99), uint32(33))
a.Equal(MustUint32(1.23, 99), uint32(1))
a.Equal(MustUint32([]byte("123"), 44), uint32(123))
a.Equal(MustUint32("1"), 1)
// 不可解析
a.Equal(MustUint32(int64(-123), 99), uint32(99))
a.Equal(MustUint32(-1.23, 99), uint32(99))
a.Equal(MustUint32(";sdf", 45), uint32(45))
a.Equal(MustUint32("str", 45), uint32(45))
a.Panic(func() {
MustUint32("str")
})
}
func TestMustUint64(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustUint64("123", 456), uint64(123))
a.Equal(MustUint64(true, 5), uint64(1))
a.Equal(MustUint64(false, 5), uint64(0))
a.Equal(MustUint64(123, 456), uint64(123))
a.Equal(MustUint64(uint(8), 9), uint64(8))
a.Equal(MustUint64(uint64(33), 99), uint64(33))
a.Equal(MustUint64(1.23, 99), uint64(1))
a.Equal(MustUint64([]byte("123"), 44), uint64(123))
a.Equal(MustUint64("1"), 1)
// 不可解析
a.Equal(MustUint64(int64(-123), 99), uint64(99))
a.Equal(MustUint64(-1.23, 99), uint64(99))
a.Equal(MustUint64(";sdf", 45), uint64(45))
a.Equal(MustUint64("str", 45), uint64(45))
a.Panic(func() {
MustUint64("str")
})
}
func TestMustFloat32(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustFloat32("123", 456), float32(123))
a.Equal(MustFloat32(true, 5), float32(1))
a.Equal(MustFloat32(false, 5), float32(0))
a.Equal(MustFloat32(123, 456), float32(123))
a.Equal(MustFloat32(int64(-123), 99), float32(-123.0))
a.Equal(MustFloat32(uint(8), 9), float32(8))
a.Equal(MustFloat32(uint64(33), 99), float32(33))
a.Equal(MustFloat32([]byte("123"), 44), float32(123))
a.Equal(MustFloat32("1"), 1)
// 不可解析
a.Equal(MustFloat32(";sdf", 45), 45)
a.Equal(MustFloat32("str", 45), 45)
a.Panic(func() {
MustFloat32("str")
})
}
func TestMustString(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustString(123, "222"), "123")
a.Equal(MustString(-11, "22"), "-11")
a.Equal(MustString(-11.111, "22"), "-11.111")
a.Equal(MustString(true, "22"), "true")
a.Equal(MustString(1), "1")
// 不可解析
a.Equal(MustString([]int{1}, "22"), "22")
a.Panic(func() {
MustString([]int{1})
})
}
func TestMustBytes(t *testing.T) {
a := assert.New(t, false)
// 可解析
a.Equal(MustBytes("123", []byte("456")), []byte{49, 50, 51})
a.Equal(MustBytes(123, []byte("456")), []byte{49, 50, 51})
a.Equal(MustBytes(uint(123), []byte("456")), []byte{49, 50, 51})
a.Equal(MustBytes(123), []byte("123"))
// 不可解析
a.Equal(MustBytes([]int{1}, []byte("123")), []byte{49, 50, 51})
a.PanicString(func() {
MustBytes([]int{1})
}, "conv: []int:[1] 无法转换成 bytes 类型")
}
func TestMustSlice(t *testing.T) {
def := []any{4, 5, 5}
a := assert.New(t, false)
// 可解析
a.Equal(MustSlice([]int{1, 2, 3}, def), []any{int(1), int(2), int(3)})
a.Equal(MustSlice([]uint64{1, 2, 3}, def), []any{uint64(1), uint64(2), uint64(3)})
a.Equal(MustSlice([]any{"1", 2, 3.0}, def), []any{"1", 2, 3.0})
a.Equal(MustSlice([]string{"1", "2", "3"}, def), []any{"1", "2", "3"})
a.Equal(MustSlice([]byte("123"), def), []any{byte(49), byte(50), byte(51)})
a.Equal(MustSlice("123", def), []any{rune(49), rune(50), rune(51)})
a.Equal(MustSlice([]int{1, 2, 3}), []any{int(1), int(2), int(3)})
a.Equal(MustSlice([]uint64{1, 2, 3}), []any{uint64(1), uint64(2), uint64(3)})
// 不可解析
a.Equal(MustSlice(7, def), []any{4, 5, 5})
a.PanicString(func() {
MustSlice(7)
}, "conv: int:7 无法转换成 slice 类型")
}
func TestBool(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result bool) {
ret, err := Bool(val)
a.Equal(ret, result)
a.Nil(err)
}
fn(5, true)
fn(int64(-11), true)
fn(int32(5), true)
fn(uint(3), true)
fn(uint64(0), false)
fn(1.32, true)
fn(0.00, false)
fn("off", false)
fn("true", true)
fn(-1.3, true)
}
func TestInt(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result int) {
ret, err := Int(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, 5)
fn(int64(-11), -11)
fn(int32(5), 5)
fn(uint(3), 3)
fn(uint64(0), 0)
fn(1.32, 1)
fn(0.00, 0)
fn(-1.3, -1)
fn("-1.1", -1)
fn("123", 123)
}
func TestInt64(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result int64) {
ret, err := Int64(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, 5)
fn(int(-11), -11)
fn(int32(5), 5)
fn(uint(3), 3)
fn(uint64(0), 0)
fn(1.32, 1)
fn(0.00, 0)
fn(-1.3, -1)
fn("-1.1", -1)
fn("123", 123)
}
func TestUint(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result uint) {
ret, err := Uint(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, 5)
fn(int32(5), 5)
fn(uint64(0), 0)
fn(1.32, 1)
fn(0.00, 0)
fn("1.1", 1)
fn("123", 123)
}
func TestUint64(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result uint64) {
ret, err := Uint64(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, 5)
fn(int32(5), 5)
fn(uint(0), 0)
fn(1.32, 1)
fn(0.00, 0)
fn("1.1", 1)
fn("123", 123)
}
func TestFloat32(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result float32) {
ret, err := Float32(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, 5)
fn(int32(5), 5)
fn(uint(0), 0)
fn(1.32, 1.32)
fn("0.00", 0)
fn("1.1", 1.1)
fn("123", 123)
}
func TestString(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result string) {
ret, err := String(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(5, "5")
fn(int32(5), "5")
fn(uint(0), "0")
fn(1.32, "1.32")
fn("0.00", "0.00")
fn(-1.1, "-1.1")
fn(1.0, "1")
}
func TestBytes(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result []byte) {
ret, err := Bytes(val)
a.Nil(err)
a.Equal(ret, result)
}
fn(11, []byte{49, 49})
fn("1.11", []byte{49, 46, 49, 49})
fn(-1.11, []byte{45, 49, 46, 49, 49, 48, 48, 48})
fn(0, []byte{48})
}
func TestSlice(t *testing.T) {
a := assert.New(t, false)
fn := func(val any, result []any) {
ret, err := Slice(val)
a.Nil(err)
a.Equal(ret, result)
}
fn("123", []any{int32(49), int32(50), int32(51)})
fn([]int{1, 2, 3}, []any{int(1), int(2), int(3)})
fn([]string{"1", "ss"}, []any{"1", "ss"})
}
func TestSliceOf(t *testing.T) {
a := assert.New(t, false)
// int ==>int
ret1, err := SliceOf[int]([]int{1, 2, 3})
a.NotError(err).Equal(ret1, []int{1, 2, 3})
// int ==> int8
ret2, err := SliceOf[int8]([]int{1, 2, 3})
a.NotError(err).Equal(ret2, []int8{1, 2, 3})
// int ==> any
ret3, err := SliceOf[any]([]int{1, 2, 3})
a.NotError(err).Equal(ret3, []any{1, 2, 3})
// int ==> int8,溢出。
ret4, err := SliceOf[int8]([]int{1000, 2, 3})
a.NotError(err).Equal(ret4, []int8{-24, 2, 3})
// []string ==> int
ret5, err := SliceOf[int]([]string{"1", "2", "3"})
a.NotError(err).Equal(ret5, []int{1, 2, 3})
// any ==> int
ret6, err := SliceOf[int]([]any{1, 2, "3"})
a.NotError(err).Equal(ret6, []int{1, 2, 3})
// string ==> int
ret7, err := SliceOf[int]("123")
a.NotError(err).Equal(ret7, []int{'1', '2', '3'})
// string ==> byte
ret8, err := SliceOf[byte]("123")
a.NotError(err).Equal(ret8, []byte{'1', '2', '3'})
}