From 4a689cddc4e987611848cdd19496142b8c6eda77 Mon Sep 17 00:00:00 2001 From: pgwhalen Date: Mon, 20 Aug 2018 21:15:45 -0500 Subject: [PATCH] Improve tests for bash completion __custom_func - check for the correct number of occurrences of function name #694 --- bash_completions_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bash_completions_test.go b/bash_completions_test.go index f63042e4..94b965da 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -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