Skip to content

Commit 3850d7f

Browse files
authored
Merge pull request #15 from arsalanc-v2/test_framework
Upgrade test script
2 parents 12e45c3 + 33805dd commit 3850d7f

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

scripts/test.sh

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
JS_SLANG="node node_modules/js-slang/dist/repl/repl.js"
44

55
SOURCEFILES=src/*/*.js
6-
SOURCE_TEST=src/test/framework/main.js
6+
SOURCE_TEST="src/test/framework/main.js"
7+
8+
DEFAULT_CHAPTER=4
9+
DEFAULT_VARIANT="default"
710

811
red=`tput setaf 1`
912
green=`tput setaf 2`
@@ -14,11 +17,14 @@ failed=0
1417

1518
# $1 is the source file to be tested
1619
# $2 is the test file
20+
# $3 is the chapter
21+
# $4 is the variant
22+
1723
test_source() {
1824

1925
# run concatenation of source and test case
2026
# compare the result with the commented last line in test case
21-
DIFF=$(diff <($JS_SLANG -e --chapter=4 "$(cat $1 $2)") \
27+
DIFF=$(diff <($JS_SLANG -e --chapter=$3 --variant=$4 "$(cat $1 $2)") \
2228
<(cat $2 | tail -1 | cut -c4-))
2329

2430
if [ "$DIFF" = "" ]
@@ -29,6 +35,30 @@ $DIFF"
2935

3036
}
3137

38+
test_source_framework() {
39+
40+
# run concatenation of source-test framework, source and test files
41+
RESULTS=$($JS_SLANG -e --chapter=$3 --variant=$4 "$(cat $SOURCE_TEST $1 $2)")
42+
43+
# retrieve names for tests that passed
44+
while read test_name
45+
do
46+
passed=$(($passed+1))
47+
echo "${green}PASS $2 $test_name"
48+
done < <(echo ${RESULTS} | grep -o '\w* PASSED' | awk -F 'PASSED' '{ print $1 }')
49+
50+
# retrieve names and error messages for tests that failed
51+
while read test_info
52+
do
53+
failed=$(($failed+1))
54+
echo $test_info | awk -F 'FAILED:' '{ print $1 ":" $2 }' | awk -F '"' '{ print $1 $2 }' |
55+
while read test_name test_error
56+
do echo "${red}FAIL $2 $test_name $test_error";
57+
done
58+
done < <(echo ${RESULTS} | grep -o '\w* FAILED:[^"]*')
59+
60+
}
61+
3262
main() {
3363
for s in ${SOURCEFILES}
3464
do
@@ -39,10 +69,20 @@ main() {
3969
# call test_source on each test case in __tests__
4070
for i in "$DIR/__tests__/$(basename ${s} .js)".*
4171
do
42-
test_source $s $i
43-
done
72+
# check if first line of test file contains 'chapter=' and retrieve its value. Set to the default chapter if it does not
73+
chapter=$(awk -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $i | awk -F ' ' '{ print $1 }')
74+
75+
# check if first line of test file contains 'variant=' and retrieve its value. Set to the default variant if it does not
76+
variant=$(awk -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $i | awk -F ' ' '{ print $1 }')
77+
78+
# check if first line of test file contains 'source-test'
79+
use_source_test=$(awk 'FNR==1{ if ($0~"source-test") print "yes" }' $i)
80+
if [[ $use_source_test == "yes" ]]
81+
then chapter=4 ; test_source_framework $s $i $chapter $variant
82+
else test_source $s $i $chapter $variant
83+
fi
84+
done
4485
fi
45-
4686
done
4787
}
4888

src/test/framework/main.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let start_time = 0;
1212
let elapsed_time = 0;
1313

1414
let current_test_status = TEST_PASS;
15+
let current_test_failure = null;
1516

1617
/**
1718
* Checks if the given condition evaluates to true
@@ -29,6 +30,10 @@ function assert(condition, message) {
2930
num_assert_fail = num_assert_fail + 1;
3031
current_test_status = TEST_FAIL;
3132
display(res);
33+
if (current_test_failure === null) {
34+
// only keep track of the first failure of any test
35+
current_test_failure = res;
36+
} else {}
3237
} else {
3338
num_assert_pass = num_assert_pass + 1;
3439
}
@@ -51,7 +56,7 @@ function report_error(message) {
5156
* @param result
5257
*/
5358
function assert_equal(expected, result) {
54-
const error_message = "assert_equal failed: " + stringify(result) + " (result) not equal to " + stringify(expected) + " (expected)";
59+
const error_message = stringify(result) + " (result) not equal to " + stringify(expected) + " (expected)";
5560
assert(equal(expected, result), error_message);
5661
}
5762

@@ -137,7 +142,8 @@ function _test_result(test_name) {
137142
display(test_name + " PASSED");
138143
num_pass = num_pass + 1;
139144
} else {
140-
display(test_name + " FAILED");
145+
display(test_name + " FAILED: " + current_test_failure);
146+
current_test_failure = null;
141147
}
142148

143149
current_test_status = TEST_PASS; // reset test status

0 commit comments

Comments
 (0)