2014-09-23 23:50:36 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2021-02-10 22:14:09 +01:00
|
|
|
# boxes - Command line filter to draw/remove ASCII boxes around text
|
2021-02-11 22:11:16 +01:00
|
|
|
# Copyright (c) 1999-2021 Thomas Jensen and the boxes contributors
|
2021-02-10 22:14:09 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
|
|
|
|
# License, version 2, as published by the Free Software Foundation.
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
# warranty of MERCHANTABILITY FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
|
|
|
|
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
# _____________________________________________________________________________________________________________________
|
2014-09-23 23:50:36 +02:00
|
|
|
#
|
2021-02-10 22:14:09 +01:00
|
|
|
# Low-tech test runner for boxes.
|
|
|
|
# _____________________________________________________________________________________________________________________
|
2014-09-23 23:50:36 +02:00
|
|
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
echo 'Usage: testrunner.sh {-suite | <testCaseFile>}'
|
|
|
|
echo ' Returns 0 for success, else non-zero'
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
if [ ${PWD##*/} != "test" ]; then
|
|
|
|
>&2 echo "Please run this script from the test folder."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Execute the entire test suite
|
|
|
|
if [ "$1" == "-suite" ]; then
|
|
|
|
declare -i overallResult=0
|
|
|
|
declare -i countExecuted=0
|
|
|
|
declare -i countFailed=0
|
|
|
|
declare tc
|
|
|
|
for tc in *.txt; do
|
|
|
|
$0 $tc
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
overallResult=1
|
|
|
|
((countFailed++))
|
|
|
|
fi
|
|
|
|
((countExecuted++))
|
|
|
|
done
|
|
|
|
echo "$countExecuted tests executed, $(($countExecuted-$countFailed)) successful, $countFailed failed."
|
|
|
|
exit $overallResult
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Execute only a single test
|
|
|
|
declare -r testCaseFile="$1"
|
|
|
|
if [ ! -f $testCaseFile ]; then
|
|
|
|
>&2 echo "Test Case '$testCaseFile' not found."
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Running test case: $testCaseFile"
|
|
|
|
|
|
|
|
declare sectionName
|
2014-10-11 21:48:06 +02:00
|
|
|
for sectionName in :ARGS :INPUT :OUTPUT-FILTER :EXPECTED :EOF; do
|
2014-09-23 23:50:36 +02:00
|
|
|
if [ $(grep -c ^$sectionName $testCaseFile) -ne 1 ]; then
|
|
|
|
>&2 echo "Missing section $sectionName in test case '$testCaseFile'."
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
declare -i expectedReturnCode=0
|
|
|
|
if [ $(grep -c "^:EXPECTED-ERROR " $testCaseFile) -eq 1 ]; then
|
|
|
|
expectedReturnCode=$(grep "^:EXPECTED-ERROR " $testCaseFile | sed -e 's/:EXPECTED-ERROR //')
|
|
|
|
fi
|
|
|
|
|
|
|
|
declare -r testInputFile=${testCaseFile/%.txt/.input.tmp}
|
|
|
|
declare -r testExpectationFile=${testCaseFile/%.txt/.expected.tmp}
|
2014-10-11 21:48:06 +02:00
|
|
|
declare -r testFilterFile=${testCaseFile/%.txt/.sed.tmp}
|
2014-09-23 23:50:36 +02:00
|
|
|
declare -r testOutputFile=${testCaseFile/%.txt/.out.tmp}
|
2021-02-13 11:27:24 +01:00
|
|
|
declare -r boxesArgs=$(cat $testCaseFile | sed -n '/^:ARGS/,+1p' | grep -v ^:INPUT | sed '1d' | tr -d '\r')
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2014-10-11 21:48:06 +02:00
|
|
|
cat $testCaseFile | sed -n '/^:INPUT/,/^:OUTPUT-FILTER/p;' | sed '1d;$d' | tr -d '\r' > $testInputFile
|
|
|
|
cat $testCaseFile | sed -n '/^:OUTPUT-FILTER/,/^:EXPECTED\b.*$/p;' | sed '1d;$d' | tr -d '\r' > $testFilterFile
|
|
|
|
cat $testCaseFile | sed -n '/^:EXPECTED/,/^:EOF/p;' | sed '1d;$d' | tr -d '\r' > $testExpectationFile
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2014-10-09 21:28:14 +02:00
|
|
|
declare boxesBinary=../src/boxes.exe
|
|
|
|
if [ ! -x $boxesBinary ]; then
|
|
|
|
boxesBinary=../src/boxes
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo " Invoking: $(basename $boxesBinary) $boxesArgs"
|
2014-09-23 23:50:36 +02:00
|
|
|
export BOXES=../boxes-config
|
|
|
|
|
2014-10-09 21:28:14 +02:00
|
|
|
cat $testInputFile | $boxesBinary $boxesArgs >$testOutputFile 2>&1
|
2014-09-23 23:50:36 +02:00
|
|
|
declare -ir actualReturnCode=$?
|
2021-02-11 10:47:16 +01:00
|
|
|
cat $testOutputFile | tr -d '\r' | sed -E -f $testFilterFile | diff - $testExpectationFile
|
2014-09-23 23:50:36 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
>&2 echo "Error in test case: $testCaseFile (top: actual; bottom: expected)"
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
if [ $actualReturnCode -ne $expectedReturnCode ]; then
|
|
|
|
>&2 echo "Error in test case: $testCaseFile (error code was $actualReturnCode, but expected $expectedReturnCode)"
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm $testInputFile
|
2014-10-11 21:48:06 +02:00
|
|
|
rm $testFilterFile
|
2014-09-23 23:50:36 +02:00
|
|
|
rm $testExpectationFile
|
|
|
|
rm $testOutputFile
|
|
|
|
|
|
|
|
echo " OK"
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
#EOF
|