saner assertions for multiline output

This commit is contained in:
Mislav Marohnić 2013-04-02 00:27:27 +02:00
parent 497911d6c0
commit b8504ed2a9
4 changed files with 42 additions and 29 deletions

View file

@ -41,8 +41,7 @@ else
exit 1 exit 1
fi" fi"
run rbenv-completions hello happy world run rbenv-completions hello happy world
assert_success assert_success "\
assert_line 0 "happy" happy
assert_line 1 "world" world"
refute_line 2
} }

View file

@ -22,10 +22,9 @@ create_executable() {
rbenv-rehash rbenv-rehash
run rbenv-completions exec run rbenv-completions exec
assert_success assert_success "\
assert_line 0 "rake" rake
assert_line 1 "ruby" ruby"
refute_line 2
} }
@test "supports hook path with spaces" { @test "supports hook path with spaces" {
@ -45,20 +44,19 @@ create_executable() {
echo \$0 echo \$0
while [[ \$# -gt 0 ]]; do while [[ \$# -gt 0 ]]; do
# hack to avoid bash builtin echo which can't output '-e' # hack to avoid bash builtin echo which can't output '-e'
cat <<<\"\$1\" printf \"%s\\n\" \"\$1\"
shift 1 shift 1
done done"
"
run rbenv-exec ruby -w -e "puts 'hello world'" -- extra args run rbenv-exec ruby -w -e "puts 'hello world'" -- extra args
assert_line 0 "${RBENV_ROOT}/versions/2.0/bin/ruby" assert_success "\
assert_line 1 "-w" ${RBENV_ROOT}/versions/2.0/bin/ruby
assert_line 2 "-e" -w
assert_line 3 "puts 'hello world'" -e
assert_line 4 "--" puts 'hello world'
assert_line 5 "extra" --
assert_line 6 "args" extra
refute_line 7 args"
} }
@test "supports ruby -S <cmd>" { @test "supports ruby -S <cmd>" {

View file

@ -22,10 +22,10 @@ create_hook() {
create_hook "$path2" exec "bueno.bash" create_hook "$path2" exec "bueno.bash"
RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec
assert_success assert_success "\
assert_line 0 "${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash" ${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash
assert_line 1 "${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash" ${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash
assert_line 2 "${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash" ${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash"
} }
@test "supports hook paths with spaces" { @test "supports hook paths with spaces" {
@ -35,9 +35,9 @@ create_hook() {
create_hook "$path2" exec "ahoy.bash" create_hook "$path2" exec "ahoy.bash"
RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec
assert_success assert_success "\
assert_line 0 "${RBENV_TEST_DIR}/my hooks/rbenv.d/exec/hello.bash" ${RBENV_TEST_DIR}/my hooks/rbenv.d/exec/hello.bash
assert_line 1 "${RBENV_TEST_DIR}/etc/rbenv hooks/exec/ahoy.bash" ${RBENV_TEST_DIR}/etc/rbenv hooks/exec/ahoy.bash"
} }
@test "resolves relative paths" { @test "resolves relative paths" {

View file

@ -26,7 +26,7 @@ assert_success() {
if [ "$status" -ne 0 ]; then if [ "$status" -ne 0 ]; then
flunk "command failed with exit status $status" flunk "command failed with exit status $status"
elif [ "$#" -gt 0 ]; then elif [ "$#" -gt 0 ]; then
assert_output "$1" assert_output_lines "$1"
fi fi
} }
@ -34,7 +34,7 @@ assert_failure() {
if [ "$status" -eq 0 ]; then if [ "$status" -eq 0 ]; then
flunk "expected failed exit status" flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then elif [ "$#" -gt 0 ]; then
assert_output "$1" assert_output_lines "$1"
fi fi
} }
@ -50,10 +50,25 @@ assert_output() {
assert_equal "$1" "$output" assert_equal "$1" "$output"
} }
# compares lines with leading whitespace trimmed
assert_output_lines() {
local -a expected
IFS=$'\n' expected=($1)
for (( i=0; i < ${#expected[@]}; i++ )); do
local wants="${expected[$i]}"
local got="${lines[$i]}"
assert_equal \
"${wants#"${wants%%[![:space:]]*}"}" \
"${got#"${got%%[![:space:]]*}"}"
done
assert_equal "${expected[$i]}" "${lines[$i]}"
}
assert_line() { assert_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then if [ "$1" -ge 0 ] 2>/dev/null; then
assert_equal "$2" "${lines[$1]}" assert_equal "$2" "${lines[$1]}"
else else
local line
for line in "${lines[@]}"; do for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then return 0; fi if [ "$line" = "$1" ]; then return 0; fi
done done
@ -63,11 +78,12 @@ assert_line() {
refute_line() { refute_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then if [ "$1" -ge 0 ] 2>/dev/null; then
num_lines="${#lines[@]}" local num_lines="${#lines[@]}"
if [ "$1" -lt "$num_lines" ]; then if [ "$1" -lt "$num_lines" ]; then
flunk "output has $num_lines lines" flunk "output has $num_lines lines"
fi fi
else else
local line
for line in "${lines[@]}"; do for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then if [ "$line" = "$1" ]; then
flunk "expected to not find line \`$line'" flunk "expected to not find line \`$line'"