-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_string_test.go
93 lines (86 loc) · 1.87 KB
/
cast_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
package pick
import (
"testing"
)
func TestStringCaster(t *testing.T) {
t.Parallel()
type int32Alias int32
type stringAlias string
type float32alias float32
testCases := []singleCastTestCase[string]{
{
input: int32Alias(123456),
expected: "123456",
expectedErr: nil,
},
{
input: stringAlias("abcd"),
expected: "abcd",
expectedErr: nil,
},
{
input: float32alias(12.123),
expected: "12.123",
expectedErr: nil,
},
}
caster := NewDefaultCaster()
runSingleCastTestCases[string](t, testCases, caster.AsString)
}
func TestStringSliceCaster(t *testing.T) {
t.Parallel()
testCases := []singleCastTestCase[[]string]{
{
input: "singe string",
expected: []string{"singe string"},
expectedErr: nil,
},
{
input: 444,
expected: []string{"444"},
expectedErr: nil,
},
{
input: []string{"string", "slice"},
expected: []string{"string", "slice"},
expectedErr: nil,
},
{
input: [2]string{"string", "array"},
expected: []string{"string", "array"},
expectedErr: nil,
},
{
input: []any{"slice", "of", 3},
expected: []string{"slice", "of", "3"},
expectedErr: nil,
},
{
input: []any{"slice", int32(12), float64(1.456)},
expected: []string{"slice", "12", "1.456"},
expectedErr: nil,
},
{
input: []int32{1, 2, 3, 4},
expected: []string{"1", "2", "3", "4"},
expectedErr: nil,
},
{
input: []int32(nil),
expected: []string(nil),
expectedErr: nil,
},
{
input: map[string]string{"": ""},
expected: nil,
expectedErr: expectInvalidType,
},
{
input: []any{"slice", map[string]string{"": ""}, 3},
expected: nil,
expectedErr: expectInvalidType,
},
}
caster := NewDefaultCaster()
runSingleCastTestCases(t, testCases, caster.AsStringSlice)
}