Refactored finger test

This commit is contained in:
Freddy Vulto 2009-06-14 12:18:24 +02:00
parent 14d0ee38ac
commit 33118de1f6
2 changed files with 34 additions and 89 deletions

View File

@ -12,134 +12,62 @@ setup
set test "Tab should complete usernames"
# Build string list of usernames, separated by regexp whitespace (\s+)
# Example string: user1\s+user2\s+user3
# Build string list of usernames
set users {}
foreach u [exec bash -c "compgen -A user"] {
# Escape special regexp characters (+) in username
regsub -all {([\+])} $u {\\\1} h
lappend users $u
}; # foreach
set users [lsort -ascii $users]
set users [join $users "\\s+"]
# Try completion
set cmd "finger "
send "$cmd\t"
set expected "^$cmd\r\n$users\r\n/@$cmd$"
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
assert_complete $users "finger " $test
sync_after_int
set test "Tab should complete partial username"
# Build string list of usernames, starting with the character of the first
# username. Separate usernames by regexp whitespace (\s+) and 'plus' (+)
# prefix. Example string: \+user1\s+\+user2\s+\+user3
# Build string list of usernames, starting with the character of the
# first username.
set users {}
set char ""
foreach u [exec bash -c "compgen -A user"] {
if {$char == ""} {set char [string range $u 0 0]}
# Only append username if starting with $char
if {[string range $u 0 0] == "$char"} {
# Escape possible special regexp characters (+) in username
regsub -all {([\+])} $u {\\\1} u
lappend users $u
}; # if
}; # foreach
# Try completion
set cmd "finger $char"
send "$cmd\t"
if {[llength $users] == 1} {
set expected "^finger $users"
} else {
set users [lsort -ascii $users]
set users [join $users "\\s+"]
set expected "^$cmd\r\n$users\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
assert_complete $users "finger $char" $test
sync_after_int
set test "Tab should complete hostnames"
# Build string list of hostnames, separated by regexp whitespace (\s+)
# Example string: host1\s+host2\s+host3
# Build string list of hostnames
set hosts {}
foreach h [exec bash -c "compgen -A hostname"] {
# Escape special regexp characters (+) in hostname
regsub -all {([\+])} $h {\\\1} h
# Prefix hosts with username 'test@'
lappend hosts "test@$h"
}; # foreach
# Try completion
set cmd "finger test@"
send "$cmd\t"
if {[llength $hosts] == 1} {
set expected "^$cmd$hosts "
} else {
set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"]
set expected "^$cmd\r\n$hosts\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
assert_complete $hosts "finger test@" $test
sync_after_int
set test "Tab should complete partial hostname"
# Build string list of hostnames, starting with the character of the first
# host. Separate hostnames by regexp whitespace (\s+) and 'plus' (+)
# prefix. Example string: \+host1\s+\+host2\s+\+host3
# Build string list of hostnames, starting with the character of the first
# host.
set hosts {}
set char ""
foreach h [exec bash -c "compgen -A hostname"] {
if {$char == ""} {set char [string range $h 0 0]}
# Only append hostname if starting with $char
if {[string range $h 0 0] == "$char"} {
# Escape special regexp characters (+) in hostname
regsub -all {([\+])} $h {\\\1} h
# Prefix hosts with username 'test@'
lappend hosts "test@$h"
}; # if
}; # foreach
# Try completion
set cmd "finger test@$char"
send "$cmd\t"
if {[llength $hosts] == 1} {
set expected "^finger $hosts"
} else {
set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"]
set expected "^$cmd\r\n$hosts\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
assert_complete $hosts "finger test@$char" $test
sync_after_int

View File

@ -54,13 +54,28 @@ proc assert_bash_type {command} {
proc assert_complete {expected cmd {test ""} {prompt /@} {size 20}} {
if {$test == ""} {set test "$cmd should show completions"}
send "$cmd\t"
expect -ex "$cmd\r\n"
if {[llength $expected] == 1} {
expect -ex "$cmd"
# Assume second word is word to complete on.
set cur [lindex [split $cmd] 1]
# 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
} else {
expect -ex "$cmd\r\n"
}; # if
if {[match_items $expected $test]} {
expect {
-re "$prompt$cmd$" { pass "$test" }
-re $prompt { unresolved "$test at prompt" }
-re eof { unresolved "eof" }
}
if {[llength $expected] == 1} {
pass "$test"
} else {
expect {
-re "$prompt$cmd$" { pass "$test" }
-re $prompt { unresolved "$test at prompt" }
-re eof { unresolved "eof" }
}; # expect
}; # if
} else {
fail "$test"
}; # if
@ -159,6 +174,7 @@ proc assert_exec {cmd {stdout ''} {test ''}} {
# @param integer $size Chunk size
# @result boolean True if successful, False if not
proc match_items {items test {size 20}} {
set items [lsort -ascii $items]
set result false
for {set i 0} {$i < [llength $items]} {set i [expr {$i + $size}]} {
set expected ""
@ -166,7 +182,8 @@ proc match_items {items test {size 20}} {
set item "[lindex $items [expr {$i + $j}]]"
# Escape special regexp characters
regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item
set expected "${expected}$item\\s+"
append expected $item
if {[llength $items] > 1} {append expected {\s+}}
}; # for
expect {
-re "$expected" { set result true }