Skip to content

Commit bc2d4d1

Browse files
committed
test: refactored tests
* focused on test readability * generalized usage of sub-tests * reduced the need for test helpers * introduced test dependency: github.com/stretchr/testify Signed-off-by: Frederic BIDON <[email protected]>
1 parent c0dd564 commit bc2d4d1

26 files changed

+3104
-3594
lines changed

bool_slice_test.go

Lines changed: 171 additions & 200 deletions
Large diffs are not rendered by default.

bool_test.go

Lines changed: 98 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"bytes"
99
"strconv"
1010
"testing"
11+
12+
"github.com/stretchr/testify/require"
1113
)
1214

1315
// This value can be a boolean ("true", "false") or "maybe"
@@ -40,6 +42,7 @@ func (v *triStateValue) Set(s string) error {
4042
} else {
4143
*v = triStateFalse
4244
}
45+
4346
return err
4447
}
4548

@@ -60,120 +63,102 @@ func setUpFlagSet(tristate *triStateValue) *FlagSet {
6063
*tristate = triStateFalse
6164
flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
6265
flag.NoOptDefVal = "true"
63-
return f
64-
}
65-
66-
func TestExplicitTrue(t *testing.T) {
67-
var tristate triStateValue
68-
f := setUpFlagSet(&tristate)
69-
err := f.Parse([]string{"--tristate=true"})
70-
if err != nil {
71-
t.Fatal("expected no error; got", err)
72-
}
73-
if tristate != triStateTrue {
74-
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
75-
}
76-
}
77-
78-
func TestImplicitTrue(t *testing.T) {
79-
var tristate triStateValue
80-
f := setUpFlagSet(&tristate)
81-
err := f.Parse([]string{"--tristate"})
82-
if err != nil {
83-
t.Fatal("expected no error; got", err)
84-
}
85-
if tristate != triStateTrue {
86-
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
87-
}
88-
}
89-
90-
func TestShortFlag(t *testing.T) {
91-
var tristate triStateValue
92-
f := setUpFlagSet(&tristate)
93-
err := f.Parse([]string{"-t"})
94-
if err != nil {
95-
t.Fatal("expected no error; got", err)
96-
}
97-
if tristate != triStateTrue {
98-
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
99-
}
100-
}
101-
102-
func TestShortFlagExtraArgument(t *testing.T) {
103-
var tristate triStateValue
104-
f := setUpFlagSet(&tristate)
105-
// The"maybe"turns into an arg, since short boolean options will only do true/false
106-
err := f.Parse([]string{"-t", "maybe"})
107-
if err != nil {
108-
t.Fatal("expected no error; got", err)
109-
}
110-
if tristate != triStateTrue {
111-
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
112-
}
113-
args := f.Args()
114-
if len(args) != 1 || args[0] != "maybe" {
115-
t.Fatal("expected an extra 'maybe' argument to stick around")
116-
}
117-
}
118-
119-
func TestExplicitMaybe(t *testing.T) {
120-
var tristate triStateValue
121-
f := setUpFlagSet(&tristate)
122-
err := f.Parse([]string{"--tristate=maybe"})
123-
if err != nil {
124-
t.Fatal("expected no error; got", err)
125-
}
126-
if tristate != triStateMaybe {
127-
t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead")
128-
}
129-
}
130-
131-
func TestExplicitFalse(t *testing.T) {
132-
var tristate triStateValue
133-
f := setUpFlagSet(&tristate)
134-
err := f.Parse([]string{"--tristate=false"})
135-
if err != nil {
136-
t.Fatal("expected no error; got", err)
137-
}
138-
if tristate != triStateFalse {
139-
t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
140-
}
141-
}
14266

143-
func TestImplicitFalse(t *testing.T) {
144-
var tristate triStateValue
145-
f := setUpFlagSet(&tristate)
146-
err := f.Parse([]string{})
147-
if err != nil {
148-
t.Fatal("expected no error; got", err)
149-
}
150-
if tristate != triStateFalse {
151-
t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
152-
}
153-
}
154-
155-
func TestInvalidValue(t *testing.T) {
156-
var tristate triStateValue
157-
f := setUpFlagSet(&tristate)
158-
var buf bytes.Buffer
159-
f.SetOutput(&buf)
160-
err := f.Parse([]string{"--tristate=invalid"})
161-
if err == nil {
162-
t.Fatal("expected an error but did not get any, tristate has value", tristate)
163-
}
67+
return f
16468
}
16569

