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)
8 ;; One selection of the existing discovered Case Records, not another registry.
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*))
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."
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")))))
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))))))
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))))))
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")))
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))
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)))))
107 (let* ((owner (local map doc (cond ((equal name "string-escape") "topic") ((equal name "string-escape2") "A00") (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 ((member name '("unicode-escape-u4-triple-string" "unicode-escape-u6-triple-string") :test #'equal)
113 (list (format nil "ZZ ~%Top")))
115 (and owner type (= 2 (length (topic-map-topics map))) (null (topic-map-associations map))
116 (same-set expected (mapcar #'topic-name-value names))
117 (every (lambda (n) (and (eq owner (topic-name-parent n)) (eq type (topic-name-type n))
118 (null (topic-name-scope n)) (null (topic-name-variants n)))) names))))))
120 (defun identity-fields (case)
121 (list (topic-maps.test-foundation:test-case-id case) (topic-maps.test-foundation:test-case-category case)
122 (topic-maps.test-foundation:test-case-source-pathname case) (topic-maps.test-foundation:test-case-document-iri case)
123 (topic-maps.test-foundation:test-case-expected-outcome case) (topic-maps.test-foundation:test-case-expected-baseline case)
124 (topic-maps.test-foundation:test-case-clauses case)))
125 (defun exercise-case (case group)
126 "Record only an actual comparison. Rejections/errors never become empty Maps."
127 (let* ((id (topic-maps.test-foundation:test-case-id case)) (doc (topic-maps.test-foundation:test-case-document-iri case))
128 (name (pathname-name (topic-maps.test-foundation:test-case-source-pathname case)))
129 (identity (copy-tree (identity-fields case))) (source (binary (topic-maps.test-foundation:test-case-source-pathname case)))
130 (evidence nil) (result nil))
132 (let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
133 (held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held))
134 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
135 (expected (binary (topic-maps.test-foundation:test-case-expected-baseline case))) (different (mismatch actual expected)))
136 (check (concatenate 'string id ":actual-TMDM-properties-and-EQ-references") (semantic-witness name group map doc))
137 (check (concatenate 'string id ":canonicalization-preserves-raw-objects-and-handles")
138 (equal before (topic-maps.import.tests::state map held)))
139 (check (concatenate 'string id ":deterministic-canonical-octets")
140 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)))
141 (check (concatenate 'string id ":unmodified-source-bytes") (equalp source (binary (topic-maps.test-foundation:test-case-source-pathname case))))
142 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
143 :diagnostics (when different (list (format nil "First different byte ~D; actual ~D bytes, expected ~D bytes." different (length actual) (length expected)))))
144 (setf evidence (fields "actualBytes" (length actual) "expectedBytes" (length expected)
145 "firstDifferentByte" different "model" (topic-maps.template.tests::observe map))
146 result (list :id id :base doc :actual (topic-maps.ctm::decode-utf8 actual)
147 :expected (topic-maps.ctm::decode-utf8 expected) :match (null different) :bytes (length actual))))
148 ((or topic-maps.ctm:ctm-unsupported topic-maps.cxtm:cxtm-unsupported) (condition)
149 (topic-maps.test-foundation:record-result case :unsupported :diagnostics (list (format nil "~A: ~A" (type-of condition) condition))))
151 (topic-maps.test-foundation:record-result case :execution-error :diagnostics (list (format nil "~A: ~A" (type-of condition) condition)))))
152 (check (concatenate 'string id ":same-contract-identity-after-execution") (equal identity (identity-fields case)))
154 (fields "id" id "group" (string-downcase (symbol-name group))
155 "sourcePathname" id "documentIRI" doc
156 "baseline" (format nil "ctm/baseline/~A.ctm.cxtm" name)
157 "status" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-status case))
158 "actualOutcome" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-actual-outcome case))
159 "diagnostics" (coerce (topic-maps.test-foundation:test-case-diagnostics case) 'vector)
160 "sourceBytes" (length source) "comparison" evidence)
164 (defun case-state (case)
166 (list (identity-fields case)
167 (topic-maps.test-foundation:test-case-status case)
168 (topic-maps.test-foundation:test-case-actual-outcome case)
169 (topic-maps.test-foundation:test-case-diagnostics case))))
172 ;; Use the same Case Records populated by the retained suites. A direct ASDF
173 ;; invocation may initialize those suites, but must never replace their corpus.
174 (unless topic-maps.ctm.tests::*cxtm-corpus*
175 (topic-maps.include.tests:run-tests))
176 (setf *checks* nil *rows* nil *results* nil)
177 (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*)
178 (root (topic-maps.test-foundation:corpus-root corpus))
179 (ids (loop for (group . names) in *groups*
180 append (mapcar (lambda (name) (format nil "ctm/in/~A.ctm" name)) names)))
181 (cases (topic-maps.test-foundation:corpus-cases corpus))
182 (previous (mapcar (lambda (case) (cons case (case-state case))) cases))
183 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
184 (base (format nil "https://ctm-tests.example.invalid/~A/"
185 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit))))
186 (setf *entry-statuses*
187 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
188 (check "registration:forty-distinct-selected-originals"
189 (and (= 40 (length ids)) (= 40 (length (remove-duplicates ids :test #'equal)))
190 (equal '(10 13 9 4 4) (mapcar (lambda (group) (length (cdr group))) *groups*))))
191 (check "registration:existing-corpus-correspondence"
192 (getf (topic-maps.test-foundation:verify-correspondence corpus) :ok))
193 (dolist (group *groups*)
194 (dolist (name (cdr group))
195 (let* ((id (format nil "ctm/in/~A.ctm" name))
196 (selected (topic-maps.test-foundation:select-cases corpus :id id))
197 (case (first selected)))
198 (check (concatenate 'string id ":unique-existing-positive-contract")
199 (and (= 1 (length selected)) (member case cases :test #'eq)
200 (eq :valid (topic-maps.test-foundation:test-case-category case))
201 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
202 (eq :not-run (topic-maps.test-foundation:test-case-status case))
203 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
204 (check (concatenate 'string id ":pinned-source-baseline-and-document-IRI")
205 (and (equal (truename (merge-pathnames id root))
206 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
207 (equal (truename (merge-pathnames (format nil "ctm/baseline/~A.ctm.cxtm" name) root))
208 (truename (topic-maps.test-foundation:test-case-expected-baseline case)))
209 (equal (concatenate 'string base id)
210 (topic-maps.test-foundation:test-case-document-iri case))))
211 (multiple-value-bind (row result) (exercise-case case (car group))
213 (when result (push result *results*)))
214 (format t "~®ISTERED-ORIGINAL ~A: ~A~{ ~A~}~%" id
215 (topic-maps.test-foundation:test-case-status case)
216 (topic-maps.test-foundation:test-case-diagnostics case)))))
217 (setf *rows* (nreverse *rows*) *results* (nreverse *results*))
218 (check "registration:forty-actual-byte-comparisons-pass"
219 (and (= 40 (length *rows*) (length *results*))
220 (every (lambda (result) (getf result :match)) *results*)
221 (every (lambda (id) (eq :pass (topic-maps.test-foundation:test-case-status
222 (first (topic-maps.test-foundation:select-cases corpus :id id))))) ids)))
223 (check "registration:all-previous-case-records-and-observations-preserved"
224 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
225 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
226 (every (lambda (entry)
227 (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
228 (equal (first (cdr entry)) (identity-fields (car entry)))
229 (equal (cdr entry) (case-state (car entry))))) previous)
230 (equal old-results topic-maps.ctm.tests::*cxtm-results*)))
231 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
232 (check "registration:only-forty-NOT-RUN-to-PASS-transitions"
233 (and (= (+ 40 (getf *entry-statuses* :pass)) (getf after :pass))
234 (= (- (getf *entry-statuses* :not-run) 40) (getf after :not-run))
235 (= (getf *entry-statuses* :fail) (getf after :fail))
236 (= (getf *entry-statuses* :unsupported) (getf after :unsupported)))))
237 (setf topic-maps.ctm.tests::*cxtm-results*
238 (append topic-maps.ctm.tests::*cxtm-results* *results*)))
239 (check "registration:unique-check-names"
240 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
241 (setf *checks* (nreverse *checks*))
242 (format t "~®ISTERED-ORIGINALS-PASS: ~D checks; forty actual byte-exact original contracts~%" (length *checks*))
245 (defun write-reports (prefix)
246 (with-open-file (out (concatenate 'string prefix ".registration.json")
247 :direction :output :if-exists :supersede :if-does-not-exist :create)
248 (topic-maps.test-foundation::emit-json-value
249 (fields "kind" "ctm-registered-original-execution"
250 "passed" (length *checks*) "failed" 0
251 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*)
252 "checks" (coerce *checks* 'vector) "cases" (coerce *rows* 'vector)) out)
254 (topic-maps.ctm.tests::write-cxtm-report
255 (concatenate 'string prefix ".validation.json")
256 (concatenate 'string prefix ".cxtm.json")
257 :additional-checks *checks*
258 :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."))
261 ;;; Slice 20B: a selection of seven existing negative Case Records.
262 ;;; Normative review: docs/slice-20b-negative-registration.md. No reader changes.
263 (defparameter *negative-contracts*
264 '(("occurrence-double-positive-infinity" :expected-occurrence-value 15 2 10
265 ("CTM 3.4.1 [28]-[34]" "CTM 3.10 [56]")
266 "Bare INF is neither a CTM number nor another literal alternative.")
267 ("occurrence-double-negative-infinity" :expected-occurrence-value 15 2 10
268 ("CTM 3.4.1 [28]-[34]" "CTM 3.10 [56]")
269 "Bare -INF is neither a CTM number nor another literal alternative.")
270 ("qname-hyphen" :unexpected-token 38 3 3
271 ("CTM 3.3.4 [9]-[11]" "CTM 3.3.6 [14]-[16]")
272 "A QName local part cannot start with a hyphen.")
273 ("string-unbalanced" :unterminated-string 8 1 9
274 ("CTM 3.4.1 [37]-[40]" "CTM 3.4.2 [41]")
275 "The escaped quote is content; the quoted string lacks a closing delimiter.")
276 ("string-unicode" :invalid-unicode-escape 11 1 12
278 "Uppercase U requires six hexadecimal digits; the closing quote follows four.")
279 ("string-unicode2" :invalid-unicode-escape 15 1 16
280 ("CTM 3.4.2 [42]-[43] and hexadecimal decoding prose")
281 "G cannot be decoded as a hexadecimal digit; see the printed-class qualification.")
282 ("topic-id-illegal" :unexpected-character 0 1 1
283 ("CTM 3.3.6 [14]-[16]")
284 "U+0361 is allowed inside an identifier, but not as its first character.")))
285 (defvar *negative-checks* nil)
286 (defvar *negative-rows* nil)
287 (defvar *negative-entry-statuses* nil)
289 (defun captured-read (thunk)
290 (handler-case (values (funcall thunk) nil)
291 (error (condition) (values nil condition))))
293 (defun condition-data (condition)
295 (let ((ctm-p (typep condition 'topic-maps.ctm:ctm-error)))
296 (fields "condition" (string (type-of condition)) "message" (princ-to-string condition)
297 "code" (when ctm-p (topic-maps.test-foundation::word (topic-maps.ctm:error-code condition)))
298 "position" (when ctm-p (topic-maps.ctm:error-position condition))
299 "line" (when ctm-p (topic-maps.ctm:error-line condition))
300 "column" (when ctm-p (topic-maps.ctm:error-column condition))
301 "positionUnit" (when ctm-p (topic-maps.test-foundation::word (topic-maps.ctm:error-position-unit condition)))
302 "source" (when ctm-p (topic-maps.ctm:error-source condition))
303 "cause" (when (and ctm-p (topic-maps.ctm:error-cause condition))
304 (princ-to-string (topic-maps.ctm:error-cause condition)))
305 "context" (when (and ctm-p (topic-maps.ctm:error-context condition))
306 (princ-to-string (topic-maps.ctm:error-context condition)))))))
308 (defun condition-signature (condition)
309 (when (typep condition 'topic-maps.ctm:ctm-error)
310 (list (type-of condition) (topic-maps.ctm:error-code condition)
311 (topic-maps.ctm:error-position condition) (topic-maps.ctm:error-line condition)
312 (topic-maps.ctm:error-column condition) (topic-maps.ctm:error-position-unit condition)
313 (topic-maps.ctm:error-source condition) (topic-maps.ctm:error-cause condition)
314 (topic-maps.ctm:error-context condition) (princ-to-string condition))))
316 (defun intended-rejection-p (condition spec id)
317 ;; Exact syntax class: unsupported/resource/wrapped model errors never qualify.
318 (and condition (eq (type-of condition) 'topic-maps.ctm:ctm-rejection)
319 (equal (subseq (condition-signature condition) 1 7)
320 (list (second spec) (third spec) (fourth spec) (fifth spec) :character id))
321 (null (topic-maps.ctm:error-cause condition))
322 (null (topic-maps.ctm:error-context condition))))
324 (defun replace-fragment (text old new)
325 (let ((position (search old text)))
326 (unless position (error "Missing reviewed source fragment ~S." old))
327 (concatenate 'string (subseq text 0 position) new (subseq text (+ position (length old))))))
329 (defun syntax-witness-and-control (name text)
330 "Check the offending source spelling; derive a valid in-memory sibling only."
332 ((equal name "occurrence-double-positive-infinity")
333 (values (equal text (format nil "topic~%occtype: INF."))
334 (replace-fragment text "INF" "1") :occurrence "1"))
335 ((equal name "occurrence-double-negative-infinity")
336 (values (equal text (format nil "topic~%occtype: -INF."))
337 (replace-fragment text "-INF" "-1") :occurrence "-1"))
338 ((equal name "qname-hyphen")
339 (values (equal text (format nil "%prefix ex http://psi.example.org/~%~%ex:-issue.~%"))
340 (replace-fragment text "ex:-issue" "ex:issue") :qname "http://psi.example.org/issue"))
341 ((equal name "string-unbalanced")
342 (values (equal text "topic - \"Foo\\\".")
343 (concatenate 'string (subseq text 0 (1- (length text))) "\".") :name "Foo\""))
344 ((equal name "string-unicode")
345 (values (equal text "a - \"\\U0041\".")
346 (replace-fragment text "\\U0041" "\\u0041") :name "A"))
347 ((equal name "string-unicode2")
348 (values (equal text (format nil "unicode - \"\\U00GG00\".~%"))
349 (replace-fragment text "\\U00GG00" "\\U000041") :name "A"))
350 ((equal name "topic-id-illegal")
351 (let ((mark (code-char #x361)))
352 (values (and (> (length text) 3) (char= mark (char text 0)) (equal (subseq text 1 3) "a."))
353 (concatenate 'string "a" (string mark) (subseq text 2))
354 :identifier (concatenate 'string "a" (string mark)))))
355 (t (error "Unreviewed negative contract ~A." name))))
357 (defun control-model-p (map doc name kind value)
360 (:qname (single-identity map :si value))
361 (:identifier (plain-locals-p map doc (list value)))
363 (let* ((owner (local map doc "topic")) (type (local map doc "occtype"))
364 (items (when owner (topic-occurrences owner))) (item (first items)))
365 (and owner type (= 2 (length (topic-map-topics map))) (= 1 (length items))
366 (null (topic-map-associations map))
367 (eq owner (occurrence-parent item)) (eq type (occurrence-type item))
368 (equal value (occurrence-value item)) (equal (integer-datatype) (occurrence-datatype item)))))
370 (let* ((owner (local map doc (cond ((equal name "string-unbalanced") "topic")
371 ((equal name "string-unicode") "a") (t "unicode"))))
372 (names (when owner (topic-names owner))) (item (first names))
373 (type (find-if (lambda (topic) (member (default-name-type-identifier)
374 (topic-subject-identifiers topic) :test #'equal)) (topic-map-topics map))))
375 (and owner type (= 2 (length (topic-map-topics map))) (= 1 (length names))
376 (null (topic-map-associations map)) (eq owner (topic-name-parent item))
377 (eq type (topic-name-type item)) (equal value (topic-name-value item))))))))
379 (defun lexical-evidence (text id)
380 ;; Explain the generic QName diagnostic from actual tokens, not its label alone.
381 (let ((topic-maps.ctm::*source-text* text) (topic-maps.ctm::*source-name* id)
382 (lexer (topic-maps.ctm::make-lexer text)) (tokens nil) (failure nil))
384 (loop for token = (topic-maps.ctm::next-token lexer) do
385 (push (list (topic-maps.ctm::token-kind token) (topic-maps.ctm::token-value token)
386 (topic-maps.ctm::token-position token)) tokens)
387 until (eq :eof (topic-maps.ctm::token-kind token)))
388 (error (condition) (setf failure condition)))
389 (values (nreverse tokens) failure)))
391 (defun exercise-negative-case (case spec)
392 (let* ((id (topic-maps.test-foundation:test-case-id case))
393 (doc (topic-maps.test-foundation:test-case-document-iri case))
394 (name (first spec)) (path (topic-maps.test-foundation:test-case-source-pathname case))
395 (source (binary path)) (text (topic-maps.ctm::decode-utf8 source))
396 (before (case-state case)))
397 (multiple-value-bind (source-witness control-text control-kind control-value)
398 (syntax-witness-and-control name text)
399 (multiple-value-bind (map condition)
400 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
401 (multiple-value-bind (repeat-map repeat-condition)
402 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
403 (multiple-value-bind (control-map control-condition)
404 (captured-read (lambda () (topic-maps.ctm:deserialize-string control-text :document-iri doc :source-name id)))
405 (multiple-value-bind (tokens lexical-condition) (lexical-evidence text id)
406 (let* ((lexical-witness
407 (if (equal name "qname-hyphen")
408 (and (null lexical-condition)
409 (equal (subseq tokens 3 6)
410 '((:identifier "ex" 36) (:punctuation #\: 38) (:punctuation #\- 39))))
412 (control-ok (and (null control-condition)
413 (control-model-p control-map doc name control-kind control-value)))
414 (repeat-ok (and (null map) (null repeat-map)
415 (intended-rejection-p repeat-condition spec id)
416 (equal (condition-signature condition) (condition-signature repeat-condition))))
417 (qualified (and source-witness lexical-witness control-ok repeat-ok
418 (intended-rejection-p condition spec id))))
419 (check (concatenate 'string id ":immutable-original-and-existing-NOT-RUN-record")
420 (and (equal before (case-state case)) (equalp source (binary path))))
421 ;; Observing a different error is evidence, not a conformance PASS.
423 (topic-maps.test-foundation:record-result case :ctm-rejection
424 :diagnostics (list (princ-to-string condition))))
425 (check (concatenate 'string id ":only-reviewed-syntax-rejection-can-be-PASS")
427 (and (eq :pass (topic-maps.test-foundation:test-case-status case))
428 (eq :ctm-rejection (topic-maps.test-foundation:test-case-actual-outcome case)))
429 (equal before (case-state case))))
430 (format t "~®ISTERED-NEGATIVE ~A: ~A; ~A~%" id
431 (topic-maps.test-foundation:test-case-status case) (or condition "ACCEPTED"))
432 (fields "id" id "sourcePathname" id "documentIRI" doc
433 "status" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-status case))
434 "actualOutcome" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-actual-outcome case))
435 "condition" (condition-data condition) "repeatCondition" (condition-data repeat-condition)
436 "clauses" (coerce (sixth spec) 'vector) "normativeReason" (seventh spec)
437 "normativeSource" "https://www.isotopicmaps.org/ctm/ctm.html"
438 "qualification" (when (equal name "string-unicode2")
439 "Printed production [43] includes all letters; rejection follows the hexadecimal decoding requirement in 3.4.2 prose, not that character class alone.")
440 "normativeValidated" (if qualified :true :false)
441 "sourceWitness" (if source-witness :true :false)
442 "lexicalWitness" (if lexical-witness :true :false)
443 "reproducibleCondition" (if repeat-ok :true :false)
444 "tokens" (coerce (loop for (kind value position) in tokens collect
445 (fields "kind" (topic-maps.test-foundation::word kind)
446 "value" (if (characterp value) (string value) value) "position" position)) 'vector)
447 "lexicalCondition" (condition-data lexical-condition)
448 "control" (fields "inMemoryOnly" :true "source" control-text
449 "accepted" (if control-ok :true :false) "condition" (condition-data control-condition)
450 "model" (when control-map (topic-maps.template.tests::observe control-map)))
451 "notRegisteredReason" (unless qualified
452 "Observed condition, syntax witness, reproducibility or accepted control did not establish the intended cause; original record remains NOT-RUN."))))))))))
454 (defun run-negative-tests ()
455 (unless topic-maps.ctm.tests::*cxtm-corpus* (run-tests))
456 (let* ((*checks* nil) (corpus topic-maps.ctm.tests::*cxtm-corpus*)
457 (cases (topic-maps.test-foundation:corpus-cases corpus))
458 (previous (mapcar (lambda (case) (cons case (case-state case))) cases))
459 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
460 (root (topic-maps.test-foundation:corpus-root corpus))
461 (base (format nil "https://ctm-tests.example.invalid/~A/"
462 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit)))
463 (ids (mapcar (lambda (spec) (format nil "ctm/invalid/~A.ctm" (first spec))) *negative-contracts*)))
464 (setf *negative-rows* nil *negative-entry-statuses*
465 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
466 (check "negative-registration:seven-distinct-existing-contracts"
467 (= 7 (length ids) (length (remove-duplicates ids :test #'equal))))
468 (dolist (spec *negative-contracts*)
469 (let* ((id (format nil "ctm/invalid/~A.ctm" (first spec)))
470 (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected)))
471 (check (concatenate 'string id ":unique-existing-negative-contract-and-document-IRI")
472 (and (= 1 (length selected)) (member case cases :test #'eq)
473 (eq :invalid (topic-maps.test-foundation:test-case-category case))
474 (eq :ctm-rejection (topic-maps.test-foundation:test-case-expected-outcome case))
475 (null (topic-maps.test-foundation:test-case-expected-baseline case))
476 (eq :not-run (topic-maps.test-foundation:test-case-status case))
477 (null (topic-maps.test-foundation:test-case-actual-outcome case))
478 (equal (truename (merge-pathnames id root))
479 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
480 (equal (concatenate 'string base id) (topic-maps.test-foundation:test-case-document-iri case))))
481 (push (exercise-negative-case case spec) *negative-rows*)))
482 (check "negative-registration:old-record-identities-and-CXTM-observations-preserved"
483 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
484 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
485 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
486 (every (lambda (entry)
487 (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
488 (equal (first (cdr entry)) (identity-fields (car entry)))
489 (equal (cdr entry) (case-state (car entry))))) previous)))
490 (let* ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))
491 (qualified (- (getf after :pass) (getf *negative-entry-statuses* :pass))))
492 (check "negative-registration:only-qualified-NOT-RUN-to-PASS-transitions"
493 (and (<= 0 qualified 7)
494 (= (- (getf *negative-entry-statuses* :not-run) qualified) (getf after :not-run))
495 (= (getf *negative-entry-statuses* :fail) (getf after :fail))
496 (= (getf *negative-entry-statuses* :unsupported) (getf after :unsupported))))
497 (check "negative-registration:unique-check-names"
498 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
499 (setf *negative-rows* (nreverse *negative-rows*) *negative-checks* (nreverse *checks*))
500 (format t "~®ISTERED-NEGATIVES-PASS: ~D checks; ~D intended original rejections registered; ~D remain NOT-RUN~%"
501 (length *negative-checks*) qualified (- 7 qualified))))
504 (defun write-negative-reports (prefix)
505 (with-open-file (out (concatenate 'string prefix ".negative.json")
506 :direction :output :if-exists :supersede :if-does-not-exist :create)
507 (topic-maps.test-foundation::emit-json-value
508 (fields "kind" "ctm-negative-original-execution" "passed" (length *negative-checks*) "failed" 0
509 "entryStatuses" (topic-maps.test-foundation::counts-object *negative-entry-statuses*)
510 "checks" (coerce *negative-checks* 'vector) "cases" (coerce *negative-rows* 'vector)) out)
512 (topic-maps.ctm.tests::write-cxtm-report
513 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
514 :additional-checks *negative-checks*
515 :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."))
518 ;;; Slice 20E: one more existing positive Case Record, using EXERCISE-CASE.
519 ;;; Run after the retained 20A/20B/20C stages to preserve their snapshots.
520 (defvar *empty2-checks* nil)
521 (defvar *empty2-row* nil)
522 (defvar *empty2-entry-statuses* nil)
523 (defun run-empty2-tests ()
524 (unless topic-maps.ctm.tests::*cxtm-corpus*
527 (uiop:symbol-call :topic-maps.prolog.tests :run-tests))
528 (setf *empty2-checks* nil *empty2-row* nil)
529 (let* ((*checks* nil)
530 (corpus topic-maps.ctm.tests::*cxtm-corpus*)
531 (root (topic-maps.test-foundation:corpus-root corpus))
532 (id "ctm/in/empty2.ctm")
533 (selected (topic-maps.test-foundation:select-cases corpus :id id))
534 (case (first selected))
535 (cases (topic-maps.test-foundation:corpus-cases corpus))
536 (previous (mapcar (lambda (c) (cons c (case-state c))) cases))
537 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
538 (base (format nil "https://ctm-tests.example.invalid/~A/"
539 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit))))
540 (setf *empty2-entry-statuses*
541 (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
542 (check "empty2:unique-existing-NOT-RUN-positive-contract"
543 (and (= 1 (length selected)) (member case cases :test #'eq)
544 (eq :valid (topic-maps.test-foundation:test-case-category case))
545 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
546 (eq :not-run (topic-maps.test-foundation:test-case-status case))
547 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
548 (check "empty2:pinned-source-baseline-and-document-IRI"
549 (and (equal (truename (merge-pathnames id root))
550 (truename (topic-maps.test-foundation:test-case-source-pathname case)))
551 (equal (truename (merge-pathnames "ctm/baseline/empty2.ctm.cxtm" root))
552 (truename (topic-maps.test-foundation:test-case-expected-baseline case)))
553 (equal (concatenate 'string base id)
554 (topic-maps.test-foundation:test-case-document-iri case))))
555 (check "empty2:original-thirteen-source-bytes"
556 (equalp (binary (topic-maps.test-foundation:test-case-source-pathname case))
557 #(37 118 101 114 115 105 111 110 32 49 46 48 10)))
558 (check "empty2:original-twenty-three-baseline-bytes"
559 (equalp (binary (topic-maps.test-foundation:test-case-expected-baseline case))
560 #(60 116 111 112 105 99 77 97 112 62 10 60 47 116 111 112 105 99 77 97 112 62 10)))
561 (multiple-value-bind (row result) (exercise-case case :comments)
562 (setf *empty2-row* row)
563 ;; EXERCISE-CASE performs real file deserialization, the semantic witness,
564 ;; raw object/handle preservation and exact octet comparison before PASS.
565 (check "empty2:actual-original-byte-comparison-passes"
566 (and result (getf result :match) (= 23 (getf result :bytes))
567 (equal (format nil "<topicMap>~%</topicMap>~%") (getf result :actual))
568 (equal (getf result :actual) (getf result :expected))
569 (eq :pass (topic-maps.test-foundation:test-case-status case))
570 (eq :cxtm-match (topic-maps.test-foundation:test-case-actual-outcome case))))
571 (check "empty2:all-previous-case-records-and-observations-preserved"
572 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
573 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
574 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
575 (every (lambda (entry)
576 (if (eq case (car entry))
577 (equal (first (cdr entry)) (identity-fields case))
578 (equal (cdr entry) (case-state (car entry))))) previous)))
579 (setf topic-maps.ctm.tests::*cxtm-results*
580 (append topic-maps.ctm.tests::*cxtm-results* (list result))))
581 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
582 (check "empty2:only-one-NOT-RUN-to-PASS-transition"
583 (and (= (1+ (getf *empty2-entry-statuses* :pass)) (getf after :pass))
584 (= (1- (getf *empty2-entry-statuses* :not-run)) (getf after :not-run))
585 (= (getf *empty2-entry-statuses* :fail) (getf after :fail))
586 (= (getf *empty2-entry-statuses* :unsupported) (getf after :unsupported)))))
587 (check "empty2:unique-check-names"
588 (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
589 (setf *empty2-checks* (nreverse *checks*))
590 (format t "~&EMPTY2-ORIGINAL ~A: ~A; 23 byte-exact CXTM bytes~%" id
591 (topic-maps.test-foundation:test-case-status case)))
592 (format t "~&EMPTY2-PASS: ~D checks; one actual original contract~%" (length *empty2-checks*))
595 (defun write-empty2-reports (prefix)
596 (with-open-file (out (concatenate 'string prefix ".registration.json")
597 :direction :output :if-exists :supersede :if-does-not-exist :create)
598 (topic-maps.test-foundation::emit-json-value
599 (fields "kind" "ctm-registered-original-execution"
600 "passed" (length *empty2-checks*) "failed" 0
601 "entryStatuses" (topic-maps.test-foundation::counts-object *empty2-entry-statuses*)
602 "checks" (coerce *empty2-checks* 'vector) "cases" (vector *empty2-row*)) out)
604 (topic-maps.ctm.tests::write-cxtm-report
605 (concatenate 'string prefix ".validation.json")
606 (concatenate 'string prefix ".cxtm.json")
607 :additional-checks *empty2-checks*
608 :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."))