bashutils/test.sh

260 lines
7 KiB
Bash
Executable file

#!/usr/bin/env bash
export "LC_ALL=C.UTF-8"
set -uo pipefail
IFS=$'\n'
unset TESTS TEST STAGE
TESTS="
# Logging
logger_diag
logger_verb
logger_sarn
logger_info
logger_warn
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 bashutils() {
set +uo pipefail
source "bashutils.sh"
set -uo pipefail
}
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
}
## Assertions
function assert_equals() {
EXPECTED="${1}"
ACTUAL="${2}"
[[ "${ACTUAL}" != "${EXPECTED}" ]] && fail "String does not match expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
}
function assert_contains() {
EXPECTED="${1}"
ACTUAL="${2}"
[[ "${ACTUAL}" != *"${EXPECTED}"* ]] && fail "String does not contain expected string\nExpected: '${EXPECTED}'\nActual: '${ACTUAL}'"
}
# Test loop
for TEST in ${TESTS}; do
[ -z "${TEST}" ] || [[ "${TEST}" == "#"* ]] && continue
echo ":: Running test ${TEST}"
export "TEST"
export "STAGE=0"
# 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)"
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
# 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
# Invalid command
else
invalid
fi
done