diff options
Diffstat (limited to 'sandbox/t/test-utils.sh')
-rw-r--r-- | sandbox/t/test-utils.sh | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/sandbox/t/test-utils.sh b/sandbox/t/test-utils.sh new file mode 100644 index 0000000..f2d9a92 --- /dev/null +++ b/sandbox/t/test-utils.sh @@ -0,0 +1,119 @@ +# t/test-utils.sh: Test framwork for poe/sandbox + +while test $# -ne 0 +do + case "$1" in + -v|--verbose) + POE_TEST_VERBOSE=y + shift + ;; + *) + echo "error: unknown option '$1'" >&2 + exit 1 + ;; + esac +done + +# Internal functions + +if test "x$TERM" != xdumb && test -t 1 +then + say_color_ok=$(tput setaf 2) # green + say_color_error=$(tput setaf 1) # red + say_color_info=$(tput setaf 3) # yellow + say_color_= # normal + say_color_reset=$(tput sgr0) + say() { + eval "color=\$say_color_$1" + shift + echo "$color$*$say_color_reset" + } +else + say() { + echo $2 + } +fi + +error() { + say error "error: $*" + POE_TEST_EXIT=y + exit 1 +} + +test_count=0 +test_success=0 +test_failure=0 + +test_run() { + { + eval " + test -n \"$POE_TEST_VERBOSE\" && set -x + $1" </dev/null >&3 2>&4 + } 2>/dev/null +} + + +# Public helper functions + +test_expect_success() { + test $# = 2 || + error "test_expect_success expects 2 arguments" + + test_count=$(($test_count+1)) + if test_run "$2" + then + test_success=$(($test_success+1)) + say "" "ok $test_count - $1" + else + test_failure=$(($test_failure+1)) + say error "not ok $test_count - $1" + printf '%s\n' "$2" | sed -e 's/^/# /' + fi +} + +test_done() { + POE_TEST_EXIT=y + + case $test_failure in + 0) + say ok "# passed all $test_count test(s)" + say "" "1..$test_count" + exit 0 + ;; + *) + say error "# failed $test_failure among $test_count test(s)" + say "" "1..$test_count" + exit 1 + ;; + esac +} + + +# Prepare for running tests + +die() { + x=$? + test -n "$POE_TEST_EXIT" && + exit $x + say error "fatal: unexpected exit with status $x" + exit 1 +} +trap 'die' EXIT + +if test -n "$POE_TEST_VERBOSE" +then + exec 3>&1 4>&2 +else + exec 3>/dev/null 4>/dev/null +fi + + +# Prepare $SANDBOX variable + +x=$(dirname $(dirname $(readlink -f "$0"))) +if test -x "$x/runner" +then + SANDBOX=$x/runner +else + error "sandbox runner binary not found in '$x'" +fi |