-
Notifications
You must be signed in to change notification settings - Fork 8
/
string_test.go
180 lines (165 loc) · 4.89 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
package cvt_test
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"math/big"
"testing"
"time"
"github.com/shockerli/cvt"
)
func TestString_HasDefault(t *testing.T) {
tests := []struct {
input interface{}
def string
expect string
}{
// supported value, def is not used, def != expect
{"hello", "world", "hello"},
{uint64(8), "xxx", "8"},
{float32(8.31), "xxx", "8.31"},
{float64(-8.31), "xxx", "-8.31"},
{true, "xxx", "true"},
{int64(-8), "xxx", "-8"},
{[]byte("8.01"), "xxx", "8.01"},
{[]rune("我❤️中国"), "xxx", "我❤️中国"},
{nil, "xxx", ""},
{aliasTypeInt0, "xxx", "0"},
{&aliasTypeString8d15Minus, "xxx", "-8.15"},
{aliasTypeBool4True, "xxx", "true"},
{errors.New("errors"), "xxx", "errors"},
{time.Friday, "xxx", "Friday"},
{big.NewInt(123), "xxx", "123"},
{TestMarshalJSON{}, "xxx", "MarshalJSON"},
{&TestMarshalJSON{}, "xxx", "MarshalJSON"},
// unsupported value, def == expect
{testing.T{}, "xxx", "xxx"},
{&testing.T{}, "xxx", "xxx"},
{[]int{}, "xxx", "xxx"},
{[]string{}, "xxx", "xxx"},
{[...]string{}, "xxx", "xxx"},
{map[int]string{}, "xxx", "xxx"},
}
for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect)
v := cvt.String(tt.input, tt.def)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}
func TestString_BaseLine(t *testing.T) {
tests := []struct {
input interface{}
expect string
}{
{testing.T{}, ""},
{&testing.T{}, ""},
{[]int{}, ""},
{[]string{}, ""},
{[...]string{}, ""},
{map[int]string{}, ""},
}
for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)
v := cvt.String(tt.input)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}
func TestStringE(t *testing.T) {
tests := []struct {
input interface{}
expect string
isErr bool
}{
{int(8), "8", false},
{int8(8), "8", false},
{int16(8), "8", false},
{int32(8), "8", false},
{int64(8), "8", false},
{uint(8), "8", false},
{uint8(8), "8", false},
{uint16(8), "8", false},
{uint32(8), "8", false},
{uint64(8), "8", false},
{float32(8.31), "8.31", false},
{float64(8.31), "8.31", false},
{true, "true", false},
{false, "false", false},
{int(-8), "-8", false},
{int8(-8), "-8", false},
{int16(-8), "-8", false},
{int32(-8), "-8", false},
{int64(-8), "-8", false},
{float32(-8.31), "-8.31", false},
{float64(-8.31), "-8.31", false},
{"hello world!", "hello world!", false},
{[]byte("-8"), "-8", false},
{[]byte("-8.01"), "-8.01", false},
{[]byte("8"), "8", false},
{[]byte("8.00"), "8.00", false},
{[]byte("8.01"), "8.01", false},
{[]rune("我❤️中国"), "我❤️中国", false},
{nil, "", false},
{pointerInterNil, "", false},
{AliasTypeBytesNil, "", false},
{&AliasTypeBytesNil, "", false},
{aliasTypeInt0, "0", false},
{&aliasTypeInt0, "0", false},
{aliasTypeInt1, "1", false},
{&aliasTypeInt1, "1", false},
{aliasTypeString0, "0", false},
{&aliasTypeString0, "0", false},
{aliasTypeString1, "1", false},
{&aliasTypeString1, "1", false},
{aliasTypeString8d15, "8.15", false},
{&aliasTypeString8d15, "8.15", false},
{aliasTypeString8d15Minus, "-8.15", false},
{&aliasTypeString8d15Minus, "-8.15", false},
{aliasTypeBool4True, "true", false},
{&aliasTypeBool4True, "true", false},
{aliasTypeBool4False, "false", false},
{&aliasTypeBool4False, "false", false},
{AliasTypeBytes("hello"), "hello", false},
{&pointerRunes, "中国", false},
{AliasTypeUint(12), "12", false},
{AliasTypeUint8(12), "12", false},
{AliasTypeUint16(12), "12", false},
{AliasTypeUint32(12), "12", false},
{AliasTypeUint64(12), "12", false},
{AliasTypeInt(-12), "-12", false},
{AliasTypeInt8(-12), "-12", false},
{AliasTypeInt16(-12), "-12", false},
{AliasTypeInt32(-12), "-12", false},
{AliasTypeInt64(-12), "-12", false},
{AliasTypeFloat32(-12.34), "-12.34", false},
{AliasTypeFloat64(12.34), "12.34", false},
{errors.New("errors"), "errors", false},
{time.Friday, "Friday", false},
{big.NewInt(123), "123", false},
{TestMarshalJSON{}, "MarshalJSON", false},
{&TestMarshalJSON{}, "MarshalJSON", false},
{template.URL("https://host.foo"), "https://host.foo", false},
{template.HTML("<html></html>"), "<html></html>", false},
{json.Number("12.34"), "12.34", false},
// errors
{testing.T{}, "", true},
{&testing.T{}, "", true},
{[]int{}, "", true},
{[]string{}, "", true},
{[...]string{}, "", true},
{map[int]string{}, "", true},
}
for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%v], isErr[%v]", i, tt.input, tt.expect, tt.isErr)
v, err := cvt.StringE(tt.input)
if tt.isErr {
assertError(t, err, "[HasErr] "+msg)
continue
}
assertNoError(t, err, "[NoErr] "+msg)
assertEqual(t, tt.expect, v, "[WithE] "+msg)
// Non-E test
v = cvt.String(tt.input)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}