49 lines
822 B
Bash
49 lines
822 B
Bash
#!/usr/bin/env bash
|
|
|
|
TEST_ASSERTIONS=0
|
|
TEST_FAILURES=0
|
|
|
|
test_fail() {
|
|
printf 'FAIL: %s\n' "$1" >&2
|
|
(( TEST_FAILURES += 1 ))
|
|
}
|
|
|
|
assert_equal() {
|
|
local expected="$1"
|
|
local actual="$2"
|
|
local label="$3"
|
|
|
|
(( TEST_ASSERTIONS += 1 ))
|
|
if [[ "$actual" != "$expected" ]]; then
|
|
test_fail "${label}: expected '${expected}', received '${actual}'"
|
|
fi
|
|
}
|
|
|
|
assert_success() {
|
|
local label="$1"
|
|
shift
|
|
|
|
(( TEST_ASSERTIONS += 1 ))
|
|
"$@" || test_fail "${label}: expected success"
|
|
}
|
|
|
|
assert_failure() {
|
|
local label="$1"
|
|
shift
|
|
|
|
(( TEST_ASSERTIONS += 1 ))
|
|
if "$@"; then
|
|
test_fail "${label}: expected failure"
|
|
fi
|
|
}
|
|
|
|
finish_tests() {
|
|
if (( TEST_FAILURES > 0 )); then
|
|
printf '%d assertion(s), %d failure(s)\n' \
|
|
"$TEST_ASSERTIONS" "$TEST_FAILURES" >&2
|
|
return 1
|
|
fi
|
|
|
|
printf '%d assertion(s) passed\n' "$TEST_ASSERTIONS"
|
|
}
|