Skip to content

Commit

Permalink
test: refactored tests
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
fredbi committed Oct 8, 2023
1 parent c0dd564 commit bc2d4d1
Show file tree
Hide file tree
Showing 26 changed files with 3,104 additions and 3,594 deletions.
371 changes: 171 additions & 200 deletions bool_slice_test.go

Large diffs are not rendered by default.

211 changes: 98 additions & 113 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

// This value can be a boolean ("true", "false") or "maybe"
Expand Down Expand Up @@ -40,6 +42,7 @@ func (v *triStateValue) Set(s string) error {
} else {
*v = triStateFalse
}

return err
}

Expand All @@ -60,120 +63,102 @@ func setUpFlagSet(tristate *triStateValue) *FlagSet {
*tristate = triStateFalse
flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
flag.NoOptDefVal = "true"
return f
}

func TestExplicitTrue(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{"--tristate=true"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateTrue {
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
}
}

func TestImplicitTrue(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{"--tristate"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateTrue {
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
}
}

func TestShortFlag(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{"-t"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateTrue {
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
}
}

func TestShortFlagExtraArgument(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
// The"maybe"turns into an arg, since short boolean options will only do true/false
err := f.Parse([]string{"-t", "maybe"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateTrue {
t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
}
args := f.Args()
if len(args) != 1 || args[0] != "maybe" {
t.Fatal("expected an extra 'maybe' argument to stick around")
}
}

func TestExplicitMaybe(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{"--tristate=maybe"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateMaybe {
t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead")
}
}

func TestExplicitFalse(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{"--tristate=false"})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateFalse {
t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
}
}

func TestImplicitFalse(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
err := f.Parse([]string{})
if err != nil {
t.Fatal("expected no error; got", err)
}
if tristate != triStateFalse {
t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
}
}

func TestInvalidValue(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
var buf bytes.Buffer
f.SetOutput(&buf)
err := f.Parse([]string{"--tristate=invalid"})
if err == nil {
t.Fatal("expected an error but did not get any, tristate has value", tristate)
}
return f
}

func TestBoolP(t *testing.T) {
b := BoolP("bool", "b", false, "bool value in CommandLine")
c := BoolP("c", "c", false, "other bool value")
args := []string{"--bool"}
if err := CommandLine.Parse(args); err != nil {
t.Error("expected no error, got ", err)
}
if *b != true {
t.Errorf("expected b=true got b=%v", *b)
}
if *c != false {
t.Errorf("expect c=false got c=%v", *c)
}
func TestBool(t *testing.T) {
t.Parallel()

t.Run("with explicit true", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{"--tristate=true"}))
require.Equalf(t, triStateTrue, triState,
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
)
})

t.Run("with implicit true", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{"--tristate"}))
require.Equalf(t, triStateTrue, triState,
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
)
})

t.Run("with short flag", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{"-t"}))
require.Equalf(t, triStateTrue, triState,
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
)
})

t.Run("with short flag extra argument", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
// The"maybe"turns into an arg, since short boolean options will only do true/false
require.NoError(t, f.Parse([]string{"-t", "maybe"}))
require.Equalf(t, triStateTrue, triState,
"expected", triStateTrue, "(triStateTrue) but got", triState, "instead",
)
args := f.Args()
require.Len(t, args, 1)
require.Equalf(t, "maybe", args[0],
"expected an extra 'maybe' argument to stick around",
)
})

t.Run("with explicit maybe", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{"--tristate=maybe"}))
require.Equalf(t, triStateMaybe, triState,
"expected", triStateMaybe, "(triStateMaybe) but got", triState, "instead",
)
})

t.Run("with explicit false", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{"--tristate=false"}))
require.Equalf(t, triStateFalse, triState,
"expected", triStateFalse, "(triStateFalse) but got", triState, "instead",
)
})

t.Run("with implicit false", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
require.NoError(t, f.Parse([]string{}))
require.Equalf(t, triStateFalse, triState,
"expected", triStateFalse, "(triStateFalse) but got", triState, "instead",
)
})

