[Bash-completion-commits] [SCM] bash-completion branch, master, updated. 67eedb203f041b2c1a7839a97ccb03fffbd873e9

Freddy Vulto fvulto at gmail.com
Fri Jun 19 12:24:47 UTC 2009


The following commit has been merged in the master branch:
commit 67eedb203f041b2c1a7839a97ccb03fffbd873e9
Author: Freddy Vulto <fvulto at gmail.com>
Date:   Fri Jun 19 14:23:57 2009 +0200

    Added ssh completion tests:
    - First argument shouldn't complete with commands
    - Tab should complete partial hostname
    
    To run the tests: $ cd test && ./runCompletion ssh.exp
    
    Added test library function: assert_complete_partial

diff --git a/test/lib/completions/ssh.exp b/test/lib/completions/ssh.exp
index 3794263..82436d5 100644
--- a/test/lib/completions/ssh.exp
+++ b/test/lib/completions/ssh.exp
@@ -42,4 +42,31 @@ expect {
 sync_after_int
 
 
+set test "First argument shouldn't complete with commands"
+# NOTE: This test assumes the machine running this test has a command "bash"
+#       but no host named "bash" ...
+set cmd "ssh bas"
+send "$cmd\t"
+expect -ex "$cmd"
+expect {
+    -timeout 1
+        # In case multiple commands `bas*' are completed
+    -re "^\r\n.*bash.*\r\n/@$cmd$" { fail "$test" }
+        # In case the single command `bash' is completed
+    -re "h $" { fail "$test" }
+    -re ".+" { unresolved "$test" }
+    timeout { pass "$test" }
+}; # expect
+
+
+sync_after_int
+
+
+set test "Tab should complete partial hostname"
+assert_complete_partial [exec bash -c "compgen -A hostname"] "ssh"
+
+
+sync_after_int
+
+
 teardown
diff --git a/test/lib/library.exp b/test/lib/library.exp
index 6b4fe1a..1d844ae 100644
--- a/test/lib/library.exp
+++ b/test/lib/library.exp
@@ -56,9 +56,9 @@ proc assert_complete {expected cmd {test ""} {prompt /@} {size 20}} {
     send "$cmd\t"
     if {[llength $expected] == 1} {
         expect -ex "$cmd"
-        # Assume second word is word to complete on.
+            # Assume second word is word to complete on.
         set cur [lindex [split $cmd] 1]
-        # Remove second word from beginning of single item $expected
+            # Remove second word from beginning of single item $expected
         if {[string first $cur $expected] == 0} {
             set expected [string range $expected [string length $cur] end]
         }; # if
@@ -82,6 +82,33 @@ proc assert_complete {expected cmd {test ""} {prompt /@} {size 20}} {
 }; # assert_complete()
 
 
+# Make sure a partial argument is completed.
+# A completion is tried with `$partial', or if this is empty, the first
+# character of the first item of `$expected'.  Only the items from $expected,
+# starting with this character are then expected as completions.
+# @param list $expected  List of all completions.
+# @param string $cmd  Command given to generate items
+# @param string $test  (optional) Test titel.  Default is "$cmd<TAB> should show completions"
+# @param integer $size  (optional) Chunk size.  Default is 20.
+# @result boolean  True if successful, False if not
+proc assert_complete_partial {expected cmd {partial ""} {test ""} {prompt /@} {size 20}} {
+    if {$test == ""} {set test "$cmd should complete partial argument"}
+    if {[llength $expected] == 0} {
+        unresolved "$test"
+    } else {
+        set pick {}
+        foreach item $expected {
+            if {$partial == ""} {set partial [string range $item 0 0]}
+                # Only append item if starting with $partial
+            if {[string range $item 0 [expr [string length $partial] - 1]] == "$partial"} {
+                lappend pick $item
+            }; # if
+        }; # foreach
+        assert_complete $pick "$cmd $partial" $test $prompt $size
+    }; # if
+}; # assert_complete_partial()
+
+
 # Make sure any completions are returned
 proc assert_complete_any {cmd {test ""} {prompt /@}} {
     if {$test == ""} {set test "$cmd should show completions"}
@@ -201,13 +228,24 @@ proc match_items {items test {size 20}} {
                 # Escape special regexp characters
             regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item
             append expected $item
-            if {[llength $items] > 1} {append expected {\s+}}
+            if {[llength $items] > 1} {append expected {\s+}};
         }; # for
-        expect {
-            -re "$expected" { set result true }
-            default { set result false; break }
-            timeout { set result false; break }
-        }; # expect
+        if {[llength $items] == 1} {
+            expect {
+                -timeout 1
+                -re "$expected" { set result true }
+                "\r\n" { set result false; break }
+                default { set result false; break }
+                timeout { set result false; break }
+            }; # expect
+        } else {
+            expect {
+                -timeout 1
+                -re "$expected" { set result true }
+                default { set result false; break }
+                timeout { set result false; break }
+            }; # expect
+        }; # if
     }; # for
     return $result
 }; # match_items()

-- 
bash-completion



More information about the Bash-completion-commits mailing list