completion: refactor __gitcomp related tests
[gitweb.git] / t / t9902-completion.sh
index 40d6f90ef6fcc48e6f23b41a6ee9364b9e4fe72f..f754de31221d2fd8f2b2fc23186031a09e463882 100755 (executable)
@@ -60,102 +60,77 @@ run_completion ()
 # 2: expected completion
 test_completion ()
 {
-       test $# -gt 1 && echo "$2" > expected
+       if test $# -gt 1
+       then
+               printf '%s\n' "$2" >expected
+       else
+               sed -e 's/Z$//' >expected
+       fi &&
        run_completion "$1" &&
        test_cmp expected out
 }
 
-# Like test_completion, but reads expectation from stdin,
-# which is convenient when it is multiline.
-test_completion_long ()
+newline=$'\n'
+
+# Test __gitcomp.
+# The first argument is the typed text so far (cur); the rest are
+# passed to __gitcomp.  Expected output comes is read from the
+# standard input, like test_completion().
+test_gitcomp ()
 {
        sed -e 's/Z$//' >expected &&
-       test_completion "$1"
+       (
+               local -a COMPREPLY &&
+               cur="$1" &&
+               shift &&
+               __gitcomp "$@" &&
+               IFS="$newline" &&
+               echo "${COMPREPLY[*]}" >out
+       ) &&
+       test_cmp expected out
 }
 
-newline=$'\n'
-
 test_expect_success '__gitcomp - trailing space - options' '
-       sed -e "s/Z$//" >expected <<-\EOF &&
+       test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
+               --reset-author" <<-EOF
        --reuse-message=Z
        --reedit-message=Z
        --reset-author Z
        EOF
-       (
-               local -a COMPREPLY &&
-               cur="--re" &&
-               __gitcomp "--dry-run --reuse-message= --reedit-message=
-                               --reset-author" &&
-               IFS="$newline" &&
-               echo "${COMPREPLY[*]}" > out
-       ) &&
-       test_cmp expected out
 '
 
 test_expect_success '__gitcomp - trailing space - config keys' '
-       sed -e "s/Z$//" >expected <<-\EOF &&
+       test_gitcomp "br" "branch. branch.autosetupmerge
+               branch.autosetuprebase browser." <<-\EOF
        branch.Z
        branch.autosetupmerge Z
        branch.autosetuprebase Z
        browser.Z
        EOF
-       (
-               local -a COMPREPLY &&
-               cur="br" &&
-               __gitcomp "branch. branch.autosetupmerge
-                               branch.autosetuprebase browser." &&
-               IFS="$newline" &&
-               echo "${COMPREPLY[*]}" > out
-       ) &&
-       test_cmp expected out
 '
 
 test_expect_success '__gitcomp - option parameter' '
-       sed -e "s/Z$//" >expected <<-\EOF &&
+       test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
+               "" "re" <<-\EOF
        recursive Z
        resolve Z
        EOF
-       (
-               local -a COMPREPLY &&
-               cur="--strategy=re" &&
-               __gitcomp "octopus ours recursive resolve subtree
-                       " "" "re" &&
-               IFS="$newline" &&
-               echo "${COMPREPLY[*]}" > out
-       ) &&
-       test_cmp expected out
 '
 
 test_expect_success '__gitcomp - prefix' '
-       sed -e "s/Z$//" >expected <<-\EOF &&
+       test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
+               "branch.maint." "me" <<-\EOF
        branch.maint.merge Z
        branch.maint.mergeoptions Z
        EOF
-       (
-               local -a COMPREPLY &&
-               cur="branch.me" &&
-               __gitcomp "remote merge mergeoptions rebase
-                       " "branch.maint." "me" &&
-               IFS="$newline" &&
-               echo "${COMPREPLY[*]}" > out
-       ) &&
-       test_cmp expected out
 '
 
 test_expect_success '__gitcomp - suffix' '
-       sed -e "s/Z$//" >expected <<-\EOF &&
+       test_gitcomp "branch.me" "master maint next pu" "branch." \
+               "ma" "." <<-\EOF
        branch.master.Z
        branch.maint.Z
        EOF
-       (
-               local -a COMPREPLY &&
-               cur="branch.me" &&
-               __gitcomp "master maint next pu
-                       " "branch." "ma" "." &&
-               IFS="$newline" &&
-               echo "${COMPREPLY[*]}" > out
-       ) &&
-       test_cmp expected out
 '
 
 test_expect_success 'basic' '
@@ -172,7 +147,7 @@ test_expect_success 'basic' '
 '
 
 test_expect_success 'double dash "git" itself' '
-       test_completion_long "git --" <<-\EOF
+       test_completion "git --" <<-\EOF
        --paginate Z
        --no-pager Z
        --git-dir=
@@ -190,7 +165,7 @@ test_expect_success 'double dash "git" itself' '
 '
 
 test_expect_success 'double dash "git checkout"' '
-       test_completion_long "git checkout --" <<-\EOF
+       test_completion "git checkout --" <<-\EOF
        --quiet Z
        --ours Z
        --theirs Z
@@ -206,7 +181,7 @@ test_expect_success 'double dash "git checkout"' '
 test_expect_success 'general options' '
        test_completion "git --ver" "--version " &&
        test_completion "git --hel" "--help " &&
-       test_completion_long "git --exe" <<-\EOF &&
+       test_completion "git --exe" <<-\EOF &&
        --exec-path Z
        --exec-path=
        EOF
@@ -247,7 +222,7 @@ test_expect_success 'setup for ref completion' '
 '
 
 test_expect_success 'checkout completes ref names' '
-       test_completion_long "git checkout m" <<-\EOF
+       test_completion "git checkout m" <<-\EOF
        master Z
        mybranch Z
        mytag Z
@@ -255,7 +230,7 @@ test_expect_success 'checkout completes ref names' '
 '
 
 test_expect_success 'show completes all refs' '
-       test_completion_long "git show m" <<-\EOF
+       test_completion "git show m" <<-\EOF
        master Z
        mybranch Z
        mytag Z
@@ -263,7 +238,7 @@ test_expect_success 'show completes all refs' '
 '
 
 test_expect_success '<ref>: completes paths' '
-       test_completion_long "git show mytag:f" <<-\EOF
+       test_completion "git show mytag:f" <<-\EOF
        file1 Z
        file2 Z
        EOF
@@ -273,7 +248,7 @@ test_expect_success 'complete tree filename with spaces' '
        echo content >"name with spaces" &&
        git add . &&
        git commit -m spaces &&
-       test_completion_long "git show HEAD:nam" <<-\EOF
+       test_completion "git show HEAD:nam" <<-\EOF
        name with spaces Z
        EOF
 '
@@ -282,7 +257,7 @@ test_expect_failure 'complete tree filename with metacharacters' '
        echo content >"name with \${meta}" &&
        git add . &&
        git commit -m meta &&
-       test_completion_long "git show HEAD:nam" <<-\EOF
+       test_completion "git show HEAD:nam" <<-\EOF
        name with ${meta} Z
        name with spaces Z
        EOF