bashutils/test.sh

261 lines
7 KiB
Bash
Raw Normal View History

2024-10-27 13:26:44 +01:00
#!/usr/bin/env bash
export "LC_ALL=C.UTF-8"
2024-10-27 13:26:44 +01:00
set -uo pipefail
IFS=$'\n'
unset TESTS TEST STAGE
2024-10-27 13:30:17 +01:00
2024-10-27 13:26:44 +01:00
TESTS="
# Logging
logger_diag
logger_verb
logger_sarn
logger_info
logger_warn
logger_error
logger_crash
# Checks
## Command type
2024-10-27 13:26:44 +01:00
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
2024-10-27 13:26:44 +01:00
"
# Utility methods
function bashutils() {
set +uo pipefail
source "bashutils.sh"
set -uo pipefail
}
2024-10-27 13:26:44 +01:00
function invalid() { echo ":: Error: Invalid test ${TEST}, skipping"; }
function fail() {
echo -e ":: Test ${TEST} failed during stage ${STAGE}\n ${*//\\n/\\n }"
[ "${TEST_FAILFAST:-true}" == "true" ] && exit 1
2024-10-27 13:26:44 +01:00
}
## Assertions
function assert_equals() {
EXPECTED="${1}"
ACTUAL="${2}"
2024-10-27 13:26:44 +01:00
[[ "${ACTUAL}" != "${EXPECTED}" ]] && fail "String does not match expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
2024-10-27 13:26:44 +01:00
}
function assert_contains() {
EXPECTED="${1}"
ACTUAL="${2}"
2024-10-27 13:26:44 +01:00
[[ "${ACTUAL}" != *"${EXPECTED}"* ]] && fail "String does not contain expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
}
2024-10-27 13:26:44 +01:00
# Test loop
for TEST in ${TESTS}; do
[ -z "${TEST}" ] || [[ "${TEST}" == "#"* ]] && continue
2024-10-27 13:26:44 +01:00
echo ":: Running test ${TEST}"
export "TEST"
export "STAGE=0"
2024-10-27 13:26:44 +01:00
# Logging
if [[ "${TEST}" == "logger_"* ]]; then
if [ "${TEST}" == "logger_crash" ]; then
assert_contains "It crashed!" "$( (
bashutils
crash "true" "It crashed!"
) &>/dev/stdout)"
else
assert_contains "This is a test message" "$( (
bashutils
# shellcheck disable=SC2034
BASHUTILS_LOGLEVEL=0
${TEST//logger_/} "This is a test message"
) &>/dev/stdout)"
2024-10-27 13:26:44 +01:00
fi
# Checks
elif [[ "${TEST}" == "check_"* ]]; then
# Command checks
if [[ "${TEST}" == "check_command_"* ]]; then
case "${TEST//check_command_/}" in
"alias")
assert_equals "0" "$(
bashutils
alias "verynicealias=ls"
is_command_alias "verynicealias"
echo "!$(type -t "verynicealias")!"
#echo -n "${?}"
)"
;;
"keyword")
assert_equals "0" "$(
bashutils
is_command_keyword "continue"
echo "${?}"
)"
;;
"function")
assert_equals "0" "$(
bashutils
is_command_function "is_command_function"
echo "${?}"
)"
;;
"builtin")
assert_equals "0" "$(
bashutils
is_command_builtin "set"
echo "${?}"
)"
;;
"file")
assert_equals "0" "$(
bashutils
is_command_file "clear"
echo "${?}"
)"
;;
"defined")
assert_equals "0" "$(
bashutils
is_command_defined "echo"
echo "${?}"
)"
;;
*)
invalid
;;
esac
2024-10-27 13:26:44 +01:00
# Input checks
elif [[ "${TEST}" == "check_input_"* ]]; then
case "${TEST//check_input_/}" in
"bool")
assert_equals "0" "$(
bashutils
is_input_bool "true"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
bashutils
is_input_bool "false"
echo "${?}"
)"
export "STAGE=2"
assert_equals "1" "$(
bashutils
is_input_bool "0"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
bashutils
is_input_bool "1"
echo "${?}"
)"
;;
"byte")
assert_equals "0" "$(
bashutils
is_input_byte "0"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
bashutils
is_input_byte "255"
echo "${?}"
)"
export "STAGE=2"
assert_equals "0" "$(
bashutils
is_input_byte "69"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
bashutils
is_input_byte "256"
echo "${?}"
)"
export "STAGE=4"
assert_equals "1" "$(
bashutils
is_input_byte "-1"
echo "${?}"
)"
export "STAGE=5"
assert_equals "1" "$(
bashutils
is_input_int "asd"
echo "${?}"
)"
;;
"int")
assert_equals "0" "$(
bashutils
is_input_int "9223372036854775807"
echo "${?}"
)"
export "STAGE=1"
assert_equals "1" "$(
bashutils
is_input_int "asd"
echo "${?}"
)"
;;
"char")
assert_equals "0" "$(
bashutils
is_input_int "!"
echo "${?}"
)"
export "STAGE=1"
assert_equals "0" "$(
bashutils
is_input_int "™"
echo "${?}"
)"
export "STAGE=2"
assert_equals "0" "$(
bashutils
is_input_int "5"
echo "${?}"
)"
export "STAGE=3"
assert_equals "1" "$(
bashutils
is_input_int "asd"
echo "${?}"
)"
export "STAGE=4"
assert_equals "1" "$(
bashutils
is_input_int "666"
echo "${?}"
)"
;;
*)
invalid
;;
esac
# Invalid command
else
invalid
fi
2024-10-27 13:26:44 +01:00
# Invalid command
else
invalid
2024-10-27 13:26:44 +01:00
fi
done