AtlatestRepositorysigil-doc

sigil-doc / tree / testtest-example-check.sgl

1;; Hidden example checks: check: and check-file: run after the shown source
2;; in the same program, and no renderer or extracted source shows them.
3(import (sigil test) (sigil fs) (sigil string) (sigil process) (sigil list)
4 (sigil doc) (sigil doc examples) (sigil doc verify)
5 (sigil doc presentation) (sigil doc publication) (sigil doc print)
6 (sigil doc render html) (sigil doc render typst)
7 (only (sigil doc author) example code))
8
9(load "test/fixtures/example-check.sgd")
10(define fixture (eval 'document))
11(define examples (document-examples fixture))
12(define (example-named id) (find (lambda (e) (string=? (example-id e) id)) examples))
13(define loc (document-location fixture))
14(define root "test/fixtures")
15(define sigil-bin
16 (or (getenv "SIGIL_D1_COMPILER") (getenv "SIGIL_BIN")
17 (error "set SIGIL_BIN to the Sigil executable the examples run under")))
19(define (raised-message thunk)
20 (guard (e (else (exception-message e))) (thunk) #f))
22(define (verify-one doc id)
23 (let ((xs (document-examples doc)))
24 (verify-example doc xs (find (lambda (e) (string=? (example-id e) id)) xs)
25 root sigil-bin #f #t)))
27(define (present doc)
28 (map (lambda (r)
29 (let ((spec (find (lambda (e) (string=? (example-id e) (verify-result-id r)))
30 (document-examples doc))))
31 (make-verified-presentation spec r (verify-result-effective-source r))))
32 (verify-document doc root: root sigil-bin: sigil-bin no-cache: #t)))
34(test "the author commands record checks, and an example without one is unchanged"
35 (let ((inline (example-named "inline-check"))
36 (file (example-named "file-check"))
37 (plain (example-named "plain")))
38 (assert-equal 'inline (example-check-kind inline))
39 (assert-equal "(equal? greeting \"hello\")" (example-check inline))
40 (assert-equal "(define greeting \"hello\")" (example-source inline))
41 (assert-equal 'file (example-check-kind file))
42 (assert-equal "example-check/settings.check.sgl" (example-check file))
43 ;; With a check the default display is the source alone; without one
44 ;; it is unchanged.
45 (assert-equal 'source (example-display inline))
46 (assert-equal 'source+expected (example-display plain))
47 (assert-equal #f (example-check-kind plain))
48 (assert-equal #f (example-check plain))
49 ;; No check entry in the datum of an example without a check.
50 (assert-equal #f (assq 'check (cdr (document->datum plain))))
51 (assert-true (assq 'check (cdr (document->datum inline))))))
53(test "a passing inline check, a check-file and example-result all verify"
54 (let ((inline (verify-one fixture "inline-check"))
55 (file (verify-one fixture "file-check"))
56 (plain (verify-one fixture "plain")))
57 (assert-equal "#t" (verify-result-value inline))
58 (assert-equal "(define greeting \"hello\")" (verify-result-effective-source inline))
59 (assert-equal "(\"dark\" 14)" (verify-result-value file))
60 (assert-false (string-contains? (verify-result-effective-source file) "assq"))
61 (assert-equal "3" (verify-result-value plain))
62 ;; The check sees the shown program's last value as example-result.
63 (assert-equal "3" (verify-result-value (verify-one fixture "result-check")))
64 (assert-equal "(+ 1 2)" (verify-result-effective-source plain))))
66(define (failure-of . examples)
67 (raised-message
68 (lambda ()
69 (verify-document (make-document 'chapter "Failing" '() examples loc)
70 root: root sigil-bin: sigil-bin no-cache: #t))))
72(define (inline-with-check id source check expected timeout)
73 (make-example id 'inline source "sigil" #f 'bytecode 'value expected "" timeout 'source loc
74 check-kind: 'inline check: check))
76(test "a failing check fails verification and names the check"
77 (let ((message (failure-of (inline-with-check "failing" "(define greeting \"hi\")"
78 "(equal? greeting \"hello\")" "#t" 30))))
79 (assert-true (string? message))
80 (assert-true (string-contains? message "value mismatch"))
81 (assert-true (string-contains? message "check inline:failing"))))
83(test "the check decides even when the shown source alone would pass"
84 ;; The shown program's own value is #t, which matches the expectation;
85 ;; the check's #f is what is compared.
86 (let ((message (failure-of (inline-with-check "shown-passes" "(define ready #t)\nready"
87 "(not ready)" "#t" 30))))
88 (assert-true (string? message))
89 (assert-true (string-contains? message "got \"#f\""))))
91(test "a check that raises or times out fails verification"
92 (let ((raised (failure-of (inline-with-check "raises" "(define x 1)" "(error \"check broke\")" "#t" 30)))
93 (looped (failure-of (inline-with-check "loops" "(define x 1)" "(let loop () (loop))" "#t" 2))))
94 (assert-true (string-contains? raised "raised unexpected Sigil exception"))
95 (assert-true (string-contains? raised "check broke"))
96 ;; The run is killed at the timeout with no value, so the value
97 ;; comparison is the first to fail.
98 (assert-true (string? looped))
99 (assert-true (string-contains? looped "check inline:loops"))))
101(test "a failing check-file fails verification and names the file"
102 (let ((message (failure-of (make-example "failing-file" 'file "example-check/settings.sgl" #f #f 'bytecode
103 'value "#t" "" 30 'source loc
104 check-kind: 'file check: "example-check/failing.check.sgl"))))
105 (assert-true (string-contains? message "value mismatch"))
106 (assert-true (string-contains? message "check example-check/failing.check.sgl"))))
108(test "a session runs earlier examples' shown source but not their checks"
109 (let ((doubled (verify-one fixture "session-doubled")))
110 (assert-equal "20" (verify-result-value doubled))
111 (assert-true (string-contains? (verify-result-effective-source doubled) "(define base 10)"))
112 (assert-false (string-contains? (verify-result-effective-source doubled) "(= base 10)"))))
114(test "constructors and author commands refuse malformed checks"
115 (assert-error (make-example "x" 'inline "1" "sigil" #f 'bytecode 'value "1" "" 5 'source loc
116 check-kind: 'inline check: #f))
117 (assert-error (make-example "x" 'inline "1" "sigil" #f 'bytecode 'value "1" "" 5 'source loc
118 check-kind: 'file check: ""))
119 (assert-error (make-example "x" 'inline "1" "sigil" #f 'bytecode 'value "1" "" 5 'source loc
120 check-kind: 'hidden check: "1"))
121 (assert-error (make-example "x" 'inline "1" "sigil" #f 'bytecode 'value "1" "" 5 'source loc
122 check: "1"))
123 ;; The expected value would be the check's, so it can't be shown.
124 (assert-error (make-example "x" 'inline "1" "sigil" #f 'bytecode 'value "1" "" 5 'source+expected loc
125 check-kind: 'inline check: "1"))
126 (assert-error (example "x" expect-value: "1" check: "1" display: 'source+expected
127 (code "sigil" "1")))
128 (assert-error (example "x" expect-value: "1" check: "1" check-file: "a.sgl"
129 (code "sigil" "1"))))
131(test "a check-file outside the verification root is refused"
132 (let* ((escaping (make-example "escaping" 'inline "1" "sigil" #f 'bytecode
133 'value "1" "" 30 'source loc
134 check-kind: 'file check: "../escape.sgl"))
135 (doc (make-document 'chapter "Escape" '() (list escaping) loc)))
136 (assert-true (string? (raised-message
137 (lambda () (verify-document doc root: root sigil-bin: sigil-bin no-cache: #t)))))))
139(test "HTML, plain text and typst show only the shown source, not the check or its value"
140 (let* ((presentations (present fixture))
141 (publication (build-publication-index (list (make-publication-document "checks" fixture))))
142 (context (make-html-render-context publication "checks"
143 (lambda (target) (string-append "#" (publication-target-id target)))
144 presentations 2))
145 (html (document->html-fragment fixture context))
146 (plain (document->plain fixture))
147 (typst (print-publication->typst
148 (make-print-publication "Checks" #f '("David Wilson")
149 (list (make-publication-document "checks" fixture))
150 presentations))))
151 (for-each
152 (lambda (output)
153 (assert-true (string-contains? output "greeting"))
154 (assert-false (string-contains? output "equal?"))
155 (assert-false (string-contains? output "assq"))
156 (assert-false (string-contains? output "(= base 10)")))
157 (list html plain typst))
158 ;; The checks return #t and no shown example's own value is #t, so #t
159 ;; on a page would be a check's value. (Typst markup uses #text, so
160 ;; there the value is looked for as a string.)
161 (assert-false (string-contains? html "#t"))
162 (assert-false (string-contains? plain "#t"))
163 (assert-false (string-contains? typst "\"#t\""))
164 (assert-true (string-contains? html "font-size"))))