]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-triple-strings.lisp
feat(xtm): support anyURI resource data on name variants
[topic-maps.git] / tests / ctm-triple-strings.lisp
1 (defpackage #:topic-maps.triple.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.registered-original.tests
4 #:check #:fields #:binary #:case-state #:identity-fields #:captured-read #:condition-data)
5 (:export #:run-tests #:write-reports))
6 (in-package #:topic-maps.triple.tests)
7 (defparameter *doc* "https://example.invalid/triple/main.ctm")
8 (defvar *checks* nil)
9 (defvar *positives* nil)
10 (defvar *negatives* nil)
11 (defvar *original-rows* nil)
12 (defvar *entry-statuses* nil)
13 (defun canonical (map) (topic-maps.cxtm:canonicalize-octets map :base-locator *doc*))
14 (defun quoted (value)
15 (with-output-to-string (out)
16 (write-char #\" out)
17 (loop for c across value do
18 (case c
19 (#\" (write-string "\\\"" out)) (#\\ (write-string "\\\\" out))
20 (#\Newline (write-string "\\n" out)) (#\Return (write-string "\\r" out)) (#\Tab (write-string "\\t" out))
21 (otherwise (write-char c out))))
22 (write-char #\" out)))
23 (defun source-for (context literal)
24 (ecase context
25 (:occurrence (format nil "topic occtype: ~A." literal))
26 (:name (format nil "topic - ~A." literal))
27 (:variant (format nil "topic - \"Name\" (~A @s)." literal))))
28 (defun value-in (context map)
29 (let ((owner (topic-maps.registered-original.tests::local map *doc* "topic")))
30 (ecase context
31 (:occurrence (let ((items (topic-occurrences owner)))
32 (and (= 1 (length items)) (eq owner (occurrence-parent (first items)))
33 (equal (string-datatype) (occurrence-datatype (first items))) (occurrence-value (first items)))))
34 (:name (let ((names (topic-names owner)))
35 (and (= 1 (length names)) (eq owner (topic-name-parent (first names))) (topic-name-value (first names)))))
36 (:variant (let* ((name (first (topic-names owner))) (items (topic-name-variants name)) (v (first items)))
37 (and (= 1 (length items)) (eq name (variant-parent v)) (equal (string-datatype) (variant-datatype v))
38 (equal (list (topic-maps.registered-original.tests::local map *doc* "s")) (variant-scope v))
39 (variant-value v)))))))
40 (defun accepted (label text)
41 (multiple-value-bind (map counter)
42 (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label :wildcard-counter 37)
43 (let* ((held (topic-maps.import.tests::handles map))
44 (before (topic-maps.import.tests::state map held)) (actual (canonical map)))
45 (check (concatenate 'string label ":model-counter-and-pure-canonicalization")
46 (and (= 37 counter) (topic-maps.reference.tests::references-current-p map)
47 (equal before (topic-maps.import.tests::state map held)) (equalp actual (canonical map))))
48 (push (fields "label" label "sourceText" text "initialCounter" 37 "finalCounter" counter
49 "actualCXTM" (topic-maps.ctm::decode-utf8 actual) "model" (topic-maps.template.tests::observe map)) *positives*))
50 map))
51 (defparameter *positive-specs*
52 (list
53 (list "empty" "" "")
54 (list "plain" "alpha" "alpha")
55 (list "single-quote" "one\"quote" "one\"quote")
56 (list "double-quote" "two\"\"quotes" "two\"\"quotes")
57 (list "leading-quote" "\"first" "\"first")
58 (list "leading-two-quotes" "\"\"first" "\"\"first")
59 (list "escaped-triple" "\\\"\\\"\\\"" "\"\"\"")
60 (list "trailing-escaped-quote" "end\\\"" "end\"")
61 (list "trailing-backslash" "end\\\\" "end\\")
62 (list "raw-then-escaped-quote" "\"\\\"" "\"\"")
63 (list "two-raw-then-escaped-quote" "\"\"\\\"" "\"\"\"")
64 (list "LF" "line1
65 line2" "line1
66 line2")
67 (list "CR" (concatenate 'string "line1" (string #\Return) "line2") (concatenate 'string "line1" (string #\Return) "line2"))
68 (list "CRLF" (concatenate 'string "line1" (string #\Return) "
69 line2") (concatenate 'string "line1" (string #\Return) "
70 line2"))
71 (list "leading-trailing-LF" (format nil "~% line ~%") (format nil "~% line ~%"))
72 (list "whitespace" " x " " x ")
73 (list "syntax-as-data" "# comment #(not)# %version $var *" "# comment #(not)# %version $var *")
74 (list "utf8" "Grüße🙂" "Grüße🙂")
75 (list "all-escapes" "\\n\\r\\t\\\\\\\"" (concatenate 'string "
76 " (string #\Return) " \\\""))
77 (list "unicode-u4" "\\u005a\\u005A" "ZZ")
78 (list "unicode-U6" "\\U00005a\\U00005A" "ZZ")
79 (list "unicode-mixed" "\\u00e9\\U01F642" "é🙂")
80 (list "unicode-quote-backslash" "\\u0022\\u005c" "\"\\")
81 ))
82 (defun positive-controls ()
83 (dolist (spec *positive-specs*)
84 (destructuring-bind (label body value) spec
85 ;; Exercise the common String token in all existing value contexts.
86 (dolist (context '(:occurrence :name :variant))
87 (let* ((key (format nil "~A:~(~A~)" label context))
88 (literal (concatenate 'string "\"\"\"" body "\"\"\""))
89 (map (accepted key (source-for context literal)))
90 (plain (topic-maps.ctm:deserialize-string (source-for context (quoted value)) :document-iri *doc*)))
91 (check (concatenate 'string key ":exact-value-and-ordinary-string-equivalence")
92 (and (equal value (value-in context map)) (equalp (canonical map) (canonical plain)))))))))
93 (defun token-controls ()
94 (loop for n from 3 to 8 do
95 (let* ((source (concatenate 'string "\"\"\"x" (make-string n :initial-element #\") ";tail"))
96 (topic-maps.ctm::*source-text* source)
97 (lexer (topic-maps.ctm::make-lexer source)) (token (topic-maps.ctm::next-token lexer)))
98 (check (format nil "tokens:closing-run-~D-consumes-exactly-three-quotes" n)
99 (and (eq :string (topic-maps.ctm::token-kind token)) (equal "x" (topic-maps.ctm::token-value token))
100 (= 0 (topic-maps.ctm::token-position token)) (= 7 (topic-maps.ctm::lexer-cursor lexer))
101 (equal (subseq source 7) (concatenate 'string (make-string (- n 3) :initial-element #\") ";tail"))))))
102 (dolist (text '("\"\"\"\"\"\"" "\"\"" "\"\"\"a\"\"\";next" "\"\"\"a\"\"\"^^<urn:type>"))
103 (multiple-value-bind (tokens failure) (topic-maps.registered-original.tests::lexical-evidence text "tokens")
104 (check (concatenate 'string "tokens:first-string:" text)
105 (and (null failure) (eq :string (caar tokens)) (= 0 (third (first tokens))))))))
106 (defun qualified-and-template-controls ()
107 (dolist (spec '(("\"\"\"001\"\"\"^^<http://www.w3.org/2001/XMLSchema#integer>" "001" "http://www.w3.org/2001/XMLSchema#integer")
108 ("\"\"\"*\"\"\"" "*" "http://www.w3.org/2001/XMLSchema#string")))
109 (destructuring-bind (literal value datatype) spec
110 (let* ((map (accepted (concatenate 'string "qualified:" literal) (source-for :occurrence literal)))
111 (o (first (topic-occurrences (topic-maps.registered-original.tests::local map *doc* "topic")))))
112 (check (concatenate 'string "qualified:" literal ":existing-datatype-path")
113 (and (equal value (occurrence-value o)) (equal datatype (occurrence-datatype o)))))))
114 (dolist (text '("def f($s) topic - $s. end f(\"\"\"Name\"\"\")"
115 "def f() topic - \"\"\"Name\"\"\". end f()"))
116 (let ((map (accepted (concatenate 'string "template:" text) text)))
117 (check (concatenate 'string "template:" text ":existing-String-token-path")
118 (equal "Name" (value-in :name map))))))
119 (defparameter *negative-specs*
120 '(
121 ("quoted-unclosed" "topic - \"x" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
122 ("quoted-invalid-escape" "topic - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 10 1 11)
123 ("quoted-invalid-u4" "topic - \"\\u00x0\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 13 1 14)
124 ("quoted-short-U6" "topic - \"\\U0000\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16)
125 ("quoted-surrogate" "topic - \"\\uD800\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9)
126 ("quoted-out-of-range" "topic - \"\\U110000\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9)
127 ("quoted-error-LF" "topic.
128 other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 17 2 11)
129 ("quoted-error-CR" "topic.
130 other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 17 2 11)
131 ("quoted-error-CRLF" "topic.
132 other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 18 2 11)
133 ("quoted-unicode-prefix" "topic - \"Ä\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 11 1 12)
134 ("quoted-adjacent-token" "topic occtype: \"a\"x." topic-maps.ctm:ctm-rejection :expected-separator 18 1 19)
135 ("quoted-variant-missing-scope" "topic - \"N\" (\"V\")." topic-maps.ctm:ctm-rejection :expected-variant-scope 16 1 17)
136 ("triple-opening-only" "topic - \"\"\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
137 ("triple-unclosed" "topic - \"\"\"body" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
138 ("triple-final-one-quote" "topic - \"\"\"body\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
139 ("triple-final-two-quotes" "topic - \"\"\"body\"\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
140 ("triple-dangling-escape" "topic - \"\"\"body\\" topic-maps.ctm:ctm-rejection :invalid-string-escape 16 1 17)
141 ("triple-unknown-escape" "topic - \"\"\"\\q\"\"\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 12 1 13)
142 ("triple-short-u4" "topic - \"\"\"\\u00\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16)
143 ("triple-short-U6" "topic - \"\"\"\\U0000\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 17 1 18)
144 ("triple-bad-hex" "topic - \"\"\"\\u00G0\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16)
145 ("triple-surrogate" "topic - \"\"\"\\uD800\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9)
146 ("triple-out-of-range" "topic - \"\"\"\\U110000\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9)
147 ("triple-unclosed-after-CRLF" "topic.
148 other - \"\"\"bad" topic-maps.ctm:ctm-rejection :unterminated-string 16 2 9)
149 ("triple-unicode-LF-error" "topic - \"\"\"Ä
150 \\q\"\"\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 14 2 2)
151 ("triple-surplus-closing-4" "topic occtype: \"\"\"x\"\"\"\";." topic-maps.ctm:ctm-rejection :unterminated-string 22 1 23)
152 ("triple-surplus-closing-5" "topic occtype: \"\"\"x\"\"\"\"\";." topic-maps.ctm:ctm-rejection :unexpected-token 22 1 23)
153 ("triple-surplus-closing-6" "topic occtype: \"\"\"x\"\"\"\"\"\";." topic-maps.ctm:ctm-rejection :unterminated-string 22 1 23)
154 ("triple-adjacent-name" "topic occtype: \"\"\"x\"\"\"extra." topic-maps.ctm:ctm-rejection :expected-separator 22 1 23)
155 ("triple-adjacent-string" "topic occtype: \"\"\"x\"\"\"\"extra\"." topic-maps.ctm:ctm-rejection :unexpected-token 22 1 23)
156 ("triple-variant-missing-scope" "topic - \"N\" (\"\"\"V\"\"\")." topic-maps.ctm:ctm-rejection :expected-variant-scope 20 1 21)
157 ))
158 ;; Construct CR explicitly; source-file line-ending normalization must not
159 ;; change these input streams. Build fresh rows rather than mutate constants.
160 (setf *negative-specs*
161 (mapcar (lambda (spec)
162 (let ((text (cond ((equal "quoted-error-CR" (first spec))
163 (format nil "topic.~Cother - \"\\q\"." #\Return))
164 ((equal "quoted-error-CRLF" (first spec))
165 (format nil "topic.~C~Cother - \"\\q\"." #\Return #\Newline))
166 ((equal "triple-unclosed-after-CRLF" (first spec))
167 (format nil "topic.~C~Cother - \"\"\"bad" #\Return #\Newline)))))
168 (if text (list* (first spec) text (cddr spec)) spec)))
169 *negative-specs*))
170 (defun negative-controls ()
171 (dolist (spec *negative-specs*)
172 (destructuring-bind (label text class code position line column) spec
173 (multiple-value-bind (map condition)
174 (captured-read (lambda () (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label)))
175 (check (concatenate 'string "negative:" label ":class-code-position-source")
176 (and (null map) condition (eq class (type-of condition))
177 (eq code (topic-maps.ctm:error-code condition)) (= position (topic-maps.ctm:error-position condition))
178 (= line (topic-maps.ctm:error-line condition)) (= column (topic-maps.ctm:error-column condition))
179 (eq :character (topic-maps.ctm:error-position-unit condition)) (equal label (topic-maps.ctm:error-source condition))
180 (null (topic-maps.ctm:error-cause condition)) (null (topic-maps.ctm:error-context condition))))
181 (push (fields "label" label "sourceText" text "condition" (condition-data condition)) *negatives*)))))
182 (defparameter *originals*
183 (list
184 (list "occurrence-string-triple" :occurrences "Occurrence \" Value" 41 367)
185 (list "occurrence-string-triple2" :occurrences "Occurrence\"\" \" Value\"" 48 370)
186 (list "unicode-escape-u4-triple-string" :escapes (format nil "ZZ ~%Top") 33 331)
187 (list "unicode-escape-u6-triple-string" :escapes (format nil "ZZ ~%Top") 37 331)))
188 (defun original-contracts ()
189 (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*) (root (topic-maps.test-foundation:corpus-root corpus))
190 (cases (topic-maps.test-foundation:corpus-cases corpus))
191 (previous (mapcar (lambda (c) (cons c (case-state c))) cases))
192 (ids (mapcar (lambda (spec) (format nil "ctm/in/~A.ctm" (first spec))) *originals*))
193 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*)) (new-results nil))
194 (setf *entry-statuses* (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
195 (dolist (spec *originals*)
196 (destructuring-bind (name group value source-size expected-size) spec
197 (let* ((id (format nil "ctm/in/~A.ctm" name))
198 (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected))
199 (*doc* (topic-maps.test-foundation:test-case-document-iri case)))
200 (check (concatenate 'string id ":existing-positive-NOT-RUN-record-and-identity")
201 (and (= 1 (length selected)) (member case cases :test #'eq)
202 (eq :valid (topic-maps.test-foundation:test-case-category case))
203 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
204 (eq :not-run (topic-maps.test-foundation:test-case-status case))
205 (null (topic-maps.test-foundation:test-case-actual-outcome case))
206 (equal *doc* (format nil "https://ctm-tests.example.invalid/~A/~A"
207 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit) id))
208 (equal (truename (merge-pathnames id root)) (truename (topic-maps.test-foundation:test-case-source-pathname case)))
209 (= source-size (length (binary (topic-maps.test-foundation:test-case-source-pathname case))))))
210 (multiple-value-bind (row result) (topic-maps.registered-original.tests::exercise-case case group)
211 (push row *original-rows*)
212 (check (concatenate 'string id ":actual-original-byte-comparison-PASS")
213 (and result (getf result :match) (= expected-size (getf result :bytes))
214 (eq :pass (topic-maps.test-foundation:test-case-status case))))
215 (let* ((context (if (eq group :occurrences) :occurrence :name))
216 (plain (source-for context (quoted value))))
217 ;; The Unicode originals use zztop as owner; retain that identity.
218 (when (eq context :name) (setf plain (concatenate 'string "zztop" (subseq plain 5))))
219 (let* ((map (accepted (concatenate 'string id ":ordinary-control") plain))
220 (actual (canonical map)))
221 (check (concatenate 'string id ":ordinary-control-same-original-CXTM")
222 (and (= expected-size (length actual))
223 (equalp actual (binary (topic-maps.test-foundation:test-case-expected-baseline case)))
224 (equal (getf result :actual) (topic-maps.ctm::decode-utf8 actual))))))
225 (push result new-results))
226 (format t "~&TRIPLE-ORIGINAL ~A: PASS; ~D byte-exact CXTM bytes~%" id expected-size))))
227 (check "originals:all-prior-case-records-and-observations-preserved"
228 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*) (equal cases (topic-maps.test-foundation:corpus-cases corpus))
229 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
230 (every (lambda (entry) (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
231 (equal (first (cdr entry)) (identity-fields (car entry)))
232 (equal (cdr entry) (case-state (car entry))))) previous)))
233 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
234 (check "originals:exactly-four-NOT-RUN-to-PASS-no-other-status-changes"
235 (and (= (+ 4 (getf *entry-statuses* :pass)) (getf after :pass))
236 (= (- (getf *entry-statuses* :not-run) 4) (getf after :not-run))
237 (= (getf *entry-statuses* :fail) (getf after :fail))
238 (= (getf *entry-statuses* :unsupported) (getf after :unsupported)))))
239 (setf topic-maps.ctm.tests::*cxtm-results* (append old-results (nreverse new-results)))))
240 (defun run-tests ()
241 (unless topic-maps.ctm.tests::*cxtm-corpus*
242 (topic-maps.registered-original.tests:run-tests)
243 (topic-maps.registered-original.tests:run-negative-tests)
244 (topic-maps.prolog.tests:run-tests)
245 (topic-maps.registered-original.tests:run-empty2-tests)
246 (topic-maps.ctm-integer.tests:run-tests))
247 (setf *positives* nil *negatives* nil *original-rows* nil)
248 (let ((topic-maps.registered-original.tests::*checks* nil))
249 (positive-controls) (token-controls) (qualified-and-template-controls) (negative-controls) (original-contracts)
250 (check "triple:unique-check-names"
251 (= (length topic-maps.registered-original.tests::*checks*)
252 (length (remove-duplicates topic-maps.registered-original.tests::*checks* :test #'equal))))
253 (setf *checks* (nreverse topic-maps.registered-original.tests::*checks*)))
254 (setf *positives* (nreverse *positives*) *negatives* (nreverse *negatives*) *original-rows* (nreverse *original-rows*))
255 (format t "~&TRIPLE-PASS: ~D checks; four originals plus ~D positive and ~D negative controls~%"
256 (length *checks*) (length *positives*) (length *negatives*)) t)
257 (defun write-reports (prefix)
258 (with-open-file (out (concatenate 'string prefix ".triple.json") :direction :output :if-exists :supersede)
259 (topic-maps.test-foundation::emit-json-value
260 (fields "kind" "ctm-triple-string-execution" "passed" (length *checks*) "failed" 0
261 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*)
262 "checks" (coerce *checks* 'vector) "positives" (coerce *positives* 'vector)
263 "negativeControls" (coerce *negatives* 'vector) "originals" (coerce *original-rows* 'vector)) out) (terpri out))
264 (topic-maps.ctm.tests::write-cxtm-report
265 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
266 :additional-checks *checks*
267 :scope "Slice 20G: four original Triple String contracts using the existing original harness; shared escapes/Unicode, exact original and ordinary-string CXTM, unchanged model/handles and counters. Prior contracts and four known FAILs preserved."))