test argument completion
This commit is contained in:
parent
424ae202cd
commit
f0a8223e8d
@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "git.rob.mx/nidito/chinampa/pkg/command"
|
. "git.rob.mx/nidito/chinampa/pkg/command"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testCommand() *Command {
|
func testCommand() *Command {
|
||||||
@ -413,3 +414,127 @@ func TestArgumentToDesc(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestArgumentCompletion(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
testcmd := func() (*Command, *cobra.Command) {
|
||||||
|
cc := &cobra.Command{
|
||||||
|
Use: "test-command",
|
||||||
|
Short: "test",
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := testCommand()
|
||||||
|
cmd.SetCobra(cc)
|
||||||
|
cmd.SetBindings()
|
||||||
|
return cmd, cc
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("empty-args", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
args := &Arguments{}
|
||||||
|
|
||||||
|
cc := &cobra.Command{}
|
||||||
|
values, directive := args.CompletionFunction(cc, []string{}, "")
|
||||||
|
|
||||||
|
if len(values) != 0 {
|
||||||
|
t.Fatal("Values offered for empty argument spec")
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveError {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty arg spec", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cmd, cc := testcmd()
|
||||||
|
values, directive := cmd.Arguments.CompletionFunction(cc, []string{}, "")
|
||||||
|
|
||||||
|
if len(values) != 1 || values[0] != "_activeHelp_ " {
|
||||||
|
t.Fatalf("Values offered for empty argument spec: (%d) %v", len(values), values)
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveError {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("first-arg", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cmd, cc := testcmd()
|
||||||
|
choices := []string{"au", "to", "com", "plete"}
|
||||||
|
cmd.Arguments[0].Values = &ValueSource{
|
||||||
|
Static: &choices,
|
||||||
|
}
|
||||||
|
|
||||||
|
values, directive := cmd.Arguments.CompletionFunction(cc, []string{}, "")
|
||||||
|
|
||||||
|
expected := append(choices, "_activeHelp_") // nolint: gocritic
|
||||||
|
if reflect.DeepEqual(expected, values) {
|
||||||
|
t.Fatalf("Unexpected values offered: %v", values)
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveDefault {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("first-arg-prefix", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
choices := []string{"au", "to", "com", "plete"}
|
||||||
|
cmd, cc := testcmd()
|
||||||
|
cmd.Arguments[0].Values = &ValueSource{
|
||||||
|
Static: &choices,
|
||||||
|
}
|
||||||
|
values, directive := cmd.Arguments.CompletionFunction(cc, []string{}, "a")
|
||||||
|
|
||||||
|
expected := []string{"au", "_activeHelp_"}
|
||||||
|
if reflect.DeepEqual(expected, values) {
|
||||||
|
t.Fatalf("Unexpected values offered: %v", values)
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveDefault {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("variadic-arg", func(t *testing.T) {
|
||||||
|
choices := []string{"au", "to", "com", "plete"}
|
||||||
|
cmd, cc := testcmd()
|
||||||
|
cmd.Arguments[1].Values = &ValueSource{
|
||||||
|
Static: &choices,
|
||||||
|
}
|
||||||
|
|
||||||
|
values, directive := cmd.Arguments.CompletionFunction(cc, []string{"au", ""}, "")
|
||||||
|
|
||||||
|
expected := append(choices, "_activeHelp_") // nolint: gocritic
|
||||||
|
if reflect.DeepEqual(expected, values) {
|
||||||
|
t.Fatalf("Unexpected values offered: %v", values)
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveDefault {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("variadic-arg-repeated", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cmd, cc := testcmd()
|
||||||
|
choices := []string{"au", "to", "com", "plete"}
|
||||||
|
cmd.Arguments[1].Values = &ValueSource{
|
||||||
|
Static: &choices,
|
||||||
|
}
|
||||||
|
|
||||||
|
values, directive := cmd.Arguments.CompletionFunction(cc, []string{"au", "au", ""}, "t")
|
||||||
|
|
||||||
|
expected := []string{"to", "_activeHelp_"}
|
||||||
|
if reflect.DeepEqual(expected, values) {
|
||||||
|
t.Fatalf("Unexpected values offered: %v", values)
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive != cobra.ShellCompDirectiveDefault {
|
||||||
|
t.Fatalf("Unexpected directive: %d", directive)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user