-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices_test.go
182 lines (166 loc) · 5.63 KB
/
slices_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
package flags_test
import (
"flag"
"strings"
"testing"
"github.com/ViBiOh/flags"
"github.com/stretchr/testify/assert"
)
func TestStringSlice(t *testing.T) {
type args struct {
defaultValue []string
overrides []flags.Override
args []string
}
cases := map[string]struct {
builder flags.Builder
preTest func()
args args
want []string
wantUsage string
}{
"simple": {
flags.New("tags", "Tags of ressources"),
nil,
args{},
nil,
"Usage of StringSlice:\n --tags string slice Tags of ressources ${STRING_SLICE_TAGS}, as a string slice, environment variable separated by \",\"\n",
},
"with default value": {
flags.New("headers", "Headers of request").Prefix("curl"),
nil,
args{
defaultValue: []string{"Authorization", "Content-Type"},
},
[]string{"Authorization", "Content-Type"},
"Usage of StringSlice:\n --curlHeaders string slice [curl] Headers of request ${STRING_SLICE_CURL_HEADERS}, as a string slice, environment variable separated by \",\" (default [Authorization, Content-Type])\n",
},
"with read from environment variable": {
flags.New("labels", "Labels of ressources").DocPrefix("metadata").EnvSeparator("|"),
func() {
t.Setenv("STRING_SLICE_LABELS", "env|found")
},
args{
defaultValue: []string{"test", "flags"},
},
[]string{"env", "found"},
"Usage of StringSlice:\n --labels string slice [metadata] Labels of ressources ${STRING_SLICE_LABELS}, as a string slice, environment variable separated by \"|\" (default [env, found])\n",
},
"with shorthand and args": {
flags.New("namespace", "Namespace").Shorthand("n"),
nil,
args{
defaultValue: []string{"default"},
args: []string{"-n", "system", "--namespace", "default"},
},
[]string{"system", "default"},
"Usage of StringSlice:\n -n, --namespace string slice Namespace ${STRING_SLICE_NAMESPACE}, as a string slice, environment variable separated by \",\" (default [default])\n",
},
"with env": {
flags.New("match", "Match").Env("MATCHES"),
func() {
t.Setenv("MATCHES", "info,error")
},
args{
defaultValue: []string{"error"},
},
[]string{"info", "error"},
"Usage of StringSlice:\n --match string slice Match ${MATCHES}, as a string slice, environment variable separated by \",\" (default [info, error])\n",
},
}
for intention, testCase := range cases {
t.Run(intention, func(t *testing.T) {
fs := flag.NewFlagSet("StringSlice", flag.ContinueOnError)
fs.Usage = flags.Usage(fs)
var writer strings.Builder
fs.SetOutput(&writer)
if testCase.preTest != nil {
testCase.preTest()
}
got := testCase.builder.StringSlice(fs, testCase.args.defaultValue, testCase.args.overrides)
fs.Usage()
assert.NoError(t, fs.Parse(testCase.args.args))
assert.Equal(t, testCase.want, *got)
assert.Equal(t, testCase.wantUsage, writer.String())
})
}
}
func TestFloat64Slice(t *testing.T) {
type args struct {
defaultValue []float64
overrides []flags.Override
args []string
}
cases := map[string]struct {
builder flags.Builder
preTest func()
args args
want []float64
wantUsage string
}{
"simple": {
flags.New("coord", "LatLng"),
nil,
args{},
nil,
"Usage of Float64Slice:\n --coord float64 slice LatLng ${FLOAT64_SLICE_COORD}, as a float64 slice, environment variable separated by \",\"\n",
},
"with default value": {
flags.New("coordinates", "Coordinates").Prefix("start"),
nil,
args{
defaultValue: []float64{1.2, 3.4},
},
[]float64{1.2, 3.4},
"Usage of Float64Slice:\n --startCoordinates float64 slice [start] Coordinates ${FLOAT64_SLICE_START_COORDINATES}, as a float64 slice, environment variable separated by \",\" (default [1.200000, 3.400000])\n",
},
"with read from environment variable": {
flags.New("coordinates", "Coordinates").Prefix("start").EnvSeparator("|"),
func() {
t.Setenv("FLOAT64_SLICE_START_COORDINATES", "5.6|7.8")
},
args{
defaultValue: []float64{1.2, 3.4},
},
[]float64{5.6, 7.8},
"Usage of Float64Slice:\n --startCoordinates float64 slice [start] Coordinates ${FLOAT64_SLICE_START_COORDINATES}, as a float64 slice, environment variable separated by \"|\" (default [5.600000, 7.800000])\n",
},
"with shorthand and args": {
flags.New("position", "GPS Position").Shorthand("g"),
nil,
args{
defaultValue: []float64{1.2, 3, 4},
args: []string{"-g", "7.8", "--position", "5.6"},
},
[]float64{7.8, 5.6},
"Usage of Float64Slice:\n -g, --position float64 slice GPS Position ${FLOAT64_SLICE_POSITION}, as a float64 slice, environment variable separated by \",\" (default [1.200000, 3.000000, 4.000000])\n",
},
"with env": {
flags.New("coordinates", "Coordinates"),
func() {
t.Setenv("FLOAT64_SLICE_COORDINATES", "5.6,7.8")
},
args{
defaultValue: []float64{1.2, 3.4},
},
[]float64{5.6, 7.8},
"Usage of Float64Slice:\n --coordinates float64 slice Coordinates ${FLOAT64_SLICE_COORDINATES}, as a float64 slice, environment variable separated by \",\" (default [5.600000, 7.800000])\n",
},
}
for intention, testCase := range cases {
t.Run(intention, func(t *testing.T) {
fs := flag.NewFlagSet("Float64Slice", flag.ContinueOnError)
fs.Usage = flags.Usage(fs)
var writer strings.Builder
fs.SetOutput(&writer)
if testCase.preTest != nil {
testCase.preTest()
}
got := testCase.builder.Float64Slice(fs, testCase.args.defaultValue, testCase.args.overrides)
fs.Usage()
assert.NoError(t, fs.Parse(testCase.args.args))
assert.Equal(t, testCase.want, *got)
assert.Equal(t, testCase.wantUsage, writer.String())
})
}
}