(in-package #:topic-maps.xtm.tests)
(defparameter *variant-original-specs*
'(("variant" 3 nil ("command") 1)
("variant-duplicate" 3 nil ("command") 2)
("variant-scope-duplicate" 3 nil ("command") 1)
("variant-scope-multiple" 4 nil ("command" "silly") 1)
("variant-inherit" 4 ("english") ("english" "command") 1)))
(defparameter *variant-negative-spec*
'("variant-missing-scope-duplicate" variant-error :invalid-variant-scope
"Variant scope must properly contain its Parent Name scope." nil nil "XTM 2.0 4.12,4.13; TMDM 5.5: proper superset of Parent Name scope"))
(defun all-variants (map)
(loop for topic in (topic-map-topics map) append
(loop for name in (topic-names topic) append (topic-name-variants name))))
(defun variants-current-p (map)
(and (names-current-p map)
(every (lambda (topic)
(every (lambda (name)
(every (lambda (v)
(and (eq v (canonical-variant v)) (eq name (variant-parent v))
(> (length (variant-scope v)) (length (topic-name-scope name)))
(subsetp (topic-name-scope name) (variant-scope v) :test #'eq)
(every (lambda (theme) (and (eq theme (canonical-topic theme))
(member theme (topic-map-topics map) :test #'eq))) (variant-scope v)))) (topic-name-variants name)))
(topic-names topic))) (topic-map-topics map))))
(defun variant-state (map)
(list (name-state map)
(loop for v in (all-variants map) collect
(list v (variant-parent v) (variant-value v) (variant-datatype v) (variant-scope v)
(item-identifiers v) (item-reifier v) (topic-maps.tmdm::%variant-forward v)))))
(defun variant-model-data (map)
(let ((topics (topic-map-topics map))
(names (loop for topic in (topic-map-topics map) append (topic-names topic))))
(topic-maps.test-foundation::fields "nameModel" (name-model-data map)
"canonicalVariantReferences" (if (variants-current-p map) :true :false)
"variants" (coerce (loop for v in (all-variants map) collect
(topic-maps.test-foundation::fields "value" (variant-value v) "datatype" (variant-datatype v)
"parentName" (1+ (position (variant-parent v) names :test #'eq))
"scope" (coerce (mapcar (lambda (theme) (1+ (position theme topics :test #'eq))) (variant-scope v)) 'vector)
"itemIdentifiers" (coerce (item-identifiers v) 'vector) "reifier" nil)) 'vector))))
(defun variant-original-model-checks (spec map raw)
(destructuring-bind (id count name-scope-ids variant-scope-ids source-count) spec
(let* ((owner (by-id map "topic")) (name (first (topic-names owner))) (v (first (topic-name-variants name)))
(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 (remove-if-not (lambda (node) (equal "variant" (topic-maps.xtm::node-name node))) (topic-maps.xtm::elements source-name)))
(label (concatenate 'string "variant-original:" id)))
(check (concatenate 'string label ":original-textual-subset")
(and (= source-count (length source-variants))
(every (lambda (node) (let ((kids (topic-maps.xtm::elements node)))
(and (null (topic-maps.xtm::node-attributes node)) (= 2 (length kids))
(equal "scope" (topic-maps.xtm::node-name (first kids)))
(equal "resourceData" (topic-maps.xtm::node-name (second kids)))
(null (topic-maps.xtm::node-attributes (second kids)))
(equal '("TOPIC!") (topic-maps.xtm::node-children (second kids)))))) source-variants)))
(check (concatenate 'string label ":native-counts-and-parent-name-eq")
(and (= count (length (topic-map-topics map))) (null (topic-map-associations map))
(= 1 (loop for topic in (topic-map-topics map) sum (length (topic-names topic))))
(= 1 (length (topic-name-variants name))) (= 1 (length (all-variants map)))
(eq owner (topic-name-parent name)) (eq name (canonical-topic-name name))
(eq name (variant-parent v)) (eq v (canonical-variant v))
(eq (by-si map (default-name-type-identifier)) (topic-name-type name))))
(check (concatenate 'string label ":value-datatype-and-scope-union")
(and (equal "Topic" (topic-name-value name)) (equal "TOPIC!" (variant-value v))
(equal (string-datatype) (variant-datatype v))
(scope-eq-p (topic-name-scope name) (mapcar (lambda (id) (by-id map id)) name-scope-ids))
(scope-eq-p (variant-scope v) (mapcar (lambda (id) (by-id map id)) variant-scope-ids))
(variants-current-p map)))
(check (concatenate 'string label ":identities-and-excluded-metadata")
(and (null (item-identifiers map)) (null (item-identifiers name)) (null (item-reifier name))
(null (item-identifiers v)) (null (item-reifier v))
(equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
(every (lambda (id) (let ((theme (by-id map id)))
(and (equal (list (concatenate 'string *document* "#" id)) (item-identifiers theme))
(null (topic-subject-identifiers theme)) (null (topic-subject-locators theme))))) variant-scope-ids)
(topic-maps.tmdm::validate-map-item-identifiers map))))))
(defun variant-original-contracts ()
(setf *variant-outputs* nil)
(dolist (spec *variant-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.12,4.13,4.16" "TMDM 5.5,6.2,6.3,6.4" "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 (variant-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 (variant-original-model-checks spec map raw) (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 (variant-model-data map))
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *variant-outputs*)
(when c (error c))
(check (concatenate 'string "variant-original:" name ":byte-exact") (equalp actual expected))
(check (concatenate 'string "variant-original:" name ":source-preserved") (equalp raw copy))
(check (concatenate 'string "variant-original:" name ":pure") (equal before (variant-state map)))
(check (concatenate 'string "variant-original:" name ":deterministic")
(equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
(check (concatenate 'string "variant-original:" name ":repeat-pure") (equal before (variant-state map)))
(let ((other (deserialize-octets raw :document-iri *document* :source-key "different-name-provenance")))
(check (concatenate 'string "variant-original:" name ":source-key-only-provenance")
(and (equalp (variant-model-data map) (variant-model-data other))
(equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
(setf *variant-outputs* (nreverse *variant-outputs*))
(check "variant-originals:55-unique-case-records"
(and (= 55 (length *cases*)) (= 48 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
(= 55 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
(defun variant-negative-original ()
(let* ((spec *variant-negative-spec*) (id "xtm2/invalid/variant-missing-scope-duplicate.xtm")
(iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(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)))))
(kids (topic-maps.xtm::elements source-name)) (v (third kids))
(scope (first (topic-maps.xtm::elements v)))
(case (topic-maps.test-foundation:make-test-case :id id :category :invalid :source-pathname (corpus-path id)
:document-iri iri :expected-outcome :ctm-rejection :clauses (list (seventh spec))))
(c (condition-from (lambda () (deserialize-octets raw :document-iri iri :source-key id)))))
(check "variant-negative:original-equal-scopes-and-textual-resource"
(and (= 3 (length kids)) (equal "scope" (topic-maps.xtm::node-name (first kids)))
(equal "variant" (topic-maps.xtm::node-name v))
(equal "#command" (topic-maps.xtm::attr (first (topic-maps.xtm::elements (first kids))) "href"))
(equal "#command" (topic-maps.xtm::attr (first (topic-maps.xtm::elements scope)) "href"))
(equal "resourceData" (topic-maps.xtm::node-name (second (topic-maps.xtm::elements v))))))
(record-negative-original-result case spec c iri id)
(setf *cases* (append *cases* (list case)))
(setf *variant-negative-outputs* (list (topic-maps.test-foundation::fields
"id" id "documentIRI" iri "sourceKey" id "sourceBytes" (length raw)
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"condition" (negative-condition-data c) "sourceVariantElementPosition"
(topic-maps.test-foundation::fields "line" (topic-maps.xtm::node-line v) "column" (topic-maps.xtm::node-column v)
"unit" "approximate-SAX-element-position-not-condition-position")
"intendedCauseReached" (if (intended-negative-p spec c iri id) :true :false)
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case))))))
(check "variant-negative:intended-native-scope-condition" (intended-negative-p spec c iri id))
(check "variant-negative:source-preserved" (equalp raw copy))
(check "variant-originals:56-unique-case-records"
(and (= 56 (length *cases*)) (= 48 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
(= 56 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))))
(defun scope-xml (ids)
(format nil "~{~A~}" (mapcar (lambda (id) (format nil "" id)) ids)))
(defun variant-xml (value themes)
(format nil "~A~A" (scope-xml themes) value))
(defun variant-name-xml (name-themes variants &optional ii)
(format nil "~A~AN~A"
(if ii (format nil "" ii) "")
(if name-themes (scope-xml name-themes) "") variants))
(defun variant-control (label body verify)
(let* ((map (read-body body)) (before (variant-state map)))
(funcall verify map)
(check (concatenate 'string "variant-control:" label ":canonical-and-proper-scope") (variants-current-p map))
(let ((actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
(check (concatenate 'string "variant-control:" label ":pure") (equal before (variant-state map)))
(push (topic-maps.test-foundation::fields "label" label "kind" "positive-XTM"
"sourceText" (document body) "model" (variant-model-data map)
"actualCXTM" (babel:octets-to-string actual :encoding :utf-8)) *variant-controls*))))
(defun variant-error-control (label body class code)
(let* ((key (concatenate 'string "variant-syntax:" label)) (source (document body))
(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 (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
((typep c 'variant-error) (variant-error-code c)) ((typep c 'item-identifier-error) (item-identifier-error-code c))))))
(when (typep c 'topic-maps.xtm:xtm-error)
(check (concatenate 'string key ":context-position")
(and (equal key (error-source-key c)) (equal *document* (error-document-iri c))
(plusp (error-line c)) (plusp (error-column c)))))
(push (topic-maps.test-foundation::fields "label" label "kind" "negative-or-unsupported-XTM"
"sourceText" source "documentIRI" *document* "sourceKey" key "condition" (negative-condition-data c)) *variant-controls*)))
(defun variant-text-controls ()
(dolist (row (list (list "simple" "V" "V") (list "empty" "" "")
(list "unicode-no-normalization" "馬籠🦊é" "馬籠🦊é")
(list "escaping-once" "< < & 🦊" "< < & 🦊")
(list "whitespace" (format nil " A~C B~% C " #\Tab) (format nil " A~C B~% C " #\Tab))
(list "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")))
(variant-control (first row) (format nil "~A" (variant-name-xml nil (variant-xml (second row) '("s"))))
(lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (topic-name-variants name))))
(check (concatenate 'string "variant-text:" (first row))
(and (equal (third row) (variant-value v)) (equal (string-datatype) (variant-datatype v))
(eq name (variant-parent v)) (scope-eq-p (variant-scope v) (list (by-id map "s")))))))))
(variant-control "explicit-xsd-string"
"NV"
(lambda (map) (check "variant:explicit-string-equals-default"
(let ((other (read-body (format nil "~A" (variant-name-xml nil (variant-xml "V" '("s")))))))
(and (equalp (variant-model-data map) (variant-model-data other))
(equalp (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
(topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
(variant-control "inherited-union-duplicates-forward"
(format nil "~A"
(variant-name-xml '("base") (concatenate 'string (variant-xml "V" '("base" "s" "t" "s")) (variant-xml "V" '("t" "s")))))
(lambda (map) (let ((name (first (topic-names (by-id map "a")))))
(check "variant:union-set-order-and-normalization"
(and (= 1 (length (topic-name-variants name)))
(scope-eq-p (variant-scope (first (topic-name-variants name))) (list (by-id map "base") (by-id map "s") (by-id map "t"))))))))
(variant-control "different-values-and-scopes"
(format nil "~A" (variant-name-xml nil
(concatenate 'string (variant-xml "V" '("s")) (variant-xml "V " '("s")) (variant-xml "V" '("t")))))
(lambda (map) (check "variant:different-properties-stay-distinct" (= 3 (length (all-variants map))))))
(dolist (kind '("subjectIdentifier" "itemIdentity"))
(variant-control (concatenate 'string "reference-hit:" kind)
(format nil "<~A href='https://themes.invalid/map#theme'/>NV" kind)
(lambda (map) (check (concatenate 'string "variant:reference-hit-no-extra-II:" kind)
(and (scope-eq-p (variant-scope (first (all-variants map))) (list (by-id map "s")))
(same-identifiers-p (item-identifiers (by-id map "s"))
(append (list (concatenate 'string *document* "#s")) (when (equal kind "itemIdentity") '("https://themes.invalid/map#theme"))))))))))
(defun variant-merge-controls ()
(dolist (target '("parent" "variant-theme" "name-theme"))
(dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(let* ((label (format nil "variant-merge:~A:~A" target kind)) (old-names nil) (old-variants nil) (old-themes nil)
(first-v (variant-xml "V" '("extra"))) (second-v first-v) (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-xml "V" '("x")) (variant-xml "V" '("y"))) "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 ":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-scope-error-controls ()
(dolist (row '(("equal" ("base") ("base")) ("explicit-subset" ("base" "s") ("base"))
("duplicate-adds-no-theme" ("base") ("base" "base"))))
(variant-error-control (first row)
(format nil "~A" (variant-name-xml (second row) (variant-xml "V" (third row))))
'variant-error :invalid-variant-scope))
(variant-error-control "SI-alias-adds-no-theme"
"NV"
'variant-error :invalid-variant-scope)
(variant-error-control "forward-merge-collapses-proper-scope"
(format nil "~A"
(variant-name-xml '("base") (variant-xml "V" '("extra")))) 'variant-error :variant-scope-collapse)
(variant-error-control "topicRef-conflicts-with-name-II"
"NV"
'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier)
(dolist (kind '(:subject-identifier :subject-locator :item-identifier))
(let* ((element (ecase kind (:subject-identifier "subjectIdentifier") (:subject-locator "subjectLocator") (:item-identifier "itemIdentity")))
(body (format nil "<~A href='urn:base'/>~A"
element element (variant-name-xml '("base") (variant-xml "V" '("extra")))))
(map (read-body body)) (v (first (all-variants map))) (before (variant-state map))
(c (condition-from (lambda () (add-topic-identifier (by-id map "extra") kind "urn:base")))))
(check (format nil "variant-collapse:~A:specific-atomic-retained" kind)
(and (eq 'variant-error (type-of c)) (eq :variant-scope-collapse (variant-error-code c))
(equal before (variant-state map)) (eq v (canonical-variant v)) (variants-current-p map)))
(push (topic-maps.test-foundation::fields "label" (format nil "collapse-~A" kind) "kind" "native-TMDM-collapse-atomicity"
"sourceText" (document body) "condition" (negative-condition-data c) "modelUnchanged" :true
"model" (variant-model-data map)) *variant-controls*))))
(defun variant-syntax-controls ()
(dolist (row '(("empty-variant" "" :missing-variant-scope)
("missing-scope" "V" :missing-variant-scope)
("missing-resource" "" :missing-variant-resource)
("empty-scope" "V" :empty-scope)
("scope-wrong-child" "NV" :unexpected-element)
("scope-text" "badV" :unexpected-text)
("multiple-scopes" "V" :duplicate-variant-scope)
("scope-after-resource" "V" :element-order)
("multiple-resources" "AB" :variant-resource-cardinality)
("mixed-resources" "A" :variant-resource-cardinality)
("variant-attribute" "V" :unexpected-attribute)
("data-attribute" "V" :unexpected-attribute)
("XTM-markup-in-data" "V" :unexpected-child)
("unknown-child" "V" :unexpected-element)
("text-in-variant" "badV" :unexpected-text)
("wrong-namespace" "V" :invalid-namespace)
("bad-href" "V" :invalid-topic-ref)
("resourceRef-missing-href" "" :missing-attribute)))
(variant-error-control (first row) (format nil "N~A" (second row)) 'xtm-rejection (third row)))
(dolist (row '(("resourceRef" "" :unsupported-construct)
("itemIdentity" "V" :unsupported-construct)
("reifier" "V" :reification)
("datatype" "V" :resource-data-datatype)
("embedded-XML" "V" :resource-data-markup)))
(variant-error-control (first row) (format nil "N~A" (second row)) 'xtm-unsupported (third row))))
(defun variant-tests ()
(setf *variant-controls* nil)
(variant-text-controls) (variant-merge-controls) (variant-scope-error-controls) (variant-syntax-controls)
(setf *variant-controls* (nreverse *variant-controls*)))