mirror of
https://github.com/spf13/cobra
synced 2025-04-10 16:57:19 +00:00
Add unit tests for HasCustomFlagCompletion method in Command struct
This commit is contained in:
parent
eeca8112eb
commit
c0e0fcc8d1
1 changed files with 80 additions and 0 deletions
|
@ -2952,3 +2952,83 @@ func TestHelpFuncExecuted(t *testing.T) {
|
||||||
|
|
||||||
checkStringContains(t, output, helpText)
|
checkStringContains(t, output, helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHasCustomFlagCompletion(t *testing.T) {
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
cmd *Command
|
||||||
|
expectedResult bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions not set",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
},
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions set true",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
AllowCustomFlagCompletions: true,
|
||||||
|
},
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions set false",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
AllowCustomFlagCompletions: false,
|
||||||
|
},
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions not set with Valid",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
||||||
|
return []string{}, ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions set true",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
AllowCustomFlagCompletions: true,
|
||||||
|
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
||||||
|
return []string{}, ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HasCustomFlagCompletion() AllowCustomFlagCompletions set false",
|
||||||
|
cmd: &Command{
|
||||||
|
Use: "c",
|
||||||
|
Run: emptyRun,
|
||||||
|
AllowCustomFlagCompletions: false,
|
||||||
|
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
||||||
|
return []string{}, ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if tc.cmd.HasCustomFlagCompletion() != tc.expectedResult {
|
||||||
|
t.Errorf("did not return expected results")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue