From: Avery Pennarun Date: Tue, 16 Feb 2010 21:40:14 +0000 (-0500) Subject: sh: Import wvtest.sh from the bup project. X-Git-Url: http://rtime.felk.cvut.cz/gitweb/wvtest.git/commitdiff_plain/dd8c28efa19e308133099d02f67d209c12bccf63 sh: Import wvtest.sh from the bup project. --- diff --git a/Makefile b/Makefile index 555ce17..583899c 100644 --- a/Makefile +++ b/Makefile @@ -5,15 +5,18 @@ all: @echo "Try: make test" runtests: - make -C python runtests - make -C dotnet runtests - make -C cpp runtests + $(MAKE) -C sh runtests + $(MAKE) -C python runtests + $(MAKE) -C dotnet runtests + $(MAKE) -C cpp runtests + test: ./wvtestrun $(MAKE) runtests clean:: rm -f *~ .*~ - make -C python clean - make -C dotnet clean - make -C cpp clean + $(MAKE) -C sh clean + $(MAKE) -C python clean + $(MAKE) -C dotnet clean + $(MAKE) -C cpp clean diff --git a/README b/README index ba1837d..73bdb82 100644 --- a/README +++ b/README @@ -259,9 +259,10 @@ Sample code is provided for these languages: C++: try typing "cd cpp; make test" C# (mono): try typing "cd dotnet; make test" Python: try typing "cd python; make test" + Shell: try typing "cd sh; make test" There's no point explaining the syntax here, because it's really -simple. Just look inside the cpp, dotnet, and python +simple. Just look inside the cpp, dotnet, python, and sh directories to learn how the tests are written. diff --git a/sh/Makefile b/sh/Makefile new file mode 100644 index 0000000..1f31192 --- /dev/null +++ b/sh/Makefile @@ -0,0 +1,13 @@ + +all: + @echo "Try: make test" + @false + +runtests: + t/twvtest.sh + +test: + ../wvtestrun $(MAKE) runtests + +clean:: + rm -f *~ t/*~ diff --git a/sh/t/twvtest.sh b/sh/t/twvtest.sh new file mode 100755 index 0000000..fbc709f --- /dev/null +++ b/sh/t/twvtest.sh @@ -0,0 +1,14 @@ +#!/bin/bash +. ./wvtest.sh + +WVSTART "main test" +WVPASS true +WVPASS true +WVPASS true +WVFAIL false +WVPASSEQ "$(ls | sort)" "$(ls)" +WVPASSNE "5" "5 " +WVPASSEQ "" "" + +WVSTART another test +WVPASS true diff --git a/sh/wvtest.sh b/sh/wvtest.sh new file mode 100644 index 0000000..36a1862 --- /dev/null +++ b/sh/wvtest.sh @@ -0,0 +1,107 @@ +# +# Include this file in your shell script by using: +# #!/bin/sh +# . ./wvtest.sh +# + +# we don't quote $TEXT in case it contains newlines; newlines +# aren't allowed in test output. However, we set -f so that +# at least shell glob characters aren't processed. +_wvtextclean() +{ + ( set -f; echo $* ) +} + +_wvfind_caller() +{ + LVL=$1 + WVCALLER_FILE=${BASH_SOURCE[2]} + WVCALLER_LINE=${BASH_LINENO[1]} +} + +_wvcheck() +{ + CODE="$1" + TEXT=$(_wvtextclean "$2") + OK=ok + if [ "$CODE" -ne 0 ]; then + OK=FAILED + fi + echo "! $WVCALLER_FILE:$WVCALLER_LINE $TEXT $OK" >&2 + if [ "$CODE" -ne 0 ]; then + exit $CODE + else + return 0 + fi +} + + +WVPASS() +{ + TEXT="$*" + + _wvfind_caller + if "$@"; then + _wvcheck 0 "$TEXT" + return 0 + else + _wvcheck 1 "$TEXT" + # NOTREACHED + return 1 + fi +} + + +WVFAIL() +{ + TEXT="$*" + + _wvfind_caller + if "$@"; then + _wvcheck 1 "NOT($TEXT)" + # NOTREACHED + return 1 + else + _wvcheck 0 "NOT($TEXT)" + return 0 + fi +} + + +_wvgetrv() +{ + ( "$@" >&2 ) + echo -n $? +} + + +WVPASSEQ() +{ + _wvfind_caller + _wvcheck $(_wvgetrv [ "$#" -eq 2 ]) "exactly 2 arguments" + echo "Comparing:" >&2 + echo "$1" >&2 + echo "--" >&2 + echo "$2" >&2 + _wvcheck $(_wvgetrv [ "$1" = "$2" ]) "'$1' = '$2'" +} + + +WVPASSNE() +{ + _wvfind_caller + _wvcheck $(_wvgetrv [ "$#" -eq 2 ]) "exactly 2 arguments" + echo "Comparing:" >&2 + echo "$1" >&2 + echo "--" >&2 + echo "$2" >&2 + _wvcheck $(_wvgetrv [ "$1" != "$2" ]) "'$1' != '$2'" +} + + +WVSTART() +{ + echo >&2 + _wvfind_caller + echo "Testing \"$*\" in $WVCALLER_FILE:" >&2 +}