klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

test-interpreter.sh (8781B)


      1 #! /bin/sh
      2 #
      3 # Test of the stand-alone interpreter.
      4 #
      5 # Does not work in MSYS shell on Windows because
      6 # of different handling of command line arguments.
      7 # TODO: Write similar test script for Windows cmd.exe or PowerShell.
      8 #
      9 
     10 if [ $# -eq 1 ] ; then
     11     KLISP="$1"
     12     VERBOSE=0
     13 elif [ $# -eq 2 -a "$1" = '--verbose' ] ; then
     14     KLISP="$2"
     15     VERBOSE=1
     16 else
     17     echo "usage: test-interpreter.sh [--verbose] KLISP-EXECUTABLE" 1>&2
     18     exit 1
     19 fi
     20 
     21 GEN1_K="test-interpreter-gen1.k"
     22 GEN2_K="test-interpreter-gen2.k"
     23 GEN_DIR="./test-interpreter-dir"
     24 TMPERR="test-interpreter-err.log"
     25 
     26 # -- functions ----------------------------------------
     27 
     28 init()
     29 {
     30     nfail=0
     31     npass=0
     32 }
     33 
     34 check_match()
     35 {
     36     expected="$1"
     37     actual="$2"
     38 
     39     if regexp=`expr match "$expected" '/\(.*\)/$'` ; then
     40         expr match "$actual" "$regexp" >/dev/null
     41     else
     42         test "$actual" = "$expected"
     43     fi
     44 }
     45 
     46 check_helper()
     47 {
     48     expected_stdout="$1"
     49     expected_stderr="$2"
     50     expected_exitstatus="$3"
     51     stdout="$4"
     52     stderr="$5"
     53     exitstatus="$6"
     54     command="$7"
     55 
     56     if ! check_match "$expected_stdout" "$stdout" ; then
     57         echo "FAIL: $command"
     58         echo " stdout => '$stdout'"
     59         echo " expected: '$expected_stdout'" 1>&2
     60         ok=0
     61     elif ! check_match "$expected_stderr" "$stderr" ;  then
     62         echo "FAIL: $command"
     63         echo " stderr => '$stderr'"
     64         echo " expected: '$expected_stderr'" 1>&2
     65         ok=0
     66     elif [ $exitstatus -ne $expected_exitstatus ] ; then
     67         echo "FAIL: $command"
     68         echo "  ==> exit status $exitstatus ; expected: $expected_exitstatus" 1>&2
     69         ok=0
     70     else
     71         ok=1
     72     fi
     73 
     74     if [ $ok -eq 1 ] ; then
     75         npass=$((1 + $npass))
     76         if [ "$VERBOSE" -eq 1 ] ; then
     77             echo "OK: $command ==> $stdout"
     78         fi
     79     else
     80         nfail=$((1 + $nfail))
     81         if [ "$VERBOSE" -eq 1 ] ; then
     82             echo
     83             echo " KLISP_INIT='$KLISP_INIT'"
     84             echo " KLISP_PATH='$KLISP_PATH'"
     85             echo " stderr => '$stderr'"
     86             echo " stdout => '$stdout'"
     87             echo
     88         fi
     89     fi
     90 }
     91 
     92 check_o()
     93 {
     94     expected_output="$1"
     95     shift
     96     o=`"$@" 2> $TMPERR`
     97     s=$?
     98     e=`cat $TMPERR`
     99     check_helper "$expected_output" '' 0 "$o" "$e" "$s" "$*"
    100 }
    101 
    102 check_os()
    103 {
    104     expected_output="$1"
    105     expected_exitstatus="$2"
    106     shift
    107     shift
    108     o=`"$@" 2> $TMPERR`
    109     s=$?
    110     e=`cat $TMPERR`
    111     check_helper "$expected_output" '' "$expected_exitstatus" "$o" "$e" "$s" "$*"
    112 }
    113 
    114 check_oi()
    115 {
    116     expected_output="$1"
    117     input="$2"
    118     shift
    119     shift
    120     o=`echo "$input" | "$@" 2> $TMPERR`
    121     s=$?
    122     e=`cat $TMPERR`
    123     check_helper "$expected_output" '' 0 "$o" "$e" "$s" "echo '$input' | $*"
    124 }
    125 
    126 check_oe()
    127 {
    128     expected_stdout="$1"
    129     expected_stderr="$2"
    130     shift
    131     shift
    132     o=`"$@" 2> $TMPERR`
    133     s=$?
    134     e=`cat $TMPERR`
    135     check_helper "$expected_stdout" "$expected_stderr" 0 "$o" "$e" "$s" "$*"
    136 }
    137 
    138 check_oes()
    139 {
    140     expected_stdout="$1"
    141     expected_stderr="$2"
    142     expected_exitstatus="$3"
    143     shift
    144     shift
    145     shift
    146     o=`"$@" 2> $TMPERR`
    147     s=$?
    148     e=`cat $TMPERR`
    149     check_helper "$expected_stdout" "$expected_stderr" "$expected_exitstatus" "$o" "$e" "$s" "$*"
    150 }
    151 
    152 report()
    153 {
    154     echo "Tests Passed: $npass"
    155     echo "Tests Failed: $nfail"
    156     echo "Tests Total: $(($npass + $nfail))"
    157 }
    158 
    159 cleanup()
    160 {
    161     rm -fr "$GEN_DIR"
    162     rm -f "$GEN1_K" "$GEN2_K" "$TMPERR"
    163 }
    164 # -- tests --------------------------------------------
    165 
    166 init
    167 
    168 # script name on the command line
    169 
    170 echo '(display 123456)' > "$GEN1_K"
    171 check_o '123456' $KLISP "$GEN1_K"
    172 
    173 # empty command line and stdin not a terminal
    174 
    175 check_oi '' '' $KLISP
    176 
    177 # '-' on the command line
    178 
    179 check_oi '2' '(display (+ 1 1))' $KLISP -
    180 
    181 # option: -e
    182 
    183 check_o 'abcdef' $KLISP '-e (display "abc")' '-e' '(display "def")'
    184 check_oes '' '/.*No object found in string.*/' 1 $KLISP -e ''
    185 check_oes '' '/.*More than one object found in string.*/' 1 $KLISP -e '1 1'
    186 
    187 # option: -i
    188 # The interpreter always show name and version
    189 # WAS check_oi 'klisp> ' '' $KLISP -i
    190 
    191 check_oi '/klisp [0-9.][0-9.]* .*\n.*klisp> /' '' $KLISP -i
    192 
    193 # option: -l
    194 # N.B. -l turns off interactive mode
    195 
    196 echo '(display "TTT")' > "$GEN1_K"
    197 echo '(display "SSS")' > "$GEN2_K"
    198 check_o 'TTTSSS' $KLISP -r "$GEN1_K" "$GEN2_K"
    199 check_oi '/.*TTTklisp> /' '' $KLISP -i -l "$GEN1_K"
    200 check_o 'TTT' $KLISP "$GEN1_K" -l "$GEN2_K"
    201 check_o '0TTT1' $KLISP -e '(display 0)' -l "$GEN1_K" -e '(display 1)'
    202 check_o 'TTTSSSSSS' $KLISP -l "$GEN1_K" -l "$GEN2_K" -l "$GEN2_K"
    203 check_o 'TTTSSSTTTSSS' $KLISP -l "$GEN1_K" -l "$GEN2_K" -l "$GEN1_K" -l "$GEN2_K"
    204 
    205 # option: -r
    206 # N.B. -r does not turn off interactive mode (TODO: test it)
    207 
    208 echo '(display "TTT")' > "$GEN1_K"
    209 echo '(display "SSS")' > "$GEN2_K"
    210 check_o 'TTTSSS' $KLISP -r "$GEN1_K" "$GEN2_K"
    211 check_oi '/.*TTTklisp> /' '' $KLISP -i -r "$GEN1_K"
    212 check_o 'TTT' $KLISP "$GEN1_K" -r "$GEN2_K"
    213 check_o '0TTT1' $KLISP -e '(display 0)' -r "$GEN1_K" -e '(display 1)'
    214 check_oi 'TTTSSS' '' $KLISP -r "$GEN1_K" -r "$GEN2_K" -r "$GEN2_K"
    215 check_oi 'TTTSSS' '' $KLISP -r "$GEN1_K" -r "$GEN2_K" -r "$GEN1_K" -r "$GEN2_K"
    216 
    217 # option: -v
    218 
    219 check_o '/klisp [0-9.][0-9.]* .*/' $KLISP -v
    220 
    221 # '--' on the command line
    222 
    223 check_o '1' $KLISP '-e (display 1)' --
    224 
    225 # exit status
    226 
    227 check_os '' 0 $KLISP -e '(exit 0)'
    228 check_os '' 1 $KLISP -e '(exit 1)'
    229 check_os '' 2 $KLISP -e '(exit 2)'
    230 check_os '' 0 $KLISP -e '(exit #t)'
    231 check_os '' 1 $KLISP -e '(exit #f)'
    232 check_os '' 0 $KLISP -e '(exit #inert)'
    233 check_os '' 1 $KLISP -e '(exit ())'
    234 check_os '' 0 $KLISP -e '(exit)'
    235 check_os '' 0 $KLISP -e '1'
    236 check_os '' 3 $KLISP -e '(apply-continuation root-continuation 3)'
    237 
    238 ## FIX the root continuation should exit without running any more 
    239 ## arguments, but it doesn't...
    240 check_os '' 0 $KLISP -e '(exit 0)' -e '(exit 1)'
    241 check_os '' 1 $KLISP -e '(exit 1)' -e '(exit 0)'
    242 
    243 # KLISP_INIT environment variable
    244 
    245 export KLISP_INIT='(display "init...")'
    246 check_o 'init...main' $KLISP -e '(display "main")'
    247 
    248 # it is an error to put empty string in KLISP_INIT,
    249 # even with interactive mode
    250 
    251 export KLISP_INIT=''
    252 check_oes '' '/.*No object found in string.*/' 1 $KLISP
    253 check_oes '' '/.*No object found in string.*/' 1 $KLISP -i
    254 
    255 # cleanup after tests with KLISP_INIT
    256 
    257 unset KLISP_INIT
    258 
    259 # KLISP_PATH environment variable
    260 # The path separator is semicolon and the 
    261 # question mark is replaced by the name
    262 # being required
    263 mkdir "$GEN_DIR"
    264 mkdir "$GEN_DIR/subdir"
    265 echo '(display 1)' > "$GEN_DIR/a.k"
    266 echo '(display 2)' > "$GEN_DIR/b.k"
    267 echo '(display 3)' > "$GEN_DIR/subdir/a.k"
    268 echo '(display 4)' > "$GEN_DIR/subdir/b.k"
    269 
    270 export KLISP_PATH="$GEN_DIR/?"
    271 check_oi '12' '' $KLISP -r a.k -r b.k
    272 
    273 export KLISP_PATH="$GEN_DIR/?.k"
    274 check_oi '12' '' $KLISP -r a -r b
    275 
    276 export KLISP_PATH="$GEN_DIR/subdir/?.k"
    277 check_oi '34' '' $KLISP -r a -r b
    278 
    279 export KLISP_PATH="$GEN_DIR/?.k;$GEN_DIR/subdir/?.k"
    280 check_oi '12' '' $KLISP -r a -r b
    281 
    282 export KLISP_PATH="$GEN_DIR/subdir/?.k;$GEN_DIR/?.k"
    283 check_oi '34' '' $KLISP -r a -r b
    284 
    285 # cleanup after tests with KLISP_PATH
    286 
    287 unset KLISP_PATH
    288 
    289 # other environment variables
    290 
    291 export KLISPTEST1=pqr
    292 check_o '"pqr"' $KLISP '-e (write (get-environment-variable "KLISPTEST1"))'
    293 check_o '#t' $KLISP '-e (write (defined-environment-variable? "KLISPTEST1"))'
    294 check_o '#f' $KLISP '-e (write (defined-environment-variable? "KLISPTEST2"))'
    295 
    296 # script arguments
    297 
    298 check_o '()' $KLISP -e '(write(get-script-arguments))'
    299 check_oi '("-" "-i")' '' $KLISP -e '(write(get-script-arguments))' - -i
    300 check_o '("/dev/null" "y")' $KLISP -e '(write(get-script-arguments))' /dev/null y
    301 check_o '()' $KLISP -e '(write(get-script-arguments))' --
    302 check_o '("/dev/null")' $KLISP -e '(write(get-script-arguments))' -- /dev/null
    303 
    304 # interpreter arguments
    305 #  (get-interpreter-arguments) returns all command line
    306 #  arguments.
    307 
    308 
    309 check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\")" \
    310     $KLISP -e '(write(get-interpreter-arguments))'
    311 check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"--\")" \
    312     $KLISP -e '(write(get-interpreter-arguments))' --
    313 check_oi "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"-\")" '' \
    314     $KLISP -e '(write(get-interpreter-arguments))' -
    315 check_o "(\"$KLISP\" \"-e\" \"(write(get-interpreter-arguments))\" \"/dev/null\")" \
    316     $KLISP -e '(write(get-interpreter-arguments))' /dev/null
    317 check_o "(\"$KLISP\" \"-e(write(get-interpreter-arguments))\" \"--\" \"/dev/null\")" \
    318     $KLISP '-e(write(get-interpreter-arguments))' -- /dev/null
    319 check_o "(\"$KLISP\" \"-e(write(get-interpreter-arguments))\" \"--\" \"/dev/null\" \"a\" \"b\" \"c\")" \
    320     $KLISP '-e(write(get-interpreter-arguments))' -- /dev/null a b c
    321 
    322 # stdout and stderr
    323 
    324 check_oe 'abc' '' $KLISP -e '(display "abc" (get-current-output-port))'
    325 check_oe '' 'abc' $KLISP -e '(display "abc" (get-current-error-port))'
    326 
    327 # done
    328 
    329 report
    330 cleanup