AtlatestRepositorysigil-doc

sigil-doc / tree / testtest-model.sgl

1(import (sigil test) (sigil syntax) (sigil doc) (fixtures practical-chapter))
2
3(test "pressure chapter model and outputs"
4 (assert-equal 'chapter (document-kind practical-chapter))
5 (assert-true (> (string-length (document->plain practical-chapter)) 500))
6 (assert-equal (document->debug practical-chapter) (document->string practical-chapter))
7 (assert-equal '((anchor "first-script")
8 (text "A Useful First Script")
9 (text "This is practical Scheme—small\nfunctions composed without ceremony.")
10 (text "The important\nidea is to shape the problem as a pipeline")
11 (text ": obtain text, split it into lines,\nselect what matters, and render the answer.")
12 (text "(import (sigil core) (sigil fs))\n(define lines (string-split (read-file-string \"tasks.txt\") \"\\n\"))\n(define todos (filter (lambda (line) (string-prefix? \"TODO \" line)) lines))\n(display (string-append \"Open tasks: \" (number->string (length todos))))\n(newline)\n(for-each (lambda (line) (display (string-append \"- \" line \"\\n\"))) todos)")
13 (text "run the script on a deliberately messy file")
14 (text "Unicode such\nas καλημέρα")
15 (text ", and a line that merely contains the word TODO.")
16 (anchor "change-the-question") (text "Change the Question")
17 (text "Once a small program works, its value comes from how readily it can answer the\nnext question.")
18 (text "add a command-line word that selects TODO or DONE")
19 (text "Then add three small\nexamples that cover an empty list, mixed tasks, and non-ASCII task text.")
20 (text "Return to ") (reference "first-script") (text "the first script")
21 (text " when the revised version starts to feel complicated."))
22 (document-events practical-chapter)))
24(test "canonical serialization begins with semantic tag"
25 (assert-equal 'document (car (document->datum practical-chapter))))
27(test "constructors reject invalid semantic values"
28 (let ((loc (locate-text "A Useful First Script" 0)))
29 (assert-error (make-document 'page "x" '("a") (document-children practical-chapter) loc))
30 (assert-error (make-text "" loc))
31 (assert-error (make-anchor "Bad_ID" loc))
32 (assert-error (make-paragraph (list (make-anchor "wrong-category" loc)) loc))
33 (assert-error (make-section "invalid" '() (document-children practical-chapter) loc))))
35(test "aggregate ownership is mutation-proof"
36 (let* ((loc (locate-text "A Useful First Script" 0))
37 (author (string-copy "Author"))
38 (authors (list author))
39 (text-value (make-text "owned" loc))
40 (children (list (make-paragraph (list text-value) loc)))
41 (doc (make-document 'chapter "Owned" authors children loc)))
42 (set-car! authors "Changed")
43 (string-set! author 0 #\X)
44 (set-car! children (make-anchor "changed" loc))
45 (assert-equal '("Author") (document-authors doc))
46 (assert-true (paragraph? (car (document-children doc))))
47 (let ((returned-authors (document-authors doc))
48 (returned-children (document-children doc)))
49 (set-car! returned-authors "Accessor changed")
50 (set-car! returned-children (make-anchor "accessor-changed" loc))
51 (assert-equal '("Author") (document-authors doc))
52 (assert-true (paragraph? (car (document-children doc)))))
53 (let* ((title-input (list (make-text "Title" loc)))
54 (label-input (list (make-text "Label" loc)))
55 (section (make-section "owned-section" title-input children loc))
56 (reference (make-reference "target" label-input loc)))
57 (set-car! title-input (make-text "Changed" loc))
58 (set-car! label-input (make-text "Changed" loc))
59 (assert-equal "Title" (text-content (car (section-title section))))
60 (assert-equal "Label" (text-content (car (reference-label reference))))
61 (let ((title-output (section-title section)) (label-output (reference-label reference)))
62 (set-car! title-output (make-text "Changed" loc))
63 (set-car! label-output (make-text "Changed" loc))
64 (assert-equal "Title" (text-content (car (section-title section))))
65 (assert-equal "Label" (text-content (car (reference-label reference))))))
66 (let* ((outer-name (string-copy "outer.sgd"))
67 (middle-name (string-copy "middle.sgd"))
68 (inner-name (string-copy "inner.sgd"))
69 (outer (make-srcloc outer-name 3 4 20 7))
70 (middle (make-generated-srcloc middle-name 2 3 10 5 outer))
71 (inner (make-generated-srcloc inner-name 1 2 1 3 middle))
72 (node (make-text "located" inner)))
73 (string-set! outer-name 0 #\X)
74 (string-set! middle-name 0 #\X)
75 (string-set! inner-name 0 #\X)
76 (let* ((returned (text-location node))
77 (chain (srcloc-origin-chain returned)))
78 (assert-equal '("outer.sgd" "middle.sgd" "inner.sgd") (map srcloc-file chain))
79 (string-set! (srcloc-file returned) 0 #\Y)
80 (string-set! (srcloc-file (srcloc-origin returned)) 0 #\Y)
81 (assert-equal '("outer.sgd" "middle.sgd" "inner.sgd")
82 (map srcloc-file (srcloc-origin-chain (text-location node))))))
83 (let ((before (document->string practical-chapter))
84 (returned (document-location practical-chapter)))
85 (string-set! (srcloc-file returned) 0 #\X)
86 (assert-equal before (document->string practical-chapter)))
87 (let loop ((depth 0) (origin (make-srcloc "outer.sgd" 1 1 0 1)))
88 (if (= depth 32)
89 (assert-equal 33 (length (srcloc-origin-chain
90 (text-location (make-text "bounded" origin)))))
91 (loop (+ depth 1)
92 (make-generated-srcloc "generated.sgd" 1 1 depth 1 origin))))))
94(test "all fixture locations recompute from manuscript bytes"
95 (for-each
96 (lambda (entry)
97 (let* ((node (car entry)) (needle (cadr entry)) (occurrence (caddr entry))
98 (actual (node-location node)) (expected (locate-text needle occurrence)))
99 (assert-equal (srcloc-file expected) (srcloc-file actual))
100 (assert-equal (srcloc-position expected) (srcloc-position actual))
101 (assert-equal (srcloc-span expected) (srcloc-span actual))
102 (assert-equal (srcloc-line expected) (srcloc-line actual))
103 (assert-equal (srcloc-column expected) (srcloc-column actual))
104 (assert-equal needle (source-slice-at actual))))
105 fixture-location-expectations)
106 (let ((utf8-location (locate-text "καλημέρα" 0)))
107 (assert-true (> (srcloc-position utf8-location) 100)))
108 (let ((first (locate-text "A Useful First Script" 0))
109 (second (locate-text "A Useful First Script" 1)))
110 (assert-true (not (= (srcloc-position first) (srcloc-position second))))))