Skip to content

Commit

Permalink
Add test cases for subcommands of default command
Browse files Browse the repository at this point in the history
  • Loading branch information
jalavosus committed Jun 21, 2022
1 parent 77feee8 commit 1b3da50
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion app_test.go
Expand Up @@ -502,7 +502,60 @@ func TestApp_RunDefaultCommand(t *testing.T) {
expect(t, err == nil, test.expected)
})
}
}

var defaultCommandSubCmdAppTests = []struct {
cmdName string
subCmd string
defaultCmd string
expected bool
}{
{"foobar", "", "foobar", true},
{"foobar", "carly", "foobar", true},
{"batbaz", "", "foobar", true},
{"b", "", "", true},
{"f", "", "", true},
{"", "", "foobar", true},
{"", "", "", true},
{"", "jimbob", "foobar", true},
{"", "j", "foobar", true},
{"", "carly", "foobar", true},
{"", "jimmers", "foobar", true},
{"", "jimmers", "", true},
{" ", "jimmers", "foobar", false},
{"", "", "", true},
{" ", "", "", false},
{" ", "j", "", false},
{"bat", "", "batbaz", false},
{"nothing", "", "batbaz", false},
{"nothing", "", "", false},
{"nothing", "j", "batbaz", false},
{"nothing", "carly", "", false},
}

func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) {
for _, test := range defaultCommandSubCmdAppTests {
testTitle := fmt.Sprintf("command=%[1]s-subcmd=%[2]s-default=%[3]s", test.cmdName, test.subCmd, test.defaultCmd)
t.Run(testTitle, func(t *testing.T) {
app := &App{
DefaultCommand: test.defaultCmd,
Commands: []*Command{
{
Name: "foobar",
Aliases: []string{"f"},
Subcommands: []*Command{
{Name: "jimbob", Aliases: []string{"j"}},
{Name: "carly"},
},
},
{Name: "batbaz", Aliases: []string{"b"}},
},
}

err := app.Run([]string{"c", test.cmdName, test.subCmd})
expect(t, err == nil, test.expected)
})
}
}

func TestApp_Setup_defaultsReader(t *testing.T) {
Expand Down Expand Up @@ -2333,4 +2386,4 @@ func TestSetupInitializesOnlyNilWriters(t *testing.T) {
if a.Writer != os.Stdout {
t.Errorf("expected a.Writer to be os.Stdout")
}
}
}

0 comments on commit 1b3da50

Please sign in to comment.