Skip to content

Commit d8c93f8

Browse files
committed
app_test.go: add tests for default command + flag
1 parent 1dfa982 commit d8c93f8

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

app_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,86 @@ func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) {
558558
}
559559
}
560560

561+
var defaultCommandFlagAppTests = []struct {
562+
cmdName string
563+
flag string
564+
defaultCmd string
565+
expected bool
566+
}{
567+
{"foobar", "", "foobar", true},
568+
{"foobar", "-c derp", "foobar", true},
569+
{"batbaz", "", "foobar", true},
570+
{"b", "", "", true},
571+
{"f", "", "", true},
572+
{"", "", "foobar", true},
573+
{"", "", "", true},
574+
{"", "-j", "foobar", true},
575+
{"", "-j", "foobar", true},
576+
{"", "-c derp", "foobar", true},
577+
{"", "--carly=derp", "foobar", true},
578+
{"", "-j", "foobar", true},
579+
{"", "-j", "", true},
580+
{" ", "-j", "foobar", false},
581+
{"", "", "", true},
582+
{" ", "", "", false},
583+
{" ", "-j", "", false},
584+
{"bat", "", "batbaz", false},
585+
{"nothing", "", "batbaz", false},
586+
{"nothing", "", "", false},
587+
{"nothing", "--jimbob", "batbaz", false},
588+
{"nothing", "--carly", "", false},
589+
}
590+
591+
func TestApp_RunDefaultCommandWithFlags(t *testing.T) {
592+
for _, test := range defaultCommandFlagAppTests {
593+
testTitle := fmt.Sprintf("command=%[1]s-flag=%[2]s-default=%[3]s", test.cmdName, test.flag, test.defaultCmd)
594+
t.Run(testTitle, func(t *testing.T) {
595+
app := &App{
596+
DefaultCommand: test.defaultCmd,
597+
Flags: []Flag{
598+
&StringFlag{
599+
Name: "carly",
600+
Aliases: []string{"c"},
601+
Required: false,
602+
},
603+
&BoolFlag{
604+
Name: "jimbob",
605+
Aliases: []string{"j"},
606+
Required: false,
607+
Value: true,
608+
},
609+
},
610+
Commands: []*Command{
611+
{
612+
Name: "foobar",
613+
Aliases: []string{"f"},
614+
},
615+
{Name: "batbaz", Aliases: []string{"b"}},
616+
},
617+
}
618+
619+
appArgs := []string{"c"}
620+
621+
if test.flag != "" {
622+
flags := strings.Split(test.flag, " ")
623+
if len(flags) > 1 {
624+
appArgs = append(appArgs, flags...)
625+
}
626+
627+
flags = strings.Split(test.flag, "=")
628+
if len(flags) > 1 {
629+
appArgs = append(appArgs, flags...)
630+
}
631+
}
632+
633+
appArgs = append(appArgs, test.cmdName)
634+
635+
err := app.Run(appArgs)
636+
expect(t, err == nil, test.expected)
637+
})
638+
}
639+
}
640+
561641
func TestApp_Setup_defaultsReader(t *testing.T) {
562642
app := &App{}
563643
app.Setup()

0 commit comments

Comments
 (0)