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