forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool_slice_test.go
204 lines (188 loc) · 5.76 KB
/
bool_slice_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag_test
import (
"io/ioutil"
"testing"
"github.com/zulucmd/zflag/v2"
)
func TestBoolSlice(t *testing.T) {
tests := []struct {
name string
flagDefault []bool
input []string
expectedErr string
expectedValues []bool
expectedStrValues string
visitor func(f *zflag.Flag)
expectedGetSlice []string
}{
{
name: "no value passed",
input: []string{},
flagDefault: []bool{},
expectedErr: "",
expectedValues: []bool{},
expectedStrValues: "[]",
expectedGetSlice: []string{},
},
{
name: "empty value passed",
input: []string{""},
flagDefault: []bool{},
expectedErr: `invalid argument "" for "--bs" flag: strconv.ParseBool: parsing "": invalid syntax`,
},
{
name: "invalid bool",
input: []string{"blabla"},
flagDefault: []bool{},
expectedErr: `invalid argument "blabla" for "--bs" flag: strconv.ParseBool: parsing "blabla": invalid syntax`,
},
{
name: "no csv",
input: []string{"true,false"},
flagDefault: []bool{},
expectedErr: `invalid argument "true,false" for "--bs" flag: strconv.ParseBool: parsing "true,false": invalid syntax`,
},
{
name: "multiple values passed",
input: []string{"true", "false"},
flagDefault: []bool{},
expectedValues: []bool{true, false},
expectedStrValues: "[true false]",
expectedGetSlice: []string{"true", "false"},
},
{
name: "with default values",
input: []string{"false", "true"},
flagDefault: []bool{true, false},
expectedValues: []bool{false, true},
expectedStrValues: "[false true]",
expectedGetSlice: []string{"false", "true"},
},
{
name: "replace values",
input: []string{"true", "false"},
visitor: func(f *zflag.Flag) {
if val, ok := f.Value.(zflag.SliceValue); ok {
_ = val.Replace([]string{"false"})
}
},
expectedValues: []bool{false},
expectedStrValues: "[false]",
expectedGetSlice: []string{"false"},
},
{
name: "replace values error",
input: []string{"true", "false"},
visitor: func(f *zflag.Flag) {
if val, ok := f.Value.(zflag.SliceValue); ok {
err := val.Replace([]string{"notbool"})
assertErr(t, err)
}
},
expectedValues: []bool{true, false},
expectedStrValues: "[true false]",
expectedGetSlice: []string{"true", "false"},
},
{
name: "add values",
input: []string{"true", "false"},
visitor: func(f *zflag.Flag) {
if val, ok := f.Value.(zflag.SliceValue); ok {
_ = val.Append("false")
}
},
expectedValues: []bool{true, false, false},
expectedStrValues: "[true false false]",
expectedGetSlice: []string{"true", "false", "false"},
},
{
name: "add values error",
input: []string{"true"},
visitor: func(f *zflag.Flag) {
if val, ok := f.Value.(zflag.SliceValue); ok {
err := val.Append("asd")
if err == nil {
t.Errorf("Expected an error when appending, got %s", err)
}
}
},
flagDefault: nil,
expectedValues: []bool{true},
expectedStrValues: "[true]",
expectedGetSlice: []string{"true"},
},
{
name: "nil default",
input: []string{},
flagDefault: nil,
expectedValues: nil,
expectedStrValues: "[]",
expectedGetSlice: []string{},
},
{
name: "trims input",
input: []string{" true ", " false "},
expectedValues: []bool{true, false},
expectedStrValues: "[true false]",
expectedGetSlice: []string{"true", "false"},
},
{
name: "all valid bool values",
input: []string{"true", "false", "1", "0", "t", "f", "TRUE", "FALSE", "1", "0", "T", "F", "True", "False"},
expectedValues: []bool{true, false, true, false, true, false, true, false, true, false, true, false, true, false},
expectedStrValues: "[true false true false true false true false true false true false true false]",
expectedGetSlice: []string{"true", "false", "true", "false", "true", "false", "true", "false", "true", "false", "true", "false", "true", "false"},
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var bs []bool
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.BoolSliceVar(&bs, "bs", test.flagDefault, "usage")
err := f.Parse(repeatFlag("--bs", test.input...))
if test.expectedErr != "" {
assertErrMsg(t, test.expectedErr, err)
return
}
assertNoErr(t, err)
if test.visitor != nil {
f.VisitAll(test.visitor)
}
assertDeepEqual(t, test.expectedValues, bs)
boolSlice, err := f.GetBoolSlice("bs")
assertNoErr(t, err)
assertDeepEqual(t, test.expectedValues, boolSlice)
boolSliceGet, err := f.Get("bs")
assertNoErr(t, err)
assertDeepEqual(t, test.expectedValues, boolSliceGet)
flag := f.Lookup("bs")
assertEqual(t, test.expectedStrValues, flag.Value.String())
sliced := flag.Value.(zflag.SliceValue)
assertDeepEqual(t, test.expectedGetSlice, sliced.GetSlice())
defer assertNoPanic(t)()
mustBoolSlice := f.MustGetBoolSlice("bs")
assertDeepEqual(t, test.expectedValues, mustBoolSlice)
})
}
}
func TestBoolSliceErrors(t *testing.T) {
t.Parallel()
var s string
var bs []bool
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.StringVar(&s, "s", "", "usage")
f.BoolSliceVar(&bs, "bs", []bool{}, "usage")
err := f.Parse([]string{})
assertNoErr(t, err)
_, err = f.GetBoolSlice("s")
assertErr(t, err)
defer assertPanic(t)()
_ = f.MustGetBoolSlice("s")
}