(in-package #:topic-maps.xtm.tests) (defparameter *name-original-specs* '(("name" 2 (("topic" "Topic" :default))) ("name-unicode" 2 (("topic" "馬籠" :default))) ("name-escaping" 2 (("att" "" :default))) ("name-duplicate" 2 (("topic" "Topic" :default))) ("name-duplicate-merge" 2 (("topic" "Topics" "plural"))) ("merge-itemid-with-names" 2 (("topic" "Topics" :default) ("topic" "Topics 2" :default))) ("name-type" 2 (("topic" "Topics" (:ii "http://psi.ontopia.net/xtm/basename/#plural")))) ("name-type-before" 2 (("topic" "Topics" :default))) ("name-type-after" 2 (("topic" "Topics" :default))) ("name-type-after-bad" 3 (("topic" "Topics" :default) ("other" "Other" :default))))) (defun names-current-p (map) (and (references-current-p map) (every (lambda (topic) (every (lambda (name) (and (eq name (canonical-topic-name name)) (eq topic (topic-name-parent name)) (eq (topic-name-type name) (canonical-topic (topic-name-type name))) (member (topic-name-type name) (topic-map-topics map) :test #'eq) (every (lambda (theme) (and (eq theme (canonical-topic theme)) (member theme (topic-map-topics map) :test #'eq))) (topic-name-scope name)))) (topic-names topic))) (topic-map-topics map)))) (defun name-state (map) (list (public-state map) (loop for topic in (topic-map-topics map) append (loop for name in (topic-names topic) collect (list name (topic-name-value name) (topic-name-type name) (topic-name-parent name) (topic-name-scope name) (topic-name-variants name) (item-identifiers name) (item-reifier name) (topic-maps.tmdm::%topic-name-forward name)))))) (defun name-model-data (map) (flet ((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 "identity" (original-model-data map) "mapItemIdentifiers" (coerce (item-identifiers map) 'vector) "canonicalNameParentTypeReferences" (if (names-current-p map) :true :false) "names" (coerce (loop for topic in topics append (loop for name in (topic-names topic) collect (fields "value" (topic-name-value name) "parent" (ref (topic-name-parent name)) "type" (ref (topic-name-type name)) "scope" (coerce (mapcar #'ref (topic-name-scope name)) 'vector) "itemIdentifiers" (coerce (item-identifiers name) 'vector) "variantCount" (length (topic-name-variants name))))) 'vector)))))) (defun name-original-model-checks (spec map) (let ((label (concatenate 'string "name-original:" (first spec))) (expected (third spec))) (check (concatenate 'string label ":topic-and-name-counts") (and (= (second spec) (length (topic-map-topics map))) (null (topic-map-associations map)) (= (length expected) (loop for topic in (topic-map-topics map) sum (length (topic-names topic)))))) (check (concatenate 'string label ":canonical-parent-type-eq") (names-current-p map)) (dolist (row expected) (destructuring-bind (owner-id value type-key) row (let* ((owner (by-id map owner-id)) (type (cond ((eq type-key :default) (by-si map (default-name-type-identifier))) ((stringp type-key) (by-id map type-key)) (t (find-if (lambda (topic) (member (second type-key) (topic-item-identifiers topic) :test #'equal)) (topic-map-topics map)))))) (check (format nil "~A:value-parent-type:~A:~A" label owner-id value) (and owner type (find-if (lambda (name) (and (equal value (topic-name-value name)) (eq owner (topic-name-parent name)) (eq type (topic-name-type name)) (eq name (canonical-topic-name name)))) (topic-names owner))))))) (check (concatenate 'string label ":excluded-name-properties-empty") (every (lambda (topic) (every (lambda (name) (and (null (topic-name-scope name)) (null (topic-name-variants name)) (null (item-identifiers name)) (null (item-reifier name)))) (topic-names topic))) (topic-map-topics map))) (check (concatenate 'string label ":one-default-type-if-used") (= (if (find :default expected :key #'third) 1 0) (count-if (lambda (topic) (member (default-name-type-identifier) (topic-subject-identifiers topic) :test #'equal)) (topic-map-topics map)))) (when (member (first spec) '("name-type-before" "name-type-after" "name-type-after-bad") :test #'equal) (check (concatenate 'string label ":declared-default-type-eq") (eq (by-id map "nametype") (by-si map (default-name-type-identifier))))) (when (equal (first spec) "name-duplicate-merge") (check (concatenate 'string label ":forward-type-merge-eq") (eq (by-id map "plural") (by-id map "flertall")))) (when (equal (first spec) "merge-itemid-with-names") (check (concatenate 'string label ":merged-parent-eq-and-identifiers") (and (eq (by-id map "topic") (by-id map "topic2")) (same-identifiers-p (list (concatenate 'string *document* "#topic") (concatenate 'string *document* "#topic2") "http://example.org/#topic") (topic-item-identifiers (by-id map "topic")))))))) (defun name-original-contracts () (setf *name-outputs* nil) (dolist (spec *name-original-specs*) (let* ((name (first spec)) (id (format nil "xtm2/in/~A.xtm" name)) (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (raw (file-bytes source)) (copy (copy-seq raw)) (expected (file-bytes baseline)) (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline :clauses '("XTM 2.0 4.2,4.10,4.11,4.14,4.20" "TMDM 5.4,6.2,6.3" "CXTM 3,4"))) (map nil) (actual nil) (c nil) (before nil)) (setf *cases* (append *cases* (list case))) (handler-case (setf map (deserialize-octets raw :document-iri *document* :source-key id) before (name-state map) actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (error (condition) (setf c condition))) ;; A reader or model failure can never satisfy this positive contract. (when (null c) (handler-case (name-original-model-checks spec map) (error (condition) (setf c condition)))) (record-original-result case actual expected c) (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8) "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8) "actualBytes" (when actual (length actual)) "expectedBytes" (length expected) "firstDifferentByte" (when actual (mismatch actual expected)) "condition" (negative-condition-data c) "model" (when map (name-model-data map)) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *name-outputs*) (when c (error c)) (check (concatenate 'string "name-original:" name ":byte-exact") (equalp actual expected)) (check (concatenate 'string "name-original:" name ":source-preserved") (equalp raw copy)) (check (concatenate 'string "name-original:" name ":pure") (equal before (name-state map))) (check (concatenate 'string "name-original:" name ":deterministic") (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))) (check (concatenate 'string "name-original:" name ":repeat-pure") (equal before (name-state map))) (let ((other (deserialize-octets raw :document-iri *document* :source-key "different-name-provenance"))) (check (concatenate 'string "name-original:" name ":source-key-only-provenance") (and (equalp (name-model-data map) (name-model-data other)) (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*))))))) (setf *name-outputs* (nreverse *name-outputs*)) (check "name-originals:43-unique-case-records" (and (= 43 (length *cases*)) (= 36 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 43 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))) (defun name-control (label body verify) (let* ((map (read-body body)) (before (name-state map))) (funcall verify map) (check (concatenate 'string "name-control:" label ":canonical-references") (names-current-p map)) (let ((actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))) (check (concatenate 'string "name-control:" label ":pure") (equal before (name-state map))) (push (topic-maps.test-foundation::fields "label" label "kind" "positive-XTM" "sourceText" (document body) "model" (name-model-data map) "actualCXTM" (babel:octets-to-string actual :encoding :utf-8)) *name-controls*)) map)) (defun name-error-control (label body class code) (let* ((source (document body)) (key (concatenate 'string "name-syntax:" label)) (c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key key))))) (check (concatenate 'string key ":class-code") (and (eq class (type-of c)) (eq code (error-code c)))) (check (concatenate 'string key ":context-position") (and (equal *document* (error-document-iri c)) (equal key (error-source-key c)) (integerp (error-line c)) (plusp (error-line c)) (integerp (error-column c)) (plusp (error-column c)))) (push (topic-maps.test-foundation::fields "label" label "kind" "negative-or-unsupported-XTM" "sourceText" source "condition" (negative-condition-data c)) *name-controls*))) (defun name-text-controls () (dolist (row (list (list "simple" "Topic" "Topic") (list "empty" "" "") (list "unicode" "馬籠🦊é" "馬籠🦊é") (list "escaping-once" "&lt; < & 🦊" "< < & 🦊") (list "whitespace" (format nil " A~C B~%C " #\Tab) (format nil " A~C B~%C " #\Tab)) (list "xml-line-endings" (format nil "A~C~CB~CC D" #\Return #\Newline #\Return) (format nil "A~%B~%C~CD" #\Return)) (list "sax-chunks" "A&BC" "A<&&BC"))) (destructuring-bind (label xml expected) row (name-control label (format nil "~A" xml) (lambda (map) (let ((name (first (topic-names (by-id map "a"))))) (check (concatenate 'string "name-text:" label) (and (equal expected (topic-name-value name)) (eq (by-id map "a") (topic-name-parent name)) (eq (by-si map (default-name-type-identifier)) (topic-name-type name))))))))) (name-control "empty-self-closing" "" (lambda (map) (check "name-text:empty-self-closing" (equal "" (topic-name-value (first (topic-names (by-id map "a"))))))))) (defun name-identity-controls () (dolist (order '("N" "N")) (name-control (if (search "id='nt'" order :end2 20) "default-before" "default-after") order (lambda (map) (check (if (search "id='nt'" order :end2 20) "name-type:before-eq" "name-type:after-eq") (and (= 2 (length (topic-map-topics map))) (eq (by-id map "nt") (topic-name-type (first (topic-names (by-id map "a")))))))))) (name-control "explicit-si-hit" "N" (lambda (map) (check "name-type:si-hit-no-extra-ii" (and (eq (by-id map "nt") (topic-name-type (first (topic-names (by-id map "a"))))) (equal (list (concatenate 'string *document* "#nt")) (topic-item-identifiers (by-id map "nt"))))))) (name-control "forward-type" "N" (lambda (map) (check "name-type:forward-eq" (eq (by-id map "nt") (topic-name-type (first (topic-names (by-id map "a")))))))) (name-control "different-types" "NN" (lambda (map) (let ((names (topic-names (by-id map "a")))) (check "name-equality:type-distinguishes" (and (= 2 (length names)) (not (eq (first names) (second names))) (not (eq (topic-name-type (first names)) (topic-name-type (second names))))))))) (name-control "different-parents" "NN" (lambda (map) (check "name-equality:parent-distinguishes" (not (eq (first (topic-names (by-id map "a"))) (first (topic-names (by-id map "b")))))))) (name-control "same-parent-type-value" "NN" (lambda (map) (check "name-equality:duplicate-collapses" (= 1 (length (topic-names (by-id map "a"))))))) (name-control "parent-is-default-type" "N" (lambda (map) (check "name-type:parent-and-type-same-eq" (and (= 1 (length (topic-map-topics map))) (eq (by-id map "a") (topic-name-type (first (topic-names (by-id map "a"))))))))) (name-control "external-type-no-fetch" "N" (lambda (map) (check "name-type:external-placeholder" (equal '("https://unfetched.invalid/map.xtm#t") (topic-item-identifiers (topic-name-type (first (topic-names (by-id map "a"))))))))) ;; This is an existing-model invariant control, not XTM Scope support. (let* ((map (read-body "N")) (owner (by-id map "a")) (unscoped (first (topic-names owner))) (scoped (add-name owner "N" :scope (list (by-id map "s"))))) (normalize-topic-map map) (check "name-equality:existing-tmdm-scope-distinguishes" (and (= 2 (length (topic-names owner))) (not (eq (canonical-topic-name unscoped) (canonical-topic-name scoped))))) (push (topic-maps.test-foundation::fields "label" "existing-tmdm-scope-invariant" "kind" "TMDM-only-control-no-XTM-Scope" "model" (name-model-data map)) *name-controls*))) (defun staged-name-control (label body after-step verify) (let* ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* label) (map (make-topic-map)) (root (topic-maps.xml:parse-octets (bytes (document body)) (make-instance 'topic-maps.xtm::source-handler)))) (topic-maps.xtm::validate-source root) (loop for node in (topic-maps.xtm::elements root) for step from 1 do (topic-maps.xtm::read-topic node map) (funcall after-step map step)) (funcall verify map) (check (concatenate 'string label ":canonical-references") (names-current-p map)) (push (topic-maps.test-foundation::fields "label" label "kind" "staged-native-reader-control" "sourceText" (document body) "model" (name-model-data map)) *name-controls*))) (defun retained-name-controls () (dolist (kind '("itemIdentity" "subjectIdentifier" "subjectLocator")) (let ((a nil) (b nil) (na nil) (nb nil) (label (concatenate 'string "retained-name:" kind))) (staged-name-control label (if (equal kind "itemIdentity") "NN" (format nil "<~A href='urn:a'/>N<~A href='urn:b'/>N<~A href='urn:a'/><~A href='urn:b'/>" kind kind kind kind)) (lambda (map step) (when (= step 1) (setf a (by-id map "a") na (first (topic-names a)))) (when (= step 2) (setf b (by-id map "b") nb (first (topic-names b))) (check (concatenate 'string label ":distinct-before-merge") (and (not (eq a b)) (not (eq na nb)))))) (lambda (map) (let* ((owner (by-id map "bridge")) (name (first (topic-names owner))) (before (name-state map))) (check (concatenate 'string label ":retained-parent-and-name-eq") (and (eq owner (canonical-topic a)) (eq owner (canonical-topic b)) (eq name (canonical-topic-name na)) (eq name (canonical-topic-name nb)) (eq owner (topic-name-parent na)) (eq owner (topic-name-parent nb)) (= 1 (length (topic-names owner))))) (topic-maps.cxtm:canonicalize-octets map :base-locator *document*) (check (concatenate 'string label ":cxtm-retains-handles") (and (equal before (name-state map)) (eq name (canonical-topic-name na)) (eq name (canonical-topic-name nb))))))))) (let ((old-type nil) (old-name nil)) (staged-name-control "retained-name:default-type-after" "NB" (lambda (map step) (when (= step 1) (setf old-name (first (topic-names (by-id map "a"))) old-type (topic-name-type old-name)))) (lambda (map) (check "retained-name:default-type-after-eq" (and (eq (canonical-topic old-type) (by-id map "nt")) (eq (by-id map "nt") (topic-name-type old-name)) (eq (topic-name-type old-name) (topic-name-type (first (topic-names (by-id map "b")))))))))) (let ((names nil) (types nil)) (staged-name-control "retained-name:type-merge" "NN" (lambda (map step) (when (= step 1) (setf names (topic-names (by-id map "a")) types (mapcar #'topic-name-type names)) (check "retained-name:type-distinct-before" (and (= 2 (length names)) (not (eq (first types) (second types))))))) (lambda (map) (let ((name (first (topic-names (by-id map "a"))))) (check "retained-name:type-and-name-canonical-after" (and (= 1 (length (topic-names (by-id map "a")))) (every (lambda (n) (eq name (canonical-topic-name n))) names) (every (lambda (type) (eq (by-id map "one") (canonical-topic type))) types) (eq (by-id map "one") (topic-name-type name))))))))) (defun name-syntax-controls () (dolist (spec '(("missing-value" "" :missing-name-value) ("missing-value-after-type" "" :missing-name-value) ("multiple-values" "AB" :duplicate-name-value) ("multiple-types" "N" :duplicate-name-type) ("type-no-ref" "N" :type-cardinality) ("type-multiple-ref" "N" :type-cardinality) ("type-after-value" "N" :element-order) ("unknown-child" "N" :unexpected-element) ("name-attribute" "N" :unexpected-attribute) ("type-attribute" "N" :unexpected-attribute) ("value-attribute" "N" :unexpected-attribute) ("nested-value" "N" :unexpected-child) ("foreign-markup-in-value" "N" :unexpected-child) ("non-topicRef-type-child" "N" :unexpected-element) ("text-in-type" "textN" :unexpected-text) ("text-in-name" "textN" :unexpected-text) ("reference-without-fragment" "N" :invalid-topic-ref) ("reference-missing-href" "N" :missing-attribute) ("reference-attribute" "N" :unexpected-attribute) ("wrong-value-namespace" "N" :invalid-namespace) ("scope-after-value" "N" :element-order) ("ii-after-value" "N" :element-order) ("variant-before-value" "VN" :element-order))) (name-error-control (first spec) (format nil "~A" (second spec)) 'xtm-rejection (third spec))) (dolist (spec '(("variant-resourceData-datatype" "N42" :resource-data-datatype) ("variant-reifier" "NV" :reification))) (name-error-control (concatenate 'string "unsupported-" (first spec)) (format nil "~A" (second spec)) 'xtm-unsupported (third spec))) (let* ((body (format nil "~%~% ~%")) (c (condition-from (lambda () (read-body body))))) (check "name-syntax:multiline-name-position" (and (eq (type-of c) 'xtm-rejection) (eq :missing-name-value (error-code c)) (= 3 (error-line c)) (plusp (error-column c))))) (let* ((raw (bytes "N")) (map (deserialize-octets raw :document-iri *document*))) (check "name-syntax:qualified-prefix-name" (equal "N" (topic-name-value (first (topic-names (by-id map "a")))))))) (defun name-tests () (setf *name-controls* nil) (name-text-controls) (name-identity-controls) (retained-name-controls) (name-syntax-controls) (setf *name-controls* (nreverse *name-controls*)))