-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
201 lines (196 loc) · 4.43 KB
/
string_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
package xconv
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestStringInt(t *testing.T) {
i , err := StringInt("123")
assert.Equal(t, int(123), i)
assert.Equal(t, nil, err)
}
func TestStringInt64(t *testing.T) {
{
i64 , err := StringInt64("123")
assert.Equal(t, int64(123), i64)
assert.Equal(t, nil, err)
}
{
{
i64 , err := StringInt64("-123")
assert.Equal(t, int64(-123), i64)
assert.Equal(t, nil, err)
}
}
}
func TestStringFloat64(t *testing.T) {
i , err := StringFloat64("123.1")
assert.Equal(t, float64(123.1), i)
assert.Equal(t, nil, err)
}
func TestStringFloat32(t *testing.T) {
i , err := StringFloat32("123.1")
assert.Equal(t, float32(123.1), i)
assert.Equal(t, nil, err)
}
func TestStringBool(t *testing.T) {
{
sList := []string{"true", "True","1", "t", "T"}
for i:=0 ; i< len(sList) ; i++ {
b, err := StringBool(sList[0])
assert.Equal(t, b, true)
assert.Equal(t, err, nil)
}
}
{
sList := []string{"false", "False","f", "F", "0"}
for i:=0 ; i< len(sList) ; i++ {
b, err := StringBool(sList[0])
assert.Equal(t, b, false)
assert.Equal(t, err, nil)
}
}
{
sList := []string{"asd", "","3t", "2e3f", "sd"}
for i:=0 ; i< len(sList) ; i++ {
b, err := StringBool(sList[0])
assert.Equal(t, b, false)
assert.EqualError(t, err, "goclub/conv: " + sList[0] + " can't conv to bool")
}
}
}
func TestStringToReflect(t *testing.T) {
type Data struct {
s string
i int
i8 int8
i16 int16
i32 int32
i64 int64
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
bool bool
f32 float32
f64 float64
bytes []byte
}
data := Data{}
assert.NoError(t,
StringToReflect("s", reflect.ValueOf(&data.s)),
)
assert.NoError(t,
StringToReflect("-1", reflect.ValueOf(&data.i)),
)
assert.NoError(t,
StringToReflect("-2", reflect.ValueOf(&data.i8)),
)
assert.NoError(t,
StringToReflect("-3", reflect.ValueOf(&data.i16)),
)
assert.NoError(t,
StringToReflect("-4", reflect.ValueOf(&data.i32)),
)
assert.NoError(t,
StringToReflect("-5", reflect.ValueOf(&data.i64)),
)
assert.NoError(t,
StringToReflect("1", reflect.ValueOf(&data.ui)),
)
assert.NoError(t,
StringToReflect("2", reflect.ValueOf(&data.ui8)),
)
assert.NoError(t,
StringToReflect("3", reflect.ValueOf(&data.ui16)),
)
assert.NoError(t,
StringToReflect("4", reflect.ValueOf(&data.ui32)),
)
assert.NoError(t,
StringToReflect("5", reflect.ValueOf(&data.ui64)),
)
assert.NoError(t,
StringToReflect("true", reflect.ValueOf(&data.bool)),
)
assert.NoError(t,
StringToReflect("0.1", reflect.ValueOf(&data.f32)),
)
assert.NoError(t,
StringToReflect("0.2", reflect.ValueOf(&data.f64)),
)
assert.NoError(t,
StringToReflect("b我", reflect.ValueOf(&data.bytes)),
)
assert.Equal(t,data, Data{"s", -1, -2, -3, -4, -5, 1, 2, 3, 4, 5, true, 0.1, 0.2, []byte("b我")})
{
{
data := struct {
Name string
}{}
assert.Errorf(t, StringToReflect("nimoc", reflect.ValueOf(data).Field(0)), "goclub/conv: StringToReflect(s, rValue) rValue must can set, mu be you should use reflect.ValueOf(pointer)")
}
{
data := struct {
Name string
}{}
assert.Errorf(t, StringToReflect("nimoc", reflect.ValueOf(data.Name)), "goclub/conv: StringToReflect(s, rValue) rValue must can set, mu be you should use reflect.ValueOf(pointer)")
}
{
data := struct {
Name string
}{}
assert.NoError(t,
StringToReflect("nimoc", reflect.ValueOf(&data).Elem().Field(0)),
)
assert.Equal(t,data.Name, "nimoc")
}
{
data := struct {
Name string
}{}
assert.NoError(t,
StringToReflect("nimoc", reflect.ValueOf(&data.Name)),
)
assert.Equal(t,data.Name, "nimoc")
}
}
}
func TestReflectToString(t *testing.T) {
type Data struct {
s string
i int
i8 int8
i16 int16
i32 int32
i64 int64
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
bool bool
f32 float32
f64 float64
bytes []byte
}
data := Data{"s", -1, -2, -3, -4, -5, 1, 2, 3, 4, 5, true, 0.1, 0.2, []byte("b我")}
testdata := []string{"s", "-1", "-2", "-3", "-4","-5", "1", "2", "3", "4", "5", "1", "0.1", "0.2", "b我"}
for i, v := range testdata {
item := reflect.ValueOf(data).Field(i)
value, err := ReflectToString(item)
assert.NoError(t, err)
assert.Equal(t, value, v)
}
}
func TestStringPointerToString(t *testing.T) {
{
var a *string
assert.Equal(t,StringPointerString(a), "")
}
var a *string
var b = "abc"
a= &b
assert.Equal(t,StringPointerString(a), "abc")
}