Skip to content

Commit 55b4ec2

Browse files
SICP package test (source-academy#581)
* test SICP draft * test-branch * Testing in node functional * Fix names and Update SICP package automatically * replace tab * Format shell script * Update writers to take in parameters * Pass arguments in shell script as string * add author * Resolved some issues * Fix formatting * Rename writer2 to writer Co-authored-by: Samuel Fang <samuelfangjw@gmail.com>
1 parent 9fc07b5 commit 55b4ec2

File tree

7 files changed

+2742
-1
lines changed

7 files changed

+2742
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
sicpjs.zip
88

9+
test_node_env/expect.txt
10+
test_node_env/result.txt
11+
test_node_env/node_modules
912
.DS_Store

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"split": "yarn process web split",
4848
"svgpdf": "./scripts/svg_to_pdf.sh",
4949
"test": "node ./scripts/test.js",
50-
"try": "cd docs_out; http-server --cors --port 8080"
50+
"try": "cd docs_out; http-server --cors --port 8080",
51+
"nodetest": "./scripts/nodetest.sh"
5152
},
5253
"husky": {
5354
"hooks": {

scripts/nodetest.sh

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#! /usr/bin/env bash
2+
3+
cd test_node_env
4+
npm update sicp
5+
cd ..
6+
7+
OUT_WRITER="test_node_env/result_writer.js"
8+
EXP_WRITER="test_node_env/expected_writer.js"
9+
10+
# must use BSD awk
11+
AWK="awk"
12+
13+
SOURCEFILES=js_programs/*/*/*/*.js
14+
15+
DEFAULT_CHAPTER=4
16+
DEFAULT_VARIANT="default"
17+
18+
red=`tput setaf 1`
19+
green=`tput setaf 2`
20+
normal=`tput setaf 7`
21+
22+
passed=0
23+
failed=0
24+
25+
# $1 is the source file to be tested
26+
# $2 is the chapter
27+
# $3 is the variant
28+
29+
test_source() {
30+
if [ $3 ]; then
31+
variant=$3
32+
else
33+
variant=$DEFAULT_VARIANT
34+
fi
35+
36+
if [ "$(cat $1 | tail -1 | cut -c 1-13)" = "// expected: " ] && [ "$3" != "non-det" ] && [ "$3" != "concurrent" ] && [ "$3" != "lazy" ]
37+
then
38+
39+
EXPECTED=`cat $1 | tail -1 | cut -c14-`
40+
41+
if [[ "$EXPECTED" != "'all threads terminated'" ]]
42+
then
43+
echo "${normal}$1, expecting: $(cat $1 | tail -1 | cut -c14-)"
44+
45+
# Run the writers
46+
node --stack_size=8000 $OUT_WRITER "$1"
47+
node $EXP_WRITER "$EXPECTED"
48+
49+
# Compare outputs
50+
DIFF=$(diff test_node_env/result.txt test_node_env/expect.txt)
51+
52+
if [ "$DIFF" = "" ]
53+
then passed=$(($passed+1)); echo "${green}PASS"
54+
else failed=$(($failed+1)); echo "${red}FAIL:
55+
$DIFF"
56+
fi
57+
fi
58+
fi
59+
}
60+
61+
main() {
62+
for s in ${SOURCEFILES}
63+
do
64+
# DIR is full path including js_programs
65+
DIR=$(dirname ${s})
66+
# CHAPTERDIR is path starting with chapterx
67+
CHAPTERDIR=${DIR#*/}
68+
# CHAPTER is just the chapter name, e.g. chapter2
69+
CHAPTER=${CHAPTERDIR%%/*}
70+
# SECTIONDIR is path starting with sectionx
71+
SECTIONDIR=${CHAPTERDIR#*/}
72+
# SECTION is just the section name, e.g. section3
73+
SECTION=${SECTIONDIR%%/*}
74+
75+
if [[ ($1 == $CHAPTER || $1 == "") && ($2 == $SECTION || $2 == "") ]];
76+
then
77+
# check if first line of test file contains 'chapter=' and retrieve
78+
# its value. Set to the default chapter if it does not
79+
chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $s | $AWK -F ' ' '{ print $1 }')
80+
81+
# check if first line of test file contains 'variant=' and retrieve
82+
# its value. Set to the default variant if it does not
83+
variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $s | $AWK -F ' ' '{ print $1 }')
84+
85+
test_source ${s} ${chapter} ${variant}
86+
fi
87+
done
88+
}
89+
90+
# optional arguments: chapter... section..., limiting testing only to the
91+
# named chapter (or section): e.g. yarn test chapter2 section3
92+
93+
main $1 $2
94+
95+
echo "${normal}test cases completed; $passed passed, $failed failed"
96+
exit 0

test_node_env/expected_writer.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Script } from "vm";
2+
import { createWriteStream } from "fs";
3+
4+
let writer = createWriteStream("test_node_env/expect.txt");
5+
const args = process.argv.slice(2);
6+
const expected = args[0];
7+
8+
writer.once("open", function (fd) {
9+
let e = new Script(expected);
10+
let r = e.runInNewContext();
11+
if (typeof r !== "undefined") {
12+
writer.write(JSON.stringify(r));
13+
} else {
14+
writer.write("undefined");
15+
}
16+
writer.end();
17+
});

0 commit comments

Comments
 (0)