Skip to content

Commit

Permalink
again
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Oct 7, 2023
1 parent 12330ae commit d19d54b
Show file tree
Hide file tree
Showing 16 changed files with 1,071 additions and 1,077 deletions.
70 changes: 35 additions & 35 deletions bool_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import (
func TestBoolSlice(t *testing.T) {
t.Parallel()

newFlag := func(bsp *[]bool) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BoolSliceVar(bsp, "bs", []bool{}, "Command separated list!")
return f
}

t.Run("with empty slice", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSet(&bs)
bs := make([]bool, 0)
f := newFlag(&bs)
require.NoError(t, f.Parse([]string{}))

getBS, err := f.GetBoolSlice("bs")
Expand All @@ -28,10 +34,10 @@ func TestBoolSlice(t *testing.T) {
})

t.Run("with truthy/falsy values", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSet(&bs)

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}))

Expand All @@ -55,11 +61,17 @@ func TestBoolSlice(t *testing.T) {
}
})

t.Run("with defaults (1)", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSetWithDefault(&bs)
newFlagWithDefault := func(bsp *[]bool) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BoolSliceVar(bsp, "bs", []bool{false, true}, "Command separated list!")
return f
}

t.Run("with defaults (1)", func(t *testing.T) {
vals := []string{"false", "T"}
bs := make([]bool, 0, len(vals))
f := newFlagWithDefault(&bs)

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

for i, v := range bs {
Expand All @@ -71,9 +83,9 @@ func TestBoolSlice(t *testing.T) {
}

getBS, erb := f.GetBoolSlice("bs")
if erb != nil {
t.Fatal("got an error from GetBoolSlice():", erb)
}
require.NoErrorf(t, erb,
"got an error from GetBoolSlice(): %v", erb,
)

for i, v := range getBS {
b, err := strconv.ParseBool(vals[i])
Expand All @@ -87,10 +99,10 @@ func TestBoolSlice(t *testing.T) {
})

t.Run("with defaults (2)", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSetWithDefault(&bs)

vals := []string{"FALSE", "1"}
bs := make([]bool, 0, len(vals))
f := newFlagWithDefault(&bs)

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

Expand All @@ -104,7 +116,7 @@ func TestBoolSlice(t *testing.T) {

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

for i, v := range getBS {
Expand All @@ -117,10 +129,10 @@ func TestBoolSlice(t *testing.T) {
})

t.Run("called twice", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSet(&bs)

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])
Expand All @@ -135,10 +147,10 @@ func TestBoolSlice(t *testing.T) {
})

t.Run("as slice value", func(t *testing.T) {
var bs []bool
f := setUpBSFlagSet(&bs)

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])
Expand Down Expand Up @@ -192,8 +204,8 @@ func TestBoolSlice(t *testing.T) {
}

for i, test := range tests {
var bs []bool
f := setUpBSFlagSet(&bs)
bs := make([]bool, 0, 7)
f := newFlag(&bs)

require.NoErrorf(t,
f.Parse([]string{fmt.Sprintf("--bs=%s", strings.Join(test.FlagArg, ","))}),
Expand All @@ -210,15 +222,3 @@ func TestBoolSlice(t *testing.T) {
}
})
}

func setUpBSFlagSet(bsp *[]bool) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BoolSliceVar(bsp, "bs", []bool{}, "Command separated list!")
return f
}

func setUpBSFlagSetWithDefault(bsp *[]bool) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.BoolSliceVar(bsp, "bs", []bool{false, true}, "Command separated list!")
return f
}
15 changes: 7 additions & 8 deletions count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/stretchr/testify/require"
)

func setUpCount(c *int) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.CountVarP(c, "verbose", "v", "a counter")
return f
}

func TestCount(t *testing.T) {
newFlag := func(c *int) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.CountVarP(c, "verbose", "v", "a counter")
return f
}