t.Run("with invalid value", func(t *testing.T) {
var triState triStateValue
f := setUpFlagSet(&triState)
var buf bytes.Buffer
f.SetOutput(&buf)
require.Errorf(t, f.Parse([]string{"--tristate=invalid"}),
"expected an error but did not get any, tristate has value", triState,
)
})

t.Run("with BoolP", func(t *testing.T) {
b := BoolP("bool", "b", false, "bool value in CommandLine")
c := BoolP("c", "c", false, "other bool value")
args := []string{"--bool"}
require.NoError(t, CommandLine.Parse(args))
require.Truef(t, *b,
"expected b=true got b=%v", *b,
)
require.Falsef(t, *c,
"expect c=false got c=%v", *c,
)
})
}
97 changes: 49 additions & 48 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (
"fmt"
"os"
"testing"
)

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

func TestBytesHex(t *testing.T) {
newFlag := func(bytesHex *[]byte) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BytesHexVar(bytesHex, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
f.BytesHexVarP(bytesHex, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
return f
}

testCases := []struct {
input string
success bool
Expand All @@ -38,7 +40,7 @@ func TestBytesHex(t *testing.T) {

for i := range testCases {
var bytesHex []byte
f := setUpBytesHex(&bytesHex)
f := newFlag(&bytesHex)

tc := &testCases[i]

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

switch {
case err != nil && tc.success:
t.Errorf("expected success, got %q", err)
continue
case err == nil && !tc.success:
// bytesHex, err := f.GetBytesHex("bytes")
t.Errorf("expected failure while processing %q", tc.input)
if !tc.success {
require.Errorf(t, err,
"expected failure while processing %q", tc.input,
)

continue
case tc.success:
bytesHex, err := f.GetBytesHex("bytes")
if err != nil {
t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
}
if fmt.Sprintf("%X", bytesHex) != tc.expected {
t.Errorf("expected %q, got '%X'", tc.expected, bytesHex)
}
}

require.NoErrorf(t, err, "expected success, got %q", err)

bytesHex, err := f.GetBytesHex("bytes")
require.NoErrorf(t, err,
"got error trying to fetch the 'bytes' flag: %v", err,
)

require.Equalf(t, tc.expected, fmt.Sprintf("%X", bytesHex),
"expected %q, got '%X'", tc.expected, bytesHex,
)

}
}
}

func setUpBytesBase64(bytesBase64 *[]byte) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
return f
}

func TestBytesBase64(t *testing.T) {
newFlag := func(bytesBase64 *[]byte) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
return f
}

testCases := []struct {
input string
success bool
Expand All @@ -100,11 +104,9 @@ func TestBytesBase64(t *testing.T) {

for i := range testCases {
var bytesBase64 []byte
f := setUpBytesBase64(&bytesBase64)

f := newFlag(&bytesBase64)
tc := &testCases[i]

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

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

switch {
case err != nil && tc.success:
t.Errorf("expected success, got %q", err)
continue
case err == nil && !tc.success:
// bytesBase64, err := f.GetBytesBase64("bytes")
t.Errorf("expected failure while processing %q", tc.input)
continue
case tc.success:
bytesBase64, err := f.GetBytesBase64("bytes")
if err != nil {
t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
}
if base64.StdEncoding.EncodeToString(bytesBase64) != tc.expected {
t.Errorf("expected %q, got '%X'", tc.expected, bytesBase64)
}
}

require.NoErrorf(t, err, "expected success, got %q", err)

bytesBase64, err := f.GetBytesBase64("bytes")
require.NoErrorf(t, err,
"got error trying to fetch the 'bytes' flag: %v", err,
)
require.Equalf(t, tc.expected, base64.StdEncoding.EncodeToString(bytesBase64),
"expected %q, got '%X'", tc.expected, bytesBase64,
)
}
}
}
Loading

0 comments on commit bc2d4d1

Please sign in to comment.