mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Improve tests for bash completion __custom_func
- check for the correct number of occurrences of function name #694
This commit is contained in:
parent
dff8b28b10
commit
4a689cddc4
1 changed files with 9 additions and 2 deletions
|
@ -22,6 +22,13 @@ func check(t *testing.T, found, expected string) {
|
|||
}
|
||||
}
|
||||
|
||||
func checkNumOccurrences(t *testing.T, found, expected string, expectedOccurrences int) {
|
||||
numOccurrences := strings.Count(found, expected)
|
||||
if numOccurrences != expectedOccurrences {
|
||||
t.Errorf("Expecting to contain %d occurrences of: \n %q\nGot %d:\n %q\n", expectedOccurrences, expected, numOccurrences, found)
|
||||
}
|
||||
}
|
||||
|
||||
func checkRegex(t *testing.T, found, pattern string) {
|
||||
matched, err := regexp.MatchString(pattern, found)
|
||||
if err != nil {
|
||||
|
@ -151,8 +158,8 @@ func TestBashCompletions(t *testing.T) {
|
|||
check(t, output, `must_have_one_flag+=("--introot=")`)
|
||||
check(t, output, `must_have_one_flag+=("--persistent-filename=")`)
|
||||
// check for custom completion function with both qualified and unqualified name
|
||||
check(t, output, `__root_custom_func`)
|
||||
check(t, output, `__custom_func`)
|
||||
checkNumOccurrences(t, output, `__custom_func`, 2) // 1. check existence, 2. invoke
|
||||
checkNumOccurrences(t, output, `__root_custom_func`, 3) // 1. check existence, 2. invoke, 3. actual definition
|
||||
// check for custom completion function body
|
||||
check(t, output, `COMPREPLY=( "hello" )`)
|
||||
// check for required nouns
|
||||
|
|
Loading…
Add table
Reference in a new issue