From b8504ed2a99c6553c5ecefeea93e70e4b9f7ef60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Tue, 2 Apr 2013 00:27:27 +0200 Subject: [PATCH] saner assertions for multiline output --- test/completions.bats | 7 +++---- test/exec.bats | 28 +++++++++++++--------------- test/hooks.bats | 14 +++++++------- test/test_helper.bash | 22 +++++++++++++++++++--- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/test/completions.bats b/test/completions.bats index 8bfb88d5..4b05357a 100644 --- a/test/completions.bats +++ b/test/completions.bats @@ -41,8 +41,7 @@ else exit 1 fi" run rbenv-completions hello happy world - assert_success - assert_line 0 "happy" - assert_line 1 "world" - refute_line 2 + assert_success "\ + happy + world" } diff --git a/test/exec.bats b/test/exec.bats index 92338aea..7d80bcd0 100644 --- a/test/exec.bats +++ b/test/exec.bats @@ -22,10 +22,9 @@ create_executable() { rbenv-rehash run rbenv-completions exec - assert_success - assert_line 0 "rake" - assert_line 1 "ruby" - refute_line 2 + assert_success "\ + rake + ruby" } @test "supports hook path with spaces" { @@ -45,20 +44,19 @@ create_executable() { echo \$0 while [[ \$# -gt 0 ]]; do # hack to avoid bash builtin echo which can't output '-e' - cat <<<\"\$1\" + printf \"%s\\n\" \"\$1\" shift 1 - done - " + done" run rbenv-exec ruby -w -e "puts 'hello world'" -- extra args - assert_line 0 "${RBENV_ROOT}/versions/2.0/bin/ruby" - assert_line 1 "-w" - assert_line 2 "-e" - assert_line 3 "puts 'hello world'" - assert_line 4 "--" - assert_line 5 "extra" - assert_line 6 "args" - refute_line 7 + assert_success "\ + ${RBENV_ROOT}/versions/2.0/bin/ruby + -w + -e + puts 'hello world' + -- + extra + args" } @test "supports ruby -S " { diff --git a/test/hooks.bats b/test/hooks.bats index da450b6a..7d3b253f 100644 --- a/test/hooks.bats +++ b/test/hooks.bats @@ -22,10 +22,10 @@ create_hook() { create_hook "$path2" exec "bueno.bash" RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec - assert_success - assert_line 0 "${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash" - assert_line 1 "${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash" - assert_line 2 "${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash" + assert_success "\ + ${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash + ${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash + ${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash" } @test "supports hook paths with spaces" { @@ -35,9 +35,9 @@ create_hook() { create_hook "$path2" exec "ahoy.bash" RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec - assert_success - assert_line 0 "${RBENV_TEST_DIR}/my hooks/rbenv.d/exec/hello.bash" - assert_line 1 "${RBENV_TEST_DIR}/etc/rbenv hooks/exec/ahoy.bash" + assert_success "\ + ${RBENV_TEST_DIR}/my hooks/rbenv.d/exec/hello.bash + ${RBENV_TEST_DIR}/etc/rbenv hooks/exec/ahoy.bash" } @test "resolves relative paths" { diff --git a/test/test_helper.bash b/test/test_helper.bash index 5fb24def..1d49ab2a 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -26,7 +26,7 @@ assert_success() { if [ "$status" -ne 0 ]; then flunk "command failed with exit status $status" elif [ "$#" -gt 0 ]; then - assert_output "$1" + assert_output_lines "$1" fi } @@ -34,7 +34,7 @@ assert_failure() { if [ "$status" -eq 0 ]; then flunk "expected failed exit status" elif [ "$#" -gt 0 ]; then - assert_output "$1" + assert_output_lines "$1" fi } @@ -50,10 +50,25 @@ assert_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() { if [ "$1" -ge 0 ] 2>/dev/null; then assert_equal "$2" "${lines[$1]}" else + local line for line in "${lines[@]}"; do if [ "$line" = "$1" ]; then return 0; fi done @@ -63,11 +78,12 @@ assert_line() { refute_line() { if [ "$1" -ge 0 ] 2>/dev/null; then - num_lines="${#lines[@]}" + local num_lines="${#lines[@]}" if [ "$1" -lt "$num_lines" ]; then flunk "output has $num_lines lines" fi else + local line for line in "${lines[@]}"; do if [ "$line" = "$1" ]; then flunk "expected to not find line \`$line'"