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