Skip to content

Commit

Permalink
Merge pull request #1768 from urfave/collapse-uint64-uint
Browse files Browse the repository at this point in the history
Collapse `uint` and `uint64` flag types
  • Loading branch information
meatballhat authored Jun 23, 2023
2 parents f0aab7f + 6a0b97b commit de6dd28
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 431 deletions.
73 changes: 8 additions & 65 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,41 +1253,11 @@ func TestCommand_ParseSliceFlags(t *testing.T) {
},
}

_ = cmd.Run(buildTestContext(t), []string{"", "cmd", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})

IntsEquals := func(a, b []int64) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

StrsEquals := func(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
expectedIntSlice := []int64{22, 80}
expectedStringSlice := []string{"8.8.8.8", "8.8.4.4"}

if !IntsEquals(parsedIntSlice, expectedIntSlice) {
t.Errorf("%v does not match %v", parsedIntSlice, expectedIntSlice)
}
r := require.New(t)

if !StrsEquals(parsedStringSlice, expectedStringSlice) {
t.Errorf("%v does not match %v", parsedStringSlice, expectedStringSlice)
}
r.NoError(cmd.Run(buildTestContext(t), []string{"", "cmd", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"}))
r.Equal([]int64{22, 80}, parsedIntSlice)
r.Equal([]string{"8.8.8.8", "8.8.4.4"}, parsedStringSlice)
}

func TestCommand_ParseSliceFlagsWithMissingValue(t *testing.T) {
Expand All @@ -1311,18 +1281,11 @@ func TestCommand_ParseSliceFlagsWithMissingValue(t *testing.T) {
},
}

_ = cmd.Run(buildTestContext(t), []string{"", "cmd", "-a", "2", "-str", "A"})

expectedIntSlice := []int64{2}
expectedStringSlice := []string{"A"}

if parsedIntSlice[0] != expectedIntSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
r := require.New(t)

if parsedStringSlice[0] != expectedStringSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
r.NoError(cmd.Run(buildTestContext(t), []string{"", "cmd", "-a", "2", "-str", "A"}))
r.Equal([]int64{2}, parsedIntSlice)
r.Equal([]string{"A"}, parsedStringSlice)
}

func TestCommand_DefaultStdin(t *testing.T) {
Expand Down Expand Up @@ -2776,16 +2739,6 @@ func TestFlagAction(t *testing.T) {
{
name: "flag_uint_error",
args: []string{"app", "--f_uint=0"},
err: "zero uint",
},
{
name: "flag_uint64",
args: []string{"app", "--f_uint64=1"},
exp: "1 ",
},
{
name: "flag_uint64_error",
args: []string{"app", "--f_uint64=0"},
err: "zero uint64",
},
{
Expand Down Expand Up @@ -2942,16 +2895,6 @@ func TestFlagAction(t *testing.T) {
},
&UintFlag{
Name: "f_uint",
Action: func(cCtx *Context, v uint) error {
if v == 0 {
return fmt.Errorf("zero uint")
}
_, err := cCtx.Command.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&Uint64Flag{
Name: "f_uint64",
Action: func(cCtx *Context, v uint64) error {
if v == 0 {
return fmt.Errorf("zero uint64")
Expand Down
30 changes: 9 additions & 21 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ import (
func TestNewContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Int64("myflag", 12, "doc")
set.Uint("myflagUint", uint(93), "doc")
set.Uint64("myflagUint64", uint64(93), "doc")
set.Uint64("myflagUint", uint64(93), "doc")
set.Float64("myflag64", float64(17), "doc")

globalSet := flag.NewFlagSet("test", 0)
globalSet.Int64("myflag", 42, "doc")
globalSet.Uint("myflagUint", uint(33), "doc")
globalSet.Uint64("myflagUint64", uint64(33), "doc")
globalSet.Uint64("myflagUint", uint64(33), "doc")
globalSet.Float64("myflag64", float64(47), "doc")

globalCtx := NewContext(nil, globalSet, nil)
Expand All @@ -33,8 +31,7 @@ func TestNewContext(t *testing.T) {

r := require.New(t)
r.Equal(int64(12), cCtx.Int("myflag"))
r.Equal(uint(93), cCtx.Uint("myflagUint"))
r.Equal(uint64(93), cCtx.Uint64("myflagUint64"))
r.Equal(uint64(93), cCtx.Uint("myflagUint"))
r.Equal(float64(17), cCtx.Float64("myflag64"))
r.Equal("mycommand", cCtx.Command.Name)
}
Expand All @@ -56,24 +53,15 @@ func TestContext_Int(t *testing.T) {

func TestContext_Uint(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Uint("myflagUint", uint(13), "doc")
set.Uint64("myflagUint", uint64(13), "doc")
parentSet := flag.NewFlagSet("test", 0)
parentSet.Uint("top-flag", uint(14), "doc")
parentSet.Uint64("top-flag", uint64(14), "doc")
parentCtx := NewContext(nil, parentSet, nil)
c := NewContext(nil, set, parentCtx)
expect(t, c.Uint("myflagUint"), uint(13))
expect(t, c.Uint("top-flag"), uint(14))
}
cCtx := NewContext(nil, set, parentCtx)

func TestContext_Uint64(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Uint64("myflagUint64", uint64(9), "doc")
parentSet := flag.NewFlagSet("test", 0)
parentSet.Uint64("top-flag", uint64(10), "doc")
parentCtx := NewContext(nil, parentSet, nil)
c := NewContext(nil, set, parentCtx)
expect(t, c.Uint64("myflagUint64"), uint64(9))
expect(t, c.Uint64("top-flag"), uint64(10))
r := require.New(t)
r.Equal(uint64(13), cCtx.Uint("myflagUint"))
r.Equal(uint64(14), cCtx.Uint("top-flag"))
}

func TestContext_Float64(t *testing.T) {
Expand Down
Loading

0 comments on commit de6dd28

Please sign in to comment.