From f0a8223e8d767d65c415d9152d3fc406f2efeb2f Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Wed, 18 Jan 2023 00:38:03 -0600 Subject: [PATCH] test argument completion --- pkg/command/arguments_test.go | 125 ++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/pkg/command/arguments_test.go b/pkg/command/arguments_test.go index bf40585..d443982 100644 --- a/pkg/command/arguments_test.go +++ b/pkg/command/arguments_test.go @@ -8,6 +8,7 @@ import ( "testing" . "git.rob.mx/nidito/chinampa/pkg/command" + "github.com/spf13/cobra" ) 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) + } + }) +}