(in-package #:topic-maps.xtm.tests) (defun variant-iid-owners (map iri) (remove-if-not (lambda (item) (member iri (item-identifiers item) :test #'equal)) (topic-maps.tmdm::map-information-items map))) (defun variant-iid-xml (value themes identifiers &optional resource) (format nil "~{~A~}~A~A" (mapcar (lambda (iri) (format nil "" iri)) identifiers) (scope-xml themes) (or resource (format nil "~A" value)))) (defun variant-iid-original-model-checks (map raw source-count label) (let* ((owner (by-id map "topic")) (name (first (topic-names owner))) (v (first (all-variants map))) (theme (by-id map "command")) (type (by-si map (default-name-type-identifier))) (iri (concatenate 'string *document* "#the-variant")) (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler))) (source-name (first (topic-maps.xtm::elements (first (topic-maps.xtm::elements root))))) (source-variants (rest (topic-maps.xtm::elements source-name)))) (check (concatenate 'string label ":actual-original-variant-elements") (and (= source-count (length source-variants)) (every (lambda (node) (let ((children (topic-maps.xtm::elements node))) (and (equal "variant" (topic-maps.xtm::node-name node)) (= 3 (length children)) (equal "itemIdentity" (topic-maps.xtm::node-name (first children))) (equal "#the-variant" (topic-maps.xtm::attr (first children) "href")) (equal "scope" (topic-maps.xtm::node-name (second children))) (equal "resourceData" (topic-maps.xtm::node-name (third children))) (null (topic-maps.xtm::node-attributes (third children))) (equal "TOPIC!" (apply #'concatenate 'string (topic-maps.xtm::node-children (third children))))))) source-variants))) (check (concatenate 'string label ":native-normalized-parent-value-datatype-scope-eq") (and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map)) (= 1 (length (topic-names owner))) (= 1 (length (all-variants map))) (eq owner (topic-name-parent name)) (eq type (topic-name-type name)) (eq name (canonical-topic-name name)) (eq name (variant-parent v)) (eq v (canonical-variant v)) (equal "Topic" (topic-name-value name)) (equal "TOPIC!" (variant-value v)) (equal (string-datatype) (variant-datatype v)) (null (topic-name-scope name)) (scope-eq-p (variant-scope v) (list theme)) (variants-current-p map))) (check (concatenate 'string label ":complete-identifier-sets-exclusive-variant-owner") (and (equal (list iri) (item-identifiers v)) (equal (list v) (variant-iid-owners map iri)) (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner)) (null (topic-subject-identifiers owner)) (null (topic-subject-locators owner)) (equal (list (concatenate 'string *document* "#command")) (item-identifiers theme)) (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type)) (null (item-identifiers type)) (null (topic-subject-locators type)) (null (topic-names type)) (null (topic-names theme)) (null (item-identifiers map)) (null (item-identifiers name)) (null (item-reifier name)) (null (item-reifier v)) (topic-maps.tmdm::validate-map-item-identifiers map))))) (defun variant-iid-original-contracts () (setf *variant-iid-outputs* nil) (dolist (spec '(("itemid-variant" 1) ("variant-duplicate-iid" 2))) (destructuring-bind (stem source-count) spec (let* ((id (format nil "xtm2/in/~A.xtm" stem)) (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (raw (file-bytes (corpus-path id))) (copy (copy-seq raw)) (expected (file-bytes (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem)))) (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 (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem)) :clauses '("XTM 2.0 4.3.1,4.3.3,4.3.4,4.6,4.12" "TMDM 5.1,5.5,6.4" "CXTM 3,4"))) (map nil) (actual nil) (c nil) (label (concatenate 'string "variant-iid-original:" stem))) (setf *cases* (append *cases* (list case))) (handler-case (progn (setf map (deserialize-octets raw :document-iri *document* :source-key id)) (variant-iid-original-model-checks map raw source-count label) (let ((before (variant-state map))) (setf actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (check (concatenate 'string label ":source-preserved-pure-deterministic") (and (= 666 (length expected)) (equalp raw copy) (equal before (variant-state map)) (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (equal before (variant-state map))))) (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:variant-item-identity"))) (check (concatenate 'string label ":source-key-independent-model-and-cxtm") (and (equalp (variant-model-data map) (variant-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) (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) "sourceVariantCount" source-count "variantIdentifier" (concatenate 'string *document* "#the-variant") "model" (when map (variant-model-data map)) "condition" (negative-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)))) *variant-iid-outputs*) (when c (error c)) (check (concatenate 'string label ":byte-exact") (equalp actual expected))))) (setf *variant-iid-outputs* (nreverse *variant-iid-outputs*)) (check "variant-iid-originals:62-unique-case-records" (and (= 62 (length *cases*)) (= 54 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 62 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))) (defun variant-iid-control (label body verify) (variant-control (concatenate 'string "variant-iid:" label) body (lambda (map) (funcall verify map) (check (concatenate 'string "variant-iid:" label ":global-identifier-uniqueness") (topic-maps.tmdm::validate-map-item-identifiers map))))) (defun variant-iid-value-controls () (dolist (row '(("absolute" "urn:variant" "urn:variant") ("relative" "../variants#v" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/variants#v") ("fragment-not-shorthand" "#2valid/not-a-topicRef" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm#2valid/not-a-topicRef") ("empty-reference" "" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm") ("unicode" "urn:馬籠🦊" "urn:馬籠🦊") ("percent-UTF8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊"))) (destructuring-bind (label href expected) row (variant-iid-control label (format nil "~A" (variant-name-xml nil (variant-iid-xml "V" '("s") (list href)))) (lambda (map) (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (all-variants map)))) (check (concatenate 'string "variant-iid:" label ":only-native-variant-owns-resolved-locator") (and (= 3 (length (topic-map-topics map))) (= 1 (length (all-variants map))) (eq name (variant-parent v)) (eq v (canonical-variant v)) (equal (list expected) (item-identifiers v)) (equal (list v) (variant-iid-owners map expected)) (null (item-identifiers name)) (null (item-identifiers (topic-name-type name))) (equal (list (concatenate 'string *document* "#a")) (item-identifiers owner)) (equal (list (concatenate 'string *document* "#s")) (item-identifiers (by-id map "s")))))))))) (let ((*document* "https://variants.example.invalid/maps/input.xtm")) (variant-iid-control "alternate-document-multiple-duplicate" (format nil "~A" (variant-name-xml nil (variant-iid-xml "V" '("s") '("#v" "#v" "../variants#v" "urn:absolute" "#m%C3%A4")))) (lambda (map) (check "variant-iid:multiple-resolved-identifier-set" (same-identifiers-p (item-identifiers (first (all-variants map))) '("https://variants.example.invalid/maps/input.xtm#v" "https://variants.example.invalid/variants#v" "urn:absolute" "https://variants.example.invalid/maps/input.xtm#mä")))))) (dolist (same '(nil t)) (variant-iid-control (if same "equal-variants-same-II" "equal-variants-different-II") (format nil "~A" (variant-name-xml '("base") (concatenate 'string (variant-iid-xml "V" '("extra" "extra") '("urn:first")) (variant-iid-xml "V" '("extra") (if same '("urn:first") '("urn:second")))))) (lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (all-variants map))) (ids (if same '("urn:first") '("urn:first" "urn:second")))) (check (if same "variant-iid:same-II-idempotent-after-normalization" "variant-iid:different-IIs-union-after-normalization") (and (= 1 (length (all-variants map))) (eq v (canonical-variant v)) (eq name (variant-parent v)) (same-identifiers-p ids (item-identifiers v)) (every (lambda (id) (equal (list v) (variant-iid-owners map id))) ids))))))) (dolist (resource '("" "urn:resource")) (variant-iid-control (if (search "resourceRef" resource) "resourceRef" "anyURI-data") (format nil "~A" (variant-name-xml nil (variant-iid-xml nil '("s") '("urn:v") resource))) (lambda (map) (let ((v (first (all-variants map)))) (check (if (search "resourceRef" resource) "variant-iid:resourceRef-properties" "variant-iid:anyURI-data-properties") (and (equal "urn:resource" (variant-value v)) (equal (iri-datatype) (variant-datatype v)) (equal '("urn:v") (item-identifiers v)))))))) (variant-iid-control "variant-II-and-topic-SI-stay-separate" (format nil "~A" (variant-name-xml nil (variant-iid-xml "V" '("s") '("urn:v")))) (lambda (map) (let ((v (first (all-variants map)))) (check "variant-iid:no-implicit-reification-or-topic-merge" (and (= 3 (length (topic-map-topics map))) (null (topic-reified (by-id map "s"))) (null (item-reifier v)) (equal (list v) (variant-iid-owners map "urn:v")))))))) (defun variant-iid-merge-controls () (dolist (target '("parent" "variant-theme" "name-theme")) (dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity")) (let* ((label (format nil "variant-iid-merge:~A:~A" target kind)) (old-names nil) (old-variants nil) (old-themes nil) (first-v (variant-iid-xml "V" '("extra") '("urn:v1"))) (second-v (variant-iid-xml "V" '("extra") '("urn:v2"))) (body nil) (*name-controls* nil)) (labels ((topic-source (id locator content) (format nil "~A~A" id (if (equal kind "itemIdentity") "" (format nil "<~A href='~A'/>" kind locator)) content)) (bridge (a b) (format nil "<~A href='~A'/><~A href='~A'/>" kind (if (equal kind "itemIdentity") (concatenate 'string "#" a) "urn:one") kind (if (equal kind "itemIdentity") (concatenate 'string "#" b) "urn:two")))) (setf body (cond ((equal target "parent") (concatenate 'string (topic-source "a" "urn:one" (variant-name-xml '("base") first-v "urn:n1")) (topic-source "b" "urn:two" (variant-name-xml '("base") second-v "urn:n2")) (bridge "a" "b"))) ((equal target "variant-theme") (concatenate 'string (format nil "~A" (variant-name-xml '("base") (concatenate 'string (variant-iid-xml "V" '("x") '("urn:v1")) (variant-iid-xml "V" '("y") '("urn:v2"))) "urn:n1")) (topic-source "x" "urn:one" "") (topic-source "y" "urn:two" "") (bridge "x" "y"))) (t (concatenate 'string (format nil "~A~A" (variant-name-xml '("x") first-v "urn:n1") (variant-name-xml '("y") second-v "urn:n2")) (topic-source "x" "urn:one" "") (topic-source "y" "urn:two" "") (bridge "x" "y"))))) (staged-name-control label body (lambda (map step) (if (equal target "parent") (when (<= step 2) (let ((name (first (topic-names (by-id map (if (= step 1) "a" "b")))))) (push name old-names) (push (first (topic-name-variants name)) old-variants))) (when (= step 1) (setf old-names (topic-names (by-id map "a"))) (setf old-variants (loop for name in old-names append (topic-name-variants name))) (setf old-themes (list (by-id map "x") (by-id map "y"))))) (when (= step (if (equal target "parent") 2 3)) (check (concatenate 'string label ":distinct-before") (and (= 2 (length old-variants)) (not (eq (first old-variants) (second old-variants))) (every (lambda (v) (eq v (canonical-variant v))) old-variants))))) (lambda (map) (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (topic-name-variants name))) (before (variant-state map)) (themes (cond ((equal target "parent") (list (by-id map "base") (by-id map "extra"))) ((equal target "variant-theme") (list (by-id map "base") (by-id map "bridge"))) (t (list (by-id map "bridge") (by-id map "extra")))))) (check (concatenate 'string label ":canonical-parent-name-variant-handles") (and (= 1 (length (topic-names owner))) (= 1 (length (all-variants map))) (every (lambda (old) (eq name (canonical-topic-name old))) old-names) (every (lambda (old) (and (eq v (canonical-variant old)) (eq name (variant-parent old)) (scope-eq-p (variant-scope old) themes))) old-variants) (if (equal target "parent") (eq owner (by-id map "b")) (every (lambda (old) (eq (by-id map "bridge") (canonical-topic old))) old-themes)) (same-identifiers-p (item-identifiers name) (if (equal target "variant-theme") '("urn:n1") '("urn:n1" "urn:n2"))) (variants-current-p map))) (topic-maps.cxtm:canonicalize-octets map :base-locator *document*) (check (concatenate 'string label ":identifier-union-and-exclusive-canonical-owner") (and (same-identifiers-p '("urn:v1" "urn:v2") (item-identifiers v)) (every (lambda (id) (equal (list v) (variant-iid-owners map id))) '("urn:v1" "urn:v2")) (every (lambda (old) (same-identifiers-p '("urn:v1" "urn:v2") (item-identifiers old))) old-variants) (topic-maps.tmdm::validate-map-item-identifiers map))) (check (concatenate 'string label ":cxtm-pure-retains-handles") (equal before (variant-state map))) (push (topic-maps.test-foundation::fields "label" label "kind" "staged-native-reader-control" "sourceText" (document body) "model" (variant-model-data map)) *variant-controls*))))))))) (defun variant-iid-error-controls () (dolist (row '(("after-scope" "V" :element-order) ("after-resource" "V" :element-order))) (variant-error-control (concatenate 'string "variant-iid:" (first row)) (format nil "N~A" (second row)) 'xtm-rejection (third row))) (dolist (row '(("missing-href" "" :missing-attribute) ("attribute" "" :unexpected-attribute) ("child" "" :unexpected-child) ("text" "text" :unexpected-text) ("invalid-iri" "" :invalid-iri) ("percent-short" "" :invalid-percent-escape) ("percent-hex" "" :invalid-percent-escape) ("percent-utf8" "" :invalid-percent-utf8) ("percent-overlong" "" :invalid-percent-utf8))) (variant-error-control (concatenate 'string "variant-iid:" (first row)) (format nil "N~AV" (second row)) 'xtm-rejection (third row))) (dolist (row '(("collision-map" "N" "urn:collision") ("collision-topic" "N" "#a") ("collision-name" "N" "urn:collision"))) (variant-error-control (concatenate 'string "variant-iid:" (first row)) (concatenate 'string (second row) (variant-iid-xml "V" '("s") (list (third row))) "") 'item-identifier-error :duplicate-item-identifier)) (dolist (row '(("different-value" "W" ("s")) ("different-datatype" "" ("s")) ("different-scope" "urn:V" ("t")))) (variant-error-control (concatenate 'string "variant-iid:collision:" (first row)) (format nil "~A" (variant-name-xml nil (concatenate 'string (variant-iid-xml "urn:V" '("s") '("urn:collision")) (variant-iid-xml nil (third row) '("urn:collision") (second row))))) 'item-identifier-error :duplicate-item-identifier)) (variant-error-control "variant-iid:collision-different-parent" (format nil "~A~A" (variant-name-xml nil (variant-iid-xml "V" '("s") '("urn:collision"))) (variant-name-xml nil (variant-iid-xml "V" '("s") '("urn:collision")))) 'item-identifier-error :duplicate-item-identifier) (variant-error-control "variant-iid:topicRef-does-not-reify-variant" (format nil "~A" (variant-name-xml nil (concatenate 'string (variant-iid-xml "V" '("s") '("#v")) (variant-iid-xml "W" '("v") nil)))) 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier) (variant-error-control "variant-iid:scope-not-proper-superset" (format nil "~A" (variant-name-xml '("base") (variant-iid-xml "V" '("base") '("urn:v")))) 'variant-error :invalid-variant-scope) (variant-error-control "variant-iid:reifier-excluded-datatype" "N42" 'xtm-unsupported :resource-resolver-required) (variant-error-control "variant-iid:other-datatype-still-unsupported" (format nil "~A" (variant-name-xml nil (variant-iid-xml nil '("s") '("urn:v") "42"))) 'xtm-unsupported :resource-resolver-required) (let* ((key "variant-iid:invalid-raw-UTF8") (raw (concatenate '(vector (unsigned-byte 8)) (bytes "NV"))) (c (condition-from (lambda () (deserialize-octets raw :document-iri *document* :source-key key))))) (check "variant-iid:invalid-raw-UTF8-is-XML-error" (and (eq 'xtm-xml-error (type-of c)) (eq :xml-not-well-formed (error-code c)) (equal key (error-source-key c)) (equal *document* (error-document-iri c)))) (push (topic-maps.test-foundation::fields "label" key "kind" "invalid-XML-octets" "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "condition" (negative-condition-data c)) *variant-controls*))) (defun variant-iid-atomic-controls () (let* ((body (concatenate 'string "" (variant-name-xml nil (concatenate 'string (variant-iid-xml "V" '("s") '("urn:v1")) (variant-iid-xml "W" '("s") '("urn:v2"))) "urn:name") "")) (map (read-body body)) (owner (by-id map "a")) (name (first (topic-names owner))) (v (first (all-variants map))) (other (second (all-variants map)))) (dolist (target (list map owner name (by-id map "s") other)) (let* ((id (first (item-identifiers target))) (before (variant-state map)) (c (condition-from (lambda () (add-item-identifier v id)))) (label (cond ((eq target map) "map") ((eq target owner) "topic") ((eq target name) "name") ((eq target other) "other-variant") (t "theme")))) (check (concatenate 'string "variant-iid:atomic-conflict:" label) (and (eq 'item-identifier-error (type-of c)) (eq :duplicate-item-identifier (item-identifier-error-code c)) (equal before (variant-state map)) (eq v (canonical-variant v)) (eq other (canonical-variant other)) (equal (list target) (variant-iid-owners map id)) (equal '("urn:v1") (item-identifiers v)) (variants-current-p map) (topic-maps.tmdm::validate-map-item-identifiers map))) (push (topic-maps.test-foundation::fields "label" (concatenate 'string "variant-iid:atomic-conflict:" label) "kind" "native-TMDM-identifier-atomicity" "sourceText" (document body) "attemptedIdentifier" id "condition" (negative-condition-data c) "modelUnchanged" :true "model" (variant-model-data map)) *variant-controls*))))) (defun variant-iid-tests () (let ((*variant-controls* nil)) (variant-iid-value-controls) (variant-iid-merge-controls) (variant-iid-error-controls) (variant-iid-atomic-controls) (setf *variant-iid-controls* (nreverse *variant-controls*))))