Skip to content

Commit 19c1841

Browse files
committed
guess I got them all
Signed-off-by: Frederic BIDON <[email protected]>
1 parent d19d54b commit 19c1841

24 files changed

+1320
-1859
lines changed

bool_slice_test.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ func TestBoolSlice(t *testing.T) {
2121
t.Run("with empty slice", func(t *testing.T) {
2222
bs := make([]bool, 0)
2323
f := newFlag(&bs)
24+
2425
require.NoError(t, f.Parse([]string{}))
2526

2627
getBS, err := f.GetBoolSlice("bs")
2728
require.NoErrorf(t, err,
2829
"got an error from GetBoolSlice(): %v", err,
2930
)
3031

31-
require.Emptyf(t, getBS,
32-
"got bs %v with len=%d but expected length=0", getBS, len(getBS),
33-
)
32+
require.Empty(t, getBS)
3433
})
3534

3635
t.Run("with truthy/falsy values", func(t *testing.T) {
3736
vals := []string{"1", "F", "TRUE", "0"}
3837
bs := make([]bool, 0, len(vals))
3938
f := newFlag(&bs)
4039

41-
arg := fmt.Sprintf("--bs=%s", strings.Join(vals, ","))
42-
require.NoError(t, f.Parse([]string{arg}))
40+
require.NoError(t, f.Parse([]string{
41+
fmt.Sprintf("--bs=%s", strings.Join(vals, ",")),
42+
}))
4343

4444
for i, v := range bs {
4545
b, err := strconv.ParseBool(vals[i])
@@ -129,41 +129,38 @@ func TestBoolSlice(t *testing.T) {
129129
})
130130

131131
t.Run("called twice", func(t *testing.T) {
132+
const argfmt = "--bs=%s"
132133
in := []string{"T,F", "T"}
133134
bs := make([]bool, 0, len(in))
134135
f := newFlag(&bs)
135-
136136
expected := []bool{true, false, true}
137-
argfmt := "--bs=%s"
138-
arg1 := fmt.Sprintf(argfmt, in[0])
139-
arg2 := fmt.Sprintf(argfmt, in[1])
140-
require.NoError(t, f.Parse([]string{arg1, arg2}))
141137

142-
for i, v := range bs {
143-
require.Equalf(t, expected[i], v,
144-
"expected bs[%d] to be %t but got %t", i, expected[i], v,
145-
)
146-
}
138+
require.NoError(t, f.Parse([]string{
139+
fmt.Sprintf(argfmt, in[0]),
140+
fmt.Sprintf(argfmt, in[1]),
141+
}))
142+
143+
require.Equal(t, expected, bs)
147144
})
148145

149146
t.Run("as slice value", func(t *testing.T) {
147+
const argfmt = "--bs=%s"
150148
in := []string{"true", "false"}
151149
bs := make([]bool, 0, len(in))
152150
f := newFlag(&bs)
153151

154-
argfmt := "--bs=%s"
155-
arg1 := fmt.Sprintf(argfmt, in[0])
156-
arg2 := fmt.Sprintf(argfmt, in[1])
157-
require.NoError(t, f.Parse([]string{arg1, arg2}))
152+
require.NoError(t, f.Parse([]string{
153+
fmt.Sprintf(argfmt, in[0]),
154+
fmt.Sprintf(argfmt, in[1]),
155+
}))
158156

159157
f.VisitAll(func(f *Flag) {
160158
if val, ok := f.Value.(SliceValue); ok {
161159
require.NoError(t, val.Replace([]string{"false"}))
162160
}
163161
})
164162

165-
require.Len(t, bs, 1)
166-
require.Equalf(t, false, bs[0],
163+
require.Equalf(t, []bool{false}, bs,
167164
"expected ss to be overwritten with 'false', but got: %v", bs,
168165
)
169166
})
@@ -213,12 +210,7 @@ func TestBoolSlice(t *testing.T) {
213210
test.FlagArg, test.Want,
214211
)
215212

216-
for j, b := range bs {
217-
require.Equalf(t, test.Want[j], b,
218-
"bad value parsed for test %d on bool %d:\nwant:\t%t\ngot:\t%t",
219-
i, j, test.Want[j], b,
220-
)
221-
}
213+
require.Equalf(t, test.Want, bs, "on test %d", i)
222214
}
223215
})
224216
}

