(t *testing.T)
| 3005 | } |
| 3006 | |
| 3007 | func TestFixedCompletionsWithCompletionHelpers(t *testing.T) { |
| 3008 | rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} |
| 3009 | // here we are mixing string, [Completion] and [CompletionWithDesc] |
| 3010 | choices := []string{"apple", Completion("banana"), CompletionWithDesc("orange", "orange are orange")} |
| 3011 | childCmd := &Command{ |
| 3012 | Use: "child", |
| 3013 | ValidArgsFunction: FixedCompletions(choices, ShellCompDirectiveNoFileComp), |
| 3014 | Run: emptyRun, |
| 3015 | } |
| 3016 | rootCmd.AddCommand(childCmd) |
| 3017 | |
| 3018 | t.Run("completion with description", func(t *testing.T) { |
| 3019 | output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "a") |
| 3020 | if err != nil { |
| 3021 | t.Errorf("Unexpected error: %v", err) |
| 3022 | } |
| 3023 | |
| 3024 | expected := strings.Join([]string{ |
| 3025 | "apple", |
| 3026 | "banana", |
| 3027 | "orange\torange are orange", // this one has the description as expected with [ShellCompRequestCmd] flag |
| 3028 | ":4", |
| 3029 | "Completion ended with directive: ShellCompDirectiveNoFileComp", "", |
| 3030 | }, "\n") |
| 3031 | |
| 3032 | if output != expected { |
| 3033 | t.Errorf("expected: %q, got: %q", expected, output) |
| 3034 | } |
| 3035 | }) |
| 3036 | |
| 3037 | t.Run("completion with no description", func(t *testing.T) { |
| 3038 | output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "a") |
| 3039 | if err != nil { |
| 3040 | t.Errorf("Unexpected error: %v", err) |
| 3041 | } |
| 3042 | |
| 3043 | expected := strings.Join([]string{ |
| 3044 | "apple", |
| 3045 | "banana", |
| 3046 | "orange", // the description is absent as expected with [ShellCompNoDescRequestCmd] flag |
| 3047 | ":4", |
| 3048 | "Completion ended with directive: ShellCompDirectiveNoFileComp", "", |
| 3049 | }, "\n") |
| 3050 | |
| 3051 | if output != expected { |
| 3052 | t.Errorf("expected: %q, got: %q", expected, output) |
| 3053 | } |
| 3054 | }) |
| 3055 | } |
| 3056 | |
| 3057 | func TestCompletionForGroupedFlags(t *testing.T) { |
| 3058 | getCmd := func() *Command { |
nothing calls this directly
no test coverage detected