33JS_SLANG=" node node_modules/js-slang/dist/repl/repl.js"
44
55SOURCEFILES=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
811red=` tput setaf 1`
912green=` 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+
1723test_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+
3262main () {
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
0 commit comments