bool_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (v *triStateValue) Set(s string) error {
4242
} else {
4343
*v = triStateFalse
4444
}
45+
4546
return err
4647
}
4748

@@ -62,6 +63,7 @@ func setUpFlagSet(tristate *triStateValue) *FlagSet {
6263
*tristate = triStateFalse
6364
flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
6465
flag.NoOptDefVal = "true"
66+
6567
return f
6668
}
6769

bytes_test.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12-
func setUpBytesHex(bytesHex *[]byte) *FlagSet {
13-
f := NewFlagSet("test", ContinueOnError)
14-
f.BytesHexVar(bytesHex, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
15-
f.BytesHexVarP(bytesHex, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
16-
return f
17-
}
18-
1912
func TestBytesHex(t *testing.T) {
13+
newFlag := func(bytesHex *[]byte) *FlagSet {
14+
f := NewFlagSet("test", ContinueOnError)
15+
f.BytesHexVar(bytesHex, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
16+
f.BytesHexVarP(bytesHex, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
17+
return f
18+
}
19+
2020
testCases := []struct {
2121
input string
2222
success bool
@@ -40,7 +40,7 @@ func TestBytesHex(t *testing.T) {
4040

4141
for i := range testCases {
4242
var bytesHex []byte
43-
f := setUpBytesHex(&bytesHex)
43+
f := newFlag(&bytesHex)
4444

4545
tc := &testCases[i]
4646

@@ -77,14 +77,14 @@ func TestBytesHex(t *testing.T) {
7777
}
7878
}
7979

80-
func setUpBytesBase64(bytesBase64 *[]byte) *FlagSet {
81-
f := NewFlagSet("test", ContinueOnError)
82-
f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
83-
f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
84-
return f
85-
}
86-
8780
func TestBytesBase64(t *testing.T) {
81+
newFlag := func(bytesBase64 *[]byte) *FlagSet {
82+
f := NewFlagSet("test", ContinueOnError)
83+
f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
84+
f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
85+
return f
86+
}
87+
8888
testCases := []struct {
8989
input string
9090
success bool
@@ -104,11 +104,9 @@ func TestBytesBase64(t *testing.T) {
104104

105105
for i := range testCases {
106106
var bytesBase64 []byte
107-
f := setUpBytesBase64(&bytesBase64)
108-
107+
f := newFlag(&bytesBase64)
109108
tc := &testCases[i]
110109

111-
// --bytes
112110
args := []string{
113111
fmt.Sprintf("--bytes=%s", tc.input),
114112
fmt.Sprintf("-B %s", tc.input),
@@ -117,7 +115,6 @@ func TestBytesBase64(t *testing.T) {
117115

118116
for _, arg := range args {
119117
err := f.Parse([]string{arg})
120-
121118
if !tc.success {
122119
require.Errorf(t, err,
123120
"expected failure while processing %q", tc.input,
@@ -132,7 +129,6 @@ func TestBytesBase64(t *testing.T) {
132129
require.NoErrorf(t, err,
133130
"got error trying to fetch the 'bytes' flag: %v", err,
134131
)
135-
136132
require.Equalf(t, tc.expected, base64.StdEncoding.EncodeToString(bytesBase64),
137133
"expected %q, got '%X'", tc.expected, bytesBase64,
138134
)

count_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,10 @@ func TestCount(t *testing.T) {
4848
continue
4949
}
5050

51-
require.NoErrorf(t, err,
52-
"expected success with %q, got %q", tc.input, err,
53-
)
51+
require.NoError(t, err)
5452

5553
c, err := f.GetCount("verbose")
56-
require.NoErrorf(t, err,
57-
"got error trying to fetch the counter flag",
58-
)
59-
60-
require.Equalf(t, tc.expected, c,
61-
"expected %d, got %d", tc.expected, c,
62-
)
54+
require.NoError(t, err)
55+
require.Equal(t, tc.expected, c)
6356
}
6457
}

duration_slice_test.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,17 @@ func TestDurationSlice(t *testing.T) {
3131
require.NoErrorf(t, err,
3232
"got an error from GetDurationSlice(): %v", err,
3333
)
34-
require.Emptyf(t, getDS,
35-
"got ds %v with len=%d but expected length=0", getDS, len(getDS),
36-
)
34+
require.Empty(t, getDS)
3735
})
3836

3937
t.Run("with values", func(t *testing.T) {
4038
vals := []string{"1ns", "2ms", "3m", "4h"}
4139
ds := make([]time.Duration, 0, len(vals))
4240
f := newFlag(&ds)
4341

44-
arg := fmt.Sprintf("--ds=%s", strings.Join(vals, ","))
45-
require.NoError(t, f.Parse([]string{arg}))
42+
require.NoError(t, f.Parse([]string{
43+
fmt.Sprintf("--ds=%s", strings.Join(vals, ",")),
44+
}))
4645

4746
for i, v := range ds {
4847
d, err := time.ParseDuration(vals[i])
@@ -106,8 +105,9 @@ func TestDurationSlice(t *testing.T) {
106105
ds := make([]time.Duration, 0, len(vals))
107106
f := newFlagWithDefault(&ds)
108107

109-
arg := fmt.Sprintf("--ds=%s", strings.Join(vals, ","))
110-
require.NoError(t, f.Parse([]string{arg}))
108+
require.NoError(t, f.Parse([]string{
109+
fmt.Sprintf("--ds=%s", strings.Join(vals, ",")),
110+
}))
111111

112112
for i, v := range ds {
113113
d, err := time.ParseDuration(vals[i])
@@ -147,28 +147,23 @@ func TestDurationSlice(t *testing.T) {
147147
}
148148
})
149149

150-
require.Len(t, ds, 1)
151-
require.Equalf(t, time.Duration(3), ds[0],
150+
require.Equalf(t, []time.Duration{time.Duration(3)}, ds,
152151
"expected ss to be overwritten with '3ns', but got: %v", ds,
153152
)
154153
})
155154

156155
t.Run("called twice", func(t *testing.T) {
156+
const argfmt = "--ds=%s"
157157
in := []string{"1ns,2ns", "3ns"}
158158
ds := make([]time.Duration, 0, len(in))
159159
f := newFlag(&ds)
160-
161160
expected := []time.Duration{1, 2, 3}
162-
argfmt := "--ds=%s"
163-
arg1 := fmt.Sprintf(argfmt, in[0])
164-
arg2 := fmt.Sprintf(argfmt, in[1])
165161

166-
require.NoError(t, f.Parse([]string{arg1, arg2}))
162+
require.NoError(t, f.Parse([]string{
163+
fmt.Sprintf(argfmt, in[0]),
164+
fmt.Sprintf(argfmt, in[1]),
165+
}))
167166

168-
for i, v := range ds {
169-
require.Equalf(t, expected[i], v,
170-
"expected ds[%d] to be %d but got: %d", i, expected[i], v,
171-
)
172-
}
167+
require.Equal(t, expected, ds)
173168
})
174169
}

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func ExampleShorthandLookup() {
14-
name := "verbose"
14+
const name = "verbose"
1515
short := name[:1]
1616

1717
pflag.BoolP(name, short, false, "verbose output")
@@ -23,7 +23,7 @@ func ExampleShorthandLookup() {
2323
}
2424

2525
func ExampleFlagSet_ShorthandLookup() {
26-
name := "verbose"
26+
const name = "verbose"
2727
short := name[:1]
2828

2929
fs := pflag.NewFlagSet("Example", pflag.ContinueOnError)

flag_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func init() {
4747
testOptionalInt = Int("test_optional_int", 0, "optional int value")
4848
}
4949

50-
func boolString(s string) string {
51-
if s == "0" {
52-
return "false"
50+
func TestVisit(t *testing.T) {
51+
boolString := func(s string) string {
52+
if s == "0" {
53+
return "false"
54+
}
55+
return "true"
5356
}
54-
return "true"
55-
}
5657

57-
func TestVisit(t *testing.T) {
5858
visitor := func(desired string, m map[string]*Flag) func(*Flag) {
5959
return func(f *Flag) {
6060
if len(f.Name) <= 5 || f.Name[0:5] != "test_" {
@@ -788,7 +788,7 @@ func TestCustomNormalizedNames(t *testing.T) {
788788
require.Truef(t, *someOtherFlag, "someOtherFlag should be true, is ", *someOtherFlag)
789789
}
790790

791-
// Every flag we add, the name (displayed also in usage) should normalized
791+
// Every flag we add, the name (displayed also in usage) should be normalized
792792
func TestNormalizationFuncShouldChangeFlagName(t *testing.T) {
793793
t.Run("with normalization after addition", func(t *testing.T) {
794794
f := NewFlagSet("normalized", ContinueOnError)

0 commit comments

Comments
 (0)