Skip to content

Commit 1e10358

Browse files
authored
almost done with ch2 revamp (source-academy#246)
1 parent 57d3a4b commit 1e10358

26 files changed

+3475
-1991
lines changed

javascript/parseXmlJs.js

-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ const tagsToRemove = new Set([
1616
"HISTORY",
1717
"ORDER",
1818
"SCHEME",
19-
"SOLUTION",
2019
"INDEX",
2120
"LABEL",
2221
"NAME"
2322
]);
24-
// SOLUTION tag handled by processSnippet
2523

2624
const ignoreTags = new Set([
2725
"CHAPTERCONTENT",

javascript/parseXmlLatex.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const tagsToRemove = new Set([
2828
"NAME",
2929
"ORDER",
3030
"SCHEME",
31-
"SOLUTION"
31+
"SOLUTION",
32+
"WEB_ONLY"
3233
]);
3334
// SOLUTION tag handled by processSnippet
3435

javascript/processingFunctions/processSnippetHtml.js

+7
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,23 @@ export const processSnippetHtml = (node, writeTo, split) => {
160160
const explicit_chap = node.getAttribute("CHAP");
161161
const chap = explicit_chap ? explicit_chap : current_chap;
162162
let variant = node.getAttribute("VARIANT");
163+
let ext = node.getAttribute("EXT");
163164
if (variant) {
164165
variant = "&variant=" + variant;
165166
} else {
166167
variant = "";
167168
}
169+
if (ext) {
170+
ext = "&ext=" + ext;
171+
} else {
172+
ext = "";
173+
}
168174
const url =
169175
sourceAcademyURL +
170176
"/playground#chap=" +
171177
chap +
172178
variant +
179+
ext +
173180
"&prgrm=" +
174181
compressed;
175182

javascript/processingFunctions/processSnippetJs.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export const setupSnippetsJs = node => {
1515
const snippet = snippets[i];
1616
const jsSnippet = snippet.getElementsByTagName("JAVASCRIPT")[0];
1717
let jsRunSnippet = snippet.getElementsByTagName("JAVASCRIPT_RUN")[0];
18-
if (!jsRunSnippet) {
19-
jsRunSnippet = jsSnippet;
18+
let jsTestSnippet = snippet.getElementsByTagName("JAVASCRIPT_TEST")[0];
19+
if (jsTestSnippet) {
20+
jsRunSnippet = jsTestSnippet;
21+
} else {
22+
if (!jsRunSnippet) {
23+
jsRunSnippet = jsSnippet;
24+
}
2025
}
2126
const snippetName = snippet.getElementsByTagName("NAME")[0];
22-
if (snippetName && jsSnippet) {
27+
if (snippetName && jsRunSnippet) {
2328
const nameStr = snippetName.firstChild.nodeValue;
2429
if (snippetStore[nameStr]) {
2530
repeatedNameWarning(nameStr);
@@ -59,10 +64,14 @@ export const processSnippetJs = (node, writeTo, fileFormat) => {
5964
if (jsSnippet) {
6065
// JavaScript source for running. Overrides JAVASCRIPT if present.
6166
let jsRunSnippet = node.getElementsByTagName("JAVASCRIPT_RUN")[0];
62-
if (!jsRunSnippet) {
63-
jsRunSnippet = jsSnippet;
67+
let jsTestSnippet = node.getElementsByTagName("JAVASCRIPT_TEST")[0];
68+
if (jsTestSnippet) {
69+
jsRunSnippet = jsTestSnippet;
70+
} else {
71+
if (!jsRunSnippet) {
72+
jsRunSnippet = jsSnippet;
73+
}
6474
}
65-
6675
const codeArr = [];
6776
recursiveProcessPureText(jsSnippet.firstChild, codeArr);
6877
const codeStr = codeArr.join("").trim();

javascript/processingFunctions/processSnippetPdf.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ export const processSnippetPdf = (node, writeTo) => {
147147
const compressed = lzString.compressToEncodedURIComponent(
148148
reqStr + codeStr_run + exampleStr
149149
);
150-
const current_chap = chapterIndex.substring(0, 1);
151-
const explicit_chap = node.getAttribute("CHAP");
152-
const chap = explicit_chap ? explicit_chap : current_chap;
150+
// in this version we dont have access to the current chapter
151+
const chap = 4; // hard-wire chapter to 4
153152
let variant = node.getAttribute("VARIANT");
154153
if (variant) {
155154
variant = "variant=" + variant + "&";

scripts/test.sh

+24-8
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,32 @@ $DIFF"
4141
main() {
4242
for s in ${SOURCEFILES}
4343
do
44-
DIR=$(dirname ${s})
45-
# check if first line of test file contains 'chapter=' and retrieve its value. Set to the default chapter if it does not
46-
chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $s | $AWK -F ' ' '{ print $1 }')
47-
48-
# check if first line of test file contains 'variant=' and retrieve its value. Set to the default variant if it does not
49-
variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $s | $AWK -F ' ' '{ print $1 }')
50-
test_source ${s} ${chapter} ${variant}
44+
# DIR is full path including js_programs
45+
DIR=$(dirname ${s})
46+
# CHAPTERDIR is path starting with chapterx
47+
CHAPTERDIR=${DIR#*/}
48+
# CHAPTER is just the chapter name, e.g. chapter2
49+
CHAPTER=${CHAPTERDIR%%/*}
50+
# SECTIONDIR is path starting with sectionx
51+
SECTIONDIR=${CHAPTERDIR#*/}
52+
# SECTION is just the section name, e.g. section3
53+
SECTION=${SECTIONDIR%%/*}
54+
if [[ ($1 == $CHAPTER || $1 == "") && ($2 == $SECTION || $2 == "") ]];
55+
then
56+
# check if first line of test file contains 'chapter=' and retrieve
57+
# its value. Set to the default chapter if it does not
58+
chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $s | $AWK -F ' ' '{ print $1 }')
59+
60+
# check if first line of test file contains 'variant=' and retrieve
61+
# its value. Set to the default variant if it does not
62+
variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $s | $AWK -F ' ' '{ print $1 }')
63+
test_source ${s} ${chapter} ${variant}
64+
fi
5165
done
5266
}
5367

54-
main
68+
# optional arguments: chapter... section..., limiting testing only to the
69+
# named chapter (or section): e.g. yarn test chapter2 section3
70+
main $1 $2
5571
echo "${normal}test cases completed; $passed passed, $failed failed"
5672
exit 0

xml/chapter2/section1/subsection1.xml

+39-12
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ function equal_rat(x, y) {
208208
<!--
209209
\indprim*{cons}
210210
\ind*{primitive
211-
<SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>
212-
(those marked <EM>ns</EM> are not in the IEEE Scheme standard)}[cons@<SCHEMEINLINE>cons</SCHEMEINLINE>]
211+
<SPLITINLINE><SCHEME>procedures</SCHEME>
212+
<JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>
213+
(those marked <EM>ns</EM> are not in the IEEE Scheme
214+
standard)}[cons@<SCHEMEINLINE>cons</SCHEMEINLINE>]
213215
-->
214216
<SPLITINLINE>
215217
<SCHEME>
@@ -298,12 +300,13 @@ function equal_rat(x, y) {
298300
(define x (cons 1 2))
299301
</SCHEME>
300302
<JAVASCRIPT>
301-
const x = pair(1,2);
303+
const x = pair(1, 2);
302304
</JAVASCRIPT>
303305
</SNIPPET>
304306

305307
<SNIPPET PAGE="85">
306308
<NAME>cons_1_2_example</NAME>
309+
<EXPECTED>1</EXPECTED>
307310
<REQUIRES>cons_1_2</REQUIRES>
308311
<SCHEME>
309312
(car x)
@@ -321,6 +324,7 @@ head(x);
321324

322325
<SNIPPET PAGE="85">
323326
<NAME>cons_1_2_example2</NAME>
327+
<EXPECTED>2</EXPECTED>
324328
<REQUIRES>cons_1_2</REQUIRES>
325329
<SCHEME>
326330
(cdr x)
@@ -354,13 +358,14 @@ tail(x);
354358
(define z (cons x y))
355359
</SCHEME>
356360
<JAVASCRIPT>
357-
const x = pair(1,2);
358-
const y = pair(3,4);
359-
const z = pair(x,y);
361+
const x = pair(1, 2);
362+
const y = pair(3, 4);
363+
const z = pair(x, y);
360364
</JAVASCRIPT>
361365
</SNIPPET>
362366
<SNIPPET PAGE="85">
363367
<NAME>cons_1_2_3_4_example</NAME>
368+
<EXPECTED>1</EXPECTED>
364369
<REQUIRES>cons_1_2_3_4</REQUIRES>
365370
<SCHEME>
366371
(car (car z))
@@ -377,6 +382,7 @@ head(head(z));
377382
</SNIPPET>
378383
<SNIPPET PAGE="85">
379384
<NAME>cons_1_2_3_4_example2</NAME>
385+
<EXPECTED>3</EXPECTED>
380386
<REQUIRES>cons_1_2_3_4</REQUIRES>
381387
<SCHEME>
382388
(car (cdr z))
@@ -539,6 +545,7 @@ const denom = tail;
539545

540546
<SNIPPET PAGE="86">
541547
<NAME>make_rat2</NAME>
548+
<EXPECTED>2</EXPECTED>
542549
<EXAMPLE>rat_example_1</EXAMPLE>
543550
<SCHEME>
544551
(define (make-rat n d) (cons n d))
@@ -652,7 +659,7 @@ const one_half = make_rat(1, 2);
652659
print_rat(one_half);
653660
</JAVASCRIPT>
654661
<JAVASCRIPT_OUTPUT>
655-
1/2
662+
"1/2"
656663
</JAVASCRIPT_OUTPUT>
657664
</SNIPPET>
658665
</TEXT>
@@ -665,6 +672,7 @@ print_rat(one_half);
665672
</SPLITINLINE>
666673
<SNIPPET PAGE="86">
667674
<NAME>print_rat_example</NAME>
675+
<EXPECTED>[ 1, 2 ]</EXPECTED>
668676
<REQUIRES>make_rat2</REQUIRES>
669677
<REQUIRES>print_rat</REQUIRES>
670678
<SCHEME>
@@ -680,8 +688,13 @@ const one_half = make_rat(1, 2);
680688

681689
print_rat(one_half);
682690
</JAVASCRIPT>
691+
<JAVASCRIPT_TEST>
692+
const one_half = make_rat(1, 2);
693+
694+
one_half;
695+
</JAVASCRIPT_TEST>
683696
<JAVASCRIPT_OUTPUT>
684-
1/2
697+
"1/2"
685698
</JAVASCRIPT_OUTPUT>
686699
</SNIPPET>
687700
<SNIPPET EVAL="no" PAGE="86" HIDE="yes">
@@ -705,6 +718,7 @@ const one_third = make_rat(1, 3);
705718

706719
<SNIPPET PAGE="87">
707720
<NAME>print_rat_example2</NAME>
721+
<EXPECTED>[ 5, 6 ]</EXPECTED>
708722
<REQUIRES>add_rat</REQUIRES>
709723
<REQUIRES>one_half</REQUIRES>
710724
<REQUIRES>one_third</REQUIRES>
@@ -718,12 +732,16 @@ const one_third = make_rat(1, 3);
718732
<JAVASCRIPT>
719733
print_rat(add_rat(one_half, one_third));
720734
</JAVASCRIPT>
735+
<JAVASCRIPT_TEST>
736+
add_rat(one_half, one_third);
737+
</JAVASCRIPT_TEST>
721738
<JAVASCRIPT_OUTPUT>
722-
5/6
739+
"5/6"
723740
</JAVASCRIPT_OUTPUT>
724741
</SNIPPET>
725742
<SNIPPET PAGE="87">
726743
<NAME>print_rat_example3</NAME>
744+
<EXPECTED>[ 1, 6 ]</EXPECTED>
727745
<REQUIRES>add_rat</REQUIRES>
728746
<REQUIRES>one_half</REQUIRES>
729747
<REQUIRES>one_third</REQUIRES>
@@ -737,12 +755,16 @@ print_rat(add_rat(one_half, one_third));
737755
<JAVASCRIPT>
738756
print_rat(mul_rat(one_half, one_third));
739757
</JAVASCRIPT>
758+
<JAVASCRIPT_TEST>
759+
mul_rat(one_half, one_third);
760+
</JAVASCRIPT_TEST>
740761
<JAVASCRIPT_OUTPUT>
741-
1/6
762+
"1/6"
742763
</JAVASCRIPT_OUTPUT>
743764
</SNIPPET>
744765
<SNIPPET PAGE="87">
745766
<NAME>print_rat_example4</NAME>
767+
<EXPECTED>[ 6, 9 ]</EXPECTED>
746768
<REQUIRES>add_rat</REQUIRES>
747769
<REQUIRES>one_third</REQUIRES>
748770
<REQUIRES>print_rat</REQUIRES>
@@ -755,8 +777,11 @@ print_rat(mul_rat(one_half, one_third));
755777
<JAVASCRIPT>
756778
print_rat(add_rat(one_third, one_third));
757779
</JAVASCRIPT>
780+
<JAVASCRIPT_TEST>
781+
add_rat(one_third, one_third);
782+
</JAVASCRIPT_TEST>
758783
<JAVASCRIPT_OUTPUT>
759-
6/9
784+
"6/9"
760785
</JAVASCRIPT_OUTPUT>
761786
</SNIPPET>
762787
</TEXT>
@@ -797,6 +822,7 @@ function denom(x) {
797822
</SNIPPET>
798823
<SNIPPET PAGE="87">
799824
<NAME>make_rat_3</NAME>
825+
<EXPECTED>[ 2, 3 ]</EXPECTED>
800826
<REQUIRES>numer</REQUIRES>
801827
<EXAMPLE>make_rat_3_example_1</EXAMPLE>
802828
<REQUIRES>gcd_definition</REQUIRES>
@@ -836,7 +862,7 @@ make_rat(4, 6);
836862
print_rat(add_rat(one_third, one_third));
837863
</JAVASCRIPT>
838864
<JAVASCRIPT_OUTPUT>
839-
2/3
865+
"2/3"
840866
</JAVASCRIPT_OUTPUT>
841867
</SNIPPET>
842868
<SNIPPET PAGE="84" HIDE="yes">
@@ -926,6 +952,7 @@ function equal_rat(x, y) {
926952
negative, only the numerator is negative.
927953
<SOLUTION>
928954
<SNIPPET>
955+
<EXPECTED>[ -3, 4 ]</EXPECTED>
929956
<REQUIRES>abs_definition</REQUIRES>
930957
<REQUIRES>gcd_definition</REQUIRES>
931958
<EXAMPLE>example_2.1</EXAMPLE>

xml/chapter2/section1/subsection2.xml

+13
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
</SPLITINLINE>
141141
<SNIPPET PAGE="89">
142142
<NAME>make_rat_4</NAME>
143+
<EXPECTED>[ 1, 2 ]</EXPECTED>
143144
<REQUIRES>gcd_definition</REQUIRES>
144145
<EXAMPLE>print_rat_example5</EXAMPLE>
145146
<SCHEME>
@@ -181,6 +182,11 @@ const one_half = make_rat(1, 2);
181182

182183
print_rat(one_half);
183184
</JAVASCRIPT>
185+
<JAVASCRIPT_TEST>
186+
const one_half = make_rat(1, 2);
187+
188+
one_half;
189+
</JAVASCRIPT_TEST>
184190
</SNIPPET>
185191
</TEXT>
186192
<TEXT>
@@ -334,6 +340,7 @@ function print_point(p) {
334340
</SNIPPET>
335341
<SOLUTION>
336342
<SNIPPET>
343+
<EXPECTED>[ 0.5, 0.5 ]</EXPECTED>
337344
<REQUIRES>print_point</REQUIRES>
338345
<EXAMPLE>example_2.2</EXAMPLE>
339346
<JAVASCRIPT>
@@ -378,6 +385,11 @@ const one_half = make_segment(make_point(0, 0),
378385
make_point(1, 1));
379386
print_point(mid_point_segment(one_half));
380387
</JAVASCRIPT>
388+
<JAVASCRIPT_TEST>
389+
const one_half = make_segment(make_point(0, 0),
390+
make_point(1, 1));
391+
mid_point_segment(one_half);
392+
</JAVASCRIPT_TEST>
381393
<SCHEME>
382394
</SCHEME>
383395
</SNIPPET>
@@ -404,6 +416,7 @@ print_point(mid_point_segment(one_half));
404416
First implementation:
405417
<SNIPPET>
406418
<EXAMPLE>example_2.3</EXAMPLE>
419+
<EXPECTED>8</EXPECTED>
407420
<JAVASCRIPT>
408421
function make_point(x,y){
409422
return pair(x,y);

0 commit comments

Comments
 (0)