add several subcommand unittests

This commit is contained in:
Alex Flint 2019-04-30 13:49:55 -07:00
parent a78c6ded26
commit 39decf197f
2 changed files with 115 additions and 4 deletions

View File

@ -292,7 +292,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
cmd.subcommands = append(cmd.subcommands, subcmd) cmd.subcommands = append(cmd.subcommands, subcmd)
isSubcommand = true isSubcommand = true
fmt.Println("found a subcommand")
default: default:
errs = append(errs, fmt.Sprintf("unrecognized tag '%s' on field %s", key, tag)) errs = append(errs, fmt.Sprintf("unrecognized tag '%s' on field %s", key, tag))
return false return false
@ -335,8 +334,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
return nil, fmt.Errorf("%T cannot have both subcommands and positional arguments", t) return nil, fmt.Errorf("%T cannot have both subcommands and positional arguments", t)
} }
fmt.Printf("parsed a command with %d subcommands\n", len(cmd.subcommands))
return &cmd, nil return &cmd, nil
} }
@ -434,7 +431,6 @@ func (p *Parser) process(args []string) error {
if !isFlag(arg) || allpositional { if !isFlag(arg) || allpositional {
// each subcommand can have either subcommands or positionals, but not both // each subcommand can have either subcommands or positionals, but not both
fmt.Printf("processing %q, with %d subcommands", arg, len(curCmd.subcommands))
if len(curCmd.subcommands) == 0 { if len(curCmd.subcommands) == 0 {
positionals = append(positionals, arg) positionals = append(positionals, arg)
continue continue

View File

@ -45,3 +45,118 @@ func TestMinimalSubcommand(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, args.List) assert.NotNil(t, args.List)
} }
func TestNamedSubcommand(t *testing.T) {
type listCmd struct {
}
var args struct {
List *listCmd `arg:"subcommand:ls"`
}
err := parse("ls", &args)
require.NoError(t, err)
assert.NotNil(t, args.List)
}
func TestEmptySubcommand(t *testing.T) {
type listCmd struct {
}
var args struct {
List *listCmd `arg:"subcommand"`
}
err := parse("", &args)
require.NoError(t, err)
assert.Nil(t, args.List)
}
func TestTwoSubcommands(t *testing.T) {
type getCmd struct {
}
type listCmd struct {
}
var args struct {
Get *getCmd `arg:"subcommand"`
List *listCmd `arg:"subcommand"`
}
err := parse("list", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
}
func TestSubcommandsWithOptions(t *testing.T) {
type getCmd struct {
Name string
}
type listCmd struct {
Limit int
}
type cmd struct {
Verbose bool
Get *getCmd `arg:"subcommand"`
List *listCmd `arg:"subcommand"`
}
{
var args cmd
err := parse("list", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
}
{
var args cmd
err := parse("list --limit 3", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
assert.Equal(t, args.List.Limit, 3)
}
{
var args cmd
err := parse("list --limit 3 --verbose", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
assert.Equal(t, args.List.Limit, 3)
assert.True(t, args.Verbose)
}
{
var args cmd
err := parse("list --verbose --limit 3", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
assert.Equal(t, args.List.Limit, 3)
assert.True(t, args.Verbose)
}
{
var args cmd
err := parse("--verbose list --limit 3", &args)
require.NoError(t, err)
assert.Nil(t, args.Get)
assert.NotNil(t, args.List)
assert.Equal(t, args.List.Limit, 3)
assert.True(t, args.Verbose)
}
{
var args cmd
err := parse("get", &args)
require.NoError(t, err)
assert.NotNil(t, args.Get)
assert.Nil(t, args.List)
}
{
var args cmd
err := parse("get --name test", &args)
require.NoError(t, err)
assert.NotNil(t, args.Get)
assert.Nil(t, args.List)
assert.Equal(t, args.Get.Name, "test")
}
}