]> rtime.felk.cvut.cz Git - wvtest.git/commitdiff
sh: Import wvtest.sh from the bup project.
authorAvery Pennarun <apenwarr@gmail.com>
Tue, 16 Feb 2010 21:40:14 +0000 (16:40 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Tue, 16 Feb 2010 22:05:42 +0000 (17:05 -0500)
Makefile
README
sh/Makefile [new file with mode: 0644]
sh/t/twvtest.sh [new file with mode: 0755]
sh/wvtest.sh [new file with mode: 0644]

index 555ce175d09008ad8228abb7ef16bbd4cf26d6f5..583899ccb4b410dd82fa675f4208c73525bb6c6b 100644 (file)
--- 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 ba1837dd70060a8eaca91414a617f75b9ec0ac85..73bdb82da105295b8cbeebf98af8e4177b89d51f 100644 (file)
--- 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 (file)
index 0000000..1f31192
--- /dev/null
@@ -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 (executable)
index 0000000..fbc709f
--- /dev/null
@@ -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 (file)
index 0000000..36a1862
--- /dev/null
@@ -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
+}