(in-package #:topic-maps.equivalence.tests) ;; Independent values fixed before either productive Reader runs. XML is never ;; serialized/repaired here; CTM explicitly carries the canonical lexical value. (defparameter *at-datatype* "http://www.w3.org/2001/XMLSchema#anyType") (defvar *at-language* nil) (defvar *at-key* nil) (defun at-literal (value) (format nil "~S^^<~A>" value *at-datatype*)) (defun at-resource (content &optional (declarations "")) (format nil "~A" *at-datatype* declarations content)) (defun at-pair-cases () (let* ((occ "Grüße 水 & B") (var "V&🦊") (norm "a&b") (norm-var "V&🦊") (clauses '("CTM 3.4.1 [28]-[29],3.10-3.12" "XTM 4.16.2-4.16.3" "TMDM 4.4,5.5-5.6,6.4-6.5"))) (flet ((spec (id kind value ctm body keys counts why &key normalized) (append (pair id ctm body keys counts why clauses) (list :anytype-kind kind :canonical-value value :normalization normalized)))) (list (spec "anytype-occurrence" :occurrence occ (format nil "a . a =. a ^. a t:~A@s ~~r." (at-literal occ)) (format nil "~A" (at-resource "Grüße 水 & B" " xmlns:p='urn:payload' xmlns:u='urn:unused'")) (list (list "a" :si '("urn:anytype:subject") :sl '("urn:anytype:resource") :ii '("urn:anytype:item")) "t" "s" "r") '(4 0 0 1 0 0 1) "Same canonical XML string/datatype, Occurrence Parent/Type/Scope/Reifier and all three Topic identifier categories.") (spec "anytype-variant" :variant var (format nil "a - nt:\"N\"@base ~~nr (~A@extra,extra ~~vr)." (at-literal var)) (format nil "N~A" (at-resource "V&🦊" " xmlns:p='urn:variant' xmlns:u='urn:unused'")) '("a" "nt" "base" "extra" "nr" "vr") '(6 1 1 0 0 0 2) "Same explicit Name Type, Name Scope, proper-superset Variant Scope, inherited XML prefix, Name/Variant Reifiers and inverses.") (spec "anytype-occurrence-normalization" :occurrence norm (format nil "a t:~A@s ~~r; t:~A@s ~~r." (at-literal norm) (at-literal norm)) (format nil "~A~A" (at-resource "") (at-resource "a&b")) '("a" "t" "s" "r") '(4 0 0 1 0 0 1) "Two source occurrences in each notation have the same Parent/Type/Scope/value/datatype and normalize to one canonical Occurrence; XML attribute order, references, CDATA and empty-tag spelling differ." :normalized t) (spec "anytype-variant-normalization" :variant norm-var (format nil "a - nt:\"N\"@base ~~nr (~A@extra ~~vr) (~A@extra ~~vr)." (at-literal norm-var) (at-literal norm-var)) (format nil "N~A~A" (at-resource "" " xmlns:p='urn:variant'") (at-resource "V&🦊" " xmlns:p='urn:variant'")) '("a" "nt" "base" "extra" "nr" "vr") '(6 1 1 0 0 0 2) "Two source Variants with equal canonical XML values retain the same Parent Name, inherited/explicit Scope and Reifier and normalize through the existing TMDM constructor." :normalized t))))) (defun at-topic (map doc id) (find-if (lambda (topic) (member (concatenate 'string doc "#" id) (item-identifiers topic) :test #'equal)) (topic-map-topics map))) (defun at-features (language map doc case) (let* ((id (getf case :id)) (owner (at-topic map doc "a")) (want (getf case :canonical-value)) (kind (getf case :anytype-kind)) (name (first (topic-names owner))) (item (if (eq kind :occurrence) (first (topic-occurrences owner)) (first (topic-name-variants name)))) (r (at-topic map doc (if (eq kind :occurrence) "r" "vr")))) (check (format nil "equivalence:~A:~A:independent-anyType-value-and-datatype" id language) (and item (equal want (if (eq kind :occurrence) (occurrence-value item) (variant-value item))) (equal *at-datatype* (if (eq kind :occurrence) (occurrence-datatype item) (variant-datatype item))))) (check (format nil "equivalence:~A:~A:expected-parent-type-scope-and-reifier-inverses" id language) (and (eq owner (canonical-topic owner)) (null (item-identifiers item)) (eq r (item-reifier item)) (if r (eq item (topic-reified r)) (null (item-reifier item))) (if (eq kind :occurrence) (and (eq owner (occurrence-parent item)) (eq (at-topic map doc "t") (occurrence-type item)) (equal (refs (occurrence-scope item)) (refs (when r (list (at-topic map doc "s")))))) (and (eq name (variant-parent item)) (eq owner (topic-name-parent name)) (equal "N" (topic-name-value name)) (eq (at-topic map doc "nt") (topic-name-type name)) (equal (refs (topic-name-scope name)) (refs (list (at-topic map doc "base")))) (equal (refs (variant-scope item)) (refs (list (at-topic map doc "base") (at-topic map doc "extra")))) (eq (at-topic map doc "nr") (item-reifier name)) (eq name (topic-reified (item-reifier name))) (null (item-identifiers name)))))) (check (format nil "equivalence:~A:~A:canonical-EQ-entire-native-map" id language) (canonical-eq-p map)))) (defun at-add-report-fields (&rest args) (setf (topic-maps.test-foundation::json-object-members (first *rows*)) (append (topic-maps.test-foundation::json-object-members (first *rows*)) (topic-maps.test-foundation::json-object-members (apply #'fields args))))) (defun at-observe (case thunk &optional right-value) ;; Observe the actual productive calls/constructors, always returning their ;; unchanged native results. This supplements the existing execute/run-pair ;; infrastructure; it creates no substitute model or registry. (let* ((ctm (symbol-function 'topic-maps.ctm:deserialize-string)) (xtm (symbol-function 'topic-maps.xtm:deserialize-octets)) (kind (getf case :anytype-kind)) (constructor (if (eq kind :occurrence) 'add-occurrence 'add-variant)) (native (symbol-function constructor)) (calls nil) (items nil)) (labels ((entry (language saved input args) (let* ((*at-language* language) (*at-key* (getf args (if (eq language :ctm) :source-name :source-key))) (doc (getf args :document-iri)) (copy (copy-seq input)) (tag (format nil "~A/~D" language (length calls))) (expect (copy-list case))) (push (fields "entryPoint" (if (eq language :ctm) "topic-maps.ctm:deserialize-string" "topic-maps.xtm:deserialize-octets") "documentIRI" doc "sourceKey" *at-key* "inputHex" (format nil "~{~2,'0X~}" (coerce (if (stringp input) (octets input) input) 'list))) calls) (let ((map (apply saved input args))) (when (and right-value (eq language :xtm)) (setf (getf expect :canonical-value) right-value)) (check (format nil "equivalence:~A:~A:actual-entry-source-unchanged" (getf case :id) tag) (equalp copy input)) (expected-features tag map doc expect) (at-features tag map doc expect) map)))) (unwind-protect (progn (setf (symbol-function 'topic-maps.ctm:deserialize-string) (lambda (input &rest args) (entry :ctm ctm input args)) (symbol-function 'topic-maps.xtm:deserialize-octets) (lambda (input &rest args) (entry :xtm xtm input args)) (symbol-function constructor) (lambda (&rest args) (let* ((map (topic-maps.tmdm::item-topic-map (first args))) (before (nth (if (eq kind :occurrence) 3 2) (counts map))) (result (apply native args)) (after (nth (if (eq kind :occurrence) 3 2) (counts map)))) (push (list *at-language* *at-key* before after result map) items) result))) (funcall thunk)) (setf (symbol-function 'topic-maps.ctm:deserialize-string) ctm (symbol-function 'topic-maps.xtm:deserialize-octets) xtm (symbol-function constructor) native))) (let ((proof nil) (times (if right-value 2 4)) (source-count (if (getf case :normalization) 2 1))) (check (format nil "equivalence:~A:both-actual-Reader-entries-and-repetitions-observed" (getf case :id)) (= times (length calls))) (dolist (call (reverse calls)) (let* ((members (topic-maps.test-foundation::json-object-members call)) (key (cdr (assoc "sourceKey" members :test #'equal))) (entry-point (cdr (assoc "entryPoint" members :test #'equal))) (language (if (search ":deserialize-string" entry-point) :ctm :xtm)) (group (reverse (remove-if-not (lambda (x) (and (eq language (first x)) (equal key (second x)))) items))) (canonical (when group (topic-maps.tmdm::canonical-item (fifth (first group)))))) (check (format nil "equivalence:~A:~A/~A:actual-source-items-normalize-to-one-live-handle" (getf case :id) language key) (and (= source-count (length group)) (equal (mapcar #'third group) (if (= source-count 2) '(0 1) '(0))) (every (lambda (x) (and (= 1 (fourth x)) (eq canonical (topic-maps.tmdm::canonical-item (fifth x))) (eq (sixth x) (topic-maps.tmdm::item-topic-map canonical)))) group))) (push (fields "entryPoint" entry-point "sourceKey" key "sourceItemCalls" (length group) "nativeItemCountsBefore" (coerce (mapcar #'third group) 'vector) "nativeItemCountsAfter" (coerce (mapcar #'fourth group) 'vector) "allReturnedHandlesCanonicalAndLive" :true) proof))) (at-add-report-fields "slice" "22T" "independentExpectedCanonicalValue" (getf case :canonical-value) "expectedXTMCanonicalValue" (or right-value (getf case :canonical-value)) "expectedDatatype" *at-datatype* "expectedNativeCounts" (coerce (getf case :counts) 'vector) "normalizationRequired" (if (getf case :normalization) :true :false) "actualReaderEntries" (coerce (reverse calls) 'vector) "nativeConstructorEvidence" (coerce (nreverse proof) 'vector))))) (defun at-run-pairs () (dolist (case (at-pair-cases)) (at-observe case (lambda () (run-pair case))))) (defun at-contrast-tests () (let* ((left "") (right "") (case (list :id "anytype-namespace-meaning" :anytype-kind :occurrence :canonical-value left :keys '("a" "t") :counts '(2 0 0 1 0 0 0))) (doc "https://equivalence.example.invalid/anytype-namespace-meaning/map")) (at-observe case (lambda () (run-contrast (getf case :id) (format nil "a t:~A." (at-literal left)) (xml (format nil "~A" (at-resource "" " xmlns:p='urn:right'"))) doc doc doc "Same prefix/local name but different namespace URI produces different canonical XML values. All other native properties are held equal; neither is rewritten.")) right)))