Improve tests for bash completion __custom_func

- check for the correct number of occurrences of function name

#694
This commit is contained in:
pgwhalen 2018-08-20 21:15:45 -05:00
parent dff8b28b10
commit 4a689cddc4

View file

@ -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) { func checkRegex(t *testing.T, found, pattern string) {
matched, err := regexp.MatchString(pattern, found) matched, err := regexp.MatchString(pattern, found)
if err != nil { 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+=("--introot=")`)
check(t, output, `must_have_one_flag+=("--persistent-filename=")`) check(t, output, `must_have_one_flag+=("--persistent-filename=")`)
// check for custom completion function with both qualified and unqualified name // check for custom completion function with both qualified and unqualified name
check(t, output, `__root_custom_func`) checkNumOccurrences(t, output, `__custom_func`, 2) // 1. check existence, 2. invoke
check(t, output, `__custom_func`) checkNumOccurrences(t, output, `__root_custom_func`, 3) // 1. check existence, 2. invoke, 3. actual definition
// check for custom completion function body // check for custom completion function body
check(t, output, `COMPREPLY=( "hello" )`) check(t, output, `COMPREPLY=( "hello" )`)
// check for required nouns // check for required nouns