From 724890bb724c425e198d44b1ef4c09033dc93780 Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Tue, 23 Sep 2014 23:50:36 +0200 Subject: [PATCH] Added low-tech test mechanism --- .gitignore | 3 +- .travis.yml | 4 +- Makefile | 4 ++ test/001_trivial.txt | 8 ++++ test/002_trivial_10x5.txt | 11 +++++ test/003_no_config_file.txt | 6 +++ test/testrunner.sh | 90 +++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 test/001_trivial.txt create mode 100644 test/002_trivial_10x5.txt create mode 100644 test/003_no_config_file.txt create mode 100755 test/testrunner.sh diff --git a/.gitignore b/.gitignore index 08907b3..1fb79f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.o *.a archive/ -test/ +test.orig/ website/ zs-* idee @@ -12,6 +12,7 @@ doc/*.tar.gz press/ *.tar.gz src/boxes +src/boxes.exe src/boxes.h src/lex.yy.c src/parser.h diff --git a/.travis.yml b/.travis.yml index f0170b4..e263dcf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,7 @@ language: c -compiler: gcc - -script: make +script: make && make test before_install: - sudo apt-get update -qq diff --git a/Makefile b/Makefile index 4bfd625..4640803 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,7 @@ RCS_FILES = $(CL_FILES) Makefile doc/boxes.el doc/create_changelog.pl ALL_FILES = COPYING README README.Win32.txt boxes-config Makefile DOC_FILES = doc/boxes.1 doc/boxes.1.in doc/boxes.el +.PHONY: clean build debug locsnap rcstest rcslocks love test build debug boxes: src/boxes.h doc/boxes.1 @echo For compilation info see the boxes website at http://boxes.thomasjensen.com/ @@ -140,6 +141,9 @@ logpage: $(CL_FILES) cd src; ../doc/create_changelog.pl $(patsubst %,../%,$(CL_FILES)) $(shell $(MAKE) -C src -s logpage) > $(CLOGFILE) +test: + cd test; ./testrunner.sh -suite + love: @echo "Not in front of the kids, honey!" diff --git a/test/001_trivial.txt b/test/001_trivial.txt new file mode 100644 index 0000000..8bd5b39 --- /dev/null +++ b/test/001_trivial.txt @@ -0,0 +1,8 @@ +:ARGS +:INPUT +foo +:EXPECTED +/*******/ +/* foo */ +/*******/ +:EOF \ No newline at end of file diff --git a/test/002_trivial_10x5.txt b/test/002_trivial_10x5.txt new file mode 100644 index 0000000..0365c75 --- /dev/null +++ b/test/002_trivial_10x5.txt @@ -0,0 +1,11 @@ +:ARGS +-s 10x5 +:INPUT +foo +:EXPECTED +/********/ +/* foo */ +/* */ +/* */ +/********/ +:EOF \ No newline at end of file diff --git a/test/003_no_config_file.txt b/test/003_no_config_file.txt new file mode 100644 index 0000000..8f44094 --- /dev/null +++ b/test/003_no_config_file.txt @@ -0,0 +1,6 @@ +:ARGS +-f nonexistent +:INPUT +:EXPECTED-ERROR 1 +boxes: Couldn't open config file 'nonexistent' for input. +:EOF diff --git a/test/testrunner.sh b/test/testrunner.sh new file mode 100755 index 0000000..72f9eff --- /dev/null +++ b/test/testrunner.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# +# Low-tech test runner for boxes. +# +# File: testrunner.sh +# Date created: September 23, 2014 (Tuesday, 20:06h) +# Author: Thomas Jensen +# _____________________________________________________________________ + +if [ $# -ne 1 ]; then + echo 'Usage: testrunner.sh {-suite | }' + 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 +for sectionName in :ARGS :INPUT :EXPECTED :EOF; do + 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} +declare -r testOutputFile=${testCaseFile/%.txt/.out.tmp} +declare -r boxesArgs=$(cat $testCaseFile | sed -n '/^:ARGS/,+1p' | grep -v ^:INPUT | sed '1d') + +cat $testCaseFile | sed -n '/^:INPUT/,/^:EXPECTED\b.*$/p;' | sed '1d;$d' | tr -d '\r' > $testInputFile +cat $testCaseFile | sed -n '/^:EXPECTED/,/^:EOF\b.*$/p;' | sed '1d;$d' | tr -d '\r' > $testExpectationFile + +echo " Invoking: boxes $boxesArgs" +export BOXES=../boxes-config + +cat $testInputFile | ../src/boxes $boxesArgs >$testOutputFile 2>&1 +declare -ir actualReturnCode=$? +cat $testOutputFile | tr -d '\r' | diff - $testExpectationFile +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 +rm $testExpectationFile +rm $testOutputFile + +echo " OK" +exit 0 + +#EOF