166-
func TestBoolP(t *testing.T) {
167-
b := BoolP("bool", "b", false, "bool value in CommandLine")
168-
c := BoolP("c", "c", false, "other bool value")
169-
args := []string{"--bool"}
170-
if err := CommandLine.Parse(args); err != nil {
171-
t.Error("expected no error, got ", err)
172-
}
173-
if *b != true {
174-
t.Errorf("expected b=true got b=%v", *b)
175-
}
176-
if *c != false {
177-
t.Errorf("expect c=false got c=%v", *c)
178-
}
70+
func TestBool(t *testing.T) {
71+
t.Parallel()
72+
73+
t.Run("with explicit true", func(t *testing.T) {
74+
var triState triStateValue
75+
f := setUpFlagSet(&triState)
76+
require.NoError(t, f.Parse([]string{"--tristate=true"}))
77+
require.Equalf(t, triStateTrue, triState,
78+
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
79+
)
80+
})
81+
82+
t.Run("with implicit true", func(t *testing.T) {
83+
var triState triStateValue
84+
f := setUpFlagSet(&triState)
85+
require.NoError(t, f.Parse([]string{"--tristate"}))
86+
require.Equalf(t, triStateTrue, triState,
87+
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
88+
)
89+
})
90+
91+
t.Run("with short flag", func(t *testing.T) {
92+
var triState triStateValue
93+
f := setUpFlagSet(&triState)
94+
require.NoError(t, f.Parse([]string{"-t"}))
95+
require.Equalf(t, triStateTrue, triState,
96+
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
97+
)
98+
})
99+
100+
t.Run("with short flag extra argument", func(t *testing.T) {
101+
var triState triStateValue
102+
f := setUpFlagSet(&triState)
103+
// The"maybe"turns into an arg, since short boolean options will only do true/false
104+
require.NoError(t, f.Parse([]string{"-t", "maybe"}))
105+
require.Equalf(t, triStateTrue, triState,
106+
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
107+
)
108+
args := f.Args()
109+
require.Len(t, args, 1)
110+
require.Equalf(t, "maybe", args[0],
111+
"expected an extra 'maybe' argument to stick around",
112+
)
113+
})
114+
115+
t.Run("with explicit maybe", func(t *testing.T) {
116+
var triState triStateValue
117+
f := setUpFlagSet(&triState)
118+
require.NoError(t, f.Parse([]string{"--tristate=maybe"}))
119+
require.Equalf(t, triStateMaybe, triState,
120+
"expected", triStateMaybe, "(triStateMaybe) but got", triState, "instead",
121+
)
122+
})
123+
124+
t.Run("with explicit false", func(t *testing.T) {
125+
var triState triStateValue
126+
f := setUpFlagSet(&triState)
127+
require.NoError(t, f.Parse([]string{"--tristate=false"}))
128+
require.Equalf(t, triStateFalse, triState,
129+
"expected", triStateFalse, "(triStateFalse) but got", triState, "instead",
130+
)
131+
})
132+
133+
t.Run("with implicit false", func(t *testing.T) {
134+
var triState triStateValue
135+
f := setUpFlagSet(&triState)
136+
require.NoError(t, f.Parse([]string{}))
137+
require.Equalf(t, triStateFalse, triState,
138+
"expected", triStateFalse, "(triStateFalse) but got", triState, "instead",
139+
)
140+
})
141+
142+
t.Run("with invalid value", func(t *testing.T) {
143+
var triState triStateValue
144+
f := setUpFlagSet(&triState)
145+
var buf bytes.Buffer
146+
f.SetOutput(&buf)
147+
require.Errorf(t, f.Parse([]string{"--tristate=invalid"}),
148+
"expected an error but did not get any, tristate has value", triState,
149+
)
150+
})
151+
152+
t.Run("with BoolP", func(t *testing.T) {
153+
b := BoolP("bool", "b", false, "bool value in CommandLine")
154+
c := BoolP("c", "c", false, "other bool value")
155+
args := []string{"--bool"}
156+
require.NoError(t, CommandLine.Parse(args))
157+
require.Truef(t, *b,
158+
"expected b=true got b=%v", *b,
159+
)
160+
require.Falsef(t, *c,
161+
"expect c=false got c=%v", *c,
162+
)
163+
})
179164
}

bytes_test.go

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import (
55
"fmt"
66
"os"
77
"testing"
8-
)
98

10-
func setUpBytesHex(bytesHex *[]byte) *FlagSet {
11-
f := NewFlagSet("test", ContinueOnError)
12-
f.BytesHexVar(bytesHex, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
13-
f.BytesHexVarP(bytesHex, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
14-
return f
15-
}
9+
"github.com/stretchr/testify/require"
10+
)
1611

1712
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+
1820
testCases := []struct {
1921
input string
2022
success bool
@@ -38,7 +40,7 @@ func TestBytesHex(t *testing.T) {
3840

3941
for i := range testCases {
4042
var bytesHex []byte
41-
f := setUpBytesHex(&bytesHex)
43+
f := newFlag(&bytesHex)
4244

4345
tc := &testCases[i]
4446

@@ -52,35 +54,37 @@ func TestBytesHex(t *testing.T) {
5254
for _, arg := range args {
5355
err := f.Parse([]string{arg})
5456

55-
switch {
56-
case err != nil && tc.success:
57-
t.Errorf("expected success, got %q", err)
58-
continue
59-
case err == nil && !tc.success:
60-
// bytesHex, err := f.GetBytesHex("bytes")
61-
t.Errorf("expected failure while processing %q", tc.input)
57+
if !tc.success {
58+
require.Errorf(t, err,
59+
"expected failure while processing %q", tc.input,
60+
)
61+
6262
continue
63-
case tc.success:
64-
bytesHex, err := f.GetBytesHex("bytes")
65-
if err != nil {
66-
t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
67-
}
68-
if fmt.Sprintf("%X", bytesHex) != tc.expected {
69-
t.Errorf("expected %q, got '%X'", tc.expected, bytesHex)
70-
}
7163
}
64+
65+
require.NoErrorf(t, err, "expected success, got %q", err)
66+
67+
bytesHex, err := f.GetBytesHex("bytes")
68+
require.NoErrorf(t, err,
69+
"got error trying to fetch the 'bytes' flag: %v", err,
70+
)
71+
72+
require.Equalf(t, tc.expected, fmt.Sprintf("%X", bytesHex),
73+
"expected %q, got '%X'", tc.expected, bytesHex,
74+
)
75+
7276
}
7377
}
7478
}
7579

76-
func setUpBytesBase64(bytesBase64 *[]byte) *FlagSet {
77-
f := NewFlagSet("test", ContinueOnError)
78-
f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
79-
f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
80-
return f
81-
}
82-
8380
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+
8488
testCases := []struct {
8589
input string
8690
success bool
@@ -100,11 +104,9 @@ func TestBytesBase64(t *testing.T) {
100104

101105
for i := range testCases {
102106
var bytesBase64 []byte
103-
f := setUpBytesBase64(&bytesBase64)
104-
107+
f := newFlag(&bytesBase64)
105108
tc := &testCases[i]
106109

107-
// --bytes
108110
args := []string{
109111
fmt.Sprintf("--bytes=%s", tc.input),
110112
fmt.Sprintf("-B %s", tc.input),
@@ -113,24 +115,23 @@ func TestBytesBase64(t *testing.T) {
113115

114116
for _, arg := range args {
115117
err := f.Parse([]string{arg})
118+
if !tc.success {
119+
require.Errorf(t, err,
120+
"expected failure while processing %q", tc.input,
121+
)
116122

117-
switch {
118-
case err != nil && tc.success:
119-
t.Errorf("expected success, got %q", err)
120-
continue
121-
case err == nil && !tc.success:
122-
// bytesBase64, err := f.GetBytesBase64("bytes")
123-
t.Errorf("expected failure while processing %q", tc.input)
124123
continue
125-
case tc.success:
126-
bytesBase64, err := f.GetBytesBase64("bytes")
127-
if err != nil {
128-
t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
129-
}
130-
if base64.StdEncoding.EncodeToString(bytesBase64) != tc.expected {
131-
t.Errorf("expected %q, got '%X'", tc.expected, bytesBase64)
132-
}
133124
}
125+
126+
require.NoErrorf(t, err, "expected success, got %q", err)
127+
128+
bytesBase64, err := f.GetBytesBase64("bytes")
129+
require.NoErrorf(t, err,
130+
"got error trying to fetch the 'bytes' flag: %v", err,
131+
)
132+
require.Equalf(t, tc.expected, base64.StdEncoding.EncodeToString(bytesBase64),
133+
"expected %q, got '%X'", tc.expected, bytesBase64,
134+
)
134135
}
135136
}
136137
}

0 commit comments

Comments
 (0)