AtlatestRepositorysigil-doc

sigil-doc / tree / testsd-p8-language-probe.sgl

1;;; SD-P8 -- both shipped languages driven END TO END through the real `#lang`
2;;; path, plus the diagnostic each refusal carries at the reader.
3;;;
4;;; WHY BOTH HALVES ARE HERE, AND WHY NEITHER ALONE WOULD DO.
5;;;
6;;; The `#lang` half is the path an author takes: the compiler reads the header,
7;;; resolves `#lang a/b/c` to the module `(a b c reader)`, loads it and calls its
8;;; `read-language-source`. Driving parsed forms through runtime `eval` instead
9;;; would test the harness as much as the product -- SD-P1's headline finding
10;;; about keyword formals turned out to be exactly that instrument artifact. So
11;;; the accept/refuse verdicts below come from `load`.
12;;;
13;;; The reader half exists because of a MEASURED defect in the compiler, not out
14;;; of caution. A root-policy refusal reaching the `#lang` loader is reported as
15;;;
16;;; Error (syntax-error): read-language-source failed for #lang sigil/doc:
17;;;
18;;; with the underlying message EMPTY. The parser's diagnostic -- which names the
19;;; language and the root it wanted -- never reaches the author. Verified
20;;; pre-existing at the base SHA with a two-`@chapter` manuscript under the old
21;;; base profile, so it is not something D8 introduced; it is in the sigil
22;;; compiler's language-loading path, outside this package. It does mean that
23;;; "the refusal names what is wrong" CANNOT be asserted at the `#lang` layer
24;;; today, so each refusal is re-driven through
25;;; `parse-document-parts-with-profile` -- the same source, the same profile the
26;;; `#lang` line selects -- and the real diagnostic is printed for the gate to
27;;; assert on.
28;;;
29;;; Every case prints a line whether it passes or fails, and the completion
30;;; marker carries the count: the output length is the denominator, so a case
31;;; that silently stopped running cannot read as a case that passed.
32(import (sigil core) (sigil fs) (sigil language)
33 (sigil doc) (sigil doc reader) (sigil doc book reader))
35(define (message thunk)
36 (guard (exn (else (exception-message exn))) (thunk) "NO-RAISE"))
38(define (header-end-of source)
39 (let loop ((i 0))
40 (cond ((>= i (string-length source)) i)
41 ((char=? (string-ref source i) #\newline) (+ i 1))
42 (else (loop (+ i 1))))))
44(define (profile-for id)
45 (cond ((string=? id "sigil/doc") sigil-doc-profile)
46 ((string=? id "sigil/doc/book") sigil-doc-book-profile)
47 (else #f)))
49;; The `#lang` verdict, from `load`. `document` is bound into THIS module by a
50;; successful load, so an accepted manuscript can be asked for its kind.
51(define (lang-verdict path)
52 (let ((raised (message (lambda () (load path)))))
53 (if (string=? raised "NO-RAISE")
54 (cons 'accept (symbol->string (document-kind (eval 'document))))
55 (cons 'refuse raised))))
57;; The reader-level diagnostic for the SAME source under the SAME profile the
58;; `#lang` line names -- read out of the manuscript rather than passed in, so a
59;; case cannot be driven through a profile its header does not select.
60(define (reader-diagnostic path)
61 (let* ((source (read-file-string path))
62 (id (language-header-id (inspect-language-source source path)))
63 (profile (profile-for id)))
64 (if (not profile) (string-append "NO-PROFILE-FOR " id)
65 (message (lambda () (parse-document-parts-with-profile
66 profile source path (header-end-of source)))))))
68(define cases
69 (list
70 ;; name path want want-kind
71 (list "base-accepts-article" "test/fixtures/sd-p1-article-root.sgd" 'accept "article")
72 (list "base-refuses-chapter" "test/fixtures/sd-p8-base-chapter-root.sgd" 'refuse "")
73 (list "base-refuses-two" "test/fixtures/sd-p8-base-two-articles.sgd" 'refuse "")
74 (list "book-accepts-chapter" "test/fixtures/sd-p8-book-chapter-root.sgd" 'accept "chapter")
75 (list "book-refuses-article" "test/fixtures/sd-p8-book-article-root.sgd" 'refuse "")
76 (list "book-refuses-two" "test/fixtures/sd-p8-book-two-chapters.sgd" 'refuse "")))
78(define ok 0)
79(define bad 0)
81(for-each
82 (lambda (c)
83 (let* ((name (car c)) (path (cadr c)) (want (caddr c)) (want-kind (cadddr c))
84 (verdict (lang-verdict path))
85 (got (car verdict))
86 (detail (cdr verdict))
87 (matched (and (eq? got want)
88 (or (not (eq? want 'accept)) (string=? detail want-kind)))))
89 (if matched (set! ok (+ ok 1)) (set! bad (+ bad 1)))
90 (display "SD_P8_LANG ") (display name)
91 (display " want=") (display want)
92 (display " got=") (display got)
93 (display (if matched " MATCH" " MISMATCH"))
94 (display " kind-or-error=") (write detail)
95 (newline)
96 ;; The reader-level diagnostic, printed for EVERY case rather than only
97 ;; the refusals: an accepted manuscript must produce `NO-RAISE` here, and
98 ;; that is the positive control that stops the refusal assertions from
99 ;; being satisfied by a reader that refuses everything.
100 (display "SD_P8_DIAG ") (display name)
101 (display " :: ") (write (reader-diagnostic path))
102 (newline)))
103 cases)
105(display "SD_P8_LANGUAGE_PROBE_COMPLETE cases ")
106(display (length cases))
107(display " matched ") (display ok)
108(display " mismatched ") (display bad)
109(newline)