(defpackage #:topic-maps.variant.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.ctm.tests #:check #:read-text #:api-topic #:cxtm #:map-state #:ctm-condition #:local-topic #:binary-file #:permutations #:run-scope-tests) (:export #:run-tests #:variant-checks #:write-variant-report)) (in-package #:topic-maps.variant.tests) (defvar *last-checks* nil) (defvar *observations* nil) (defvar *ordering-witness* nil) (defun variant-checks () *last-checks*) (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 condition-from (thunk) (handler-case (progn (funcall thunk) nil) (error (c) c))) (defun handle-state (handles) ;; Old objects matter too: serialization must not compress their forward chains. (mapcar (lambda (v) (list v (topic-maps.tmdm::%variant-forward v) (topic-maps.tmdm::%variant-parent v) (copy-list (topic-maps.tmdm::%variant-scope v)))) handles)) (defun variant-invariants (map) (every (lambda (topic) (every (lambda (name) (every (lambda (v) (and (eq v (canonical-variant v)) (eq name (variant-parent v)) (eq name (topic-maps.tmdm::%variant-parent v)) (topic-maps.tmdm::variant-scope-valid-p (variant-scope v) (topic-name-scope name)) (every (lambda (theme) (and (eq theme (canonical-topic theme)) (eq map (topic-parent theme)) (member theme (topic-map-topics map) :test #'eq))) (topic-maps.tmdm::%variant-scope v)))) (topic-name-variants name))) (topic-names topic))) (topic-map-topics map))) (defun observe-map (map) (let ((topics (topic-map-topics map))) (labels ((ordinal (topic) (1+ (position topic topics :test #'eq))) (themes (scope) (topic-maps.test-foundation::json-array (mapcar #'ordinal scope)))) (topic-maps.test-foundation::fields "topics" (topic-maps.test-foundation::json-array (loop for topic in topics collect (topic-maps.test-foundation::fields "ordinal" (ordinal topic) "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers topic)) "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers topic)) "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators topic))))) "names" (topic-maps.test-foundation::json-array (loop for topic in topics append (loop for name in (topic-names topic) collect (topic-maps.test-foundation::fields "owner" (ordinal topic) "value" (topic-name-value name) "type" (ordinal (topic-name-type name)) "scope" (themes (topic-name-scope name)) "variants" (topic-maps.test-foundation::json-array (loop for v in (topic-name-variants name) collect (topic-maps.test-foundation::fields "value" (variant-value v) "datatype" (variant-datatype v) "scope" (themes (variant-scope v)) "parentIsName" (if (eq name (variant-parent v)) :true :false) "rawParentIsName" (if (eq name (topic-maps.tmdm::%variant-parent v)) :true :false)))))))))))) (defun original-cases () (setf *observations* nil) (dolist (file '("variant" "variants" "variant-duplicate")) (let* ((id (format nil "ctm/in/~A.ctm" file)) (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))) (doc (topic-maps.test-foundation:test-case-document-iri case)) (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id)) (owner (local-topic map "topic" doc)) (name (first (topic-names owner))) (expected-count (if (string= file "variants") 2 1)) (before (map-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))) (check (concatenate 'string id ":exact-items-and-references") (and (= (+ 2 expected-count) (length (topic-map-topics map))) (= 1 (length (topic-names owner))) (= expected-count (length (all-variants map))) (= 1 (loop for topic in (topic-map-topics map) sum (length (topic-names topic)))) (null (topic-map-associations map)) (every (lambda (topic) (null (topic-occurrences topic))) (topic-map-topics map)) (string= "Name" (topic-name-value name)) (null (topic-name-scope name)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers (topic-name-type name))) (variant-invariants map))) (loop for v in (topic-name-variants name) for text in '("name" "nome") for theme in '("theme" "theme2") do (check (format nil "~A:~A:value-datatype-theme-identity" id text) (and (string= text (variant-value v)) (string= (string-datatype) (variant-datatype v)) (equal (list (local-topic map theme doc)) (variant-scope v)) (null (variant-item-identifiers v))))) (check (concatenate 'string id ":byte-exact-baseline") (equalp actual expected)) (check (concatenate 'string id ":serialization-pure") (equal before (map-state map))) (topic-maps.test-foundation:record-result case :cxtm-match) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.4.1" "CTM 3.11" "CTM 3.12" "TMDM 5.5" "TMDM 6.3" "TMDM 6.4" "CXTM 3.6" "CXTM 3.7" "CXTM 3.12" "CXTM 3.14" "CXTM 3.16" "CXTM 3.19" "CXTM 4.8")) (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results* (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8) :match t :bytes (length actual))))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "actualObjects" (observe-map map)) *observations*) (format t "~&VARIANT-CXTM ~A: PASS (~D bytes)~%" id (length actual)))) (let* ((id "ctm/invalid/variant-no-scope.ctm") (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))) (e (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id))))) (check "variant-no-scope:original-specific-rejection-position" (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq :expected-variant-scope (topic-maps.ctm:error-code e)) (= 28 (topic-maps.ctm:error-position e)) (= 3 (topic-maps.ctm:error-line e)) (= 22 (topic-maps.ctm:error-column e)))) (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string e))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.12" "TMDM 5.5"))) (setf *observations* (nreverse *observations*))) (defun api-tests () (let* ((map (make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type")) (p (api-topic map "p")) (a (api-topic map "a")) (b (api-topic map "b")) (name (add-name owner "N" :type type :scope (list p))) (input (list p a a)) (one (add-variant name "V" :scope input)) (same (add-variant name "V" :scope (list a p))) (different-scope (add-variant name "V" :scope (list p b))) (different-type (add-variant name "V" :datatype "urn:datatype" :scope (list a p)))) (check "variant-api:sets-deduplicate-and-replacement-handles" (and (eq (canonical-variant one) same) (not (eq one same)) (= 3 (length (topic-name-variants name))) (not (eq same different-scope)) (not (eq same different-type)) (variant-invariants map))) (setf (first input) owner) (let ((output (variant-scope one))) (setf (first output) type)) (let ((output (topic-name-variants name))) (setf (first output) nil)) (check "variant-api:defensive-set-copies" (and (equal (list p a) (variant-scope one)) (every #'identity (topic-name-variants name)))) (let* ((foreign (api-topic (make-topic-map) "foreign")) (cycle (list a))) (setf (cdr cycle) cycle) (dolist (fixture (list (list nil :invalid-variant-scope) (list (list a) :invalid-variant-scope) (list (list p) :invalid-variant-scope) (list (list foreign) :foreign-theme) (list (list "a") :invalid-theme) (list (cons p a) :invalid-scope) (list cycle :invalid-scope))) (let* ((before (map-state map)) (e (condition-from (lambda () (add-variant name "X" :scope (first fixture)))))) (check (format nil "variant-api:~A:~D:atomic" (second fixture) (length topic-maps.ctm.tests::*checks*)) (and (typep e (if (eq (second fixture) :invalid-variant-scope) 'variant-error 'scope-error)) (eq (second fixture) (if (typep e 'variant-error) (variant-error-code e) (scope-error-code e))) (equal before (map-state map))))))) (dolist (fixture (list (list "x" "relative" :invalid-datatype) (list "x" (integer-datatype) :invalid-integer-value) (list "x" (decimal-datatype) :invalid-decimal-value) (list "0000-01-01" (date-datatype) :invalid-date-value) (list "x" (datetime-datatype) :invalid-datetime-value) (list "relative" (iri-datatype) :relative-uri-value))) (let* ((before (map-state map)) (e (condition-from (lambda () (add-variant name (first fixture) :datatype (second fixture) :scope (list p a)))))) (check (format nil "variant-api:~A:~A:literal-atomic" (second fixture) (third fixture)) (and (typep e 'variant-error) (eq (third fixture) (variant-error-code e)) (equal before (map-state map)))))) (let ((other-name (add-name owner "Other" :type type :scope (list p)))) (check "variant-api:different-parent-prevents-merging" (not (eq (canonical-variant one) (add-variant other-name "V" :scope (list p a)))))) (let* ((n (add-name owner "Empty scope" :type type)) (before (map-state map))) (check "variant-api:empty-parent-still-needs-nonempty-variant" (and (typep (condition-from (lambda () (add-variant n "V"))) 'variant-error) (equal before (map-state map))))) (dolist (thunk (list (lambda () (add-variant name nil :scope (list p a))) (lambda () (add-variant name "V" :datatype nil :scope (list p a))) (lambda () (add-variant owner "V" :scope (list p a))))) (let ((before (map-state map))) (check (format nil "variant-api:type-error:~D" (length topic-maps.ctm.tests::*checks*)) (and (typep (condition-from thunk) 'type-error) (equal before (map-state map)))))))) (defun merge-tests () (let* ((map (make-topic-map)) (o (api-topic map "o")) (type1 (api-topic map "type1")) (type2 (api-topic map "type2")) (theme (api-topic map "theme")) (n1 (add-name o "N" :type type1)) (n2 (add-name o "N" :type type2)) (v1 (add-variant n1 "one" :scope (list theme))) (v2 (add-variant n2 "two" :scope (list theme)))) (merge-topics type1 type2) (check "variant-merge:name-union-preserves-distinct-children-and-stored-parents" (and (= 1 (length (topic-names o))) (= 2 (length (all-variants map))) (eq (canonical-topic-name n1) (canonical-topic-name n2)) (eq (canonical-topic-name n1) (variant-parent v1)) (eq (canonical-topic-name n2) (variant-parent v2)) (variant-invariants map)))) (dolist (kind '(:owner :type :name-scope :variant-scope)) (dolist (reverse '(nil t)) (let* ((map (make-topic-map)) (o (api-topic map "owner")) (t1 (api-topic map "type")) (a (api-topic map "a")) (b (api-topic map "b")) (x (api-topic map "x")) (o2 (if (eq kind :owner) (api-topic map "owner2") o)) (t2 (if (eq kind :type) (api-topic map "type2") t1)) (n1 (add-name o "N" :type t1 :scope (list a))) (n2 (add-name o2 "N" :type t2 :scope (list (if (eq kind :name-scope) b a)))) (v1 (add-variant n1 "V" :scope (list a x))) (v2 (add-variant n2 "V" :scope (list (if (eq kind :name-scope) b a) (if (eq kind :variant-scope) b x)))) (pair (ecase kind (:owner (list o o2)) (:type (list t1 t2)) (:name-scope (list a b)) (:variant-scope (list x b))))) (when reverse (setf pair (reverse pair))) (apply #'merge-topics pair) (check (format nil "variant-merge:~A:~A:parents-and-forward-handles" kind reverse) (and (eq (canonical-topic-name n1) (canonical-topic-name n2)) (eq (canonical-variant v1) (canonical-variant v2)) (= 1 (length (all-variants map))) (eq (canonical-topic-name n1) (variant-parent v1)) (eq (canonical-topic o) (topic-name-parent (variant-parent v2))) (variant-invariants map))) (let ((before (map-state map)) (handles (handle-state (list v1 v2)))) (check (format nil "variant-merge:~A:~A:pure-output-and-old-handles" kind reverse) (and (string= (cxtm map) (cxtm map)) (equal before (map-state map)) (equal handles (handle-state (list v1 v2))))))))) ;; Parent scope loses a duplicate theme but retains an additional Variant theme. (let* ((map (make-topic-map)) (o (api-topic map "o")) (a (api-topic map "a")) (b (api-topic map "b")) (x (api-topic map "x")) (n (add-name o "N" :scope (list a b))) (v (add-variant n "V" :scope (list a b x))) (c (merge-topics a b))) (check "variant-merge:scope-cardinality-reduction-remains-valid" (and (equal (list c) (topic-name-scope n)) (= 2 (length (variant-scope v))) (member c (variant-scope v) :test #'eq) (member x (variant-scope v) :test #'eq) (variant-invariants map))) (let ((again (add-variant n "V" :scope (list b a x)))) (check "variant-merge:retained-theme-handles-at-api-boundary" (eq again (canonical-variant v))))) (dolist (kind '(:explicit :item-identifier :subject-identifier :subject-locator)) (let* ((map (make-topic-map)) (o (api-topic map "o")) (p (api-topic map "p")) (x (api-topic map "x")) (n (add-name o "N" :scope (list p))) (v (add-variant n "V" :scope (list p x)))) (unless (eq kind :explicit) (add-topic-identifier p kind "urn:shared")) (let* ((before (map-state map)) (handles (handle-state (list v))) (e (condition-from (lambda () (if (eq kind :explicit) (merge-topics p x) (add-topic-identifier x kind "urn:shared")))))) (check (format nil "variant-merge:~A:collapse-rejected-before-any-mutation" kind) (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e)) (equal before (map-state map)) (equal handles (handle-state (list v))) (eq p (canonical-topic p)) (eq x (canonical-topic x)) (variant-invariants map)))))) ;; Global preflight must catch a transitive collapse before the first pair merge. (let* ((map (make-topic-map)) (o (api-topic map "o")) (p (api-topic map "p")) (x (api-topic map "x")) (y (api-topic map "y")) (n (add-name o "N" :scope (list p)))) (add-variant n "V" :scope (list p x y)) (setf (topic-maps.tmdm::%topic-subject-identifiers p) '("urn:one") (topic-maps.tmdm::%topic-subject-identifiers x) '("urn:one" "urn:two") (topic-maps.tmdm::%topic-subject-identifiers y) '("urn:two")) (let ((before (map-state map)) (e (condition-from (lambda () (normalize-topic-map map))))) (check "variant-merge:transitive-normalization-atomic" (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e)) (equal before (map-state map))))))) (defun reader-tests () (let* ((map (read-text "a - kind:\"N\"@p (\"V\"@x,p,x)(\"W\"@y).")) (n (first (topic-names (local-topic map "a")))) (vs (topic-name-variants n))) (check "variant-reader:explicit-vs-resulting-scope" (and (= 5 (length (topic-map-topics map))) (= 2 (length vs)) (eq (local-topic map "kind") (topic-name-type n)) (equal (list (local-topic map "p")) (topic-name-scope n)) (every (lambda (v) (= 2 (length (variant-scope v)))) vs) (member (local-topic map "x") (variant-scope (first vs)) :test #'eq) (member (local-topic map "p") (variant-scope (second vs)) :test #'eq) (variant-invariants map)))) (let* ((map (read-text "%prefix x a - \"N\" (\"01\"^^x:datatype @x:theme)(x:value @x:theme).")) (vs (all-variants map))) (check "variant-reader:datatype-and-uri-never-synthesized-topics" (and (= 3 (length (topic-map-topics map))) (equal '("01" "urn:value") (mapcar #'variant-value vs)) (equal (list "urn:datatype" (iri-datatype)) (mapcar #'variant-datatype vs))))) (let* ((map (read-text "%prefix xsd a - \"N\" (1@t)(\"01\"^^xsd:integer@t)(1.0@t)(2000-01-01@t)(2000-01-01T24:00:00Z@t).")) (vs (all-variants map)) (before (map-state map))) (check "variant-reader:shared-literal-semantics-and-distinct-lexical-equality" (and (= 5 (length vs)) (equal '("1" "01" "1.0" "2000-01-01" "2000-01-02T00:00:00Z") (mapcar #'variant-value vs)) (equal (list (integer-datatype) (integer-datatype) (decimal-datatype) (date-datatype) (datetime-datatype)) (mapcar #'variant-datatype vs)) (search "1" (cxtm map)) (equal before (map-state map)) (= 5 (length (all-variants map)))))) (dolist (fixture '(("a - \"N\" (\"V\")." :expected-variant-scope) ("a - \"N\" (\"V\"@)." :expected-scope-theme) ("a - \"N\" (\"V\"@t,)." :expected-scope-theme) ("a - \"N\" (\"V\"@t." :missing-variant-close) ("a - \"N\" (@t)." :expected-variant-value) ("a - \"N\"@t (\"V\"@t)." :invalid-variant-scope) ("a t:\"V\" (\"bad\"@t)." :unexpected-variant) ("a - \"N\" (\"V\"@x:t)." :undefined-prefix) ("a - \"N\" (\"V\"@t); - \"N\" (\"W\"@t)." nil))) (let ((e (ctm-condition (first fixture)))) (check (concatenate 'string "variant-reader:diagnostic:" (first fixture)) (if (null (second fixture)) (null e) (and (eq (type-of e) (if (eq (second fixture) :undefined-prefix) 'topic-maps.ctm:undefined-prefix 'topic-maps.ctm:ctm-rejection)) (eq (second fixture) (topic-maps.ctm:error-code e)) (<= 0 (topic-maps.ctm:error-position e) (length (first fixture))) (= 1 (topic-maps.ctm:error-line e)) (= (1+ (topic-maps.ctm:error-position e)) (topic-maps.ctm:error-column e))))))) (let ((e (ctm-condition (format nil "a - \"N\" (~% \"V\").")))) (check "variant-reader:missing-scope-exact-line-column" (and (eq :expected-variant-scope (topic-maps.ctm:error-code e)) (= 2 (topic-maps.ctm:error-line e)) (= 5 (topic-maps.ctm:error-column e))))) (let ((e (ctm-condition "a - \"N\"@p (\"V\"@x). p . x ."))) (check "variant-reader:identity-triggered-collapse-is-specific-ctm-rejection" (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq :variant-scope-collapse (topic-maps.ctm:error-code e)) (plusp (topic-maps.ctm:error-position e))))) (let* ((map (read-text "a - \"N\" (\"one\"@t); - \"N\" (\"two\"@t).")) (n (first (topic-names (local-topic map "a"))))) (check "variant-reader:duplicate-name-preserves-all-children" (and (= 1 (length (topic-names (local-topic map "a")))) (= 2 (length (topic-name-variants n)))))) (dolist (file '("variant-reifier" "variant-scope-embedded" "variant-scope-embedded2" "variant-scope-embedded3" "variant-scope-embedded4")) (let* ((case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id (format nil "ctm/in/~A.ctm" file)))) (e (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri (topic-maps.test-foundation:test-case-document-iri case)))))) (check (concatenate 'string "variant-reader:reference-contract-remains-unexecuted:" file) (and (if (member file '("variant-reifier" "variant-scope-embedded" "variant-scope-embedded2") :test #'string=) (null e) (typep e 'topic-maps.ctm:ctm-unsupported)) (eq :not-run (topic-maps.test-foundation:test-case-status case))))))) (defun ordering-tests () (let ((reference nil)) (dolist (order (permutations '(0 1 2 3 4))) (let* ((map (make-topic-map)) (o (api-topic map "owner")) (type (api-topic map "type")) (a (api-topic map "a")) (b (api-topic map "b")) (n (add-name o "N" :type type)) (specs (list (list "A" (string-datatype) (list a)) (list "V" "urn:a" (list b)) (list "V" "urn:b" (list a)) (list "V" "urn:b" (list b a)) (list "urn:0" (iri-datatype) (list a))))) (dolist (i order) (let ((spec (nth i specs))) (add-variant n (first spec) :datatype (second spec) :scope (third spec)))) (let ((before (map-state map)) (output (cxtm map))) (when reference (check (format nil "variant-order:~S:construction-independent" order) (string= reference output))) (setf reference output *ordering-witness* output) (check (format nil "variant-order:~S:deterministic-and-mutation-free" order) (and (string= output (cxtm map)) (equal before (map-state map)))))))) (let* ((map (make-topic-map)) (o (api-topic map "o")) (a (api-topic map "a")) (n1 (add-name o "A")) (n2 (add-name o "Z")) (v1 (add-variant n1 "V" :scope (list a))) (v2 (add-variant n2 "V" :scope (list a)))) (let ((topic-maps.cxtm::*base-locator* "https://example.invalid/a/doc.ctm") (topic-maps.cxtm::*locator-cache* (make-hash-table :test #'equal))) (check "variant-order:parent-name-final-comparison-key" (minusp (topic-maps.cxtm::variant-order v1 v2)))) (check "variant-order:numbering-restarts-in-each-name" (= 2 (loop with xml = (cxtm map) with start = 0 for pos = (search "" xml :start2 start) while pos count pos do (setf start (1+ pos)))))) (let* ((map (read-text "a - \"N\" (\"V\"@t).")) (v (first (all-variants map))) (n (variant-parent v)) (saved (variant-scope v))) (dolist (scope (list nil (list (first saved) (first saved)) (list (api-topic (make-topic-map) "foreign")))) (unwind-protect (progn (setf (topic-maps.tmdm::%variant-scope v) scope) (let ((before (map-state map))) (check (format nil "variant-cxtm:invalid-scope:~D:refused-without-mutation" (length scope)) (and (typep (condition-from (lambda () (cxtm map))) 'topic-maps.cxtm:cxtm-error) (equal before (map-state map)))))) (setf (topic-maps.tmdm::%variant-scope v) saved))) (let ((old (add-variant n "V" :scope saved))) (declare (ignore old)) (let ((current (topic-name-variants n))) (unwind-protect (progn (setf (topic-maps.tmdm::%topic-name-variants n) (list v)) (check "variant-cxtm:obsolete-variant-in-live-set-rejected" (eq :invalid-parent (topic-maps.cxtm:error-code (condition-from (lambda () (cxtm map))))))) (setf (topic-maps.tmdm::%topic-name-variants n) current)))))) (defun run-tests () (run-scope-tests) (let ((topic-maps.ctm.tests::*checks* nil)) (original-cases) (api-tests) (merge-tests) (reader-tests) (ordering-tests) (check "variant:only-four-original-contract-transitions" (equal '(:pass 71 :fail 2 :unsupported 0 :not-run 200) (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses))) (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*)) (format t "~&VARIANTS-PASS: ~D checks; 3 original byte-exact comparisons, 1 specific rejection; 71 PASS, 2 known FAIL, 200 NOT-RUN.~%" (length *last-checks*)) t)) (defun write-variant-report (path) (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value (topic-maps.test-foundation::fields "kind" "variant-evidence" "passed" (length *last-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-checks*) "orderingWitnessCXTM" *ordering-witness* "cases" (topic-maps.test-foundation::json-array *observations*)) out) (terpri out)))