(in-package #:topic-maps.xtm.tests) (defun map-reifier-xml (iri &optional (body "")) (format nil "~A" iri body)) (defun map-reifier-state (map) ;; Retain actual references and forward handles, including the computed inverse. (list (variant-state map) (loop for item in (topic-maps.tmdm::map-information-items map) collect (list item (topic-maps.tmdm::canonical-item item) (when (topic-maps.tmdm::reifiable-p item) (item-reifier item)) (when (typep item 'topic) (list (topic-reified item) (topic-maps.tmdm::%topic-forward item))))))) (defun map-reifier-current-p (map) (and (variants-current-p map) (topic-maps.tmdm::validate-map-reification map) (topic-maps.tmdm::validate-map-item-identifiers map) (let ((r (item-reifier map))) (and (typep r 'topic) (eq r (canonical-topic r)) (eq map (topic-parent r)) (member r (topic-map-topics map) :test #'eq) (eq map (topic-reified r)))))) (defun map-reifier-model-data (map) (let ((r (item-reifier map))) (topic-maps.test-foundation::fields "identity" (variant-model-data map) "reifierTopic" (when r (1+ (position r (topic-map-topics map) :test #'eq))) "canonicalReifierAndInverseEQ" (if (map-reifier-current-p map) :true :false) "mapItemIdentifiers" (coerce (item-identifiers map) 'vector) "topicReifiedMapEQ" (coerce (mapcar (lambda (topic) (if (eq map (topic-reified topic)) :true :false)) (topic-map-topics map)) 'vector)))) (defun map-reifier-condition-data (c) (if (typep c 'reification-error) (topic-maps.test-foundation::fields "class" (string (type-of c)) "code" (string-downcase (string (reification-error-code c))) "detail" (topic-maps.tmdm::reification-error-detail c) "message" (princ-to-string c) "line" nil "column" nil "positionUnit" nil "documentIRI" nil "sourceKey" nil "cause" nil) (negative-condition-data c))) (defun map-reifier-original-contract () (let* ((id "xtm2/in/tm-reifier.xtm") (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (raw (file-bytes (corpus-path id))) (copy (copy-seq raw)) (baseline (corpus-path "xtm2/baseline/tm-reifier.xtm.cxtm")) (expected (file-bytes baseline)) (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id) :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline :clauses '("XTM 2.0 4.3.1,4.3.2,4.3.4,4.4" "TMDM 5.2,5.3.4,5.3.5,6.2" "CXTM 3,4"))) (iri (concatenate 'string *document* "#reifier")) (map nil) (actual nil) (c nil)) (setf *cases* (append *cases* (list case))) (handler-case (progn (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (calls 0) (same nil)) (unwind-protect (progn (setf (symbol-function 'topic-maps.xml:parse-octets) (lambda (source handler) (incf calls) (setf same (eq raw source)) (funcall saved source handler))) (setf map (deserialize-octets raw :document-iri *document* :source-key id)) (check "map-reifier-original:qualified-parser-once-original-octets-eq" (and (= 1 calls) same))) (setf (symbol-function 'topic-maps.xml:parse-octets) saved))) (let* ((root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler))) (r (item-reifier map)) (before (map-reifier-state map))) (check "map-reifier-original:actual-source-attribute-no-topic-definitions" (and (= 94 (length raw)) (equal "#reifier" (topic-maps.xtm::attr root "reifier")) (null (topic-maps.xtm::elements root)))) (check "map-reifier-original:native-map-topic-canonical-inverse-eq" (and (= 1 (length (topic-map-topics map))) (null (topic-map-associations map)) (eq r (first (topic-map-topics map))) (eq r (by-id map "reifier")) (map-reifier-current-p map))) (check "map-reifier-original:complete-separated-identifiers-and-empty-statements" (and (equal (list iri) (item-identifiers r)) (null (item-identifiers map)) (null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (null (topic-names r)) (null (topic-occurrences r)) (null (topic-roles-played r)))) (setf actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (check "map-reifier-original:source-model-handles-preserved-deterministic" (and (= 128 (length expected)) (equalp raw copy) (equal before (map-reifier-state map)) (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (equal before (map-reifier-state map)) (eq r (item-reifier map)) (eq map (topic-reified r)))) (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:map-reifier"))) (check "map-reifier-original:source-key-independent-model-and-cxtm" (and (equalp (map-reifier-model-data map) (map-reifier-model-data other)) (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*))))))) (error (condition) (setf c condition))) (record-original-result case actual expected c) (setf *map-reifier-outputs* (list (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) "reifierLocator" iri "model" (when map (map-reifier-model-data map)) "condition" (map-reifier-condition-data c) "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)) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))))) (when c (error c)) (check "map-reifier-original:byte-exact" (equalp actual expected)) (check "map-reifier-original:63-unique-case-records-one-new-positive" (and (= 63 (length *cases*)) (= 55 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 63 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))) (defun map-reifier-control (label source verify) (let* ((raw (bytes source)) (copy (copy-seq raw)) (map (deserialize-octets raw :document-iri *document* :source-key label))) (funcall verify map) (check (concatenate 'string "map-reifier:" label ":canonical-native-model") (map-reifier-current-p map)) (let* ((before (map-reifier-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))) (check (concatenate 'string "map-reifier:" label ":pure-deterministic-source-preserved") (and (equalp raw copy) (equal before (map-reifier-state map)) (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (equal before (map-reifier-state map)))) (push (topic-maps.test-foundation::fields "label" label "sourceText" source "model" (map-reifier-model-data map) "actualCXTM" (babel:octets-to-string actual :encoding :utf-8) "condition" nil) *map-reifier-controls*)))) (defun map-reifier-error-control (label source class code &optional thunk map) (let* ((before (when map (map-reifier-state map))) (c (condition-from (or thunk (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key label))))) (actual-code (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c)) ((typep c 'reification-error) (reification-error-code c)) ((typep c 'item-identifier-error) (item-identifier-error-code c))))) (push (topic-maps.test-foundation::fields "label" label "sourceText" source "expectedClass" (string class) "expectedCode" (string-downcase (string code)) "condition" (map-reifier-condition-data c) "atomicNativeOperation" (when map (if (equal before (map-reifier-state map)) :true :false))) *map-reifier-controls*) (check (concatenate 'string "map-reifier-error:" label ":specific-class-code") (and (eq class (type-of c)) (eq code actual-code))) (when (typep c 'topic-maps.xtm:xtm-error) (check (concatenate 'string "map-reifier-error:" label ":source-context-position") (and (equal *document* (error-document-iri c)) (equal label (error-source-key c)) (integerp (error-line c)) (plusp (error-line c)) (integerp (error-column c)) (not (minusp (error-column c)))))) (when map (check (concatenate 'string "map-reifier-error:" label ":identities-handles-atomic") (equal before (map-reifier-state map)))))) (defun map-reifier-tests () (dolist (row `(("absolute" "urn:map-reifier" "urn:map-reifier") ("relative" "../reifiers/map" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/reifiers/map") ("fragment" "#r" ,(concatenate 'string *document* "#r")) ("fragment-not-topicref" "#2r/path" ,(concatenate 'string *document* "#2r/path")) ("no-fragment" "other.xtm" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/other.xtm") ("empty-reference" "" ,*document*) ("unicode" "urn:馬🦊" "urn:馬🦊") ("percent-utf8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊") ("xml-escaped" "urn:a&b" "urn:a&b"))) (destructuring-bind (label reference expected) row (map-reifier-control label (map-reifier-xml reference) (lambda (map) (check (concatenate 'string "map-reifier:" label ":ii-only-map-identity-separated") (let ((r (item-reifier map))) (and (= 1 (length (topic-map-topics map))) (equal (list expected) (item-identifiers r)) (null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (null (item-identifiers map))))))))) (map-reifier-control "map-item-identities" (map-reifier-xml "#r" "") (lambda (map) (check "map-reifier:map-item-identities:separate-complete-sets" (and (equal '("urn:map") (item-identifiers map)) (eq (by-id map "r") (item-reifier map)) (equal (list (concatenate 'string *document* "#r")) (item-identifiers (item-reifier map))))))) (map-reifier-control "forward-name-variant" (map-reifier-xml "#r" " R & 馬🦊 V ") (lambda (map) (let* ((r (item-reifier map)) (name (first (topic-names r))) (v (first (topic-name-variants name)))) (check "map-reifier:forward-name-variant:canonical-parents-values-scopes" (and (eq r (by-id map "r")) (eq r (topic-name-parent name)) (eq name (variant-parent v)) (equal " R & 馬🦊 " (topic-name-value name)) (equal " V " (variant-value v)) (scope-eq-p (list (by-id map "s")) (variant-scope v))))))) ;; Stage only existing production operations to observe the attribute BEFORE ;; later Topic definitions; the original contract above uses the public API. (dolist (kind '("itemIdentity" "subjectIdentifier" "subjectLocator")) (let* ((label (concatenate 'string "forward-merge-" kind)) (locator (if (equal kind "itemIdentity") "#r" "urn:shared")) (source (map-reifier-xml "#r" (format nil "<~A href='~A'/><~A href='~A'/>" kind locator kind locator))) (topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* label) (root (topic-maps.xml:parse-octets (bytes source) (make-instance 'topic-maps.xtm::source-handler))) (map (make-topic-map))) (topic-maps.xtm::validate-source root) (topic-maps.xtm::read-map-reifier root map) (let ((held (item-reifier map))) (check (concatenate 'string "map-reifier:" label ":attribute-creates-before-definitions") (and (= 1 (length (topic-map-topics map))) (eq map (topic-reified held)))) (dolist (node (topic-maps.xtm::elements root)) (topic-maps.xtm::read-topic node map)) (check (concatenate 'string "map-reifier:" label ":held-handle-canonical-eq") (and (= 1 (length (topic-map-topics map))) (eq (canonical-topic held) (by-id map "other")) (eq (canonical-topic held) (by-id map "r")) (eq (canonical-topic held) (item-reifier map)) (eq map (topic-reified held)) (map-reifier-current-p map) (same-identifiers-p (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#other")) (item-identifiers (item-reifier map))) (equal (if (equal kind "subjectIdentifier") '("urn:shared") nil) (topic-subject-identifiers (item-reifier map))) (equal (if (equal kind "subjectLocator") '("urn:shared") nil) (topic-subject-locators (item-reifier map))))) (let* ((before (map-reifier-state map)) (forward (topic-maps.tmdm::%topic-forward held)) (current (canonical-topic held)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))) (check (concatenate 'string "map-reifier:" label ":cxtm-preserves-retained-forward-handle-and-inverse") (and (equal before (map-reifier-state map)) (eq forward (topic-maps.tmdm::%topic-forward held)) (eq current (canonical-topic held)) (eq map (topic-reified held)) (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (equal before (map-reifier-state map)) (eq forward (topic-maps.tmdm::%topic-forward held))))) (map-reifier-control label source (lambda (other) (check (concatenate 'string "map-reifier:" label ":public-api-equivalent") (equalp (map-reifier-model-data map) (map-reifier-model-data other)))))))) (let* ((map (make-topic-map)) (iri "urn:existing-ii") (r (intern-topic map :item-identifier iri)) (topic-maps.xtm::*document-iri* *document*) (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml iri)) (make-instance 'topic-maps.xtm::source-handler))) (saved (symbol-function 'intern-topic)) (calls 0)) (unwind-protect (progn (setf (symbol-function 'intern-topic) (lambda (&rest args) (incf calls) (apply saved args))) (topic-maps.xtm::read-map-reifier root map) (check "map-reifier:existing-ii:lookup-reuses-topic-without-interning" (and (zerop calls) (eq r (item-reifier map)) (eq map (topic-reified r)) (equal (list iri) (item-identifiers r)) (null (topic-subject-identifiers r))))) (setf (symbol-function 'intern-topic) saved))) (dolist (kind '(:subject-identifier :subject-locator)) (let* ((map (make-topic-map)) (iri "urn:only-other-category") (old (intern-topic map kind iri)) (topic-maps.xtm::*document-iri* *document*) (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml iri)) (make-instance 'topic-maps.xtm::source-handler)))) (topic-maps.xtm::read-map-reifier root map) (check (format nil "map-reifier:lookup:~A-is-not-ii-hit" kind) (and (equal (list iri) (item-identifiers (item-reifier map))) (if (eq kind :subject-identifier) ;; Native intern-topic merges SI/II identity but MUST add the II. (and (eq old (item-reifier map)) (= 1 (length (topic-map-topics map)))) (and (not (eq old (item-reifier map))) (= 2 (length (topic-map-topics map))) (null (item-identifiers old)) (null (topic-reified old)))) (map-reifier-current-p map))))) (dolist (row '(("bad-percent" "urn:%GG" :invalid-percent-escape) ("truncated-percent" "urn:%A" :invalid-percent-escape) ("bad-percent-utf8" "urn:%C3%28" :invalid-percent-utf8) ("surrogate-percent-utf8" "urn:%ED%A0%80" :invalid-percent-utf8) ("space" "urn:a b" :invalid-iri) ("decoded-control" "urn:%00" :invalid-iri) ("relative-colon" "1bad:subject" :invalid-iri) ("two-fragments" "#a#b" :invalid-iri))) (map-reifier-error-control (first row) (map-reifier-xml (second row)) 'xtm-rejection (third row))) (dolist (row '(("extra-attribute" "" :unexpected-attribute) ("qualified-reifier" "" :unexpected-attribute) ("missing-version" "" :missing-attribute) ("invalid-version" "" :invalid-version) ("wrong-namespace" "" :invalid-namespace) ("identity-after-topic" "" :element-order) ("reifier-child" "" :unexpected-element))) (map-reifier-error-control (first row) (second row) 'xtm-rejection (third row))) (dolist (row '(("name-reification" "N" :reification) ("variant-reification" "NV" :reification) ("occurrence-reification" "O" :unsupported-construct) ("association-role-reification" "" :unsupported-construct) ("mergeMap" "" :unsupported-construct))) (map-reifier-error-control (first row) (map-reifier-xml "#map-r" (second row)) 'xtm-unsupported (third row))) (map-reifier-error-control "map-ii-reifier-ii-collision" (map-reifier-xml "#r" "") 'item-identifier-error :duplicate-item-identifier) (let* ((map (deserialize-octets (bytes (map-reifier-xml "#r" "N")) :document-iri *document*)) (r (item-reifier map)) (name (first (topic-names (by-id map "owner"))))) (map-reifier-error-control "reuse-reifier" nil 'reification-error :reifier-conflict (lambda () (set-item-reifier name r)) map) (add-item-identifier name "urn:name-item") (let* ((topic-maps.xtm::*document-iri* *document*) (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml "urn:name-item")) (make-instance 'topic-maps.xtm::source-handler)))) (map-reifier-error-control "existing-name-ii-collision" nil 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier (lambda () (topic-maps.xtm::read-map-reifier root map)) map)) (let ((other (intern-topic map :item-identifier "urn:other-reifier"))) (set-item-reifier name other) (map-reifier-error-control "merge-two-different-reified-items" nil 'reification-error :reifier-conflict (lambda () (merge-topics r other)) map)) (check "map-reifier:conflicts:map-name-reifiers-and-inverses-preserved" (and (eq r (item-reifier map)) (eq map (topic-reified r)) (eq name (topic-reified (item-reifier name))) (map-reifier-current-p map)))) ;; Wrong native conditions are execution errors in the existing classifier. (let* ((map (make-topic-map)) (r (intern-topic map :item-identifier "urn:guard")) (owner (intern-topic map :item-identifier "urn:owner")) (name (add-default-name owner "N"))) (set-item-reifier map r) (let* ((c (condition-from (lambda () (set-item-reifier name r)))) (case (topic-maps.test-foundation:make-test-case :id "local:reification-classifier" :category :valid :expected-outcome :cxtm-equal))) (record-original-result case #(1) #(1) c) (check "map-reifier:classifier:native-reification-conflict-never-positive-pass" (and (eq :fail (topic-maps.test-foundation:test-case-status case)) (eq :execution-error (topic-maps.test-foundation:test-case-actual-outcome case)))) (record-negative-original-result case (first *negative-original-specs*) c *document* "local:guard") (check "map-reifier:classifier:wrong-expected-condition-never-negative-pass" (and (eq :fail (topic-maps.test-foundation:test-case-status case)) (eq :execution-error (topic-maps.test-foundation:test-case-actual-outcome case)))))))