diff --git a/scripts/test.sh b/scripts/test.sh index b9c530c..5e32bad 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -3,7 +3,10 @@ JS_SLANG="node node_modules/js-slang/dist/repl/repl.js" SOURCEFILES=src/*/*.js -SOURCE_TEST=src/test/framework/main.js +SOURCE_TEST="src/test/framework/main.js" + +DEFAULT_CHAPTER=4 +DEFAULT_VARIANT="default" red=`tput setaf 1` green=`tput setaf 2` @@ -14,11 +17,14 @@ failed=0 # $1 is the source file to be tested # $2 is the test file +# $3 is the chapter +# $4 is the variant + test_source() { # run concatenation of source and test case # compare the result with the commented last line in test case - DIFF=$(diff <($JS_SLANG -e --chapter=4 "$(cat $1 $2)") \ + DIFF=$(diff <($JS_SLANG -e --chapter=$3 --variant=$4 "$(cat $1 $2)") \ <(cat $2 | tail -1 | cut -c4-)) if [ "$DIFF" = "" ] @@ -29,6 +35,30 @@ $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)") + + # 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 }') + + # 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 }' | + while read test_name test_error + do echo "${red}FAIL $2 $test_name $test_error"; + done + done < <(echo ${RESULTS} | grep -o '\w* FAILED:[^"]*') + +} + main() { for s in ${SOURCEFILES} do @@ -39,10 +69,20 @@ main() { # call test_source on each test case in __tests__ for i in "$DIR/__tests__/$(basename ${s} .js)".* do - test_source $s $i - done + # 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 'source-test' + use_source_test=$(awk 'FNR==1{ if ($0~"source-test") 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 + fi + done fi - done } diff --git a/src/test/framework/main.js b/src/test/framework/main.js index c2cd6cc..54cac87 100644 --- a/src/test/framework/main.js +++ b/src/test/framework/main.js @@ -12,6 +12,7 @@ let start_time = 0; let elapsed_time = 0; let current_test_status = TEST_PASS; +let current_test_failure = null; /** * Checks if the given condition evaluates to true @@ -29,6 +30,10 @@ function assert(condition, message) { num_assert_fail = num_assert_fail + 1; current_test_status = TEST_FAIL; display(res); + if (current_test_failure === null) { + // only keep track of the first failure of any test + current_test_failure = res; + } else {} } else { num_assert_pass = num_assert_pass + 1; } @@ -51,7 +56,7 @@ function report_error(message) { * @param result */ function assert_equal(expected, result) { - const error_message = "assert_equal failed: " + stringify(result) + " (result) not equal to " + stringify(expected) + " (expected)"; + const error_message = stringify(result) + " (result) not equal to " + stringify(expected) + " (expected)"; assert(equal(expected, result), error_message); } @@ -137,7 +142,8 @@ function _test_result(test_name) { display(test_name + " PASSED"); num_pass = num_pass + 1; } else { - display(test_name + " FAILED"); + display(test_name + " FAILED: " + current_test_failure); + current_test_failure = null; } current_test_status = TEST_PASS; // reset test status