AtlatestRepositorysigil-doc

sigil-doc / tree / testtest-root-profile.sgl

1;;; SD-P1 -- the root container as a reader/profile parameter (review B2).
2;;;
3;;; Two properties this suite is built around, both learned the expensive way
4;;; on this track:
5;;;
6;;; * EVERY location assertion recomputes its expected byte offset, line and
7;;; column INDEPENDENTLY, walking the manuscript character by character.
8;;; Asking the parser to confirm its own arithmetic passes under any
9;;; indexing bug.
10;;; * The span arm asserts its DISCRIMINATING PRECONDITION -- that byte and
11;;; scalar offsets genuinely disagree at the point it probes -- rather than
12;;; believing it about the fixture. SD-P4's first state test probed a point
13;;; where its two manuscripts were both still ASCII and would have passed
14;;; vacuously.
15;;;
16;;; Everything here is imported from `(sigil doc reader)` alone. That is
17;;; deliberate: it demonstrates that a delegating language such as
18;;; `#lang folio/doc` needs no private module to parameterize the root
19;;; container. The three-argument `parse-document-source` / `parse-document-forms`
20;;; spellings are unchanged and are covered by `test/test-reader.sgl`, which
21;;; this phase does not touch.
22(import (sigil test) (sigil fs) (sigil syntax)
23 (sigil doc) (sigil doc author) (sigil doc private author-runtime)
24 (sigil doc reader)
25 ;; ONLY the profile. `(sigil doc book reader)` also exports
26 ;; `read-language-source`, and an unqualified import silently shadows
27 ;; the BASE language's reader -- which this suite calls by that name.
28 ;; Cost of learning that: one arm that reported a `sigil/doc/book:`
29 ;; diagnostic while claiming to exercise `#lang sigil/doc`.
30 (only (sigil doc book reader) sigil-doc-book-profile))
32;; ---------------------------------------------------------------- helpers
34(define (utf8-width c)
35 (let ((n (char->integer c)))
36 (cond ((<= n #x7f) 1) ((<= n #x7ff) 2) ((<= n #xffff) 3) (else 4))))
38;; Independent UTF-8 byte offset of scalar index `index`.
39(define (byte-offset source index)
40 (let loop ((i 0) (bytes 0))
41 (if (= i index) bytes (loop (+ i 1) (+ bytes (utf8-width (string-ref source i)))))))
43;; Independent scalar line/column at scalar index `index`.
44(define (cursor source index)
45 (let loop ((i 0) (line 1) (column 1))
46 (if (= i index) (list line column)
47 (if (char=? (string-ref source i) #\newline)
48 (loop (+ i 1) (+ line 1) 1)
49 (loop (+ i 1) line (+ column 1))))))
51;; Scalar index of the single occurrence of `needle`. Raises when the needle is
52;; absent OR repeated: a fixture edit that made an anchor ambiguous would
53;; otherwise silently move the assertion to a different node.
54(define (index-of source needle)
55 (let* ((n (string-length source)) (m (string-length needle)))
56 (let loop ((i 0) (found #f))
57 (cond
58 ((> (+ i m) n)
59 (if found found (error (string-append "anchor not found: " needle))))
60 ((string=? (substring source i (+ i m)) needle)
61 (if found (error (string-append "anchor is ambiguous: " needle))
62 (loop (+ i 1) i)))
63 (else (loop (+ i 1) found))))))
65(define (contains? haystack needle)
66 (let ((n (string-length haystack)) (m (string-length needle)))
67 (let loop ((i 0))
68 (cond ((> (+ i m) n) #f)
69 ((string=? (substring haystack i (+ i m)) needle) #t)
70 (else (loop (+ i 1)))))))
72(define (message thunk)
73 (guard (exn (else (exception-message exn))) (thunk) "NO ERROR"))
75(define (source-of path) (read-file-string path))
77;; The manuscript origin of a lowered root. Generated syntax is located at
78;; `<sigil/doc-generated>` and carries the authoritative manuscript srcloc as
79;; its origin; that origin is what a consumer needs and what this suite pins.
80(define (root-origin root) (srcloc-origin (syntax-srcloc root)))
82(define folio-profile (make-document-profile "folio/doc" '(note project) 'one-or-more))
83(define folio-one-profile (make-document-profile "folio/doc" '(note project) 'one))
84;; The byte offset just past the `#lang` line. Derived from the source rather
85;; than written as 16 or 21: SD-P8 gave this suite manuscripts in two languages
86;; whose header lines differ in length, and a hardcoded offset would silently
87;; slice into the wrong place for one of them.
88(define (header-end-of source)
89 (let loop ((i 0))
90 (cond ((>= i (string-length source)) i)
91 ((char=? (string-ref source i) #\newline) (+ i 1))
92 (else (loop (+ i 1))))))
94(define folio-path "test/fixtures/sd-p1-folio-roots.sgd")
95(define folio-source (source-of folio-path))
97;; Roots checked across the suite, printed so the gate can assert the walk was
98;; not empty. A span arm that examined nothing must never read as a pass.
99(define roots-checked 0)
101;; Assert one root's whole location against independently recomputed truth, and
102;; that its span names exactly the slice it claims.
103(define (check-root root source open-anchor close-anchor name)
104 (let* ((start (index-of source open-anchor))
105 (end (+ (index-of source close-anchor) (string-length close-anchor)))
106 (start-cursor (cursor source start))
107 (loc (root-origin root)))
108 (set! roots-checked (+ roots-checked 1))
109 ;; `name` is not asserted from the lowered syntax shape here. A root lowers
110 ;; to `(author-at <loc> (lambda () (author-normalize (<name> ...) ...)))`,
111 ;; so digging the name out means walking four levels of generated syntax
112 ;; that this phase does not own. The identity assertions below are the
113 ;; source SLICE -- which must literally open with this root's own `@name[`
114 ;; and close with its own terminator -- and the arm that evaluates an
115 ;; `@article` root to a document of kind `article`, which proves dispatch by
116 ;; name end to end.
117 name
118 (assert-equal (car start-cursor) (srcloc-line loc))
119 (assert-equal (cadr start-cursor) (srcloc-column loc))
120 (assert-equal (byte-offset source start) (srcloc-position loc))
121 (assert-equal (- (byte-offset source end) (byte-offset source start))
122 (srcloc-span loc))
123 ;; The slice the span names must actually be this root's surface syntax.
124 (assert-equal open-anchor (substring source start (+ start (string-length open-anchor))))
125 (assert-equal close-anchor (substring source (- end (string-length close-anchor)) end))
126 (list start end)))
128;; ---------------------------------------------------------------- suite
130(test "a profile with a different root set parses, with exact locations"
131 (let* ((parts (parse-document-parts-with-profile
132 folio-profile folio-source folio-path 16))
133 (forms (car parts))
134 (roots (cadr parts)))
135 (assert-equal '() forms)
136 (assert-equal 2 (length roots))
137 (let* ((first-span (check-root (car roots) folio-source
138 "@note[\"topics/alpha\"" "@end note\n" 'note))
139 (second-span (check-root (cadr roots) folio-source
140 "@project[\"projects/beta\"" "@end project\n" 'project))
141 (second-start (car second-span)))
142 ;; THE DISCRIMINATING PRECONDITION. Multi-byte prose sits inside the first
143 ;; root, so at the second root's start the byte offset and the scalar
144 ;; index must genuinely differ. Without this the byte assertions above
145 ;; would hold just as well under a parser that returned scalars.
146 (assert-true (not (= (byte-offset folio-source second-start) second-start)))
147 ;; ...and the roots are returned in manuscript order, not scan-stack order.
148 (assert-true (< (car first-span) second-start)))))
150(test "the root allowlist rejects a root it does not name, in both directions"
151 ;; The CLASS, not a spelling: a root outside the set is refused whichever
152 ;; profile is asked, so a parser that ignored the allowlist reddens here
153 ;; twice rather than once.
154 ;;
155 ;; SD-P8 made this arm three-way rather than two-way. Since D8 the base and
156 ;; the book genre name DIFFERENT roots, so `@chapter` is now a root some
157 ;; sigil-doc profile rejects -- which is the case the two-profile version
158 ;; could not express.
159 (let ((article-source "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{body}}")
160 (chapter-source "#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{body}}"))
161 ;; A folio profile must NOT accept sigil/doc's root.
162 (assert-equal
163 "folio/doc: top-level author forms must precede at least one @note or @project"
164 (message (lambda () (parse-document-parts-with-profile
165 folio-profile article-source "c.sgd" 16))))
166 ;; ...and sigil/doc's profile must NOT accept a folio root.
167 (assert-equal
168 "sigil/doc: top-level author forms must precede exactly one @article"
169 (message (lambda () (parse-document-parts-with-profile
170 sigil-doc-profile folio-source folio-path 16))))
171 ;; ...nor the genre language's root, which is the direction D8 created.
172 (assert-equal
173 "sigil/doc: top-level author forms must precede exactly one @article"
174 (message (lambda () (parse-document-parts-with-profile
175 sigil-doc-profile chapter-source "c.sgd" 21))))
176 ;; ...and the genre language refuses the BASE's root, symmetrically.
177 (assert-equal
178 "sigil/doc/book: top-level author forms must precede exactly one @chapter"
179 (message (lambda () (parse-document-parts-with-profile
180 sigil-doc-book-profile article-source "a.sgd" 16))))
181 ;; Each profile still accepts the root it DOES name -- otherwise a parser
182 ;; that rejected everything would satisfy every assertion above.
183 (let ((parts (parse-document-parts-with-profile
184 sigil-doc-profile article-source "c.sgd" 16)))
185 (assert-equal 1 (length (cadr parts))))
186 (let ((parts (parse-document-parts-with-profile
187 sigil-doc-book-profile chapter-source "c.sgd" 21)))
188 (assert-equal 1 (length (cadr parts))))))
190(test "multiplicity is a parameter, and is enforced in both directions"
191 (let ((single "#lang sigil/doc\n@note[\"topics/only\"]{@p{one root}}\n")
192 (two-articles
193 "#lang sigil/doc\n@article[\"A\" \"A\"]{@p{x}}\n@article[\"B\" \"B\"]{@p{y}}\n")
194 (two-chapters
195 "#lang sigil/doc/book\n@chapter[\"A\" \"A\"]{@p{x}}\n@chapter[\"B\" \"B\"]{@p{y}}\n"))
196 ;; one-or-more accepts one AND several.
197 (assert-equal 1 (length (cadr (parse-document-parts-with-profile
198 folio-profile single "s.sgd" 16))))
199 (assert-equal 2 (length (cadr (parse-document-parts-with-profile
200 folio-profile folio-source folio-path 16))))
201 ;; The same root set under `one` refuses the second root, and says so.
202 (assert-equal
203 "folio/doc: top-level author forms must precede exactly one @note or @project"
204 (message (lambda () (parse-document-parts-with-profile
205 folio-one-profile folio-source folio-path 16))))
206 ;; ...and still accepts a single-root manuscript, so the arm above is not
207 ;; passing because `one` rejects everything.
208 (assert-equal 1 (length (cadr (parse-document-parts-with-profile
209 folio-one-profile single "s.sgd" 16))))
210 ;; sigil/doc's own multiplicity is unchanged by SD-P8; only its root moved.
211 (assert-equal
212 "sigil/doc: top-level author forms must precede exactly one @article"
213 (message (lambda () (parse-document-parts-with-profile
214 sigil-doc-profile two-articles "t.sgd" 16))))
215 ;; ...and the genre language carries the same `one`, which is what makes a
216 ;; two-chapter manuscript a diagnostic rather than a dropped root.
217 (assert-equal
218 "sigil/doc/book: top-level author forms must precede exactly one @chapter"
219 (message (lambda () (parse-document-parts-with-profile
220 sigil-doc-book-profile two-chapters "t.sgd" 21))))))
222(test "the base profile is D8's policy and D8's diagnostics, exactly"
223 ;; THE PINNED LITERALS. These are the two strings `#lang sigil/doc` raises,
224 ;; and they are DERIVED from `sigil-doc-profile` rather than written out in
225 ;; the parser, so this arm is what stops the derivation from quietly
226 ;; rewording a shipped diagnostic.
227 ;;
228 ;; SD-P8 CHANGED THEM, from `@chapter` to `@article`, and that is exactly the
229 ;; migration shape this estate distrusts: updating a pinned literal to match
230 ;; the new output proves nothing on its own. Each literal below was therefore
231 ;; shown still able to FAIL after the migration -- see
232 ;; `test/sabotage-sd-p8.sh` legs 1-3, which perturb the PRODUCT (the profile
233 ;; and the derivation) rather than the assertion and require this suite to
234 ;; redden at these arms by name.
235 (assert-equal "sigil/doc" (document-profile-name sigil-doc-profile))
236 (assert-equal '(article) (document-profile-roots sigil-doc-profile))
237 (assert-equal 'one (document-profile-multiplicity sigil-doc-profile))
238 (assert-equal
239 "sigil/doc: source must contain exactly one @article"
240 (message (lambda () (parse-document-parts-with-profile
241 sigil-doc-profile "#lang sigil/doc\n@(define x 1)\n" "n.sgd" 16))))
242 (assert-equal
243 "sigil/doc: source must contain exactly one @article"
244 (message (lambda () (parse-document-parts-with-profile
245 sigil-doc-profile "#lang sigil/doc\n" "e.sgd" 16))))
246 (assert-equal
247 "sigil/doc: top-level author forms must precede exactly one @article"
248 (message (lambda () (parse-document-parts-with-profile
249 sigil-doc-profile
250 "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}\n@(define y 2)\n"
251 "a.sgd" 16))))
252 ;; Three-or-more root sets get an Oxford-comma phrase; pinned so the
253 ;; formatter is covered beyond the one- and two-root cases. The names here
254 ;; are ARBITRARY command symbols, deliberately not the model's kinds -- root
255 ;; commands are the profile's vocabulary and document kinds are the model's,
256 ;; and D8 is the ruling that separated them.
257 (assert-equal
258 "sigil/doc: source must contain exactly one @article, @memo, or @chapter"
259 (message (lambda () (parse-document-parts-with-profile
260 (make-document-profile "sigil/doc" '(article memo chapter) 'one)
261 "#lang sigil/doc\n" "o.sgd" 16)))))
263(test "the book genre language is a sibling profile with its own diagnostics"
264 ;; SD-P8. `#lang sigil/doc/book` is the D8 home of the book/print genre. It
265 ;; is a PROFILE, not a second parser: everything below the root -- the
266 ;; grammar, the locations, the author vocabulary -- is `#lang sigil/doc`'s,
267 ;; unchanged and undelegated-around.
268 ;;
269 ;; Its two literals are pinned for the same reason the base's are, and are
270 ;; shown able to fail by the same sabotage legs. The `sigil/doc/book:` prefix
271 ;; is the load-bearing part: a genre language that reported `sigil/doc:`
272 ;; would send an author to the wrong language's documentation.
273 (assert-equal "sigil/doc/book" (document-profile-name sigil-doc-book-profile))
274 (assert-equal '(chapter) (document-profile-roots sigil-doc-book-profile))
275 (assert-equal 'one (document-profile-multiplicity sigil-doc-book-profile))
276 (assert-equal
277 "sigil/doc/book: source must contain exactly one @chapter"
278 (message (lambda () (parse-document-parts-with-profile
279 sigil-doc-book-profile "#lang sigil/doc/book\n" "e.sgd" 21))))
280 (assert-equal
281 "sigil/doc/book: source must contain exactly one @chapter"
282 (message (lambda () (parse-document-parts-with-profile
283 sigil-doc-book-profile
284 "#lang sigil/doc/book\n@(define x 1)\n" "n.sgd" 21))))
285 (assert-equal
286 "sigil/doc/book: top-level author forms must precede exactly one @chapter"
287 (message (lambda () (parse-document-parts-with-profile
288 sigil-doc-book-profile
289 "#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{b}}\n@(define y 2)\n"
290 "b.sgd" 21))))
291 ;; POSITIVE CONTROL. Every assertion above is a refusal, and a profile that
292 ;; refused everything would satisfy all three. This one must be accepted.
293 (let ((parts (parse-document-parts-with-profile
294 sigil-doc-book-profile
295 "#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{b}}\n" "g.sgd" 21)))
296 (assert-equal '() (car parts))
297 (assert-equal 1 (length (cadr parts)))))
299(test "the model's kinds are reachable from source, and `book` is gone"
300 ;; B2's other half, rewritten by SD-P8.
301 ;;
302 ;; BEFORE D8 this arm had to build a `container-profile` naming `@article`
303 ;; and `@book`, because neither kind was reachable from any shipped language.
304 ;; `article` no longer needs one: it is what `#lang sigil/doc` itself admits,
305 ;; so the arm now drives the DEFAULT profile and the coverage got stronger
306 ;; rather than weaker. `chapter` is reachable through the genre profile.
307 ;;
308 ;; `book` is gone from the model entirely (D8 item 4): it was constructed
309 ;; nowhere in this package or in folio, and a book is an ASSEMBLY built by
310 ;; `make-print-publication`, not a document kind. Its old arm is replaced by
311 ;; the refusal below, which is a stronger statement than the construction it
312 ;; replaces -- it says the kind cannot come back by accident.
313 (define (evaluate profile source name)
314 (let loop ((forms (parse-document-forms-with-profile profile source name
315 (header-end-of source)))
316 (last #f))
317 (if (null? forms) last (loop (cdr forms) (eval (car forms))))))
318 ;; `@article` through the SHIPPED base profile, from the fixture on disk.
319 (let ((article (evaluate sigil-doc-profile
320 (source-of "test/fixtures/sd-p1-article-root.sgd")
321 "test/fixtures/sd-p1-article-root.sgd")))
322 (assert-true (document? article))
323 (assert-equal 'article (document-kind article))
324 (assert-equal "An Article Root" (document-title article)))
325 ;; `@chapter` through the SHIPPED genre profile. This is also the positive
326 ;; control on the arm: a failure above is then about the root set rather than
327 ;; about evaluation being broken.
328 (let ((chapter (evaluate sigil-doc-book-profile
329 "#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{body}}" "c.sgd")))
330 (assert-true (document? chapter))
331 (assert-equal 'chapter (document-kind chapter)))
332 ;; THE DROPPED KIND. `make-document` must refuse `book` by name, and must
333 ;; still accept the two that survive -- otherwise a constructor that refused
334 ;; everything would satisfy the first assertion.
335 (let ((loc (make-srcloc "k.sgd" 1 1 0 1)))
336 (assert-error (make-document 'book "A Book" (list "A")
337 (list (make-paragraph (list (make-text "b" loc)) loc)) loc))
338 (assert-true (contains?
339 (message (lambda () (make-document 'book "A Book" (list "A")
340 (list (make-paragraph (list (make-text "b" loc)) loc))
341 loc)))
342 "kind must be article or chapter"))
343 (assert-equal 'article (document-kind
344 (make-document 'article "An Article" (list "A")
345 (list (make-paragraph (list (make-text "b" loc)) loc)) loc)))
346 (assert-equal 'chapter (document-kind
347 (make-document 'chapter "A Chapter" (list "A")
348 (list (make-paragraph (list (make-text "b" loc)) loc)) loc)))))
350;; ---------------------------------------------------------------- t-3aecae
352(test "a manuscript-local define with keyword and rest parameters binds under #lang"
353 ;; WHAT THIS ARM CAN AND CANNOT SHOW. It gates like any other arm -- it goes
354 ;; red when the behaviour breaks. What it cannot do is demonstrate
355 ;; red-before / green-after for the ticket that produced it, because it was
356 ;; never red: the behaviour it pins has always worked on the `#lang` path. It
357 ;; exists because a comment in this very file asserted the opposite (added
358 ;; 2026-08-10 in `acb9adf`, corrected here 2026-08-11) and a ticket was filed
359 ;; against the wrong component before anyone contradicted it. Its job is to
360 ;; make that false claim impossible to restate: if anyone "fixes" the parser or
361 ;; the reader on the belief that this spelling does not bind in a manuscript,
362 ;; this goes red.
363 ;;
364 ;; It CAN fail, which is worth demonstrating rather than asserting.
365 ;; Sabotage-verified 2026-08-11 against master `7066ffc`: mutating the
366 ;; fixture's `kind` default from "note" to "info" took this suite from 12
367 ;; passed to 11 passed / 1 failed with
368 ;; expected "note: Default" but got "info: Default"
369 ;; and the fixture was then restored. Do the same before trusting any edit
370 ;; here. Also verified load-bearing: removing `(rest: body)`, replacing it with
371 ;; a dotted rest, or switching `(keys: …)` to `(opts: …)` each turn this red.
372 ;;
373 ;; The real defect t-3aecae turned out to name lives in sigil core, not in
374 ;; sigil/doc, and is reachable from the REPL and nREPL rather than from a
375 ;; manuscript -- see folio `investigations/eval-skips-define-macro-expansion`.
376 ;;
377 ;; The manuscript is brought in with `load`, NOT through the `evaluate` helper
378 ;; above, and that is the point: `load` runs the whole file through the compile
379 ;; path -- language reader, then the top-level macro-expansion pass -- which is
380 ;; the path an author's `#lang sigil/doc` manuscript actually takes, and the
381 ;; pass runtime `eval` never runs. Note `load` does NOT create a module: it
382 ;; evaluates into the CALLER's module, so the manuscript's `callout` and
383 ;; `document` land at this file's top level, which is why `document` is
384 ;; reachable below. The load is inside the arm rather than at file scope so a
385 ;; fixture problem fails THIS arm instead of the whole file.
386 (load "test/fixtures/sd-manuscript-keyword-define.sgd")
387 (let ((doc (eval 'document)))
388 (assert-true (document? doc))
389 (assert-equal 'chapter (document-kind doc))
390 (assert-equal "Keyword Define" (document-title doc))
391 ;; Shape is asserted BEFORE anything indexes into it, so a wrong shape
392 ;; reports as a failed length assertion rather than as a `car` type error.
393 (let ((sections (document-children doc)))
394 (assert-equal 1 (length sections))
395 (let ((callouts (section-children (car sections))))
396 (assert-equal 2 (length callouts))
397 ;; Callout 1: the keyword argument was passed EXPLICITLY (`kind: "WARN"`),
398 ;; and the `(rest: body)` parameter captured the `@p{tail}` child. Both
399 ;; halves of the spelling under test, in one node.
400 (let ((explicit (car callouts)))
401 (assert-true (admonition? explicit))
402 (assert-equal 'tip (admonition-kind explicit))
403 (assert-equal "WARN: Explicit" (text-content (car (admonition-title explicit))))
404 (assert-equal 1 (length (admonition-children explicit)))
405 (assert-equal "tail" (text-content
406 (car (paragraph-children
407 (car (admonition-children explicit)))))))
408 ;; Callout 2: the SAME command with no `kind:` argument took the parameter
409 ;; list's default. Without the keyword machinery there is no default to
410 ;; take, so this is what distinguishes "keyword params work" from "the
411 ;; command happened to be callable". Guarded identically to callout 1.
412 (let ((defaulted (cadr callouts)))
413 (assert-true (admonition? defaulted))
414 (assert-equal 'tip (admonition-kind defaulted))
415 (assert-equal "note: Default" (text-content (car (admonition-title defaulted))))
416 (assert-equal 1 (length (admonition-children defaulted)))
417 (assert-equal "plain" (text-content
418 (car (paragraph-children
419 (car (admonition-children defaulted)))))))))))
421(test "a profile is validated where it is constructed"
422 ;; A profile admitting no root would reject every document with a message
423 ;; naming no command -- an always-red gate that looks like a manuscript
424 ;; error. Every one of these must fail at construction, not at parse time.
425 (assert-error (make-document-profile "sigil/doc" '() 'one))
426 (assert-error (make-document-profile "sigil/doc" '(chapter "note") 'one))
427 (assert-error (make-document-profile "sigil/doc" '(chapter chapter) 'one))
428 (assert-error (make-document-profile "sigil/doc" '(chapter) 'zero-or-more))
429 (assert-error (make-document-profile "sigil/doc" '(chapter) 'many))
430 (assert-error (make-document-profile "" '(chapter) 'one))
431 (assert-error (make-document-profile 'sigil-doc '(chapter) 'one))
432 (assert-error (make-document-profile "sigil/doc" 'chapter 'one))
433 ;; ...and a valid one is accepted, so the arm is not passing because the
434 ;; constructor rejects everything.
435 (assert-true (document-profile? (make-document-profile "x/doc" '(a b) 'one-or-more)))
436 ;; A non-profile must not be accepted as one by the parse entry points.
437 (assert-error (parse-document-parts-with-profile
438 'not-a-profile "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}" "x.sgd" 16)))
440(test "a profile does not leak the aggregates it owns"
441 ;; The ownership rule the semantic model uses, applied to configuration: a
442 ;; caller must not be able to widen a profile's root set after construction,
443 ;; from either side of it.
444 (let* ((roots (list 'note 'project))
445 (profile (make-document-profile "folio/doc" roots 'one-or-more)))
446 (set-car! roots 'chapter)
447 (assert-equal '(note project) (document-profile-roots profile))
448 (let ((handed-back (document-profile-roots profile)))
449 (set-car! handed-back 'chapter)
450 (assert-equal '(note project) (document-profile-roots profile)))))
452(test "@text and @expr are dispatched as author commands, not pseudo-kinds"
453 ;; The lowering half of SD-P4's recorded collision. The parser's internal
454 ;; prose and author-expression nodes have five slots; a COMMAND has six. Every
455 ;; classification below is by slot count, so an author's `@text{}`/`@expr{}`
456 ;; is never mistaken for one of the parser's own nodes -- which matters most
457 ;; at top level, where this phase's root allowlist is itself a head-symbol
458 ;; test. Measured before/after: test/sd-p1-classification-probe.sgl.
459 ;; `sigil-doc-profile` explicitly rather than the three-argument
460 ;; `parse-document-forms`: this suite imports only `(sigil doc reader)`, to
461 ;; prove a delegating language needs no private module. The three-argument
462 ;; spellings are unchanged and are covered by `test/test-reader.sgl`.
463 (define (evaluate source name)
464 (let loop ((forms (parse-document-forms-with-profile sigil-doc-profile source name 16))
465 (last #f))
466 (if (null? forms) last (loop (cdr forms) (eval (car forms))))))
467 ;; A manuscript that defines `expr` gets ITS command. Before this phase the
468 ;; lowering path spliced the node's raw internal child list into generated
469 ;; syntax instead of calling anything.
470 (let* ((source (string-append
471 "#lang sigil/doc\n"
472 "@(define (expr . body) (apply em body))\n"
473 "@article[\"Collision\" \"A\"]{@p{before @expr{inside} after}}"))
474 (children (paragraph-children (car (document-children (evaluate source "x.sgd"))))))
475 (assert-equal 3 (length children))
476 (assert-true (emphasis? (cadr children)))
477 (assert-equal "inside" (text-content (car (emphasis-children (cadr children))))))
478 ;; `@text{}` at top level is a COMMAND, so it reaches the root policy and is
479 ;; refused by name. Before this phase it reached the empty-prose filter, where
480 ;; `caddr` of a command node is its child LIST and `trim` was handed a list.
481 (assert-equal
482 "sigil/doc: top-level author forms must precede exactly one @article"
483 (message (lambda () (parse-document-parts-with-profile
484 sigil-doc-profile "#lang sigil/doc\n@text{a text command}\n"
485 "t.sgd" 16))))
486 ;; `trim-body-edges` is the subtle site: it does not merely misread a
487 ;; `@text{}` command, it REBUILDS it as a five-slot prose node carrying the
488 ;; folded source slice, so everything downstream then behaves correctly on the
489 ;; wrong node. At a body edge the command must survive as a command.
490 (let ((message-at-edge
491 (message (lambda () (evaluate
492 "#lang sigil/doc\n@article[\"E\" \"A\"]{\n@text{at the edge}\n}"
493 "e.sgd")))))
494 (assert-true (not (contains? message-at-edge "NO ERROR")))
495 ;; Dispatched to the author `text` command, which wants a string and was
496 ;; handed a node -- NOT silently rendered as a paragraph of literal source.
497 (assert-true (contains? message-at-edge "make-text")))
498 ;; `inline-code-content` concatenates `caddr` of each child; for a command
499 ;; that is the child LIST, so a head-symbol test hands `string-append` a list.
500 (assert-true
501 (contains? (message (lambda () (evaluate
502 "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{@code{@text{x}}}}"
503 "c.sgd")))
504 "inline @code content must be text and document escapes"))
505 ;; POSITIVE CONTROLS -- the genuine five-slot pseudo-nodes must be unaffected.
506 ;; Bare prose is still folded into paragraphs...
507 (let ((doc (evaluate "#lang sigil/doc\n@article[\"P\" \"A\"]{\nbare prose\n}" "p.sgd")))
508 (assert-true (paragraph? (car (document-children doc))))
509 (assert-equal "bare prose" (text-content (car (paragraph-children (car (document-children doc)))))))
510 ;; ...and a real top-level `@(...)` is still hoisted as an author form.
511 (let ((parts (parse-document-parts-with-profile
512 sigil-doc-profile
513 "#lang sigil/doc\n@(define z 1)\n@article[\"Z\" \"A\"]{@p{b}}" "z.sgd" 16)))
514 (assert-equal 1 (length (car parts)))
515 (assert-equal 1 (length (cadr parts)))))
517(test "the delegating reader emits a usable form sequence for both multiplicities"
518 (define (context name) (list (cons source-name: name) (cons header-end-offset: 16)))
519 ;; `one` produces exactly the shape `#lang sigil/doc` has always produced:
520 ;; prelude import, author forms, `(define document <root>)`, `document`.
521 ;; NOTE ON SHAPE: each emitted FORM is a syntax object, but its datum is an
522 ;; ordinary list whose head is a bare symbol -- `(define document <root>)`,
523 ;; where only `<root>` is itself syntax. So `syntax-datum` applies to the form,
524 ;; and plain list accessors apply inside it.
525 (let* ((forms (read-language-source
526 "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}" (context "c.sgd")))
527 (tail (syntax-datum (car (reverse forms))))
528 (definition (syntax-datum (cadr (reverse forms)))))
529 (assert-equal 3 (length forms))
530 (assert-equal 'document tail)
531 (assert-equal 'define (car definition))
532 (assert-equal 'document (cadr definition)))
533 ;; `one-or-more` binds the list instead, in manuscript order.
534 (let* ((forms (read-language-source-with-profile
535 folio-profile folio-source (context folio-path)))
536 (tail (syntax-datum (car (reverse forms))))
537 (definition (syntax-datum (cadr (reverse forms)))))
538 (assert-equal 'document-roots tail)
539 (assert-equal 'define (car definition))
540 (assert-equal 'document-roots (cadr definition))
541 ;; `(list <root> <root>)` -- the `list` head plus two roots, in order.
542 (assert-equal 'list (car (caddr definition)))
543 (assert-equal 3 (length (caddr definition)))))
545(test "a root-policy error still precedes any lowering error"
546 ;; The pre-SD-P1 loop decided "is this the last node?" BEFORE lowering the
547 ;; chapter, so a policy failure always won over a failure inside the root's
548 ;; body. A single-pass rewrite that lowers each root as it meets it inverts
549 ;; that silently, and only a doubly-malformed manuscript can tell. This pins
550 ;; the precedence so the two-pass structure cannot be "simplified" away.
551 (let* ((bad-code "@code[\"a\" \"b\"]{x}")
552 (both (string-append "#lang sigil/doc\n@article[\"C\" \"A\"]{\n"
553 bad-code "\n}\ntrailing prose\n"))
554 (only-bad-code (string-append "#lang sigil/doc\n@article[\"C\" \"A\"]{\n"
555 bad-code "\n}\n"))
556 (both-message (message (lambda () (parse-document-parts-with-profile
557 sigil-doc-profile both "b.sgd" 16))))
558 (code-message (message (lambda () (parse-document-parts-with-profile
559 sigil-doc-profile only-bad-code "o.sgd" 16)))))
560 ;; POSITIVE CONTROL FIRST: the body really is malformed, and lowering really
561 ;; does report it when nothing else is wrong. Without this, the assertion
562 ;; below would hold just as well over a body that lowers cleanly.
563 (assert-true (contains? code-message "invalid arguments for @code"))
564 ;; ...and with a policy violation ALSO present, the policy error is the one
565 ;; reported -- the manuscript is never partially lowered.
566 (assert-equal
567 "sigil/doc: top-level author forms must precede exactly one @article"
568 both-message)
569 (assert-true (not (contains? both-message "invalid arguments for @code")))))
571(test "the suite checked a non-empty set of roots"
572 ;; A denominator, printed into the gate transcript. Every location assertion
573 ;; above lives inside `check-root`; if that walk ever collapsed to nothing,
574 ;; the arms would pass in silence.
575 (display "SD_P1_ROOTS_CHECKED ") (display roots-checked) (newline)
576 (assert-true (>= roots-checked 2)))