Skip to content

Commit

Permalink
Add subcommand test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Jun 5, 2023
1 parent a149103 commit b68cbf6
Showing 1 changed file with 60 additions and 36 deletions.
96 changes: 60 additions & 36 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,21 +1162,19 @@ func TestDefaultCompleteWithFlags(t *testing.T) {

for _, tc := range []struct {
name string
c *Context
cmd *Command
a *App
argv []string
expected string
}{
{
name: "empty",
c: &Context{App: &App{}},
cmd: &Command{},
a: &App{},
argv: []string{"prog", "cmd"},
expected: "",
},
/*{
{
name: "typical-flag-suggestion",
c: &Context{App: &App{
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
Expand All @@ -1185,66 +1183,92 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
Commands: []*Command{
{Name: "putz"},
},
}},
cmd: &Command{
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
argv: []string{"cmd", "--e", "--generate-bash-completion"},
expected: "--excitement\n",
expected: "--everybody-jump-on\n",
},
{
name: "typical-command-suggestion",
c: &Context{App: &App{
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
}},
cmd: &Command{
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
argv: []string{"cmd", "--generate-bash-completion"},
expected: "putz\n",
},
{
name: "typical-subcommand-suggestion",
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
Commands: []*Command{
{
Name: "putz",
HideHelp: true,
Subcommands: []*Command{
{Name: "futz"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
argv: []string{"cmd", "--generate-bash-completion"},
argv: []string{"cmd", "--happiness", "putz", "--generate-bash-completion"},
expected: "futz\n",
},
{
name: "autocomplete-with-spaces",
c: &Context{App: &App{
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
}},
cmd: &Command{
Name: "putz",
Subcommands: []*Command{
{Name: "help"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{Name: "help"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
argv: []string{"cmd", "--url", "http://localhost:8000", "h", "--generate-bash-completion"},
argv: []string{"cmd", "--happiness", "putz", "h", "--generate-bash-completion"},
expected: "help\n",
},*/
},
} {
t.Run(tc.name, func(ct *testing.T) {
writer := &bytes.Buffer{}
tc.c.App.Writer = writer

tc.c.App.Run(tc.argv)
tc.a.EnableBashCompletion = true
tc.a.HideHelp = true
tc.a.Writer = writer
os.Args = tc.argv
tc.a.Run(tc.argv)

written := writer.String()

Expand Down

0 comments on commit b68cbf6

Please sign in to comment.