diff --git a/scripts/test.sh b/scripts/test.sh index e21918e..08e3deb 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,14 +2,12 @@ JS_SLANG="node --stack-size=4000 node_modules/js-slang/dist/repl/repl.js" -# must use BSD awk -AWK="awk" - SOURCEFILES=src/*/*.js SOURCE_TEST="src/test/framework/main.js" DEFAULT_CHAPTER=4 -DEFAULT_VARIANT="default" +DEFAULT_VARIANT='default' +DEFAULT_EXECUTION_METHOD='' red=`tput setaf 1` green=`tput setaf 2` @@ -22,6 +20,7 @@ failed=0 # $2 is the test file # $3 is the chapter # $4 is the variant +# $5 specifies the execution method. '-i' for 'interpreter'. '' for 'native'. test_source() { # read in test file to find the number of comment lines @@ -42,7 +41,7 @@ test_source() { # if program throws error, ignore the output and compare the error message only # NOTE: error output line is striped of line number as it is machine dependent - ERROR=$( { $JS_SLANG -e --chapter=$3 "$(cat $1 $2)">$DIR/__tests__/output; } 2>&1 ) + ERROR=$( { $JS_SLANG $5 -e --chapter=$3 --variant=$4 "$(cat $1 $2)">$DIR/__tests__/output; } 2>&1 ) if [ ! -z "$ERROR" ] then DIFF=$(diff <(echo $ERROR | grep -o 'Error:.*') <(cat $2 | tail -$NUM_VALID_COMMENT | grep -o 'Error:.*')) @@ -61,24 +60,23 @@ $DIFF" } test_source_framework() { - # run concatenation of source-test framework, source and test files - RESULTS=$($JS_SLANG -e --chapter=$3 --variant=$4 "$(cat $SOURCE_TEST $1 $2)") + RESULTS=$($JS_SLANG $5 -e --chapter=$3 --variant=$4 "$(cat $SOURCE_TEST $1 $2)") # retrieve names for tests that passed while read test_name do passed=$(($passed+1)) echo "${green}PASS $2 $test_name" - done < <(echo ${RESULTS} | grep -o '\w* PASSED' | $AWK -F 'PASSED' '{ print $1 }') + done < <(echo ${RESULTS} | grep -o '\w* PASSED' | cut -d ' ' -f 1) # retrieve names and error messages for tests that failed while read test_info do failed=$(($failed+1)) - echo $test_info | $AWK -F 'FAILED:' '{ print $1 ":" $2 }' | $AWK -F '"' '{ print $1 $2 }' | + echo $test_info | grep -o 'FAILED:.*' | cut -d ' ' -f 2- | while read test_name test_error - do echo "${red}FAIL $2 $test_name $test_error"; + do echo "${red}FAIL $2 $test_name : $test_error"; done done < <(echo ${RESULTS} | grep -o '\w* FAILED:[^"]*') } @@ -94,17 +92,29 @@ main() { for i in "$DIR/__tests__/$(basename ${s} .js)".* do if [ -f "$i" ]; then - # check if first line of test file contains 'chapter=' and retrieve its value. Set to the default chapter if it does not - chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $i | $AWK -F ' ' '{ print $1 }') - - # check if first line of test file contains 'variant=' and retrieve its value. Set to the default variant if it does not - variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $i | $AWK -F ' ' '{ print $1 }') + ### check if first line of test file contains various keys and retrieve their values ### + + chapter=$(cat $i | head -n1 | grep -o 'chapter=.*' | cut -d '=' -f 2 | cut -d ' ' -f 1) + if [ "$chapter" = "" ] + then chapter=$DEFAULT_CHAPTER + fi + + variant=$(cat $i | head -n1 | grep -o 'variant=.*' | cut -d '=' -f 2 | cut -d ' ' -f 1) + if [ "$variant" = "" ] + then variant=$DEFAULT_VARIANT + fi + + execution_method=$(cat $i | head -n1 | grep -o 'executionMethod=.*' | cut -d '=' -f 2 | cut -d ' ' -f 1) + if [ "$execution_method" = "interpreter" ] + then execution_method='-i' + else execution_method=$DEFAULT_EXECUTION_METHOD + fi # check if first line of test file contains 'framework' - use_source_test=$($AWK 'FNR==1{ if ($0~"framework") print "yes" }' $i) - if [[ $use_source_test == "yes" ]] - then chapter=4 ; test_source_framework $s $i $chapter $variant - else test_source $s $i $chapter $variant + use_source_test=$(cat $i | head -n1 | grep -o 'framework') + if [[ $use_source_test == "framework" ]] + then chapter=4 ; test_source_framework $s $i $chapter $variant $execution_method + else test_source $s $i $chapter $variant $execution_method fi fi done