]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-registered-originals.lisp
perf(tmdm): index association normalization merge candidates
[topic-maps.git] / tests / ctm-registered-originals.lisp
1 (defpackage #:topic-maps.registered-original.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:export #:run-tests #:write-reports #:candidate-groups
4 #:run-negative-tests #:write-negative-reports
5 #:run-empty2-tests #:write-empty2-reports))
6 (in-package #:topic-maps.registered-original.tests)
7
8 ;; One selection of the existing discovered Case Records, not another registry.
9 (defparameter *groups*
10 '((:comments "comment-ml" "comment-ml-nested" "comment-ml-nested2" "comment-sl" "comment-sl2"
11 "comment-sl3" "comment-sl4" "comment-sl5" "comment-sl6" "empty")
12 (:identity "association-qname-iid" "item-identifier-INF" "item-identifier-relative" "qname-date" "qname-digits"
13 "rootless-uri" "subject-identifier-dot" "subject-identifier-non-http" "subject-identifier-relative"
14 "subject-locator-dot" "subject-locator-relative" "topic-identifier-INF" "topic-identifier-unicode")
15 (:occurrences "occurrence-decimal-dot-neg2" "occurrence-decimal-dot-pos" "occurrence-decimal-leading-zeros2"
16 "occurrence-duplicate-string" "occurrence-duplicate-uri" "occurrence-qname-value"
17 "occurrence-string-multiline" "occurrence-string-multiline2" "occurrence-uri-semicolon")
18 (:prefixes "prefix-multiline2" "prefix-multiline3" "prefix-multiline4" "prefix-with-qm")
19 (:escapes "string-escape" "string-escape2" "unicode-escape-u4" "unicode-escape-u6")))
20 (defun candidate-groups () (copy-tree *groups*))
21 (defvar *checks* nil)
22 (defvar *rows* nil)
23 (defvar *results* nil)
24 (defvar *entry-statuses* nil)
25 (defun check (label truth)
26 (unless truth (error "Registered original check failed: ~A" label))
27 (push label *checks*))
28 (defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
29 (defun binary (path) (topic-maps.ctm.tests::binary-file path))
30 (defun same-set (a b &optional (test #'equal))
31 (and (= (length a) (length b)) (subsetp a b :test test) (subsetp b a :test test)))
32 (defun local (map doc name)
33 (find-if (lambda (topic) (member (concatenate 'string doc "#" name) (topic-item-identifiers topic) :test #'equal))
34 (topic-map-topics map)))
35 (defun single-identity (map kind value)
36 (let ((topics (topic-map-topics map)))
37 (and (= 1 (length topics)) (null (topic-map-associations map))
38 (let ((topic (first topics)))
39 (and (equal (topic-item-identifiers topic) (when (eq kind :ii) (list value)))
40 (equal (topic-subject-identifiers topic) (when (eq kind :si) (list value)))
41 (equal (topic-subject-locators topic) (when (eq kind :sl) (list value)))
42 (null (topic-names topic)) (null (topic-occurrences topic)))))))
43 (defun plain-locals-p (map doc names)
44 (and (= (length names) (length (topic-map-topics map))) (null (topic-map-associations map))
45 (every (lambda (name) (let ((topic (local map doc name)))
46 (and topic (equal (list (concatenate 'string doc "#" name)) (topic-item-identifiers topic))
47 (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic))
48 (null (topic-names topic)) (null (topic-occurrences topic))))) names)))
49 (defun semantic-witness (name group map doc)
50 "Independent concrete object properties, in addition to exact XML equality."
51 (ecase group
52 (:comments
53 (plain-locals-p map doc
54 (cond ((member name '("empty" "empty2" "comment-sl3") :test #'equal) nil)
55 ((member name '("comment-ml" "comment-ml-nested" "comment-ml-nested2" "comment-sl2") :test #'equal) '("topic1" "topic3"))
56 ((equal name "comment-sl6") '("topic" "topic2")) (t '("topic")))))
57 (:identity
58 (if (equal name "association-qname-iid")
59 (let* ((base "http://www.example.org/my-ctm#")
60 (a (first (topic-map-associations map))) (roles (when a (association-roles a)))
61 (topics (topic-map-topics map)))
62 (flet ((named (suffix) (find-if (lambda (topic) (equal (list (concatenate 'string base suffix)) (topic-item-identifiers topic))) topics)))
63 (and (= 3 (length topics)) (= 1 (length (topic-map-associations map))) (= 1 (length roles))
64 (every (lambda (topic) (and (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) topics)
65 (eq (named "assoctype") (association-type a)) (eq (named "roletype") (association-role-type (first roles)))
66 (eq (named "topic") (association-role-player (first roles))) (eq a (association-role-parent (first roles))))))
67 (let ((expected (cond
68 ((member name '("item-identifier-INF" "topic-identifier-INF") :test #'equal) (list :ii (concatenate 'string doc "#INF")))
69 ((equal name "item-identifier-relative") (list :ii (concatenate 'string doc "#topic")))
70 ((equal name "topic-identifier-unicode") (list :ii (concatenate 'string doc "#ü͡ØΨ㬟͡")))
71 ((equal name "qname-date") '(:si "http://psi.semagia.com/iso8601/1947-01-08"))
72 ((equal name "qname-digits") '(:si "http://psi.example.org/007"))
73 ((equal name "rootless-uri") '(:si "urn:uuid:d84e2777-8928-4bd4-a3e4-8ca835f92304"))
74 ((equal name "subject-identifier-dot") '(:si "http://psi.example.org/fake"))
75 ((equal name "subject-identifier-non-http") '(:si "ftp://psi.example.org/photo.jpg"))
76 ((equal name "subject-identifier-relative") (list :si (concatenate 'string doc "#fake")))
77 ((equal name "subject-locator-dot") '(:sl "http://www.example.org/"))
78 ((equal name "subject-locator-relative") (list :sl (concatenate 'string doc "#fake"))))))
79 (and expected (single-identity map (first expected) (second expected))))))
80 (:prefixes
81 (if (equal name "prefix-with-qm")
82 (single-identity map :sl "http://www.youtube.com/watch?v=fd85SK7dESU")
83 (single-identity map :si "http://psi.example.org/fake")))
84 (:occurrences
85 (let* ((owner (local map doc (if (equal name "occurrence-qname-value") "john" "topic")))
86 (type (local map doc (if (equal name "occurrence-qname-value") "wikipedia" "occtype")))
87 (items (when owner (topic-occurrences owner))) (o (first items))
88 (expected (cond
89 ((equal name "occurrence-string-triple") (list "Occurrence \" Value" (string-datatype)))
90 ((equal name "occurrence-string-triple2") (list "Occurrence\"\" \" Value\"" (string-datatype)))
91 ((equal name "occurrence-ctm-integer") (list "*" "http://psi.topicmaps.org/iso13250/ctm-integer"))
92 ((equal name "occurrence-decimal-dot-neg2") (list "-0.03" (decimal-datatype)))
93 ((equal name "occurrence-decimal-dot-pos") (list "0.3" (decimal-datatype)))
94 ((equal name "occurrence-decimal-leading-zeros2") (list "2.3" (decimal-datatype)))
95 ((equal name "occurrence-duplicate-string") (list "Occurrence" (string-datatype)))
96 ((equal name "occurrence-duplicate-uri") (list "http://www.example.org/resource" (iri-datatype)))
97 ((equal name "occurrence-qname-value") (list "http://en.wikipedia.org/wiki/John_Lennon" (iri-datatype)))
98 ((equal name "occurrence-string-multiline") (list (format nil "Occurrence~%Value") (string-datatype)))
99 ((equal name "occurrence-string-multiline2") (list (format nil "Occurrence~C~CValue" #\Return #\Newline) (string-datatype)))
100 ((equal name "occurrence-uri-semicolon") (list "http://www.example.org/;something;;somewhere" (iri-datatype))))))
101 (and owner type (= 2 (length (topic-map-topics map))) (= 1 (length items)) (null (topic-map-associations map))
102 (every (lambda (topic) (null (topic-names topic))) (topic-map-topics map))
103 (eq owner (occurrence-parent o)) (eq type (occurrence-type o))
104 (equal expected (list (occurrence-value o) (occurrence-datatype o)))
105 (null (occurrence-scope o)) (null (item-identifiers o)))))
106 (:escapes
107 (let* ((owner (local map doc (cond ((equal name "string-escape") "topic") ((equal name "string-escape2") "A00") ((equal name "encoding-8859-5") "russia") (t "zztop"))))
108 (names (when owner (topic-names owner)))
109 (type (find-if (lambda (topic) (member (default-name-type-identifier) (topic-subject-identifiers topic) :test #'equal)) (topic-map-topics map)))
110 (expected (cond ((equal name "string-escape") (list "\\" "Quote (\") character" (format nil "Some whitespace ~C~C~C" #\Newline #\Return #\Tab)))
111 ((equal name "string-escape2") '("A00"))
112 ((equal name "encoding-8859-5") '("Россия"))
113 ((member name '("unicode-escape-u4-triple-string" "unicode-escape-u6-triple-string") :test #'equal)
114 (list (format nil "ZZ ~%Top")))
115 (t '("ZZ Top")))))
116 (and owner type (= 2 (length (topic-map-topics map))) (null (topic-map-associations map))
117 (same-set expected (mapcar #'topic-name-value names))
118 (every (lambda (n) (and (eq owner (topic-name-parent n)) (eq type (topic-name-type n))
119 (null (topic-name-scope n)) (null (topic-name-variants n)))) names))))))
120
121 (defun identity-fields (case)
122 (list (topic-maps.test-foundation:test-case-id case) (topic-maps.test-foundation:test-case-category case)
123 (topic-maps.test-foundation:test-case-source-pathname case) (topic-maps.test-foundation:test-case-document-iri case)
124 (topic-maps.test-foundation:test-case-expected-outcome case) (topic-maps.test-foundation:test-case-expected-baseline case)
125 (topic-maps.test-foundation:test-case-clauses case)))
126 (defun exercise-case (case group)
127 "Record only an actual comparison. Rejections/errors never become empty Maps."
128 (let* ((id (topic-maps.test-foundation:test-case-id case)) (doc (topic-maps.test-foundation:test-case-document-iri case))
129 (name (pathname-name (topic-maps.test-foundation:test-case-source-pathname case)))
130 (identity (copy-tree (identity-fields case))) (source (binary (topic-maps.test-foundation:test-case-source-pathname case)))
131 (evidence nil) (result nil))
132 (handler-case
133 (let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
134 (held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held))
135 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
136 (expected (binary (topic-maps.test-foundation:test-case-expected-baseline case))) (different (mismatch actual expected)))
137 (check (concatenate 'string id ":actual-TMDM-properties-and-EQ-references") (semantic-witness name group map doc))
138 (check (concatenate 'string id ":canonicalization-preserves-raw-objects-and-handles")
139 (equal before (topic-maps.import.tests::state map held)))
140 (check (concatenate 'string id ":deterministic-canonical-octets")
141 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)))
142 (check (concatenate 'string id ":unmodified-source-bytes") (equalp source (binary (topic-maps.test-foundation:test-case-source-pathname case))))
143 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
144 :diagnostics (when different (list (format nil "First different byte ~D; actual ~D bytes, expected ~D bytes." different (length actual) (length expected)))))
145 (setf evidence (fields "actualBytes" (length actual) "expectedBytes" (length expected)
146 "firstDifferentByte" different "model" (topic-maps.template.tests::observe map))
147 result (list :id id :base doc :actual (topic-maps.ctm::decode-utf8 actual)
148 :expected (topic-maps.ctm::decode-utf8 expected) :match (null different) :bytes (length actual))))
149 ((or topic-maps.ctm:ctm-unsupported topic-maps.cxtm:cxtm-unsupported) (condition)
150 (topic-maps.test-foundation:record-result case :unsupported :diagnostics (list (format nil "~A: ~A" (type-of condition) condition))))
151 (error (condition)
152 (topic-maps.test-foundation:record-result case :execution-error :diagnostics (list (format nil "~A: ~A" (type-of condition) condition)))))
153 (check (concatenate 'string id ":same-contract-identity-after-execution") (equal identity (identity-fields case)))
154 (values
155 (fields "id" id "group" (string-downcase (symbol-name group))
156 "sourcePathname" id "documentIRI" doc
157 "baseline" (format nil "ctm/baseline/~A.ctm.cxtm" name)
158 "status" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-status case))
159 "actualOutcome" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-actual-outcome case))
160 "diagnostics" (coerce (topic-maps.test-foundation:test-case-diagnostics case) 'vector)
161 "sourceBytes" (length source) "comparison" evidence)
162 result)))
163
164
165 (defun case-state (case)
166 (copy-tree
167 (list (identity-fields case)
168 (topic-maps.test-foundation:test-case-status case)
169 (topic-maps.test-foundation:test-case-actual-outcome case)
170 (topic-maps.test-foundation:test-case-diagnostics case))))
171
172 (defun run-tests ()
173 ;; Use the same Case Records populated by the retained suites. A direct ASDF
174 ;; invocation may initialize those suites, but must never replace their corpus.
175 (unless topic-maps.ctm.tests::*cxtm-corpus*
176 (topic-maps.include.tests:run-tests))
177 (setf *checks* nil *rows* nil *results* nil)
178 (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*)
179 (root (topic-maps.test-foundation:corpus-root corpus))
180 (ids (loop for (group . names) in *groups*
181 append (mapcar (lambda (name) (format nil "ctm/in/~A.ctm" name)) names)))
182 (cases (topic-maps.test-foundation:corpus-cases corpus))
183 (previous (mapcar (lambda (case) (cons case (case-state case))) cases))
184 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
185 (base (format nil "https://ctm-tests.example.invalid/~A/"
186 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit))))
187 (setf *entry-statuses*
188 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
189 (check "registration:forty-distinct-selected-originals"
190 (and (= 40 (length ids)) (= 40 (length (remove-duplicates ids :test #'equal)))
191 (equal '(10 13 9 4 4) (mapcar (lambda (group) (length (cdr group))) *groups*))))
192 (check "registration:existing-corpus-correspondence"
193 (getf (topic-maps.test-foundation:verify-correspondence corpus) :ok))
194 (dolist (group *groups*)
195 (dolist (name (cdr group))
196 (let* ((id (format nil "ctm/in/~A.ctm" name))
197 (selected (topic-maps.test-foundation:select-cases corpus :id id))
198 (case (first selected)))
199 (check (concatenate 'string id ":unique-existing-positive-contract")
200 (and (= 1 (length selected)) (member case cases :test #'eq)
201 (eq :valid (topic-maps.test-foundation:test-case-category case))
202 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
203 (eq :not-run (topic-maps.test-foundation:test-case-status case))
204 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
205 (check (concatenate 'string id ":pinned-source-baseline-and-document-IRI")
206 (and (equal (truename (merge-pathnames id root))
207 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
208 (equal (truename (merge-pathnames (format nil "ctm/baseline/~A.ctm.cxtm" name) root))
209 (truename (topic-maps.test-foundation:test-case-expected-baseline case)))
210 (equal (concatenate 'string base id)
211 (topic-maps.test-foundation:test-case-document-iri case))))
212 (multiple-value-bind (row result) (exercise-case case (car group))
213 (push row *rows*)
214 (when result (push result *results*)))
215 (format t "~&REGISTERED-ORIGINAL ~A: ~A~{ ~A~}~%" id
216 (topic-maps.test-foundation:test-case-status case)
217 (topic-maps.test-foundation:test-case-diagnostics case)))))
218 (setf *rows* (nreverse *rows*) *results* (nreverse *results*))
219 (check "registration:forty-actual-byte-comparisons-pass"
220 (and (= 40 (length *rows*) (length *results*))
221 (every (lambda (result) (getf result :match)) *results*)
222 (every (lambda (id) (eq :pass (topic-maps.test-foundation:test-case-status
223 (first (topic-maps.test-foundation:select-cases corpus :id id))))) ids)))
224 (check "registration:all-previous-case-records-and-observations-preserved"
225 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
226 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
227 (every (lambda (entry)
228 (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
229 (equal (first (cdr entry)) (identity-fields (car entry)))
230 (equal (cdr entry) (case-state (car entry))))) previous)
231 (equal old-results topic-maps.ctm.tests::*cxtm-results*)))
232 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
233 (check "registration:only-forty-NOT-RUN-to-PASS-transitions"
234 (and (= (+ 40 (getf *entry-statuses* :pass)) (getf after :pass))
235 (= (- (getf *entry-statuses* :not-run) 40) (getf after :not-run))
236 (= (getf *entry-statuses* :fail) (getf after :fail))
237 (= (getf *entry-statuses* :unsupported) (getf after :unsupported)))))
238 (setf topic-maps.ctm.tests::*cxtm-results*
239 (append topic-maps.ctm.tests::*cxtm-results* *results*)))
240 (check "registration:unique-check-names"
241 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
242 (setf *checks* (nreverse *checks*))
243 (format t "~&REGISTERED-ORIGINALS-PASS: ~D checks; forty actual byte-exact original contracts~%" (length *checks*))
244 t)
245
246 (defun write-reports (prefix)
247 (with-open-file (out (concatenate 'string prefix ".registration.json")
248 :direction :output :if-exists :supersede :if-does-not-exist :create)
249 (topic-maps.test-foundation::emit-json-value
250 (fields "kind" "ctm-registered-original-execution"
251 "passed" (length *checks*) "failed" 0
252 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*)
253 "checks" (coerce *checks* 'vector) "cases" (coerce *rows* 'vector)) out)
254 (terpri out))
255 (topic-maps.ctm.tests::write-cxtm-report
256 (concatenate 'string prefix ".validation.json")
257 (concatenate 'string prefix ".cxtm.json")
258 :additional-checks *checks*
259 :scope "Slice 20A: forty newly executed original CTM comparisons; prior Case Records, observations and four known DateTime/Include FAILs retained. Full retained regression and XTM integration are evidenced separately."))
260
261
262 ;;; Slice 20B: a selection of seven existing negative Case Records.
263 ;;; Normative review: docs/slice-20b-negative-registration.md. No reader changes.
264 (defparameter *negative-contracts*
265 '(("occurrence-double-positive-infinity" :expected-occurrence-value 15 2 10
266 ("CTM 3.4.1 [28]-[34]" "CTM 3.10 [56]")
267 "Bare INF is neither a CTM number nor another literal alternative.")
268 ("occurrence-double-negative-infinity" :expected-occurrence-value 15 2 10
269 ("CTM 3.4.1 [28]-[34]" "CTM 3.10 [56]")
270 "Bare -INF is neither a CTM number nor another literal alternative.")
271 ("qname-hyphen" :unexpected-token 38 3 3
272 ("CTM 3.3.4 [9]-[11]" "CTM 3.3.6 [14]-[16]")
273 "A QName local part cannot start with a hyphen.")
274 ("string-unbalanced" :unterminated-string 8 1 9
275 ("CTM 3.4.1 [37]-[40]" "CTM 3.4.2 [41]")
276 "The escaped quote is content; the quoted string lacks a closing delimiter.")
277 ("string-unicode" :invalid-unicode-escape 11 1 12
278 ("CTM 3.4.2 [42]")
279 "Uppercase U requires six hexadecimal digits; the closing quote follows four.")
280 ("string-unicode2" :invalid-unicode-escape 15 1 16
281 ("CTM 3.4.2 [42]-[43] and hexadecimal decoding prose")
282 "G cannot be decoded as a hexadecimal digit; see the printed-class qualification.")
283 ("topic-id-illegal" :unexpected-character 0 1 1
284 ("CTM 3.3.6 [14]-[16]")
285 "U+0361 is allowed inside an identifier, but not as its first character.")))
286 (defvar *negative-checks* nil)
287 (defvar *negative-rows* nil)
288 (defvar *negative-entry-statuses* nil)
289
290 (defun captured-read (thunk)
291 (handler-case (values (funcall thunk) nil)
292 (error (condition) (values nil condition))))
293
294 (defun condition-data (condition)
295 (when condition
296 (let ((ctm-p (typep condition 'topic-maps.ctm:ctm-error)))
297 (fields "condition" (string (type-of condition)) "message" (princ-to-string condition)
298 "code" (when ctm-p (topic-maps.test-foundation::word (topic-maps.ctm:error-code condition)))
299 "position" (when ctm-p (topic-maps.ctm:error-position condition))
300 "line" (when ctm-p (topic-maps.ctm:error-line condition))
301 "column" (when ctm-p (topic-maps.ctm:error-column condition))
302 "positionUnit" (when ctm-p (topic-maps.test-foundation::word (topic-maps.ctm:error-position-unit condition)))
303 "source" (when ctm-p (topic-maps.ctm:error-source condition))
304 "cause" (when (and ctm-p (topic-maps.ctm:error-cause condition))
305 (princ-to-string (topic-maps.ctm:error-cause condition)))
306 "context" (when (and ctm-p (topic-maps.ctm:error-context condition))
307 (princ-to-string (topic-maps.ctm:error-context condition)))))))
308
309 (defun condition-signature (condition)
310 (when (typep condition 'topic-maps.ctm:ctm-error)
311 (list (type-of condition) (topic-maps.ctm:error-code condition)
312 (topic-maps.ctm:error-position condition) (topic-maps.ctm:error-line condition)
313 (topic-maps.ctm:error-column condition) (topic-maps.ctm:error-position-unit condition)
314 (topic-maps.ctm:error-source condition) (topic-maps.ctm:error-cause condition)
315 (topic-maps.ctm:error-context condition) (princ-to-string condition))))
316
317 (defun intended-rejection-p (condition spec id)
318 ;; Exact syntax class: unsupported/resource/wrapped model errors never qualify.
319 (and condition (eq (type-of condition) 'topic-maps.ctm:ctm-rejection)
320 (equal (subseq (condition-signature condition) 1 7)
321 (list (second spec) (third spec) (fourth spec) (fifth spec) :character id))
322 (null (topic-maps.ctm:error-cause condition))
323 (null (topic-maps.ctm:error-context condition))))
324
325 (defun replace-fragment (text old new)
326 (let ((position (search old text)))
327 (unless position (error "Missing reviewed source fragment ~S." old))
328 (concatenate 'string (subseq text 0 position) new (subseq text (+ position (length old))))))
329
330 (defun syntax-witness-and-control (name text)
331 "Check the offending source spelling; derive a valid in-memory sibling only."
332 (cond
333 ((equal name "occurrence-double-positive-infinity")
334 (values (equal text (format nil "topic~%occtype: INF."))
335 (replace-fragment text "INF" "1") :occurrence "1"))
336 ((equal name "occurrence-double-negative-infinity")
337 (values (equal text (format nil "topic~%occtype: -INF."))
338 (replace-fragment text "-INF" "-1") :occurrence "-1"))
339 ((equal name "qname-hyphen")
340 (values (equal text (format nil "%prefix ex http://psi.example.org/~%~%ex:-issue.~%"))
341 (replace-fragment text "ex:-issue" "ex:issue") :qname "http://psi.example.org/issue"))
342 ((equal name "string-unbalanced")
343 (values (equal text "topic - \"Foo\\\".")
344 (concatenate 'string (subseq text 0 (1- (length text))) "\".") :name "Foo\""))
345 ((equal name "string-unicode")
346 (values (equal text "a - \"\\U0041\".")
347 (replace-fragment text "\\U0041" "\\u0041") :name "A"))
348 ((equal name "string-unicode2")
349 (values (equal text (format nil "unicode - \"\\U00GG00\".~%"))
350 (replace-fragment text "\\U00GG00" "\\U000041") :name "A"))
351 ((equal name "topic-id-illegal")
352 (let ((mark (code-char #x361)))
353 (values (and (> (length text) 3) (char= mark (char text 0)) (equal (subseq text 1 3) "a."))
354 (concatenate 'string "a" (string mark) (subseq text 2))
355 :identifier (concatenate 'string "a" (string mark)))))
356 (t (error "Unreviewed negative contract ~A." name))))
357
358 (defun control-model-p (map doc name kind value)
359 (and map
360 (ecase kind
361 (:qname (single-identity map :si value))
362 (:identifier (plain-locals-p map doc (list value)))
363 (:occurrence
364 (let* ((owner (local map doc "topic")) (type (local map doc "occtype"))
365 (items (when owner (topic-occurrences owner))) (item (first items)))
366 (and owner type (= 2 (length (topic-map-topics map))) (= 1 (length items))
367 (null (topic-map-associations map))
368 (eq owner (occurrence-parent item)) (eq type (occurrence-type item))
369 (equal value (occurrence-value item)) (equal (integer-datatype) (occurrence-datatype item)))))
370 (:name
371 (let* ((owner (local map doc (cond ((equal name "string-unbalanced") "topic")
372 ((equal name "string-unicode") "a") (t "unicode"))))
373 (names (when owner (topic-names owner))) (item (first names))
374 (type (find-if (lambda (topic) (member (default-name-type-identifier)
375 (topic-subject-identifiers topic) :test #'equal)) (topic-map-topics map))))
376 (and owner type (= 2 (length (topic-map-topics map))) (= 1 (length names))
377 (null (topic-map-associations map)) (eq owner (topic-name-parent item))
378 (eq type (topic-name-type item)) (equal value (topic-name-value item))))))))
379
380 (defun lexical-evidence (text id)
381 ;; Explain the generic QName diagnostic from actual tokens, not its label alone.
382 (let ((topic-maps.ctm::*source-text* text) (topic-maps.ctm::*source-name* id)
383 (lexer (topic-maps.ctm::make-lexer text)) (tokens nil) (failure nil))
384 (handler-case
385 (loop for token = (topic-maps.ctm::next-token lexer) do
386 (push (list (topic-maps.ctm::token-kind token) (topic-maps.ctm::token-value token)
387 (topic-maps.ctm::token-position token)) tokens)
388 until (eq :eof (topic-maps.ctm::token-kind token)))
389 (error (condition) (setf failure condition)))
390 (values (nreverse tokens) failure)))
391
392 (defun exercise-negative-case (case spec)
393 (let* ((id (topic-maps.test-foundation:test-case-id case))
394 (doc (topic-maps.test-foundation:test-case-document-iri case))
395 (name (first spec)) (path (topic-maps.test-foundation:test-case-source-pathname case))
396 (source (binary path)) (text (topic-maps.ctm::decode-utf8 source))
397 (before (case-state case)))
398 (multiple-value-bind (source-witness control-text control-kind control-value)
399 (syntax-witness-and-control name text)
400 (multiple-value-bind (map condition)
401 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
402 (multiple-value-bind (repeat-map repeat-condition)
403 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
404 (multiple-value-bind (control-map control-condition)
405 (captured-read (lambda () (topic-maps.ctm:deserialize-string control-text :document-iri doc :source-name id)))
406 (multiple-value-bind (tokens lexical-condition) (lexical-evidence text id)
407 (let* ((lexical-witness
408 (if (equal name "qname-hyphen")
409 (and (null lexical-condition)
410 (equal (subseq tokens 3 6)
411 '((:identifier "ex" 36) (:punctuation #\: 38) (:punctuation #\- 39))))
412 t))
413 (control-ok (and (null control-condition)
414 (control-model-p control-map doc name control-kind control-value)))
415 (repeat-ok (and (null map) (null repeat-map)
416 (intended-rejection-p repeat-condition spec id)
417 (equal (condition-signature condition) (condition-signature repeat-condition))))
418 (qualified (and source-witness lexical-witness control-ok repeat-ok
419 (intended-rejection-p condition spec id))))
420 (check (concatenate 'string id ":immutable-original-and-existing-NOT-RUN-record")
421 (and (equal before (case-state case)) (equalp source (binary path))))
422 ;; Observing a different error is evidence, not a conformance PASS.
423 (when qualified
424 (topic-maps.test-foundation:record-result case :ctm-rejection
425 :diagnostics (list (princ-to-string condition))))
426 (check (concatenate 'string id ":only-reviewed-syntax-rejection-can-be-PASS")
427 (if qualified
428 (and (eq :pass (topic-maps.test-foundation:test-case-status case))
429 (eq :ctm-rejection (topic-maps.test-foundation:test-case-actual-outcome case)))
430 (equal before (case-state case))))
431 (format t "~&REGISTERED-NEGATIVE ~A: ~A; ~A~%" id
432 (topic-maps.test-foundation:test-case-status case) (or condition "ACCEPTED"))
433 (fields "id" id "sourcePathname" id "documentIRI" doc
434 "status" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-status case))
435 "actualOutcome" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-actual-outcome case))
436 "condition" (condition-data condition) "repeatCondition" (condition-data repeat-condition)
437 "clauses" (coerce (sixth spec) 'vector) "normativeReason" (seventh spec)
438 "normativeSource" "https://www.isotopicmaps.org/ctm/ctm.html"
439 "qualification" (when (equal name "string-unicode2")
440 "Printed production [43] includes all letters; rejection follows the hexadecimal decoding requirement in 3.4.2 prose, not that character class alone.")
441 "normativeValidated" (if qualified :true :false)
442 "sourceWitness" (if source-witness :true :false)
443 "lexicalWitness" (if lexical-witness :true :false)
444 "reproducibleCondition" (if repeat-ok :true :false)
445 "tokens" (coerce (loop for (kind value position) in tokens collect
446 (fields "kind" (topic-maps.test-foundation::word kind)
447 "value" (if (characterp value) (string value) value) "position" position)) 'vector)
448 "lexicalCondition" (condition-data lexical-condition)
449 "control" (fields "inMemoryOnly" :true "source" control-text
450 "accepted" (if control-ok :true :false) "condition" (condition-data control-condition)
451 "model" (when control-map (topic-maps.template.tests::observe control-map)))
452 "notRegisteredReason" (unless qualified
453 "Observed condition, syntax witness, reproducibility or accepted control did not establish the intended cause; original record remains NOT-RUN."))))))))))
454
455 (defun run-negative-tests ()
456 (unless topic-maps.ctm.tests::*cxtm-corpus* (run-tests))
457 (let* ((*checks* nil) (corpus topic-maps.ctm.tests::*cxtm-corpus*)
458 (cases (topic-maps.test-foundation:corpus-cases corpus))
459 (previous (mapcar (lambda (case) (cons case (case-state case))) cases))
460 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
461 (root (topic-maps.test-foundation:corpus-root corpus))
462 (base (format nil "https://ctm-tests.example.invalid/~A/"
463 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit)))
464 (ids (mapcar (lambda (spec) (format nil "ctm/invalid/~A.ctm" (first spec))) *negative-contracts*)))
465 (setf *negative-rows* nil *negative-entry-statuses*
466 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
467 (check "negative-registration:seven-distinct-existing-contracts"
468 (= 7 (length ids) (length (remove-duplicates ids :test #'equal))))
469 (dolist (spec *negative-contracts*)
470 (let* ((id (format nil "ctm/invalid/~A.ctm" (first spec)))
471 (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected)))
472 (check (concatenate 'string id ":unique-existing-negative-contract-and-document-IRI")
473 (and (= 1 (length selected)) (member case cases :test #'eq)
474 (eq :invalid (topic-maps.test-foundation:test-case-category case))
475 (eq :ctm-rejection (topic-maps.test-foundation:test-case-expected-outcome case))
476 (null (topic-maps.test-foundation:test-case-expected-baseline case))
477 (eq :not-run (topic-maps.test-foundation:test-case-status case))
478 (null (topic-maps.test-foundation:test-case-actual-outcome case))
479 (equal (truename (merge-pathnames id root))
480 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
481 (equal (concatenate 'string base id) (topic-maps.test-foundation:test-case-document-iri case))))
482 (push (exercise-negative-case case spec) *negative-rows*)))
483 (check "negative-registration:old-record-identities-and-CXTM-observations-preserved"
484 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
485 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
486 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
487 (every (lambda (entry)
488 (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
489 (equal (first (cdr entry)) (identity-fields (car entry)))
490 (equal (cdr entry) (case-state (car entry))))) previous)))
491 (let* ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))
492 (qualified (- (getf after :pass) (getf *negative-entry-statuses* :pass))))
493 (check "negative-registration:only-qualified-NOT-RUN-to-PASS-transitions"
494 (and (<= 0 qualified 7)
495 (= (- (getf *negative-entry-statuses* :not-run) qualified) (getf after :not-run))
496 (= (getf *negative-entry-statuses* :fail) (getf after :fail))
497 (= (getf *negative-entry-statuses* :unsupported) (getf after :unsupported))))
498 (check "negative-registration:unique-check-names"
499 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
500 (setf *negative-rows* (nreverse *negative-rows*) *negative-checks* (nreverse *checks*))
501 (format t "~&REGISTERED-NEGATIVES-PASS: ~D checks; ~D intended original rejections registered; ~D remain NOT-RUN~%"
502 (length *negative-checks*) qualified (- 7 qualified))))
503 t)
504
505 (defun write-negative-reports (prefix)
506 (with-open-file (out (concatenate 'string prefix ".negative.json")
507 :direction :output :if-exists :supersede :if-does-not-exist :create)
508 (topic-maps.test-foundation::emit-json-value
509 (fields "kind" "ctm-negative-original-execution" "passed" (length *negative-checks*) "failed" 0
510 "entryStatuses" (topic-maps.test-foundation::counts-object *negative-entry-statuses*)
511 "checks" (coerce *negative-checks* 'vector) "cases" (coerce *negative-rows* 'vector)) out)
512 (terpri out))
513 (topic-maps.ctm.tests::write-cxtm-report
514 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
515 :additional-checks *negative-checks*
516 :scope "Slice 20B: seven negative originals actually executed; only intended syntax rejections registered. Earlier positive comparisons and four known DateTime/Include FAILs preserved; full retained regression, Slice 20A and XTM evidence separate."))
517
518
519 ;;; Slice 20E: one more existing positive Case Record, using EXERCISE-CASE.
520 ;;; Run after the retained 20A/20B/20C stages to preserve their snapshots.
521 (defvar *empty2-checks* nil)
522 (defvar *empty2-row* nil)
523 (defvar *empty2-entry-statuses* nil)
524 (defun run-empty2-tests ()
525 (unless topic-maps.ctm.tests::*cxtm-corpus*
526 (run-tests)
527 (run-negative-tests)
528 (uiop:symbol-call :topic-maps.prolog.tests :run-tests))
529 (setf *empty2-checks* nil *empty2-row* nil)
530 (let* ((*checks* nil)
531 (corpus topic-maps.ctm.tests::*cxtm-corpus*)
532 (root (topic-maps.test-foundation:corpus-root corpus))
533 (id "ctm/in/empty2.ctm")
534 (selected (topic-maps.test-foundation:select-cases corpus :id id))
535 (case (first selected))
536 (cases (topic-maps.test-foundation:corpus-cases corpus))
537 (previous (mapcar (lambda (c) (cons c (case-state c))) cases))
538 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
539 (base (format nil "https://ctm-tests.example.invalid/~A/"
540 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit))))
541 (setf *empty2-entry-statuses*
542 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
543 (check "empty2:unique-existing-NOT-RUN-positive-contract"
544 (and (= 1 (length selected)) (member case cases :test #'eq)
545 (eq :valid (topic-maps.test-foundation:test-case-category case))
546 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
547 (eq :not-run (topic-maps.test-foundation:test-case-status case))
548 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
549 (check "empty2:pinned-source-baseline-and-document-IRI"
550 (and (equal (truename (merge-pathnames id root))
551 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
552 (equal (truename (merge-pathnames "ctm/baseline/empty2.ctm.cxtm" root))
553 (truename (topic-maps.test-foundation:test-case-expected-baseline case)))
554 (equal (concatenate 'string base id)
555 (topic-maps.test-foundation:test-case-document-iri case))))
556 (check "empty2:original-thirteen-source-bytes"
557 (equalp (binary (topic-maps.test-foundation:test-case-source-pathname case))
558 #(37 118 101 114 115 105 111 110 32 49 46 48 10)))
559 (check "empty2:original-twenty-three-baseline-bytes"
560 (equalp (binary (topic-maps.test-foundation:test-case-expected-baseline case))
561 #(60 116 111 112 105 99 77 97 112 62 10 60 47 116 111 112 105 99 77 97 112 62 10)))
562 (multiple-value-bind (row result) (exercise-case case :comments)
563 (setf *empty2-row* row)
564 ;; EXERCISE-CASE performs real file deserialization, the semantic witness,
565 ;; raw object/handle preservation and exact octet comparison before PASS.
566 (check "empty2:actual-original-byte-comparison-passes"
567 (and result (getf result :match) (= 23 (getf result :bytes))
568 (equal (format nil "<topicMap>~%</topicMap>~%") (getf result :actual))
569 (equal (getf result :actual) (getf result :expected))
570 (eq :pass (topic-maps.test-foundation:test-case-status case))
571 (eq :cxtm-match (topic-maps.test-foundation:test-case-actual-outcome case))))
572 (check "empty2:all-previous-case-records-and-observations-preserved"
573 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
574 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
575 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
576 (every (lambda (entry)
577 (if (eq case (car entry))
578 (equal (first (cdr entry)) (identity-fields case))
579 (equal (cdr entry) (case-state (car entry))))) previous)))
580 (setf topic-maps.ctm.tests::*cxtm-results*
581 (append topic-maps.ctm.tests::*cxtm-results* (list result))))
582 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
583 (check "empty2:only-one-NOT-RUN-to-PASS-transition"
584 (and (= (1+ (getf *empty2-entry-statuses* :pass)) (getf after :pass))
585 (= (1- (getf *empty2-entry-statuses* :not-run)) (getf after :not-run))
586 (= (getf *empty2-entry-statuses* :fail) (getf after :fail))
587 (= (getf *empty2-entry-statuses* :unsupported) (getf after :unsupported)))))
588 (check "empty2:unique-check-names"
589 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
590 (setf *empty2-checks* (nreverse *checks*))
591 (format t "~&EMPTY2-ORIGINAL ~A: ~A; 23 byte-exact CXTM bytes~%" id
592 (topic-maps.test-foundation:test-case-status case)))
593 (format t "~&EMPTY2-PASS: ~D checks; one actual original contract~%" (length *empty2-checks*))
594 t)
595
596 (defun write-empty2-reports (prefix)
597 (with-open-file (out (concatenate 'string prefix ".registration.json")
598 :direction :output :if-exists :supersede :if-does-not-exist :create)
599 (topic-maps.test-foundation::emit-json-value
600 (fields "kind" "ctm-registered-original-execution"
601 "passed" (length *empty2-checks*) "failed" 0
602 "entryStatuses" (topic-maps.test-foundation::counts-object *empty2-entry-statuses*)
603 "checks" (coerce *empty2-checks* 'vector) "cases" (vector *empty2-row*)) out)
604 (terpri out))
605 (topic-maps.ctm.tests::write-cxtm-report
606 (concatenate 'string prefix ".validation.json")
607 (concatenate 'string prefix ".cxtm.json")
608 :additional-checks *empty2-checks*
609 :scope "Slice 20E: empty2.ctm executed using the Slice 20A original comparison harness; empty Map, exact source and CXTM bytes, stable Document IRI and non-mutating canonicalization. All earlier contract records and four known FAILs retained."))