Work around some clang warnings.
[paraslash.git] / t / test-lib.sh
index e444e5780d2f152eb9755916375996c9340016cd..b7d675ea0def671529e13732c52143bb22290288 100644 (file)
@@ -61,13 +61,36 @@ say()
        say_color info "$*"
 }
 
+retval_ok()
+{
+       local rv="$1" expectation="$2"
+
+       if [[ "$expectation" == "success" ]]; then
+               (($rv == 0)) && return 0 || return 1
+       fi
+       if (($rv > 129 && $rv <= 192)); then
+               echo >&2 "died by signal"
+               return 1
+       fi
+       if (($rv == 127)); then
+                echo >&2 "command not found"
+               return 1
+       fi
+       if (($rv == 0)); then
+                echo >&2 "command was supposed to fail but succeeded"
+               return 1
+       fi
+       return 0
+}
+
 _test_run()
 {
-       local f
+       local f expectation="$3" ret
 
        let test_count++
        eval >&3 2>&4 "$2"
-       if (($? == 0)); then
+       ret=$?
+       if retval_ok "$ret" "$expectation"; then
                let test_success++
                say_color ok "ok $test_count - $1"
                return
@@ -140,7 +163,15 @@ test_expect_success()
 {
        (($# != 2)) && error "bug: not 2 parameters to test_expect_success()"
        say >&3 "expecting success: $2"
-       _test_run "$1" "$2"
+       _test_run "$1" "$2" "success"
+       echo >&3 ""
+}
+
+test_expect_failure()
+{
+       (($# != 2)) && error "bug: not 2 parameters to test_expect_failure()"
+       say >&3 "expecting failure: $2"
+       _test_run "$1" "$2" "failure"
        echo >&3 ""
 }