|
| 1 | +# the following helpers are borrowed from the test_helper.bash in https://github.com/sstephenson/rbenv |
| 2 | + |
| 3 | +flunk() { |
| 4 | + { if [ "$#" -eq 0 ]; then cat - |
| 5 | + else echo "$@" |
| 6 | + fi |
| 7 | + } >&2 |
| 8 | + return 1 |
| 9 | +} |
| 10 | + |
| 11 | +assert_success() { |
| 12 | + if [ "$status" -ne 0 ]; then |
| 13 | + flunk "command failed with exit status $status: $output" |
| 14 | + elif [ "$#" -gt 0 ]; then |
| 15 | + assert_output "$1" |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +assert_failure() { |
| 20 | + if [ "$status" -ne 1 ]; then |
| 21 | + flunk $(printf "expected failed exit status=1, got status=%d" $status) |
| 22 | + elif [ "$#" -gt 0 ]; then |
| 23 | + assert_output "$1" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +assert_equal() { |
| 28 | + if [ "$1" != "$2" ]; then |
| 29 | + { echo "expected: $1" |
| 30 | + echo "actual: $2" |
| 31 | + } | flunk |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +assert_output() { |
| 36 | + local expected |
| 37 | + if [ $# -eq 0 ]; then expected="$(cat -)" |
| 38 | + else expected="$1" |
| 39 | + fi |
| 40 | + assert_equal "$expected" "$output" |
| 41 | +} |
| 42 | + |
| 43 | +assert_matches() { |
| 44 | + local pattern="${1}" |
| 45 | + local actual="${2}" |
| 46 | + |
| 47 | + if [ $# -eq 1 ]; then |
| 48 | + actual="$output" |
| 49 | + fi |
| 50 | + |
| 51 | + if ! grep -E -q "${pattern}" <<<"${actual}"; then |
| 52 | + { echo "pattern: ${pattern}" |
| 53 | + echo "actual: ${actual}" |
| 54 | + } | flunk |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +assert_number() { |
| 59 | + assert_matches "^-?[0-9]+$" "$output" |
| 60 | +} |
| 61 | + |
| 62 | +assert_empty() { |
| 63 | + local actual="${1}" |
| 64 | + |
| 65 | + if [ $# -eq 0 ]; then |
| 66 | + actual="$(cat -)" |
| 67 | + fi |
| 68 | + |
| 69 | + if [ -n "${actual}" ]; then |
| 70 | + { echo "actual: ${actual}" |
| 71 | + } | flunk |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +assert_line() { |
| 76 | + if [ "$1" -ge 0 ] 2>/dev/null; then |
| 77 | + assert_equal "$2" "$(collapse_ws ${lines[$1]})" |
| 78 | + else |
| 79 | + local line |
| 80 | + for line in "${lines[@]}"; do |
| 81 | + if [ "$(collapse_ws $line)" = "$1" ]; then return 0; fi |
| 82 | + done |
| 83 | + flunk "expected line \`$1'" |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +refute_line() { |
| 88 | + if [ "$1" -ge 0 ] 2>/dev/null; then |
| 89 | + local num_lines="${#lines[@]}" |
| 90 | + if [ "$1" -lt "$num_lines" ]; then |
| 91 | + flunk "output has $num_lines lines" |
| 92 | + fi |
| 93 | + else |
| 94 | + local line |
| 95 | + for line in "${lines[@]}"; do |
| 96 | + if [ "$line" = "$1" ]; then |
| 97 | + flunk "expected to not find line \`$line'" |
| 98 | + fi |
| 99 | + done |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +assert() { |
| 104 | + if ! "$@"; then |
| 105 | + flunk "failed: $*" |
| 106 | + fi |
| 107 | +} |
0 commit comments