Skip to content

Commit

Permalink
guess I got them all
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Oct 8, 2023
1 parent d19d54b commit 19c1841
Show file tree
Hide file tree
Showing 24 changed files with 1,320 additions and 1,859 deletions.
46 changes: 19 additions & 27 deletions bool_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ func TestBoolSlice(t *testing.T) {
t.Run("with empty slice", func(t *testing.T) {
bs := make([]bool, 0)
f := newFlag(&bs)

require.NoError(t, f.Parse([]string{}))

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

require.Emptyf(t, getBS,
"got bs %v with len=%d but expected length=0", getBS, len(getBS),
)
require.Empty(t, getBS)
})

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

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

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

t.Run("called twice", func(t *testing.T) {
const argfmt = "--bs=%s"
in := []string{"T,F", "T"}
bs := make([]bool, 0, len(in))
f := newFlag(&bs)

expected := []bool{true, false, true}
argfmt := "--bs=%s"
arg1 := fmt.Sprintf(argfmt, in[0])
arg2 := fmt.Sprintf(argfmt, in[1])
require.NoError(t, f.Parse([]string{arg1, arg2}))

for i, v := range bs {
require.Equalf(t, expected[i], v,
"expected bs[%d] to be %t but got %t", i, expected[i], v,
)
}
require.NoError(t, f.Parse([]string{
fmt.Sprintf(argfmt, in[0]),
fmt.Sprintf(argfmt, in[1]),
}))

require.Equal(t, expected, bs)
})

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

argfmt := "--bs=%s"
arg1 := fmt.Sprintf(argfmt, in[0])
arg2 := fmt.Sprintf(argfmt, in[1])
require.NoError(t, f.Parse([]string{arg1, arg2}))
require.NoError(t, f.Parse([]string{
fmt.Sprintf(argfmt, in[0]),
fmt.Sprintf(argfmt, in[1]),
}))

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

require.Len(t, bs, 1)
require.Equalf(t, false, bs[0],
require.Equalf(t, []bool{false}, bs,
"expected ss to be overwritten with 'false', but got: %v", bs,
)
})
Expand Down Expand Up @@ -213,12 +210,7 @@ func TestBoolSlice(t *testing.T) {
test.FlagArg, test.Want,
)

for j, b := range bs {
require.Equalf(t, test.Want[j], b,
"bad value parsed for test %d on bool %d:\nwant:\t%t\ngot:\t%t",
i, j, test.Want[j], b,
)
}
require.Equalf(t, test.Want, bs, "on test %d", i)
}
})
}
2 changes: 2 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (v *triStateValue) Set(s string) error {
} else {
*v = triStateFalse
}

return err
}

Expand All @@ -62,6 +63,7 @@ func setUpFlagSet(tristate *triStateValue) *FlagSet {
*tristate = triStateFalse
flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
flag.NoOptDefVal = "true"

return f
}

Expand Down
36 changes: 16 additions & 20 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/stretchr/testify/require"
)

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
}

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 @@ -40,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 Down Expand Up @@ -77,14 +77,14 @@ func TestBytesHex(t *testing.T) {
}
}

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 @@ -104,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 @@ -117,7 +115,6 @@ 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,
Expand All @@ -132,7 +129,6 @@ func TestBytesBase64(t *testing.T) {
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,
)
Expand Down
13 changes: 3 additions & 10 deletions count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,10 @@ func TestCount(t *testing.T) {
continue
}

require.NoErrorf(t, err,
"expected success with %q, got %q", tc.input, err,
)
require.NoError(t, err)

c, err := f.GetCount("verbose")
require.NoErrorf(t, err,
"got error trying to fetch the counter flag",
)

require.Equalf(t, tc.expected, c,
"expected %d, got %d", tc.expected, c,
)
require.NoError(t, err)
require.Equal(t, tc.expected, c)
}
}
33 changes: 14 additions & 19 deletions duration_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ func TestDurationSlice(t *testing.T) {
require.NoErrorf(t, err,
"got an error from GetDurationSlice(): %v", err,
)
require.Emptyf(t, getDS,
"got ds %v with len=%d but expected length=0", getDS, len(getDS),
)
require.Empty(t, getDS)
})

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

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

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

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

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

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

t.Run("called twice", func(t *testing.T) {
const argfmt = "--ds=%s"
in := []string{"1ns,2ns", "3ns"}
ds := make([]time.Duration, 0, len(in))
f := newFlag(&ds)

expected := []time.Duration{1, 2, 3}
argfmt := "--ds=%s"
arg1 := fmt.Sprintf(argfmt, in[0])
arg2 := fmt.Sprintf(argfmt, in[1])

require.NoError(t, f.Parse([]string{arg1, arg2}))
require.NoError(t, f.Parse([]string{
fmt.Sprintf(argfmt, in[0]),
fmt.Sprintf(argfmt, in[1]),
}))

for i, v := range ds {
require.Equalf(t, expected[i], v,
"expected ds[%d] to be %d but got: %d", i, expected[i], v,
)
}
require.Equal(t, expected, ds)
})
}
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func ExampleShorthandLookup() {
name := "verbose"
const name = "verbose"
short := name[:1]

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

func ExampleFlagSet_ShorthandLookup() {
name := "verbose"
const name = "verbose"
short := name[:1]

fs := pflag.NewFlagSet("Example", pflag.ContinueOnError)
Expand Down
14 changes: 7 additions & 7 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func init() {
testOptionalInt = Int("test_optional_int", 0, "optional int value")
}

func boolString(s string) string {
if s == "0" {
return "false"
func TestVisit(t *testing.T) {
boolString := func(s string) string {
if s == "0" {
return "false"
}
return "true"
}
return "true"
}

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

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

0 comments on commit 19c1841

Please sign in to comment.