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
This commit is contained in:
Freddy Vulto 2009-06-19 14:23:57 +02:00
parent 362090a160
commit 67eedb203f
2 changed files with 73 additions and 8 deletions

View File

@ -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

View File

@ -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
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()