testCases := []struct {
input []string
success bool
Expand All @@ -35,12 +35,11 @@ func TestCount(t *testing.T) {

for i := range testCases {
var count int
f := setUpCount(&count)
f := newFlag(&count)

tc := &testCases[i]

err := f.Parse(tc.input)

if !tc.success {
require.Errorf(t, err,
"expected failure with %q, got success", tc.input,
Expand Down
65 changes: 32 additions & 33 deletions duration_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,34 @@ import (
"github.com/stretchr/testify/require"
)

func setUpDSFlagSet(dsp *[]time.Duration) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.DurationSliceVar(dsp, "ds", []time.Duration{}, "Command separated list!")
return f
}

func setUpDSFlagSetWithDefault(dsp *[]time.Duration) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.DurationSliceVar(dsp, "ds", []time.Duration{0, 1}, "Command separated list!")
return f
}

func TestDurationSlice(t *testing.T) {
t.Parallel()

newFlag := func(dsp *[]time.Duration) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.DurationSliceVar(dsp, "ds", []time.Duration{}, "Command separated list!")
return f
}

t.Run("with empty slice", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSet(&ds)
ds := make([]time.Duration, 0)
f := newFlag(&ds)
require.NoError(t, f.Parse([]string{}))

getDS, err := f.GetDurationSlice("ds")
require.NoErrorf(t, err,
"got an error from GetDurationSlice():", 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),
)
})

t.Run("with values", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSet(&ds)

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}))

Expand All @@ -70,11 +64,16 @@ func TestDurationSlice(t *testing.T) {
}
})

t.Run("with default (1)", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSetWithDefault(&ds)
newFlagWithDefault := func(dsp *[]time.Duration) *FlagSet {
f := NewFlagSet("test", ContinueOnError)
f.DurationSliceVar(dsp, "ds", []time.Duration{0, 1}, "Command separated list!")
return f
}

t.Run("with default (1)", func(t *testing.T) {
vals := []string{"0s", "1ns"}
ds := make([]time.Duration, 0, len(vals))
f := newFlagWithDefault(&ds)

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

Expand All @@ -88,13 +87,13 @@ func TestDurationSlice(t *testing.T) {

getDS, erd := f.GetDurationSlice("ds")
require.NoErrorf(t, erd,
"got an error from GetDurationSlice():", erd,
"got an error from GetDurationSlice(): %v", erd,
)

for i, v := range getDS {
d, err := time.ParseDuration(vals[i])
require.NoErrorf(t, err,
"got an error from GetDurationSlice():", err,
"got an error from GetDurationSlice(): %v", err,
)
require.Equalf(t, v, d,
"expected ds[%d] to be %d from GetDurationSlice but got: %d", i, d, v,
Expand All @@ -103,10 +102,10 @@ func TestDurationSlice(t *testing.T) {
})

t.Run("with default (2)", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSetWithDefault(&ds)

vals := []string{"1ns", "2ns"}
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}))

Expand All @@ -120,7 +119,7 @@ func TestDurationSlice(t *testing.T) {

getDS, erd := f.GetDurationSlice("ds")
require.NoErrorf(t, erd,
"got an error from GetDurationSlice():", erd,
"got an error from GetDurationSlice(): %v", erd,
)

for i, v := range getDS {
Expand All @@ -133,10 +132,10 @@ func TestDurationSlice(t *testing.T) {
})

t.Run("as SliceValue", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSet(&ds)

in := []string{"1ns", "2ns"}
ds := make([]time.Duration, 0, len(in))
f := newFlag(&ds)

argfmt := "--ds=%s"
arg1 := fmt.Sprintf(argfmt, in[0])
arg2 := fmt.Sprintf(argfmt, in[1])
Expand All @@ -155,10 +154,10 @@ func TestDurationSlice(t *testing.T) {
})

t.Run("called twice", func(t *testing.T) {
var ds []time.Duration
f := setUpDSFlagSet(&ds)

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])
Expand Down
Loading

0 comments on commit d19d54b

Please sign in to comment.