Attempting to address shortcomings and whitespace issues

This commit is contained in:
Tyler Akins
2023-04-07 19:35:25 -05:00
parent 8e9fd680d4
commit febd3467c8
123 changed files with 2008 additions and 1137 deletions

139
run-tests
View File

@@ -1,45 +1,116 @@
#!/usr/bin/env bash
testCase() {
echo "Input: $1"
echo "Expected: $2"
}
cd "${0%/*}" || exit 1
indirect() {
unset -v "$1"
printf -v "$1" '%s' "$2"
}
# shellcheck disable=SC1091
. ./mo
PASS=0
FAIL=0
getValue() {
local name temp len hardSpace
for TEST in tests/*.expected; do
export BASE="${TEST%.expected}"
export MO_FALSE_IS_EMPTY=
name=$2
hardSpace=" "
echo -n "$BASE ... "
if declare -f "$name" &> /dev/null; then
temp=$("$name"; echo -n "$hardSpace")
len=$((${#temp} - 1))
(
if [[ -f "${BASE}.sh" ]]; then
# Run a shell script if one exists
"${BASE}.sh"
else
# Fall back to using .env and .template
# shellcheck disable=SC1090
. "${BASE}.env"
echo "Do not read this input" | mo "${BASE}.template"
if [[ "${temp:$len}" == "$hardSpace" ]]; then
temp=${temp:0:$len}
fi
) | diff -U5 - "${TEST}" > "${BASE}.diff"
statusCode=$?
if [[ $statusCode -ne 0 ]]; then
echo "FAIL (status code $statusCode)"
FAIL=$(( FAIL + 1 ))
else
echo "ok"
PASS=$(( PASS + 1 ))
rm "${BASE}.diff"
temp=${!name}
fi
done
echo ""
echo "Pass: $PASS"
echo "Fail: $FAIL"
if [[ $FAIL -gt 0 ]]; then
exit 1
local "$1" && indirect "$1" "$temp"
}
runTest() (
local testTemplate testExpected testActual hardSpace len testReturnCode testFail
hardSpace=" "
. ../mo
getValue testTemplate template
getValue testExpected expected
testActual=$(echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1; echo -n "$hardSpace$?")
testReturnCode=${testActual##*$hardSpace}
testActual=${testActual%$hardSpace*}
testFail=false
if [[ "$testActual" != "$testExpected" ]]; then
echo "Failure"
echo "Expected:"
echo "$testExpected"
echo "Actual:"
echo "$testActual"
if [[ -n "${MO_DEBUG-}" ]]; then
declare -p testExpected
declare -p testActual
fi
testFail=true
fi
if [[ "$testReturnCode" != "$returnCode" ]]; then
echo "Expected return code $returnCode, but got $testReturnCode"
testFail=true
fi
if [[ "$testFail" == "true" ]]; then
return 1
fi
return 0
)
runTestFile() (
local file=$1
echo "Test: $file"
"$file"
)
runTests() (
PASS=0
FAIL=0
if [[ $# -gt 0 ]]; then
for TEST in "$@"; do
runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1))
done
else
cd "${0%/*}"
for TEST in tests/*; do
if [[ -f "$TEST" ]]; then
runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1))
fi
done
fi
echo ""
echo "Pass: $PASS"
echo "Fail: $FAIL"
if [[ $FAIL -gt 0 ]]; then
exit 1
fi
)
# Clear test related variables
template="Template not defined"
expected="Expected not defined"
returnCode=0
arguments=()
# If sourced, load functions.
# If executed, perform the actions as expected.
if [[ "$0" == "${BASH_SOURCE[0]}" ]] || [[ -z "${BASH_SOURCE[0]}" ]]; then
runTests ${@+"${@}"}
fi