Fix test framework and add input type check tests

This commit's tests do not run through properly, I will fix that in the next few commits.
This commit is contained in:
JeremyStar™ 2024-10-27 22:12:35 +01:00
parent 5be44a87bf
commit 6368928cc2
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

145
test.sh
View file

@ -1,8 +1,9 @@
#!/usr/bin/env bash
export "LC_ALL=C.UTF-8"
set -uo pipefail
IFS=$'\n'
unset TEST TESTS
unset TESTS TEST STAGE
TESTS="
# Logging
@ -15,34 +16,38 @@ logger_error
logger_crash
# Checks
## Command type
check_command_alias
check_command_keyword
check_command_function
check_command_builtin
check_command_file
check_command_defined
## Input type
check_input_bool
check_input_byte
check_input_int
check_input_char
"
# Utility methods
function invalid() { echo ":: Error: Invalid test ${TEST}, skipping"; }
function fail() {
echo -e ":: Test ${TEST} failed\n ${*//\\n/\\n }"
[ "${TEST_FAILFAST}" == "true" ] && exit 1
echo -e ":: Test ${TEST} failed during stage ${STAGE}\n ${*//\\n/\\n }"
[ "${TEST_FAILFAST:-true}" == "true" ] && exit 1
}
## Assertions
function assert_equals() {
EXPECTED="${1}"
shift
ACTUAL="${*}"
ACTUAL="${2}"
[[ "${ACTUAL}" != "${EXPECTED}" ]] && fail "${1}" "String does not match expected string\nExpected: ${EXPECTED}\nActual: ${ACTUAL}"
[[ "${ACTUAL}" != "${EXPECTED}" ]] && fail "String does not match expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
}
function assert_contains() {
EXPECTED="${1}"
shift
ACTUAL="${*}"
ACTUAL="${2}"
[[ "${ACTUAL}" != *"${EXPECTED}"* ]] && fail "${1}" "String does not contain expected string\nExpected: ${EXPECTED}\nActual: ${ACTUAL}"
[[ "${ACTUAL}" != *"${EXPECTED}"* ]] && fail "String does not contain expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
}
# Test loop
@ -50,7 +55,8 @@ for TEST in ${TESTS}; do
[ -z "${TEST}" ] || [[ "${TEST}" == "#"* ]] && continue
echo ":: Running test ${TEST}"
export TEST
export "TEST"
export "STAGE=0"
# Logging
if [[ "${TEST}" == "logger_"* ]]; then
@ -73,7 +79,7 @@ for TEST in ${TESTS}; do
elif [[ "${TEST}" == "check_"* ]]; then
# Command checks
if [[ "${TEST}" == "check_command_"* ]]; then
case "${TEST//check_command/}" in
case "${TEST//check_command_/}" in
"alias")
assert_equals "0" "$(
source "bashutils.sh"
@ -117,6 +123,123 @@ for TEST in ${TESTS}; do
echo "${?}"
)"
;;
*)
invalid
;;
esac
# Input checks
elif [[ "${TEST}" == "check_input_"* ]]; then
case "${TEST//check_input_/}" in
"bool")
assert_equals "0" "$(
source "bashutils.sh"
is_input_bool "true"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
source "bashutils.sh"
is_input_bool "false"
echo "${?}"
)"
export "STAGE=2"
assert_equals "1" "$(
source "bashutils.sh"
is_input_bool "0"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
source "bashutils.sh"
is_input_bool "1"
echo "${?}"
)"
;;
"byte")
assert_equals "0" "$(
source "bashutils.sh"
is_input_byte "0"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
source "bashutils.sh"
is_input_byte "255"
echo "${?}"
)"
export "STAGE=2"
assert_equals "0" "$(
source "bashutils.sh"
is_input_byte "69"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
source "bashutils.sh"
is_input_byte "256"
echo "${?}"
)"
export "STAGE=4"
assert_equals "1" "$(
source "bashutils.sh"
is_input_byte "-1"
echo "${?}"
)"
export "STAGE=5"
assert_equals "1" "$(
source "bashutils.sh"
is_input_int "asd"
echo "${?}"
)"
;;
"int")
assert_equals "0" "$(
source "bashutils.sh"
is_input_int "9223372036854775807"
echo "${?}"
)"
export "STAGE=1"
assert_equals "1" "$(
source "bashutils.sh"
is_input_int "asd"
echo "${?}"
)"
;;
"char")
assert_equals "0" "$(
source "bashutils.sh"
is_input_int "!"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
source "bashutils.sh"
is_input_int "™"
echo "${?}"
)"
export "STAGE=2"
assert_equals "0" "$(
source "bashutils.sh"
is_input_int "5"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
source "bashutils.sh"
is_input_int "asd"
echo "${?}"
)"
export "STAGE=4"
assert_equals "1" "$(
source "bashutils.sh"
is_input_int "666"
echo "${?}"
)"
;;
*)
invalid
;;
esac
# Invalid command