From 39decf197fbf257081eee533974412140d7364e2 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Tue, 30 Apr 2019 13:49:55 -0700 Subject: [PATCH] add several subcommand unittests --- parse.go | 4 -- subcommand_test.go | 115 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/parse.go b/parse.go index 63059fc..2a3442a 100644 --- a/parse.go +++ b/parse.go @@ -292,7 +292,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { cmd.subcommands = append(cmd.subcommands, subcmd) isSubcommand = true - fmt.Println("found a subcommand") default: errs = append(errs, fmt.Sprintf("unrecognized tag '%s' on field %s", key, tag)) 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) } - fmt.Printf("parsed a command with %d subcommands\n", len(cmd.subcommands)) - return &cmd, nil } @@ -434,7 +431,6 @@ func (p *Parser) process(args []string) error { if !isFlag(arg) || allpositional { // 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 { positionals = append(positionals, arg) continue diff --git a/subcommand_test.go b/subcommand_test.go index 02c7b54..6df3147 100644 --- a/subcommand_test.go +++ b/subcommand_test.go @@ -45,3 +45,118 @@ func TestMinimalSubcommand(t *testing.T) { require.NoError(t, err) 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") + } +}