Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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" = "" ]
Expand All @@ -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
Expand All @@ -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
}

Expand Down
10 changes: 8 additions & 2 deletions src/test/framework/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down