interplane.Get Omega
Read in your language

omtex

aien-dev/omtex

Brought by drakestapleton

branch main Branches Tags History

omtex / tests / run.sh

212 lines · 10700 bytes Raw Blame History
1#!/bin/sh
2# tests/run.sh -- omtex test suite. Every refusal case is paired with a minimally different accepted
3# case, so a refusal cannot pass by the library refusing everything. Prints one line per case and a
4# summary; exits 1 on any FAIL. Cases that cannot run on this host print NOT_RUN (never PASS).
5# Env: OMC (pinned omc; fetched by tools/pin.sh when absent), OMTEX_NICE as in tools/build.sh.
6set -u
7here=$(cd "$(dirname "$0")/.." && pwd)
8. "$here/tools/pin.sh"
9nice=${OMTEX_NICE-nice -n 19 ionice -c3}
10( omtex_check_omc ) || { echo "FAIL pin: omc missing or not the pinned build"; exit 1; }
11T=$(mktemp -d "${TMPDIR:-/tmp}/omtex-test.XXXXXX")
12trap '[ -n "${OMTEX_KEEP:-}" ] || rm -rf "$T"' EXIT
13pass=0 fail=0 notrun=0
14ok() { pass=$((pass + 1)); echo "PASS $1"; }
15bad() { fail=$((fail + 1)); echo "FAIL $1: $2"; }
16nr() { notrun=$((notrun + 1)); echo "NOT_RUN $1: $2"; }
17
18# doc NAME: stdin holds "text" lines, a line "---", then body words; wrapped in a minimal document.
19doc() {
20 { echo 'text zzcls "article"'; echo 'text zzopt "11pt"'
21 sed '/^---$/,$d'
22 echo 'fn main to int'; echo 'docclass zzopt zzcls'; echo 'begindoc'
23 sed -n '/^---$/,$p' "$T/$1.in" | sed 1d
24 echo 'enddoc'; echo 'ret 0'; echo 'end'; } < "$T/$1.in" > "$T/$1.om"
25}
26# gen NAME [ARGS]: compile lib+case, run the generator; sets rc, output in $T/NAME.out / .err.
27gen() {
28 n=$1; shift
29 if ! cat "$here/lib/omtex.om" "$T/$n.om" | $nice "$OMC" > "$T/$n.gen" 2> "$T/$n.cerr"; then
30 rc=compile; return
31 fi
32 chmod +x "$T/$n.gen"
33 "$T/$n.gen" "$@" > "$T/$n.out" 2> "$T/$n.err"; rc=$?
34}
35# expect NAME RC [FIXED-STRING...]: case built by doc(); generator exit must be RC and stdout must
36# contain each string.
37expect() {
38 n=$1 want=$2; shift 2
39 doc "$n"; gen "$n"
40 if [ "$rc" != "$want" ]; then bad "$n" "rc=$rc want=$want $(cat "$T/$n.err" "$T/$n.cerr" 2>/dev/null | head -2)"; return; fi
41 for s in "$@"; do
42 grep -qF -- "$s" "$T/$n.out" || { bad "$n" "output lacks: $s"; return; }
43 done
44 if [ "$want" = 111 ]; then
45 grep -q '^omtex: refused element [0-9]*: ' "$T/$n.err" || { bad "$n" "no refusal message"; return; }
46 fi
47 ok "$n"
48}
49case_() { cat > "$T/$1.in"; }
50
51# ---- escaping (prose) --------------------------------------------------------------------------
52case_ esc <<'EOF'
53text p "50% & a_b #1 {x} ^ < > | tie~here, tilde \\~ and backslash \\\\ done"
54---
55para p
56EOF
57expect esc 0 '50\% \& a\_b \#1 \{x\} \textasciicircum{} \textless{} \textgreater{} \textbar{} tie~here, tilde \textasciitilde{} and backslash \textbackslash{} done'
58
59# Leak detector: a TeX special preceded by anything but a backslash. Negative control: the detector
60# must fire on the raw prose, otherwise it proves nothing about the escaped output.
61leak() { grep -E '(^|[^\\])[%&_#]' "$1" > /dev/null; }
62grep '^text p ' "$T/esc.in" > "$T/esc.raw"
63if ! leak "$T/esc.raw"; then bad esc-leak-control "detector does not fire on raw % & _ #"
64elif sed -n '/begin{document}/,/end{document}/p' "$T/esc.out" > "$T/esc.body" && leak "$T/esc.body"; then
65 bad esc-leak "unescaped special in output: $(grep -E '(^|[^\\])[%&_#]' "$T/esc.body" | head -1)"
66else ok esc-leak; fi
67
68case_ markup <<'EOF'
69text p "*em* **bold** `a_b{c}` $x^2$ \"quoted\" [@k1,k2] [#sec:a] \\* \\$ \\[x\\]"
70---
71para p
72EOF
73expect markup 0 '\emph{em} \textbf{bold} \texttt{a\_b\{c\}} $x^2$ ``quoted'"''"' ~\cite{k1,k2} ~\ref{sec:a} * \$ {[}x{]}'
74
75case_ utf8 <<'EOF'
76text p "Gödel – π"
77---
78para p
79EOF
80expect utf8 0 'Gödel – π'
81
82# ---- refusal pairs: (accepted, refused) ----------------------------------------------------------
83pair() { # pair NAME BODY-WORD GOOD-TEXT BAD-TEXT
84 printf 'text v "%s"\n---\n%s v\n' "$3" "$2" > "$T/$1-ok.in"
85 printf 'text v "%s"\n---\n%s v\n' "$4" "$2" > "$T/$1-refused.in"
86 expect "$1-ok" 0; expect "$1-refused" 111
87}
88pair backslash para 'C:\\\\temp' 'C:\\temp'
89pair emph para 'F\\* and *x*' 'F* and x'
90pair emph-nest para '**a *b* c**' '**a *b** c*'
91pair quote para '\"hi\" she said' '\"hi she said'
92pair code para '`x`' '`x'
93pair math-pct para '$50\\%$' '$50%$'
94pair math-dollar para '$a$ and $b$' '$a$ and $b'
95pair math-brace para '$x^{2}$' '$x^{2$'
96pair math-input para '$\\mathrm{input}$' '$\\input{/etc/passwd}$'
97pair math-csname para '$\\alpha$' '$\\csname foo\\endcsname$'
98pair disp-def equation 'e^{i\\pi}' '\\def\\x{1}\\x'
99pair disp-write displaymath 'a = b' '\\immediate\\write18{id}'
100pair amp-outside para '$a \\le b$' '$a & b$'
101pair cite-key para '[@shannon48]' '[@bad key]'
102pair ref-key para '[#sec:a]' '[#sec a]'
103# labels go through sectionl, which takes two texts
104printf 'text s "Intro"\ntext v "sec:intro"\n---\nsectionl s v\n' > "$T/label-ok.in"
105printf 'text s "Intro"\ntext v "sec:intro}\\\\input{x}"\n---\nsectionl s v\n' > "$T/label-refused.in"
106expect label-ok 0 '\section{Intro}\label{sec:intro}'; expect label-refused 111
107printf 'text o "leftmargin=*"\ntext i "a"\n---\nitemizeo o\nitem i\nendlist\n' > "$T/opt-ok.in"
108printf 'text o "leftmargin=*]\\\\input{x}"\ntext i "a"\n---\nitemizeo o\nitem i\nendlist\n' > "$T/opt-refused.in"
109expect opt-ok 0 '\begin{itemize}[leftmargin=*]'; expect opt-refused 111
110printf 'text i "a"\n---\nitemize\nitem i\nendlist\n' > "$T/list-ok.in"
111printf 'text i "a"\n---\nitemize\nitem i\n' > "$T/list-refused.in"
112expect list-ok 0; expect list-refused 111
113printf 'text i "a"\n---\nitemize\nitem i\nendlist\n' > "$T/item-ok.in"
114printf 'text i "a"\n---\nitem i\n' > "$T/item-refused.in"
115expect item-ok 0; expect item-refused 111
116printf 'text sh "sh"\ntext c "c"\ntext l "lst:x"\ntext b "echo end{lstlisting}\\n"\n---\ncodelang sh\nlisting c l b\n' > "$T/lst-ok.in"
117printf 'text sh "sh"\ntext c "c"\ntext l "lst:x"\ntext b "echo \\\\end{lstlisting}\\n"\n---\ncodelang sh\nlisting c l b\n' > "$T/lst-refused.in"
118expect lst-ok 0; expect lst-refused 111
119printf 'text c "c"\ntext l "lst:x"\ntext b "x\\n"\ntext sh "sh"\n---\ncodelang sh\nlisting c l b\n' > "$T/lang-ok.in"
120printf 'text c "c"\ntext l "lst:x"\ntext b "x\\n"\n---\nlisting c l b\n' > "$T/lang-refused.in"
121expect lang-ok 0; expect lang-refused 111
122
123# phase order: a body word before begindoc, a preamble word after it
124cat > "$T/order-ok.om" <<'EOF'
125text c "article"
126text o "11pt"
127text p "amsmath"
128text v "x"
129fn main to int
130docclass o c
131usepkg p
132begindoc
133para v
134enddoc
135ret 0
136end
137EOF
138sed 's/^usepkg p$/para v/' "$T/order-ok.om" > "$T/order-refused.om"
139for n in order-ok order-refused; do
140 gen "$n"; want=0; [ "$n" = order-refused ] && want=111
141 [ "$rc" = "$want" ] && ok "$n" || bad "$n" "rc=$rc want=$want"
142done
143cp "$T/order-ok.om" "$T/order-late.om"
144sed -i 's/^para v$/usepkg p/' "$T/order-late.om"
145gen order-late; [ "$rc" = 111 ] && ok order-late-preamble || bad order-late-preamble "rc=$rc want=111"
146
147# ---- listings check mode ------------------------------------------------------------------------
148lcase() { # lcase NAME REFUSE-RC|- CODE (CODE uses \n, the Omega string escape)
149 { printf 'text c "c"\ntext l "lst:t"\ntext b "%s"\n---\n' "$3"
150 [ "$2" = - ] || echo "omrefuse $2"
151 echo 'omlisting c l b'; } > "$T/$1.in"
152 doc "$1"
153 # omlang belongs to the preamble
154 sed -i 's/^begindoc$/omlang\nbegindoc/' "$T/$1.om"
155}
156lcheck() { # lcheck NAME WANT-RC WANT-LINE
157 mkdir -p "$T/$1.d"; gen "$1" check "$OMC" "$T/$1.d"
158 if [ "$rc" != "$2" ]; then bad "$1" "rc=$rc want=$2 $(cat "$T/$1.err" | head -1)"; return; fi
159 grep -qF -- "$3" "$T/$1.out" || { bad "$1" "no line: $3"; return; }
160 ok "$1"
161}
162lcase lst-compiles - 'fn main to int\nret 0\nend\n'
163lcheck lst-compiles 0 'LISTING lst:t want=0 got=0 PASS'
164lcase lst-broken - 'fn main to int\nret 0\nret 1\nend\n'
165lcheck lst-broken 113 'LISTING lst:t want=0 got=17 FAIL'
166lcase lst-refusal-ok 17 'fn main to int\nret 0\nret 1\nend\n'
167lcheck lst-refusal-ok 0 'LISTING lst:t want=17 got=17 PASS'
168lcase lst-refusal-wrong 16 'fn main to int\nret 0\nret 1\nend\n'
169lcheck lst-refusal-wrong 113 'LISTING lst:t want=16 got=17 FAIL'
170lcase lst-refusal-compiles 17 'fn main to int\nret 0\nend\n'
171lcheck lst-refusal-compiles 113 'LISTING lst:t want=17 got=0 FAIL'
172
173# ---- determinism --------------------------------------------------------------------------------
174gen esc; cp "$T/esc.out" "$T/esc.1"; gen esc
175cmp -s "$T/esc.1" "$T/esc.out" && ok determinism-tex || bad determinism-tex "two runs differ"
176if command -v pdflatex > /dev/null; then
177 ex=$here/examples/listings/paper.om
178 ( cd "$here" && SOURCE_DATE_EPOCH=1700000000 tools/build.sh "$ex" "$T/b1" ) > "$T/b1.log" 2>&1 \
179 && ( cd "$here" && SOURCE_DATE_EPOCH=1700000000 tools/build.sh "$ex" "$T/b2" ) > "$T/b2.log" 2>&1
180 if [ $? -ne 0 ]; then bad determinism-pdf "build failed: $(tail -2 "$T/b1.log" "$T/b2.log")"
181 elif cmp -s "$T/b1/paper.tex" "$T/b2/paper.tex" && cmp -s "$T/b1/paper.pdf" "$T/b2/paper.pdf"; then ok determinism-pdf
182 else bad determinism-pdf "tex or pdf differ between two builds"; fi
183else nr determinism-pdf "pdflatex not installed"; fi
184
185# ---- keyword parity with the pinned compiler ----------------------------------------------------
186kw=$(dirname "$OMC")/../omc/genkw.awk
187om2='rotr rotl unroll inline fresh abstract seal open holds layout field at little big part out ops export requires ensures invariant result old om2 ir'
188if [ -r "$kw" ]; then
189 # note is the line-comment word: it is highlighted as a comment (morecomment), not a keyword
190 sed -n 's/.*n=split("\([^"]*\)".*/\1/p' "$kw" | tr ' ' '\n' | cut -d: -f1 | grep -E '^[a-z0-9]+$' | grep -vx note > "$T/kw.pin"
191 grep -qF 'morecomment=[l]{note}' "$here/lib/omtex.om" || bad keyword-note "note is not the listings comment"
192 for w in $om2; do echo "$w"; done >> "$T/kw.pin"
193 sort -u -o "$T/kw.pin" "$T/kw.pin"
194 grep '^text zlang[2-5] ' "$here/lib/omtex.om" | sed 's/.*{\([^}]*\)}.*/\1/' | tr ',' '\n' | sort -u > "$T/kw.lib"
195 if cmp -s "$T/kw.pin" "$T/kw.lib"; then ok keyword-parity
196 else bad keyword-parity "$(diff "$T/kw.pin" "$T/kw.lib" | grep '^[<>]' | tr '\n' ' ')"; fi
197else nr keyword-parity "no genkw.awk next to $OMC"; fi
198
199# ---- pin refusal --------------------------------------------------------------------------------
200cp "$OMC" "$T/omc.other"; printf 'x' >> "$T/omc.other"
201if ( OMC=$T/omc.other; omtex_check_omc ) 2> /dev/null; then bad pin-refusal "a modified omc was accepted"
202else ok pin-refusal; fi
203
204# ---- zenodo-draft never publishes (offline: the refusal happens before any request) ------------
205printf '%%PDF-1.4\n' > "$T/z.pdf"
206ZENODO_TOKEN=offline-test sh "$here/tools/zenodo-draft.sh" newversion '1/actions/publish#' "$T/z.pdf" > /dev/null 2> "$T/z.err"
207zrc=$?
208if [ "$zrc" = 3 ] && grep -q 'never publishes' "$T/z.err"; then ok zenodo-publish-refusal
209else bad zenodo-publish-refusal "rc=$zrc $(head -1 "$T/z.err")"; fi
210
211echo "TESTS pass=$pass fail=$fail not_run=$notrun"
212[ "$fail" = 0 ]