navi/test/core.sh

69 lines
1.2 KiB
Bash
Raw Normal View History

2019-09-20 21:44:51 +00:00
#!/usr/bin/env bash
export NAVI_FORCE_GNU=true
source "${SCRIPT_DIR}/src/main.sh"
source "${SCRIPT_DIR}/test/log.sh"
2019-09-20 21:44:51 +00:00
opts::eval "$@"
TEST_DIR="${SCRIPT_DIR}/test"
2019-09-23 11:56:15 +00:00
PASSED=0
FAILED=0
SKIPPED=0
2019-09-26 15:15:11 +00:00
SUITE=""
2019-09-23 11:56:15 +00:00
test::set_suite() {
SUITE="$*"
}
2019-09-20 21:44:51 +00:00
test::success() {
2019-09-23 11:56:15 +00:00
PASSED=$((PASSED+1))
log::success "Test passed!"
2019-09-20 21:44:51 +00:00
}
test::fail() {
2019-09-23 11:56:15 +00:00
FAILED=$((FAILED+1))
log::error "Test failed..."
2019-09-23 11:56:15 +00:00
return
2019-09-21 22:21:46 +00:00
}
test::skip() {
echo
log::note "${SUITE:-unknown} - ${1:-unknown}"
SKIPPED=$((SKIPPED+1))
log::warning "Test skipped..."
return
2019-09-26 15:15:11 +00:00
}
2019-09-21 22:21:46 +00:00
test::run() {
2019-09-21 22:24:09 +00:00
echo
2019-09-26 15:15:11 +00:00
log::note "${SUITE:-unknown} - ${1:-unknown}"
2019-09-21 22:24:09 +00:00
shift
eval "$*" && test::success || test::fail
2019-09-23 11:56:15 +00:00
}
test::equals() {
2019-09-25 14:44:57 +00:00
local -r actual="$(cat)"
local -r expected="$(echo "${1:-}")"
if [[ "$actual" != "$expected" ]]; then
2019-09-25 14:44:57 +00:00
log::error "Expected '${expected}' but got '${actual}'"
return 2
fi
}
2019-09-23 11:56:15 +00:00
test::finish() {
echo
if [ $SKIPPED -gt 0 ]; then
log::warning "${SKIPPED} tests skipped!"
fi
2019-09-23 11:56:15 +00:00
if [ $FAILED -gt 0 ]; then
log::error "${PASSED} tests passed but ${FAILED} failed... :("
2019-09-23 11:56:15 +00:00
exit "${FAILED}"
else
log::success "All ${PASSED} tests passed! :)"
2019-09-23 11:56:15 +00:00
exit 0
fi
}