forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv_test.go
168 lines (140 loc) · 3.77 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
package goutil_test
import (
"math"
"reflect"
"testing"
"github.com/gookit/goutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestToBool(t *testing.T) {
is := assert.New(t)
blVal, err := goutil.ToBool("1")
is.Nil(err)
is.True(blVal)
blVal = goutil.Bool("1")
is.True(blVal)
is.False(goutil.Bool(false))
is.False(goutil.Bool(1))
}
func TestToString(t *testing.T) {
is := assert.New(t)
str, err := goutil.ToString(23)
is.Nil(err)
is.Eq("23", str)
str = goutil.String(23)
is.Eq("23", str)
}
func TestToInt(t *testing.T) {
is := assert.New(t)
// To int
iVal, err := goutil.ToInt("2")
is.Nil(err)
is.Eq(int(2), iVal)
iVal = goutil.Int("-2")
is.Nil(err)
is.Eq(int(-2), iVal)
// To int64
i64Val, err := goutil.ToInt64("2")
is.Nil(err)
is.Eq(int64(2), i64Val)
i64Val = goutil.Int64("-2")
is.Nil(err)
is.Eq(int64(-2), i64Val)
// To uint
uVal, err := goutil.ToUint("2")
is.Nil(err)
is.Eq(uint(2), uVal)
uVal = goutil.Uint("2")
is.Nil(err)
is.Eq(uint(2), uVal)
// To uint64
u64Val, err := goutil.ToUint64("2")
is.Nil(err)
is.Eq(uint64(2), u64Val)
u64Val = goutil.Uint64("2")
is.Nil(err)
is.Eq(uint64(2), u64Val)
}
func TestBaseTypeVal(t *testing.T) {
is := assert.New(t)
val, err := goutil.BaseTypeVal(uint64(23))
is.NoErr(err)
is.Eq(uint64(23), val)
val, err = goutil.BaseTypeVal(23)
is.NoErr(err)
is.Eq(int64(23), val)
val, err = goutil.BaseTypeVal(nil)
is.Err(err)
is.Nil(val)
}
func TestConvTo(t *testing.T) {
is := assert.New(t)
tests := []struct {
val any
kind reflect.Kind
out any
ok bool
}{
// success
{"23", reflect.Int, 23, true},
{"23", reflect.Int8, int8(23), true},
{"23", reflect.Uint8, uint8(23), true},
{"23", reflect.Int16, int16(23), true},
{"23", reflect.Uint16, uint16(23), true},
{"23", reflect.Int32, int32(23), true},
{"23", reflect.Uint32, uint32(23), true},
{"23", reflect.Int64, int64(23), true},
{"23", reflect.Uint64, uint64(23), true},
{"23", reflect.Float64, float64(23), true},
{"23", reflect.Float32, float32(23), true},
{"23", reflect.String, "23", true},
{"true", reflect.Bool, true, true},
{nil, reflect.Int, 0, true},
{nil, reflect.Uint, uint(0), true},
// failed
{"23", reflect.Bool, nil, false},
{"abc", reflect.Float64, nil, false},
{"abc", reflect.Float32, nil, false},
{"abc", reflect.Int, nil, false},
{"abc", reflect.Int8, nil, false},
{"abc", reflect.Int16, nil, false},
{"abc", reflect.Int32, nil, false},
{"abc23", reflect.Int64, nil, false},
{"abc", reflect.Uint, nil, false},
{"abc", reflect.Uint8, nil, false},
{"abc", reflect.Uint16, nil, false},
{"abc", reflect.Uint32, nil, false},
{"abc45", reflect.Uint64, nil, false},
// overflow
{uint64(math.MaxInt + 2), reflect.Int, nil, false},
{uint64(math.MaxInt8 + 2), reflect.Int8, nil, false},
{uint64(math.MaxUint8 + 2), reflect.Uint8, nil, false},
{uint64(math.MaxInt16 + 2), reflect.Int16, nil, false},
{uint64(math.MaxUint16 + 2), reflect.Uint16, nil, false},
{uint64(math.MaxInt32 + 2), reflect.Int32, nil, false},
{uint64(math.MaxUint32 + 2), reflect.Uint32, nil, false},
{math.MaxFloat32 * 2.0, reflect.Float32, nil, false},
}
// test for goutil.ConvTo
for _, item := range tests {
val, err := goutil.ConvTo(item.val, item.kind)
if item.ok {
is.NoErr(err)
is.Eq(item.out, val)
} else {
is.Err(err, "val: %v, kind: %v", item.val, item.kind)
}
}
t.Run("extra func", func(t *testing.T) {
// SafeConv
is.Eq(23, goutil.SafeConv("23", reflect.Int))
is.Eq(23, goutil.SafeKind("23", reflect.Int))
is.Eq(nil, goutil.SafeKind("abc", reflect.Int))
// ConvOrDefault
is.Eq(23, goutil.ConvOrDefault("23", reflect.Int, 0))
is.Eq(23, goutil.ConvOrDefault("abc", reflect.Int, 23))
})
// ToKind
_, err := goutil.ToKind([]int{23}, reflect.Int, nil)
is.Err(err)
}