-
Notifications
You must be signed in to change notification settings - Fork 0
/
caster_test.go
217 lines (185 loc) · 3.77 KB
/
caster_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
package caster_test
import (
"testing"
"github.com/gomig/caster"
)
func TestIsNil(t *testing.T) {
c := caster.NewCaster(nil)
if !c.IsNil() {
t.Fatal("failed")
}
}
func TestSlice(t *testing.T) {
c := caster.NewCaster([]any{false, "A", 1.234})
if len(c.Slice()) != 3 {
t.Fatal("failed")
}
}
func TestBool(t *testing.T) {
c := caster.NewCaster(true)
if c.BoolSafe(false) == false {
t.Fatal("failed")
}
}
func TestBoolSlice(t *testing.T) {
c := caster.NewCaster([]any{false, true, true})
if c.BoolSlice(nil) == nil {
t.Fatal("failed")
}
}
func TestInt(t *testing.T) {
c := caster.NewCaster(5331)
if c.IntSafe(1) == 1 {
t.Fatal("failed")
}
}
func TestIntSlice(t *testing.T) {
c := caster.NewCaster([]any{4, 5, 6})
if c.IntSlice(nil) == nil {
t.Fatal("failed")
}
}
func TestIn8(t *testing.T) {
c := caster.NewCaster(300)
if c.Int8Safe(2) != 2 {
t.Fatal("failed")
}
}
func TestInt8Slice(t *testing.T) {
c := caster.NewCaster([]any{25, 10, 12})
if c.Int8Slice(nil) == nil {
t.Fatal("failed")
}
}
func TestInt16(t *testing.T) {
c := caster.NewCaster(120)
if c.Int16Safe(3) == 3 {
t.Fatal("failed")
}
}
func TestInt16Slice(t *testing.T) {
c := caster.NewCaster([]any{10, 20, 30})
if c.Int16Slice(nil) == nil {
t.Fatal("failed")
}
}
func TestInt32(t *testing.T) {
c := caster.NewCaster(555)
if c.Int32Safe(9) == 9 {
t.Fatal("failed")
}
}
func TestInt32Slice(t *testing.T) {
c := caster.NewCaster([]any{5, 9, 100})
if c.Int32Slice(nil) == nil {
t.Fatal("failed")
}
}
func TestInt64(t *testing.T) {
c := caster.NewCaster(120.99)
if c.Int64Safe(8) != 8 {
t.Fatal("failed")
}
}
func TestInt64Slice(t *testing.T) {
c := caster.NewCaster([]any{2131, 55555})
if c.Int64Slice(nil) == nil {
t.Fatal("failed")
}
}
func TestUInt(t *testing.T) {
c := caster.NewCaster(-100)
if c.UIntSafe(4) != 4 {
t.Fatal("failed")
}
}
func TestUIntSlice(t *testing.T) {
c := caster.NewCaster([]any{100, -20, 30})
if len(c.UIntSlice(nil)) != 2 {
t.Fatal("failed")
}
}
func TestUInt8(t *testing.T) {
c := caster.NewCaster(255)
if c.UInt8Safe(3) == 3 {
t.Fatal("failed")
}
}
func TestUInt8Slice(t *testing.T) {
c := caster.NewCaster([]any{100, -20, 300})
if len(c.UInt8Slice(nil)) != 1 {
t.Fatal("failed")
}
}
func TestUInt16(t *testing.T) {
c := caster.NewCaster(255)
if c.UInt16Safe(3) == 3 {
t.Fatal("failed")
}
}
func TestUInt16Slice(t *testing.T) {
c := caster.NewCaster([]any{100, -20, 300})
if len(c.UInt16Slice(nil)) != 2 {
t.Fatal("failed")
}
}
func TestUInt32(t *testing.T) {
c := caster.NewCaster(255)
if c.UInt32Safe(3) == 3 {
t.Fatal("failed")
}
}
func TestUInt32Slice(t *testing.T) {
c := caster.NewCaster([]any{100, -20, 300})
if len(c.UInt32Slice(nil)) != 2 {
t.Fatal("failed")
}
}
func TestUInt64(t *testing.T) {
c := caster.NewCaster(255)
if c.UInt64Safe(3) == 3 {
t.Fatal("failed")
}
}
func TestUInt64Slice(t *testing.T) {
c := caster.NewCaster([]any{-1, -20, 300})
if len(c.UInt64Slice(nil)) != 1 {
t.Fatal("failed")
}
}
func TestFloat32(t *testing.T) {
c := caster.NewCaster("1234.12aaa")
if c.Float32Safe(11) != 11 {
t.Fatal("failed")
}
}
func TestFloat32Slice(t *testing.T) {
c := caster.NewCaster([]any{2.3, 700, -19.22})
if len(c.Float32Slice(nil)) != 3 {
t.Fatal("failed")
}
}
func TestFloat64(t *testing.T) {
c := caster.NewCaster(33.444123123)
if c.Float64Safe(4) == 4 {
t.Fatal("failed")
}
}
func TestFloat64Slice(t *testing.T) {
c := caster.NewCaster([]any{10, true, 20.11})
if len(c.Float64Slice(nil)) != 2 {
t.Fatal("failed")
}
}
func TestString(t *testing.T) {
c := caster.NewCaster(123)
if c.StringSafe("hi") != "hi" {
t.Fatal("failed")
}
}
func TestStringSlice(t *testing.T) {
c := caster.NewCaster([]any{"John", "Jack", "Jimmy"})
if len(c.StringSlice(nil)) != 3 {
t.Fatal("failed")
}
}