commit 955613e496747b76e2fa582d03cc7d724dd851b3
parent 1f703c012dbe70ed5dd3741733ee632021c93a25
Author: Oto Havle <havleoto@gmail.com>
Date:   Sun, 27 Nov 2011 19:51:44 +0100
Improved test of interpreter argument handling.
Diffstat:
1 file changed, 82 insertions(+), 25 deletions(-)
diff --git a/src/tests/test-interpreter.sh b/src/tests/test-interpreter.sh
@@ -10,6 +10,7 @@ fi
 
 KLISP="$1"
 GEN_K="test-interpreter-gen.k"
+TMPERR="test-interpreter-err.log"
 
 # -- functions ----------------------------------------
 
@@ -19,26 +20,46 @@ init()
     npass=0
 }
 
-check_helper()
+check_match()
 {
-    expected_output="$1"
-    expected_exitstatus="$2"
-    output="$3"
-    exitstatus="$4"
-    command="$5"
+    expected="$1"
+    actual="$2"
+
+    if expr match "$expected" '^/.*/$' >/dev/null ; then
+        regexp=`expr substr "$expected" 2 $((${#expected} - 2))`
+        expr match "$actual" "$regexp" >/dev/null
+    else
+        test "$actual" = "$expected"
+    fi
+}
 
-    if [ "$output" != "$expected_output" ] ; then
+check_helper()
+{
+    expected_stdout="$1"
+    expected_stderr="$2"
+    expected_exitstatus="$3"
+    stdout="$4"
+    stderr="$5"
+    exitstatus="$6"
+    command="$7"
+
+    if ! check_match "$expected_stdout" "$stdout" ; then
+        echo "FAIL: $command"
+        echo " stdout => '$stdout'"
+        echo " expected: '$expected_stdout'" 1>&2
+        nfail=$((1 + $nfail))
+    elif ! check_match "$expected_stderr" "$stderr" ;  then
         echo "FAIL: $command"
-        echo "  =======> $output"
-        echo " expected: $expected_output" 1>&2
-        nfail=$((1 + nfail))
+        echo " stderr => '$stderr'"
+        echo " expected: '$expected_stderr'" 1>&2
+        nfail=$((1 + $nfail))
     elif [ $exitstatus -ne $expected_exitstatus ] ; then
         echo "FAIL: $command"
         echo "  ==> exit status $exitstatus ; expected: $expected_exitstatus" 1>&2
-        nfail=$((1 + nfail))
+        nfail=$((1 + $nfail))
     else
-        ## echo "OK: $command ==> $output"
-        npass=$((1 + npass))
+        ## echo "OK: $command ==> $stdout"
+        npass=$((1 + $npass))
     fi
 }
 
@@ -46,9 +67,10 @@ check_o()
 {
     expected_output="$1"
     shift
-    o=`"$@"`
+    o=`"$@" 2> $TMPERR`
     s=$?
-    check_helper "$expected_output" 0 "$o" "$s" "$*"
+    e=`cat $TMPERR`
+    check_helper "$expected_output" '' 0 "$o" "$e" "$s" "$*"
 }
 
 check_os()
@@ -57,29 +79,47 @@ check_os()
     expected_exitstatus="$2"
     shift
     shift
-    o=`"$@"`
+    o=`"$@" 2> $TMPERR`
     s=$?
-    check_helper "$expected_output" "$expected_exitstatus" "$o" "$s" "$*"
+    e=`cat $TMPERR`
+    check_helper "$expected_output" '' "$expected_exitstatus" "$o" "$e" "$s" "$*"
 }
 
-check_io()
+check_oi()
 {
     expected_output="$1"
     input="$2"
     shift
     shift
-    o=`echo "$input" | "$@"`
+    o=`echo "$input" | "$@" 2> $TMPERR`
+    s=$?
+    e=`cat $TMPERR`
+    check_helper "$expected_output" '' 0 "$o" "$e" "$s" "echo '$input' | $*"
+}
+
+check_oe()
+{
+    expected_stdout="$1"
+    expected_stderr="$2"
+    shift
+    shift
+    o=`"$@" 2> $TMPERR`
     s=$?
-    check_helper "$expected_output" 0 "$o" "$s" "$*"
+    e=`cat $TMPERR`
+    check_helper "$expected_stdout" "$expected_stderr" 0 "$o" "$e" "$s" "$*"
 }
 
 report()
 {
     echo "Tests Passed: $npass"
     echo "Tests Failed: $nfail"
-    echo "Tests Total: $((npass + nfail))"
+    echo "Tests Total: $(($npass + $nfail))"
 }
 
+cleanup()
+{
+    rm -f "$GEN_K" "$TMPERR"
+}
 # -- tests --------------------------------------------
 
 init
@@ -88,16 +128,27 @@ init
 
 echo '(display 123456)' > "$GEN_K"
 check_o '123456' $KLISP "$GEN_K"
-rm "$GEN_K"
+
+# empty command line and stdin not a terminal
+
+check_oi '' '' $KLISP
 
 # '-' on the command line
 
-check_io '2' '(display (+ 1 1))' $KLISP -
+check_oi '2' '(display (+ 1 1))' $KLISP -
 
 # option: -e
 
 check_o 'abcdef' $KLISP '-e (display "abc")' '-e' '(display "def")'
 
+# option: -i
+
+check_oi 'klisp> ' '' $KLISP -i
+
+# option: -v
+
+check_o '/^klisp [0-9.]+ .*/' $KLISP -v
+
 # '--' on the command line
 
 check_o '1' $KLISP '-e (display 1)' --
@@ -130,7 +181,7 @@ check_o '#f' $KLISP '-e (write (get-environment-variable "KLISPTEST2"))'
 # script arguments
 
 check_o '()' $KLISP -e '(write(get-script-arguments))'
-check_io '("-" "-i")' '' $KLISP -e '(write(get-script-arguments))' - -i
+check_oi '("-" "-i")' '' $KLISP -e '(write(get-script-arguments))' - -i
 check_o '("/dev/null" "y")' $KLISP -e '(write(get-script-arguments))' /dev/null y
 check_o '()' $KLISP -e '(write(get-script-arguments))' --
 check_o '("/dev/null")' $KLISP -e '(write(get-script-arguments))' -- /dev/null
@@ -147,7 +198,7 @@ check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\")" \
     $KLISP -e '(write(get-interpreter-arguments))'
 check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"--\")" \
     $KLISP -e '(write(get-interpreter-arguments))' --
-check_io "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"-\")" '' \
+check_oi "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"-\")" '' \
     $KLISP -e '(write(get-interpreter-arguments))' -
 check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"/dev/null\")" \
     $KLISP -e '(write(get-interpreter-arguments))' /dev/null
@@ -156,6 +207,12 @@ check_o "(\"$KLISP\" \"-e(write(get-interpreter-arguments))\" \"--\" \"/dev/null
 check_o "(\"$KLISP\" \"-e(write(get-interpreter-arguments))\" \"--\" \"/dev/null\" \"a\" \"b\" \"c\")" \
     $KLISP '-e(write(get-interpreter-arguments))' -- /dev/null a b c
 
+# stdout and stderr
+
+check_oe 'abc' '' $KLISP -e '(display "abc" (get-current-output-port))'
+check_oe '' 'abc' $KLISP -e '(display "abc" (get-current-error-port))'
+
 # done
 
 report
+cleanup