(defpackage #:topic-maps.xtm.tests
(:use #:cl #:topic-maps.tmdm)
(:import-from #:topic-maps.xtm #:deserialize-octets #:xtm-rejection #:xtm-unsupported
#:xtm-xml-error #:error-code #:error-document-iri #:error-source-key #:error-line #:error-column)
(:export #:run-tests #:write-report))
(in-package #:topic-maps.xtm.tests)
(defparameter *document* "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm")
(defvar *checks* nil)
(defvar *cases* nil)
(defvar *outputs* nil)
(defvar *fixture* nil)
(defvar *negative-outputs* nil)
(defvar *negative-controls* nil)
(defvar *name-outputs* nil)
(defvar *name-controls* nil)
(defvar *scope-outputs* nil)
(defvar *scope-controls* nil)
(defvar *iid-outputs* nil)
(defvar *iid-controls* nil)
(defvar *variant-outputs* nil)
(defvar *variant-negative-outputs* nil)
(defvar *variant-controls* nil)
(defun check (name truth)
(push name *checks*)
(unless truth (error "XTM test failed: ~A" name))
(format t "XTM-CHECK-PASS ~A~%" name))
(defun bytes (text &optional (encoding :utf-8) bom)
(concatenate '(vector (unsigned-byte 8))
(if bom (ecase encoding (:utf-8 #(239 187 191)) (:utf-16le #(255 254)) (:utf-16be #(254 255))) #())
(babel:string-to-octets text :encoding encoding :use-bom nil)))
(defun file-bytes (path)
(with-open-file (in path :element-type '(unsigned-byte 8))
(let ((data (make-array (file-length in) :element-type '(unsigned-byte 8))))
(assert (= (read-sequence data in) (length data))) data)))
(defun corpus-path (relative) (merge-pathnames relative (topic-maps.test-foundation:default-corpus-root)))
(defun document (body) (format nil "~A" body))
(defun read-body (body &optional (iri *document*))
(deserialize-octets (bytes (document body)) :document-iri iri :source-key "local:xtm-semantic-test"))
(defun by-id (map name)
(find-if (lambda (topic) (member (concatenate 'string *document* "#" name)
(topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))
(defun by-si (map iri) (find iri (topic-map-topics map) :test (lambda (iri topic) (member iri (topic-subject-identifiers topic) :test #'string=))))
(defun core (map name) (by-si map (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
(defun condition-from (thunk) (handler-case (progn (funcall thunk) nil) (error (c) c)))
(defun rejection (name source class code)
(let ((c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key name)))))
(check (concatenate 'string name ":class") (typep c class))
(check (concatenate 'string name ":code") (eq code (error-code c)))
(check (concatenate 'string name ":context") (and (equal (error-document-iri c) *document*) (equal (error-source-key c) name)))
(check (concatenate 'string name ":position") (and (integerp (error-line c)) (plusp (error-line c))
(integerp (error-column c)) (not (minusp (error-column c)))))))
(defun public-state (map)
;; Actual references are retained in the snapshot, not only equivalent XML.
(list (item-identifiers map)
(mapcar (lambda (topic) (list topic (topic-parent topic) (topic-item-identifiers topic)
(topic-subject-identifiers topic) (topic-subject-locators topic)
(topic-names topic) (topic-occurrences topic) (topic-roles-played topic))) (topic-map-topics map))
(mapcar (lambda (a) (list a (association-parent a) (association-type a) (association-scope a) (item-identifiers a)
(mapcar (lambda (r) (list r (association-role-parent r) (association-role-type r)
(association-role-player r) (item-identifiers r))) (association-roles a))))
(topic-map-associations map))))
(defun graph-checks (label map owner type)
(let* ((a (first (topic-map-associations map))) (roles (association-roles a))
(instance (find (core map "instance") roles :key #'association-role-type :test #'eq))
(type-role (find (core map "type") roles :key #'association-role-type :test #'eq)))
(check (concatenate 'string label ":counts") (and (= 5 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= 2 (length roles))))
(check (concatenate 'string label ":map-parent-eq") (and (eq map (association-parent a)) (every (lambda (topic) (eq map (topic-parent topic))) (topic-map-topics map))))
(check (concatenate 'string label ":association-type-eq") (eq (association-type a) (core map "type-instance")))
(check (concatenate 'string label ":role-parent-eq") (every (lambda (role) (eq a (association-role-parent role))) roles))
(check (concatenate 'string label ":players-eq") (and (eq owner (association-role-player instance)) (eq type (association-role-player type-role))))
(check (concatenate 'string label ":roles-played-eq") (and (member instance (topic-roles-played owner) :test #'eq) (member type-role (topic-roles-played type) :test #'eq)))
(check (concatenate 'string label ":canonical-handles") (and (eq a (canonical-association a)) (every (lambda (r) (eq r (canonical-association-role r))) roles)))))
;; Slice 21A's twenty successful, previously unregistered originals. Keep this
;; explicit: similar filenames and .sub helpers are not additional contracts.
(defparameter *additional-originals*
'("itemid-duplicate" "itemid-fragment" "itemid-relative" "itemid"
"merge-itemid-with-types" "merge-itemid" "merge-subjid" "merge-subjloc"
"subjid-duplicate" "subjid-escaping" "subjid-escaping2" "subjid-fragment"
"subjid-relative" "subjid-sameas-itemid" "subjid"
"subjloc-duplicate" "subjloc-fragment" "subjloc-multiple" "subjloc-relative" "subjloc"))
(defun references-current-p (map)
(let ((topics (topic-map-topics map)) (associations (topic-map-associations map)))
(flet ((live (topic) (and (member topic topics :test #'eq) (eq topic (canonical-topic topic)))))
(and
(every (lambda (topic) (and (live topic) (eq map (topic-parent topic))
(every (lambda (role) (and (eq topic (association-role-player role))
(member (association-role-parent role) associations :test #'eq)
(member role (association-roles (association-role-parent role)) :test #'eq)))
(topic-roles-played topic)))) topics)
(every (lambda (a) (and (eq a (canonical-association a)) (eq map (association-parent a))
(live (association-type a)) (every #'live (association-scope a))
(every (lambda (role) (and (eq role (canonical-association-role role))
(eq a (association-role-parent role)) (live (association-role-type role))
(live (association-role-player role))
(member role (topic-roles-played (association-role-player role)) :test #'eq)))
(association-roles a)))) associations)))))
(defun same-identifiers-p (a b)
(and (= (length a) (length b)) (null (set-exclusive-or a b :test #'equal))))
(defun additional-model-checks (name map)
(let* ((topic (by-id map "topic"))
(ii (list (concatenate 'string *document* "#topic"))) (si nil) (sl nil)
(directory (subseq *document* 0 (1+ (position #\/ *document* :from-end t))))
(shared "http://example.org/#topic"))
(check (concatenate 'string name ":declared-topic-live-eq")
(and topic (eq topic (canonical-topic topic)) (member topic (topic-map-topics map) :test #'eq)))
(cond
((equal name "itemid-fragment") (push (concatenate 'string *document* "#topic2") ii))
((equal name "itemid-relative") (push (concatenate 'string directory "more-tests/test.xtm#topic") ii))
((equal name "itemid") (push shared ii))
((member name '("merge-itemid" "merge-itemid-with-types" "merge-subjid" "merge-subjloc") :test #'equal)
(push (concatenate 'string *document* "#topic2") ii)
(cond ((member name '("merge-itemid" "merge-itemid-with-types") :test #'equal) (push shared ii))
((equal name "merge-subjid") (push shared si))
(t (push shared sl)))
(check (concatenate 'string name ":merged-declarations-eq") (eq topic (by-id map "topic2"))))
((member name '("subjid-duplicate" "subjid-relative") :test #'equal)
(push (concatenate 'string directory "photo.jpg") si))
((equal name "subjid-escaping") (push "http://example.org/test+folder/#topic" si))
((equal name "subjid-escaping2") (push "http://example.org/test folder/#topic" si))
((equal name "subjid-fragment") (push (concatenate 'string *document* "#topic") si))
((equal name "subjid-sameas-itemid")
(push "http://psi.example.org/test" ii) (push "http://psi.example.org/test" si))
((equal name "subjid") (push shared si))
((member name '("subjloc-duplicate" "subjloc-relative") :test #'equal)
(push (concatenate 'string directory "photo.jpg") sl))
((equal name "subjloc-fragment") (push (concatenate 'string *document* "#topic") sl))
((equal name "subjloc-multiple") (setf sl (list shared "http://example.org/#subject")))
((equal name "subjloc") (push shared sl)))
(check (concatenate 'string name ":exact-ii-set") (same-identifiers-p ii (topic-item-identifiers topic)))
(check (concatenate 'string name ":exact-si-set") (same-identifiers-p si (topic-subject-identifiers topic)))
(check (concatenate 'string name ":exact-sl-set") (same-identifiers-p sl (topic-subject-locators topic)))
(check (concatenate 'string name ":no-unrequested-statements-or-reification")
(and (null (item-identifiers map)) (null (item-reifier map))
(every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic))
(null (topic-reified topic)))) (topic-map-topics map))))
(when (equal name "merge-itemid-with-types")
(dolist (type-id '("topictype1" "topictype2"))
(let* ((type (by-id map type-id))
(role (find type (loop for a in (topic-map-associations map) append (association-roles a))
:key #'association-role-player :test #'eq))
(a (and role (association-role-parent role))))
(check (concatenate 'string name ":forward-reference:" type-id)
(and type role (eq type (canonical-topic type))
(equal (list (concatenate 'string *document* "#" type-id)) (topic-item-identifiers type))
(eq (association-role-type role) (core map "type"))
(eq (association-type a) (core map "type-instance"))
(= 2 (length (association-roles a)))
(find-if (lambda (r) (and (eq topic (association-role-player r))
(eq (core map "instance") (association-role-type r)))) (association-roles a))))))
(dolist (core-id '("type" "instance" "type-instance"))
(check (concatenate 'string name ":core-no-extra-ii:" core-id)
(null (topic-item-identifiers (core map core-id))))))))
(defun original-condition-data (condition)
(when condition
(let ((code (cond ((typep condition 'topic-maps.xtm:xtm-error) (error-code condition))
((typep condition 'item-identifier-error) (item-identifier-error-code condition)))))
(topic-maps.test-foundation::fields
"class" (string (type-of condition)) "code" (when code (string-downcase (string code)))
"message" (princ-to-string condition)))))
(defun record-original-result (case actual expected condition)
;; No rejection (XML, XTM or TMDM) can satisfy a positive original contract.
(topic-maps.test-foundation:record-result case
(cond ((typep condition 'xtm-unsupported) :unsupported) (condition :execution-error)
((equalp actual expected) :cxtm-match) (t :cxtm-mismatch))
:diagnostics (list (if condition (format nil "~A: ~A" (type-of condition) condition)
(format nil "Actual ~D bytes; original baseline ~D bytes." (length actual) (length expected))))))
(defun original-model-data (map)
(when map
(flet ((array (items) (coerce items 'vector))
(fields (&rest args) (apply #'topic-maps.test-foundation::fields args)))
(let ((topics (topic-map-topics map)))
(labels ((ref (topic) (1+ (position topic topics :test #'eq))))
(fields "topicCount" (length topics) "associationCount" (length (topic-map-associations map))
"canonicalParentsAndInverseReferences" (if (references-current-p map) :true :false)
"topics" (array (loop for topic in topics collect
(fields "ordinal" (ref topic) "itemIdentifiers" (array (topic-item-identifiers topic))
"subjectIdentifiers" (array (topic-subject-identifiers topic)) "subjectLocators" (array (topic-subject-locators topic)))))
"associations" (array (loop for a in (topic-map-associations map) collect
(fields "type" (ref (association-type a)) "roles" (array (loop for role in (association-roles a) collect
(fields "type" (ref (association-role-type role)) "player" (ref (association-role-player role))))))))))))))
(defun original-contracts ()
(setf *cases* nil *outputs* nil)
(dolist (name (append '("empty" "topic" "itemid-tm" "topic-type" "topic-type-duplicate" "merge-three-way") *additional-originals*))
(let* ((id (format nil "xtm2/in/~A.xtm" name))
(iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
(case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
:document-iri iri :expected-outcome :cxtm-equal :expected-baseline baseline
:clauses '("XTM 2.0 4.3-4.9,4.20" "TMDM 5.1-5.3,5.7-5.8,6,7.2" "CXTM 3.13,3.18,4")))
(raw (file-bytes source)) (raw-before (copy-seq raw))
(expected (file-bytes baseline)) (map nil) (actual nil) (condition nil) (before nil))
(push case *cases*)
(handler-case
(setf map (deserialize-octets raw :document-iri iri :source-key id)
before (public-state map)
actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri))
(error (c) (setf condition c)))
(record-original-result case actual expected condition)
(push (list id actual expected (original-model-data map) (original-condition-data condition) (length raw)) *outputs*)
(when condition (error condition))
(check (concatenate 'string name ":original-byte-exact") (equalp actual expected))
(check (concatenate 'string name ":pure") (equal before (public-state map)))
(check (concatenate 'string name ":deterministic") (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri)))
(check (concatenate 'string name ":repeat-pure") (equal before (public-state map)))
(check (concatenate 'string name ":topic-count") (= (length (topic-map-topics map))
(cond ((member name '("empty" "itemid-tm") :test #'equal) 0)
((member name '("topic-type" "topic-type-duplicate") :test #'equal) 5)
((equal name "merge-itemid-with-types") 6) (t 1))))
(check (concatenate 'string name ":association-count") (= (length (topic-map-associations map))
(cond ((member name '("topic-type" "topic-type-duplicate") :test #'equal) 1)
((equal name "merge-itemid-with-types") 2) (t 0))))
(check (concatenate 'string name ":canonical-parent-and-inverse-eq") (references-current-p map))
(check (concatenate 'string name ":source-octets-preserved") (equalp raw raw-before))
(when (member name *additional-originals* :test #'equal)
(let* ((*document* iri)
(other (deserialize-octets raw :document-iri iri :source-key "different-provenance-only")))
(additional-model-checks name map)
(check (concatenate 'string name ":source-key-only-provenance")
(and (equalp (original-model-data map) (original-model-data other))
(equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator iri))))))
(when (equal name "topic")
(check "topic:actual-item-locator" (equal (topic-item-identifiers (first (topic-map-topics map))) (list (concatenate 'string iri "#topic")))))
(when (equal name "itemid-tm")
(check "map:actual-item-locator" (equal (item-identifiers map) (list (concatenate 'string iri "#the-topic-map")))))
(when (equal name "merge-three-way")
(check "merge:three-identifiers" (= 3 (length (topic-item-identifiers (first (topic-map-topics map)))))))
(when (member name '("topic-type" "topic-type-duplicate") :test #'equal)
(let ((*document* iri)) (graph-checks name map (by-id map "xtm") (by-id map "format"))))))
(setf *cases* (nreverse *cases*) *outputs* (nreverse *outputs*))
(check "originals:26-unique-case-records"
(and (= 26 (length *cases*))
(= 26 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
(defun result-classification-tests ()
(flet ((case-record () (topic-maps.test-foundation:make-test-case
:id "local:classification-control" :category :valid :expected-outcome :cxtm-equal)))
(let* ((case (case-record)) (expected (third (first *outputs*))) (actual (copy-seq expected)))
(setf (aref actual 0) (logxor 1 (aref actual 0)))
(record-original-result case actual expected nil)
(check "classification:changed-actual-cxtm-is-fail"
(and (eq :fail (topic-maps.test-foundation:test-case-status case))
(eq :cxtm-mismatch (topic-maps.test-foundation:test-case-actual-outcome case)))))
(dolist (spec (list (list "xml" "" 'xtm-xml-error :fail)
(list "xtm" (document "") 'xtm-rejection :fail)
(list "tmdm" (document "") 'item-identifier-error :fail)
(list "unsupported" (document "N") 'xtm-unsupported :unsupported)))
(let ((case (case-record))
(c (condition-from (lambda () (deserialize-octets (bytes (second spec)) :document-iri *document*)))))
(record-original-result case nil #() c)
(check (concatenate 'string "classification:" (first spec) ":never-positive-pass")
(and (typep c (third spec)) (eq (fourth spec) (topic-maps.test-foundation:test-case-status case))
(not (eq :cxtm-match (topic-maps.test-foundation:test-case-actual-outcome case)))))))))
(defun semantic-tests ()
(dolist (body '(""
""))
(let ((map (read-body body)))
(graph-checks (if (search "id='b'/>" body :start2 0 :end2 (min 20 (length body))) "backward" "forward") map (by-id map "a") (by-id map "b"))))
(let* ((map (read-body ""))
(type (by-id map "type")))
(graph-checks "si-hit" map (by-id map "a") type)
(check "si-hit:no-extra-ii" (equal (topic-item-identifiers type) (list (concatenate 'string *document* "#type")))))
(let* ((map (read-body ""))
(type (by-id map "type")))
(graph-checks "forward-si" map (by-id map "a") type)
(check "forward-si:created-ii-retained" (= 2 (length (topic-item-identifiers type)))))
(let* ((map (read-body ""))
(type (by-id map "type")))
(graph-checks "ii-hit" map (by-id map "a") type)
(check "ii-hit:identifiers-unchanged" (= 2 (length (topic-item-identifiers type)))))
(let* ((map (make-topic-map)) (si (intern-topic map :subject-identifier (concatenate 'string *document* "#s")))
(hit (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#s"))))
(check "direct-si:actual-eq" (eq si hit))
(check "direct-si:no-ii" (null (topic-item-identifiers si))))
;; Retain the exact object produced by a forward reference, then deserialize
;; a later declaration with the same reader operation in the same context.
(let* ((map (make-topic-map))
(old (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#future")))
(other (intern-topic map :subject-identifier "urn:shared"))
(a (make-association map (core-topic-for-test map "type-instance")
(list (list (core-topic-for-test map "instance") old) (list (core-topic-for-test map "type") other))))
(role (find old (association-roles a) :key #'association-role-player :test #'eq)))
(let* ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "retained-forward")
(root (topic-maps.xml:parse-octets
(bytes (document ""))
(make-instance 'topic-maps.xtm::source-handler))))
(topic-maps.xtm::validate-source root)
(topic-maps.xtm::read-topic (first (topic-maps.xtm::elements root)) map))
(check "retained:forward-handle-eq" (and (eq (canonical-topic old) (canonical-topic other)) (not (eq old (canonical-topic old)))))
(check "retained:role-player-canonical" (eq (association-role-player role) (canonical-topic old)))
(check "retained:role-parent-canonical" (eq (association-role-parent role) (canonical-association a)))
(let ((before (public-state map)) (pointer (topic-maps.tmdm::%topic-forward old)))
(topic-maps.cxtm:canonicalize map :base-locator *document*)
(check "retained:cxtm-does-not-mutate" (and (equal before (public-state map)) (eq pointer (topic-maps.tmdm::%topic-forward old))))))
(let ((a (read-body "") ) (b (read-body "" "https://other.example/map.xtm")))
(check "documents:distinct-object" (not (eq (first (topic-map-topics a)) (first (topic-map-topics b)))))
(check "documents:distinct-local-locators" (not (equal (topic-item-identifiers (first (topic-map-topics a))) (topic-item-identifiers (first (topic-map-topics b)))))))
(let ((map (read-body "")))
(check "identity:three-categories" (let ((topic (first (topic-map-topics map))))
(and (= 2 (length (topic-item-identifiers topic)))
(equal '("urn:subject") (topic-subject-identifiers topic))
(equal (list (concatenate 'string (subseq *document* 0 (1+ (position #\/ *document* :from-end t))) "resource")) (topic-subject-locators topic))))))
(let ((map (read-body "")))
(check "unicode:id-collapse" (by-id map "Ä水"))
(check "unicode:href-percent-utf8" (equal '("http://example.org/ä/🦊") (topic-subject-identifiers (first (topic-map-topics map))))))
(dolist (pair '(("../folder/./x#t" "https://example.test/folder/x#t")
("#t" "https://example.test/a/map.xtm#t")
("?q#t" "https://example.test/a/map.xtm?q#t")
("http://EXAMPLE.test:80/a" "http://EXAMPLE.test:80/a")
("http://example.test/a%20b#t" "http://example.test/a b#t")))
(let ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "iri-test"))
(check (concatenate 'string "iri:" (first pair))
(equal (topic-maps.xtm::resolve-iri (first pair) "https://example.test/a/map.xtm" nil) (second pair)))))
(let* ((source (document ""))
(one (read-body ""))
(two (read-body "")))
(check "order:canonical-independence" (equal (topic-maps.cxtm:canonicalize one :base-locator *document*) (topic-maps.cxtm:canonicalize two :base-locator *document*)))
(dolist (spec '((:utf-8 t "UTF-8") (:utf-16le t "UTF-16") (:utf-16be t "UTF-16")
(:utf-16le nil "UTF-16LE") (:utf-16be nil "UTF-16BE")))
(destructuring-bind (encoding bom label) spec
(let* ((xml (format nil "~A" label source))
(map (deserialize-octets (bytes xml encoding bom) :document-iri *document*)))
(check (format nil "encoding:~A:~A" encoding bom) (equal (topic-maps.cxtm:canonicalize one :base-locator *document*) (topic-maps.cxtm:canonicalize map :base-locator *document*))))))))
(defun core-topic-for-test (map name)
(intern-topic map :subject-identifier (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
(defun identity-cascade-tests ()
(let ((map (read-body "")))
(check "transitive:mixed-identifiers-one-topic" (= 1 (length (topic-map-topics map))))
(check "transitive:three-ii-two-si" (let ((topic (first (topic-map-topics map))))
(and (= 3 (length (topic-item-identifiers topic))) (= 2 (length (topic-subject-identifiers topic)))))))
(let* ((map (read-body ""))
(s (by-id map "s")) (x (by-id map "x")))
(check "reference:subject-locator-is-not-si-or-ii" (and s x (not (eq s x)) (= 6 (length (topic-map-topics map))))))
(let* ((map (make-topic-map)) (topic-maps.xtm::*document-iri* *document*)
(topic-maps.xtm::*source-key* "association-cascade")
(root (topic-maps.xml:parse-octets
(bytes (document ""))
(make-instance 'topic-maps.xtm::source-handler)))
(nodes (topic-maps.xtm::elements (topic-maps.xtm::validate-source root))))
(topic-maps.xtm::read-topic (first nodes) map)
(let* ((associations (topic-map-associations map))
(roles (loop for a in associations append (association-roles a)))
(old-type (by-id map "t2")))
(check "cascade:before-two-associations" (= 2 (length associations)))
(topic-maps.xtm::read-topic (second nodes) map)
(let ((a (first (topic-map-associations map))))
(check "cascade:after-one-association" (and (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a)))))
(check "cascade:retained-association-handles-eq" (every (lambda (old) (eq a (canonical-association old))) associations))
(check "cascade:retained-role-handles-eq" (every (lambda (r) (member (canonical-association-role r) (association-roles a) :test #'eq)) roles))
(check "cascade:stored-type-player-canonical" (every (lambda (r) (and (eq a (association-role-parent r))
(eq (association-role-type r) (canonical-topic (association-role-type r)))
(eq (association-role-player r) (canonical-topic (association-role-player r))))) (association-roles a)))
(check "cascade:retained-topic-handle-eq" (eq (canonical-topic old-type) (by-id map "t1")))
(let* ((handles (append associations roles (list old-type)))
(pointers (mapcar #'topic-maps.tmdm::canonical-item handles))
(before (public-state map)))
(topic-maps.cxtm:canonicalize map :base-locator *document*)
(check "cascade:all-handles-pure" (and (equal before (public-state map))
(every #'eq pointers (mapcar #'topic-maps.tmdm::canonical-item handles))))))))
(let* ((prefixed "")
(source (bytes prefixed))
(a (deserialize-octets source :document-iri *document* :source-key "disk:a"))
(b (deserialize-octets source :document-iri *document* :source-key "memory:b")))
(check "namespace:prefix-equivalent" (by-id a "a"))
(check "source-key:not-topic-identity" (equal (topic-item-identifiers (by-id a "a")) (topic-item-identifiers (by-id b "a")))))
(let* ((map (read-body ""))
(type (find-if (lambda (topic) (member "https://external.example/map.xtm#unfetched" (topic-item-identifiers topic) :test #'equal)) (topic-map-topics map))))
(graph-checks "external-reference:no-retrieval" map (by-id map "a") type)))
(defun negative-tests ()
(dolist (spec '(("missing-id" "" :missing-id)
("invalid-id" "" :invalid-id)
("id-colon" "" :invalid-id)
("id-empty" "" :invalid-id)
("id-spaces" "" :invalid-id)
("duplicate-id" "" :duplicate-id)
("unknown-attribute" "" :unexpected-attribute)
("qualified-attribute" "" :unexpected-attribute)
("unknown-element" "" :unexpected-element)
("wrong-namespace-child" "" :invalid-namespace)
("topic-identity-order" "" :element-order)
("map-identity-order" "" :element-order)
("instance-duplicate" "" :duplicate-instance-of)
("empty-instance" "" :empty-instance-of)
("missing-href" "" :missing-attribute)
("nonempty-locator" "" :unexpected-child)
("locator-text" "text" :unexpected-text)
("non-whitespace" "text" :unexpected-text)
("no-ref-fragment" "" :invalid-topic-ref)
("bad-ref-fragment" "" :invalid-topic-ref)
("empty-ref-fragment" "" :invalid-topic-ref)
("xpointer-ref" "" :invalid-topic-ref)
("relative-colon" "" :invalid-iri)
("decoded-control" "" :invalid-iri)
("bad-percent" "" :invalid-percent-escape)
("bad-percent-utf8" "" :invalid-percent-utf8)
("invalid-iri" "" :invalid-iri)))
(rejection (first spec) (document (second spec)) 'xtm-rejection (third spec)))
(rejection "missing-version" "" 'xtm-rejection :missing-attribute)
(rejection "version-21" "" 'xtm-rejection :invalid-version)
(rejection "no-namespace" "" 'xtm-rejection :invalid-namespace)
(rejection "namespace-10" "" 'xtm-rejection :invalid-namespace)
(rejection "xml-misnested" "" 'xtm-xml-error :xml-not-well-formed)
(rejection "xml-duplicate-attribute" "" 'xtm-xml-error :xml-not-well-formed)
(rejection "xml-undeclared-prefix" "" 'xtm-xml-error :xml-not-well-formed)
(dolist (body '("" ""
""
"]>"))
(rejection (concatenate 'string "doctype:" body) (concatenate 'string body (document "")) 'xtm-xml-error :doctype-forbidden))
(dolist (spec '(("unsupported-name-variant-resourceRef" "Name" :unsupported-construct)
("unsupported-occurrence" "v" :unsupported-construct)
("unsupported-association" "" :unsupported-construct)
("unsupported-mergemap" "" :unsupported-construct)))
(rejection (first spec) (document (second spec)) 'xtm-unsupported (third spec)))
(rejection "unsupported-reifier" "" 'xtm-unsupported :reification)
(let ((c (condition-from (lambda () (read-body (format nil "~%~%"))))))
(check "diagnostic:multiline-observed-position" (and (typep c 'xtm-rejection) (= 3 (error-line c)) (plusp (error-column c)))))
(dolist (iri '(nil "relative.xtm" "https://example.test/a#fragment"))
(let ((c (condition-from (lambda () (deserialize-octets (bytes (document "")) :document-iri iri :source-key "document")))))
(check (format nil "document-iri:~S" iri) (and (typep c 'xtm-rejection) (eq (error-code c) :invalid-document-iri)))))
(check "api:raw-bytes-required" (typep (condition-from (lambda () (deserialize-octets (document "") :document-iri *document*))) 'type-error))
(let* ((source "")
(c (condition-from (lambda () (read-body source)))))
(check "tmdm:identifier-conflict-not-xml-or-xtm" (and (typep c 'item-identifier-error) (not (typep c 'topic-maps.xtm:xtm-error))))
(check "tmdm:identifier-conflict-specific" (eq (item-identifier-error-code c) :duplicate-item-identifier)))
(let ((c (condition-from (lambda () (read-body "")))))
(check "tmdm:core-subject-conflict-preserved" (and (typep c 'core-subject-error) (eq :core-subject-conflict (core-subject-error-code c)))))
(dolist (bad '(#(195 40) #(192 175) #(226 130) #(237 160 128)))
(let ((c (condition-from (lambda () (deserialize-octets
(concatenate '(vector (unsigned-byte 8)) (bytes "")) :document-iri *document*)))))
(check (format nil "actual-reader:invalid-utf8:~S" bad) (and (typep c 'xtm-xml-error) (eq :xml-not-well-formed (error-code c))))))
;; Astral Unicode in an href exercises the qualified UTF-16 decoder through
;; the real XTM reader. No pre-decoding strings are accepted by its API.
(dolist (encoding '(:utf-16le :utf-16be))
(let ((map (deserialize-octets (bytes (document "") encoding t) :document-iri *document*)))
(check (format nil "actual-reader:astral-surrogate:~A" encoding) (by-si map "urn:🦊"))))
(dolist (bad '(#(0 216) #(0 220) #(0 216 65 0)))
(let ((c (condition-from (lambda ()
(deserialize-octets (concatenate '(vector (unsigned-byte 8))
(bytes "" :utf-16le t) (coerce bad '(vector (unsigned-byte 8))) (bytes "" :utf-16le)) :document-iri *document*)))))
(check (format nil "actual-reader:invalid-surrogate:~S" bad) (typep c 'xtm-xml-error))))
;; Verify the required entry point is actually called exactly once and sees
;; the original octet vector by EQ, not a reconstructed decoded XML string.
(let* ((original (symbol-function 'topic-maps.xml:parse-octets))
(source (bytes (document ""))) (calls 0) (same nil))
(unwind-protect
(progn (setf (symbol-function 'topic-maps.xml:parse-octets)
(lambda (bytes handler) (incf calls) (setf same (eq source bytes)) (funcall original bytes handler)))
(deserialize-octets source :document-iri *document*)
(check "boundary:real-parse-octets-only-once" (and (= 1 calls) same)))
(setf (symbol-function 'topic-maps.xml:parse-octets) original))))
(defun helper-source-test ()
(let* ((id "ctm/in/mergemap-xtm.sub")
(iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(*document* iri) (map (deserialize-octets (file-bytes (corpus-path id)) :document-iri iri :source-key id)))
(graph-checks "direct-sub" map (by-id map "lars") (by-id map "person"))
(check "direct-sub:exact-lars-ii" (equal (list (concatenate 'string iri "#lars")) (topic-item-identifiers (by-id map "lars"))))
(check "direct-sub:exact-person-ii" (equal (list (concatenate 'string iri "#person")) (topic-item-identifiers (by-id map "person"))))
(check "direct-sub:no-names-occurrences" (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic)))) (topic-map-topics map)))
(setf *fixture* map)))
(defun run-tests ()
(setf *checks* nil *fixture* nil *negative-outputs* nil *negative-controls* nil *name-outputs* nil *name-controls* nil *scope-outputs* nil *scope-controls* nil *iid-outputs* nil *iid-controls* nil *variant-outputs* nil *variant-negative-outputs* nil *variant-controls* nil)
(handler-case
(progn (original-contracts) (negative-original-controls) (negative-original-contracts)
(name-original-contracts) (name-tests) (scope-original-contracts) (scope-tests) (iid-original-contracts) (iid-tests) (variant-original-contracts) (variant-negative-original) (variant-tests) (result-classification-tests)
(semantic-tests) (identity-cascade-tests) (negative-tests) (helper-source-test))
(error (condition)
;; Preserve the actual failed outcome before ASDF propagates the failure.
(let ((report (uiop:getenv "TOPIC_MAPS_XTM_REPORT")))
(when report (write-report report)))
(error condition)))
(check "checks:unique-test-names" (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
(format t "XTM-SUMMARY ~D PASS 0 FAIL; ~D original CXTM PASS; ~D negative originals PASS; helper source PASS~%"
(length *checks*) (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)
(count :invalid *cases* :key #'topic-maps.test-foundation:test-case-category))
(length *checks*))
(defun write-report (pathname)
(let ((cases *cases*) (fixture *fixture*))
(flet ((fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
(array (items) (coerce items 'vector)))
(with-open-file (out pathname :direction :output :if-exists :supersede :external-format :utf-8)
(topic-maps.test-foundation::emit-json-value
(fields "scope" "XTM 2.0 identity/Name/Scope/Item-Identity/Variant subset, 48 positive and eight cause-qualified negative original contracts; no CTM Mergemap integration"
"corpusCommit" "cd165b150176c1d6842b37be9cc9051b40d4996b"
"statuses" (fields "pass" (count :pass cases :key #'topic-maps.test-foundation:test-case-status)
"fail" (count :fail cases :key #'topic-maps.test-foundation:test-case-status)
"unsupported" (count :unsupported cases :key #'topic-maps.test-foundation:test-case-status)
"not-run" (count :not-run cases :key #'topic-maps.test-foundation:test-case-status))
"otherCorpusInputs" (fields "positiveUnexecuted" (- (length (directory (corpus-path "xtm2/in/*.xtm"))) (count :valid cases :key #'topic-maps.test-foundation:test-case-category))
"negativeUnregisteredUnexecuted" (- (length (directory (corpus-path "xtm2/invalid/*.xtm")))
(count :invalid cases :key #'topic-maps.test-foundation:test-case-category)))
"checks" (array (reverse *checks*))
"cases" (array (loop for case in cases collect
(fields "id" (topic-maps.test-foundation:test-case-id case)
"documentIRI" (topic-maps.test-foundation:test-case-document-iri case)
"status" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-status case)))
"actualOutcome" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-actual-outcome case)))
"diagnostics" (array (topic-maps.test-foundation:test-case-diagnostics case)))))
"helperSource" (fields "id" "ctm/in/mergemap-xtm.sub" "status" (if fixture "pass" "not-run") "topics" (when fixture (length (topic-map-topics fixture)))
"associations" (when fixture (length (topic-map-associations fixture))) "roles" 2
"ctmMergemapContract" "NOT-RUN; direct helper deserialization only")
"negativeOriginals" (array *negative-outputs*)
"negativeControls" (array *negative-controls*)
"nameOriginals" (array *name-outputs*)
"nameControls" (array *name-controls*)
"scopeOriginals" (array *scope-outputs*)
"scopeControls" (array *scope-controls*)
"nameItemIdentityOriginals" (array *iid-outputs*)
"nameItemIdentityControls" (array *iid-controls*)
"variantOriginals" (array *variant-outputs*)
"variantNegativeOriginals" (array *variant-negative-outputs*)
"variantControls" (array *variant-controls*)
"actualAndExpected" (array (loop for (id actual expected model condition source-length) in *outputs* collect
(fields "id" id "actual" (when actual (babel:octets-to-string actual :encoding :utf-8))
"expected" (babel:octets-to-string expected :encoding :utf-8) "byteLength" (when actual (length actual))
"expectedByteLength" (length expected) "sourceByteLength" source-length
"firstDifferentByte" (when actual (mismatch actual expected))
"model" model "condition" condition)))) out)
(terpri out)))))