Skip to content

Commit

Permalink
app_test.go: add tests for default command + flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jalavosus committed Jun 21, 2022
1 parent 1dfa982 commit d8c93f8
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app_test.go
Expand Up @@ -558,6 +558,86 @@ func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) {
}
}

var defaultCommandFlagAppTests = []struct {
cmdName string
flag string
defaultCmd string
expected bool
}{
{"foobar", "", "foobar", true},
{"foobar", "-c derp", "foobar", true},
{"batbaz", "", "foobar", true},
{"b", "", "", true},
{"f", "", "", true},
{"", "", "foobar", true},
{"", "", "", true},
{"", "-j", "foobar", true},
{"", "-j", "foobar", true},
{"", "-c derp", "foobar", true},
{"", "--carly=derp", "foobar", true},
{"", "-j", "foobar", true},
{"", "-j", "", true},
{" ", "-j", "foobar", false},
{"", "", "", true},
{" ", "", "", false},
{" ", "-j", "", false},
{"bat", "", "batbaz", false},
{"nothing", "", "batbaz", false},
{"nothing", "", "", false},
{"nothing", "--jimbob", "batbaz", false},
{"nothing", "--carly", "", false},
}

func TestApp_RunDefaultCommandWithFlags(t *testing.T) {
for _, test := range defaultCommandFlagAppTests {
testTitle := fmt.Sprintf("command=%[1]s-flag=%[2]s-default=%[3]s", test.cmdName, test.flag, test.defaultCmd)
t.Run(testTitle, func(t *testing.T) {
app := &App{
DefaultCommand: test.defaultCmd,
Flags: []Flag{
&StringFlag{
Name: "carly",
Aliases: []string{"c"},
Required: false,
},
&BoolFlag{
Name: "jimbob",
Aliases: []string{"j"},
Required: false,
Value: true,
},
},
Commands: []*Command{
{
Name: "foobar",
Aliases: []string{"f"},
},
{Name: "batbaz", Aliases: []string{"b"}},
},
}

appArgs := []string{"c"}

if test.flag != "" {
flags := strings.Split(test.flag, " ")
if len(flags) > 1 {
appArgs = append(appArgs, flags...)
}

flags = strings.Split(test.flag, "=")
if len(flags) > 1 {
appArgs = append(appArgs, flags...)
}
}

appArgs = append(appArgs, test.cmdName)

err := app.Run(appArgs)
expect(t, err == nil, test.expected)
})
}
}

func TestApp_Setup_defaultsReader(t *testing.T) {
app := &App{}
app.Setup()
Expand Down

0 comments on commit d8c93f8

Please sign in to comment.