From 6a6054a7290327ba15dd6678d171422392c43cc3 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Tue, 29 Oct 2019 08:22:33 +0200 Subject: [PATCH] Improve tests --- subcmd_test.go | 130 +++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 58 deletions(-) diff --git a/subcmd_test.go b/subcmd_test.go index 2d9370a..9bd5cbd 100644 --- a/subcmd_test.go +++ b/subcmd_test.go @@ -3,13 +3,45 @@ package subcmd import ( "bytes" "flag" - "io/ioutil" "strings" "testing" "github.com/stretchr/testify/assert" ) +type testCommand struct { + *Cmd + sub1 *Cmd + sub11 *Cmd + sub2 *Cmd + rootFlag *bool + sub1Flag *string + sub11Flag *string + out bytes.Buffer +} + +func testRoot() *testCommand { + var root testCommand + + root.Cmd = Root( + OptName("cmd"), + OptDescription("description"), + OptErrorHandling(flag.ContinueOnError), + OptOutput(&root.out)) + + root.rootFlag = root.Bool("flag", false, "example of bool flag") + + root.sub1 = root.SubCommand("sub1", "a sub command with flags and sub commands") + root.sub1Flag = root.sub1.String("flag", "", "example of string flag") + + root.sub11 = root.sub1.SubCommand("sub1", "sub command of sub command") + root.sub11Flag = root.sub11.String("flag", "", "example of string flag") + + root.sub2 = root.SubCommand("sub2", "a sub command without flags and sub commands") + + return &root +} + func TestSubCmd(t *testing.T) { t.Parallel() @@ -22,7 +54,6 @@ func TestSubCmd(t *testing.T) { rootFlag bool sub1Flag string sub11Flag string - sub2Flag int wantArgs []string }{ { @@ -50,16 +81,15 @@ func TestSubCmd(t *testing.T) { sub11Flag: "value", }, { - args: []string{"cmd", "sub2", "-flag", "1"}, + args: []string{"cmd", "sub2"}, sub2Parsed: true, - sub2Flag: 1, }, { args: []string{"cmd", "-no-such-flag"}, wantErr: true, }, { - args: []string{"cmd", "-flag", "-no-such-flag"}, + args: []string{"cmd", "-rootflag", "-no-such-flag"}, wantErr: true, }, { @@ -79,31 +109,18 @@ func TestSubCmd(t *testing.T) { for _, tt := range tests { t.Run(strings.Join(tt.args, " "), func(t *testing.T) { - - root := Root(OptErrorHandling(flag.ContinueOnError), OptOutput(ioutil.Discard)) - rootFlag := root.Bool("flag", false, "example of bool flag") - - sub1 := root.SubCommand("sub1", "sub command of root command") - sub1Flag := sub1.String("flag", "", "example of string flag") - - sub11 := sub1.SubCommand("sub1", "sub command of sub command") - sub11Flag := sub11.String("flag", "", "example of string flag") - - sub2 := root.SubCommand("sub2", "another sub command of root command") - sub2Flag := sub2.Int("flag", 0, "example of int flag") - + root := testRoot() err := root.Parse(tt.args) if tt.wantErr { assert.Error(t, err) } else { assert.NoError(t, err) - assert.Equal(t, tt.sub1Parsed, sub1.Parsed()) - assert.Equal(t, tt.sub11Parsed, sub11.Parsed()) - assert.Equal(t, tt.sub2Parsed, sub2.Parsed()) - assert.Equal(t, tt.rootFlag, *rootFlag) - assert.Equal(t, tt.sub1Flag, *sub1Flag) - assert.Equal(t, tt.sub11Flag, *sub11Flag) - assert.Equal(t, tt.sub2Flag, *sub2Flag) + assert.Equal(t, tt.sub1Parsed, root.sub1.Parsed()) + assert.Equal(t, tt.sub11Parsed, root.sub11.Parsed()) + assert.Equal(t, tt.sub2Parsed, root.sub2.Parsed()) + assert.Equal(t, tt.rootFlag, *root.rootFlag) + assert.Equal(t, tt.sub1Flag, *root.sub1Flag) + assert.Equal(t, tt.sub11Flag, *root.sub11Flag) } }) } @@ -112,55 +129,52 @@ func TestSubCmd(t *testing.T) { func TestHelp(t *testing.T) { t.Parallel() - rootHelp := `cmd description -Subcommands: - sub1 sub command of root command - sub2 another sub command of root command -` - sub1Help := `sub1 sub command of root command -Subcommands: - sub1 sub command of sub command -Flags: - -flag string - example of string flag -` - tests := []struct { args []string want string }{ { args: []string{"cmd", "-h"}, - want: rootHelp, + want: `cmd description +Subcommands: + sub1 a sub command with flags and sub commands + sub2 a sub command without flags and sub commands +Flags: + -flag + example of bool flag +`, }, { args: []string{"cmd", "sub1", "-h"}, - want: sub1Help, + want: `sub1 a sub command with flags and sub commands +Subcommands: + sub1 sub command of sub command +Flags: + -flag string + example of string flag +`, + }, + { + args: []string{"cmd", "sub2", "-h"}, + want: `sub2 a sub command without flags and sub commands +`, + }, + { + args: []string{"cmd", "sub1", "sub1", "-h"}, + want: `sub1 sub command of sub command +Flags: + -flag string + example of string flag +`, }, } for _, tt := range tests { t.Run(strings.Join(tt.args, " "), func(t *testing.T) { - var out bytes.Buffer - - root := Root( - OptName("cmd"), - OptDescription("description"), - OptErrorHandling(flag.ContinueOnError), - OptOutput(&out)) - - sub1 := root.SubCommand("sub1", "sub command of root command") - _ = sub1.String("flag", "", "example of string flag") - - sub11 := sub1.SubCommand("sub1", "sub command of sub command") - _ = sub11.String("flag", "", "example of string flag") - - sub2 := root.SubCommand("sub2", "another sub command of root command") - _ = sub2.Int("flag", 0, "example of int flag") - + root := testRoot() err := root.Parse(tt.args) assert.Equal(t, err, flag.ErrHelp) - assert.Equal(t, tt.want, out.String()) + assert.Equal(t, tt.want, root.out.String()) }) } }