MCPcopy
hub / github.com/spf13/cobra / TestInitDefaultCompletionCmd

Function TestInitDefaultCompletionCmd

completions_test.go:3930–4017  ·  view source on GitHub ↗

A test to make sure the InitDefaultCompletionCmd function works as expected in case a project calls it directly.

(t *testing.T)

Source from the content-addressed store, hash-verified

3928// A test to make sure the InitDefaultCompletionCmd function works as expected
3929// in case a project calls it directly.
3930func TestInitDefaultCompletionCmd(t *testing.T) {
3931
3932 testCases := []struct {
3933 desc string
3934 hasChildCmd bool
3935 args []string
3936 expectCompCmd bool
3937 }{
3938 {
3939 desc: "no child command and not calling the completion command",
3940 hasChildCmd: false,
3941 args: []string{"somearg"},
3942 expectCompCmd: false,
3943 },
3944 {
3945 desc: "no child command but calling the completion command",
3946 hasChildCmd: false,
3947 args: []string{"completion"},
3948 expectCompCmd: true,
3949 },
3950 {
3951 desc: "no child command but calling __complete on the root command",
3952 hasChildCmd: false,
3953 args: []string{"__complete", ""},
3954 expectCompCmd: false,
3955 },
3956 {
3957 desc: "no child command but calling __complete on the completion command",
3958 hasChildCmd: false,
3959 args: []string{"__complete", "completion", ""},
3960 expectCompCmd: true,
3961 },
3962 {
3963 desc: "with child command",
3964 hasChildCmd: true,
3965 args: []string{"child"},
3966 expectCompCmd: true,
3967 },
3968 {
3969 desc: "no child command not passing args",
3970 hasChildCmd: false,
3971 args: nil,
3972 expectCompCmd: false,
3973 },
3974 {
3975 desc: "with child command not passing args",
3976 hasChildCmd: true,
3977 args: nil,
3978 expectCompCmd: true,
3979 },
3980 }
3981
3982 for _, tc := range testCases {
3983 t.Run(tc.desc, func(t *testing.T) {
3984 rootCmd := &Command{Use: "root", Run: emptyRun}
3985 childCmd := &Command{Use: "child", Run: emptyRun}
3986
3987 expectedNumSubCommands := 0

Callers

nothing calls this directly

Calls 4

AddCommandMethod · 0.95
initCompleteCmdMethod · 0.95
CommandsMethod · 0.95

Tested by

no test coverage detected