Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
posener committed Oct 29, 2019
1 parent 3e5ef33 commit 6a6054a
Showing 1 changed file with 72 additions and 58 deletions.
130 changes: 72 additions & 58 deletions subcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -22,7 +54,6 @@ func TestSubCmd(t *testing.T) {
rootFlag bool
sub1Flag string
sub11Flag string
sub2Flag int
wantArgs []string
}{
{
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -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)
}
})
}
Expand All @@ -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())
})
}
}

0 comments on commit 6a6054a

Please sign in to comment.