-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
137 lines (127 loc) · 3.08 KB
/
Copy pathhelpers_test.go
File metadata and controls
137 lines (127 loc) · 3.08 KB
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
package blackstart
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestInputAs_StringSliceFromAny(t *testing.T) {
in := NewInputFromValue([]any{"SELECT", "UPDATE"})
v, err := InputAs[[]string](in, true)
require.NoError(t, err)
require.Equal(t, []string{"SELECT", "UPDATE"}, v)
}
func TestInputAs_RequiredString(t *testing.T) {
_, err := InputAs[string](NewInputFromValue(""), true)
require.Error(t, err)
}
func TestContextInputAs(t *testing.T) {
ctx := InputsToContext(
context.Background(),
map[string]Input{
"name": NewInputFromValue("blackstart"),
},
)
name, err := ContextInputAs[string](ctx, "name", true)
require.NoError(t, err)
require.Equal(t, "blackstart", name)
}
func TestInputAs_BoolToBoolPtr(t *testing.T) {
in := NewInputFromValue(true)
v, err := InputAs[*bool](in, true)
require.NoError(t, err)
require.NotNil(t, v)
require.True(t, *v)
}
func TestInputAs_SliceCoercions_Primitives(t *testing.T) {
tests := []struct {
name string
run func(t *testing.T)
}{
{
name: "AnySliceToIntSlice",
run: func(t *testing.T) {
in := NewInputFromValue([]any{1, 2, 3})
v, err := InputAs[[]int](in, true)
require.NoError(t, err)
require.Equal(t, []int{1, 2, 3}, v)
},
},
{
name: "AnySliceToInt64Slice",
run: func(t *testing.T) {
in := NewInputFromValue([]any{1, 2, 3})
v, err := InputAs[[]int64](in, true)
require.NoError(t, err)
require.Equal(t, []int64{1, 2, 3}, v)
},
},
{
name: "AnySliceToFloat64Slice",
run: func(t *testing.T) {
in := NewInputFromValue([]any{1.5, 2.25, 3.75})
v, err := InputAs[[]float64](in, true)
require.NoError(t, err)
require.Equal(t, []float64{1.5, 2.25, 3.75}, v)
},
},
{
name: "AnyNumericSliceToFloat64Slice",
run: func(t *testing.T) {
in := NewInputFromValue([]any{1, 2, 3})
v, err := InputAs[[]float64](in, true)
require.NoError(t, err)
require.Equal(t, []float64{1, 2, 3}, v)
},
},
{
name: "AnySliceToBoolSlice",
run: func(t *testing.T) {
in := NewInputFromValue([]any{true, false, true})
v, err := InputAs[[]bool](in, true)
require.NoError(t, err)
require.Equal(t, []bool{true, false, true}, v)
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.run)
}
}
func TestInputAs_SliceCoercions_InvalidCases(t *testing.T) {
tests := []struct {
name string
input any
run func(t *testing.T, in Input)
}{
{
name: "AnySliceToIntSlice_WithString",
input: []any{1, "two"},
run: func(t *testing.T, in Input) {
_, err := InputAs[[]int](in, true)
require.Error(t, err)
},
},
{
name: "AnySliceToBoolSlice_WithInt",
input: []any{true, 1},
run: func(t *testing.T, in Input) {
_, err := InputAs[[]bool](in, true)
require.Error(t, err)
},
},
{
name: "AnySliceToStringSlice_WithInt",
input: []any{"one", 2},
run: func(t *testing.T, in Input) {
_, err := InputAs[[]string](in, true)
require.Error(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := NewInputFromValue(tt.input)
tt.run(t, in)
})
}
}