(in-package #:topic-maps.xtm.tests) (defvar *registered-mergemap-outputs* nil) (defparameter *registered-mergemap-originals* '("mergemap-merge" "mergemap-ontopia-issue-28-2" "mergemap-ontopia-issue-28" "mergemap-tm-reifier" "mergemap")) (defun mm-registered-model-checks (name map child imports) (let* ((source (first (first imports))) (resolve (second (first imports))) (owner (by-id map "topic")) (type-case (equal name "mergemap-merge")) (identity-case (search "ontopia-issue-28" name)) (reifier-case (equal name "mergemap-tm-reifier"))) (check (concatenate 'string "mm-registered:" name ":canonical-import-mapper-and-source-separation") (and (not (eq source map)) (mm-current-p source) (mm-current-p map) (every (lambda (item) (let ((target (funcall resolve item))) (and (not (eq item target)) (eq target (topic-maps.tmdm::canonical-item target)) (eq map (topic-maps.tmdm::item-topic-map target))))) (remove source (topic-maps.tmdm::map-information-items source) :test #'eq)) (null (item-identifiers map)) (null (item-reifier map)) (null (item-identifiers source)) (eq reifier-case (not (null (item-reifier source)))))) (let ((c (condition-from (lambda () (funcall resolve source))))) (check (concatenate 'string "mm-registered:" name ":source-map-item-is-not-imported-content") (and (typep c 'map-import-error) (eq :not-imported (map-import-error-code c))))) (check (concatenate 'string "mm-registered:" name ":no-unrequested-characteristics") (and (null (all-occurrences map)) (null (all-variants map)) (every (lambda (topic) (and (null (topic-subject-locators topic)) (null (topic-reified topic)))) (topic-map-topics map)) (every (lambda (n) (and (null (topic-name-scope n)) (null (item-identifiers n)) (null (item-reifier n)))) (name-reifier-names map)))) (cond (type-case (let* ((type (mm-local map child "type")) (name-item (first (topic-names type))) (a (first (topic-map-associations map))) (instance-role (find (core map "instance") (association-roles a) :key #'association-role-type :test #'eq)) (type-role (find (core map "type") (association-roles a) :key #'association-role-type :test #'eq))) (check "mm-registered:mergemap-merge:exact-type-instance-and-name-EQ" (and (= 6 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a))) (= 1 (length (name-reifier-names map))) (eq (association-type a) (core map "type-instance")) (null (association-scope a)) (eq owner (association-role-player instance-role)) (eq type (association-role-player type-role)) (eq a (association-role-parent instance-role)) (eq a (association-role-parent type-role)) (member instance-role (topic-roles-played owner) :test #'eq) (member type-role (topic-roles-played type) :test #'eq) (eq type (topic-name-parent name-item)) (equal "Type" (topic-name-value name-item)) (eq (by-si map (default-name-type-identifier)) (topic-name-type name-item)) (equal (list (concatenate 'string child "#type")) (item-identifiers type)) (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner)))))) (identity-case (check (concatenate 'string "mm-registered:" name ":exact-cross-SI-II-identity-and-retained-identifiers") (and (= 1 (length (topic-map-topics map))) (eq owner (mm-local map child "sub-topic")) (null (topic-map-associations map)) (null (name-reifier-names map)) (same-identifiers-p (item-identifiers owner) (list (concatenate 'string *document* "#topic") (concatenate 'string child "#sub-topic") "http://www.example.org/ontopia-issue-28")) (same-identifiers-p (topic-subject-identifiers owner) '("http://www.example.org/ontopia-issue-28" "ftp://www.example.org/ontopia-issue-28"))))) (t (let ((imported (mm-local map child (if reifier-case "reifier" "topic")))) (check (concatenate 'string "mm-registered:" name ":two-document-local-topics-no-root-metadata") (and (= 2 (length (topic-map-topics map))) (not (eq owner imported)) (null (topic-map-associations map)) (null (name-reifier-names map)) (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner)) (equal (list (concatenate 'string child (if reifier-case "#reifier" "#topic"))) (item-identifiers imported)) (every (lambda (topic) (null (topic-subject-identifiers topic))) (topic-map-topics map)) (or (not reifier-case) (and (eq source (topic-reified (item-reifier source))) (eq imported (funcall resolve (item-reifier source))) (null (topic-reified imported))))))))))) (defun mm-registered-read (label raw child child-raw child-key key verify) ;; Reuse MM-CALL's actual public reader/import path, with passive context trace. (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (parses nil) (map nil)) (unwind-protect (progn (setf (symbol-function 'topic-maps.xml:parse-octets) (lambda (input &rest args) (push (mm-fields "documentIRI" topic-maps.xtm::*document-iri* "sourceKey" topic-maps.xtm::*source-key* "bytes" (length input) "originalRootOctetsEQ" (if (eq raw input) :true :false) "originalChildOctetsEQ" (if (eq child-raw input) :true :false)) parses) (apply saved input args))) (setf map (mm-call label raw (list (list child child-raw child-key)) verify :key key))) (setf (symbol-function 'topic-maps.xml:parse-octets) saved)) (values map (coerce (nreverse parses) 'vector)))) (defun registered-mergemap-original-contracts () (setf *registered-mergemap-outputs* nil) (let ((previous (copy-list *cases*)) (executed nil) (requested (mapcar (lambda (name) (format nil "xtm2/in/~A.xtm" name)) *registered-mergemap-originals*))) (dolist (name *registered-mergemap-originals*) (let* ((id (format nil "xtm2/in/~A.xtm" name)) (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (child-name (concatenate 'string name ".sub")) (child (mm-child-iri child-name)) (child-id (concatenate 'string "xtm2/in/" child-name)) (raw (file-bytes (corpus-path id))) (child-raw (file-bytes (corpus-path child-id))) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (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.2,4.3,4.5-4.10,4.20-4.21" "TMDM 5.2,5.3.5,6.2,7.2" "CXTM 3,4"))) (map nil) (actual nil) (condition nil) (parses nil) (trace nil) (source-model nil)) (check (concatenate 'string "mm-registered:" name ":original-not-already-registered") (null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) (setf *cases* (append *cases* (list case))) (handler-case (let ((*mergemap-controls* nil)) (multiple-value-setq (map parses) (mm-registered-read (concatenate 'string "registered:" name) raw child child-raw child-id id (lambda (target imports calls) (check (concatenate 'string "mm-registered:" name ":exact-resolver-context") (equal calls (list (list child :mergemap *document* "http://psi.topicmaps.org/iso13250/xtm")))) (setf source-model (mm-model (first (first imports)))) (mm-registered-model-checks name target child imports)))) (setf trace (first *mergemap-controls*) actual (mm-cxtm map)) (multiple-value-bind (other other-parses) (mm-registered-read (concatenate 'string "independent:" name) raw child child-raw "diagnostic:child" "diagnostic:parent" (lambda (target imports calls) (declare (ignore imports calls)) (check (concatenate 'string "mm-registered:" name ":repeated-native-EQ") (mm-current-p target)))) ;; The repeated read has different keys and exactly the same Document IRIs. (check (concatenate 'string "mm-registered:" name ":source-keys-independent-of-identities") (and (equalp (mm-model map) (mm-model other)) (equalp actual (mm-cxtm other)) (= 2 (length other-parses)))))) (error (c) (setf condition c))) (record-original-result case actual expected condition) (push id executed) (push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "childDocumentIRI" child "childSourceKey" child-id "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "childBytes" (length child-raw) "childHex" (format nil "~{~2,'0X~}" (coerce child-raw 'list)) "actualBytes" (when actual (length actual)) "expectedBytes" (length expected) "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8) "firstDifferentByte" (when actual (mismatch actual expected)) "qualifiedParserTrace" parses "importTrace" trace "sourceModel" source-model "targetModel" (when map (mm-model map)) "condition" (mm-condition condition) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *registered-mergemap-outputs*) (when condition (error condition)) (check (concatenate 'string "mm-registered:" name ":complete-original-CXTM-byte-exact") (equalp actual expected)))) (check "mm-registered:exact-requested-set-once-previous-cases-preserved" (and (same-identifiers-p requested executed) (every (lambda (case) (and (member case *cases* :test #'eq) (eq :pass (topic-maps.test-foundation:test-case-status case)))) previous) (every (lambda (id) (= 1 (count id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) requested) (same-identifiers-p requested (mapcar #'topic-maps.test-foundation:test-case-id (set-difference *cases* previous :test #'eq))))) (setf *registered-mergemap-outputs* (nreverse *registered-mergemap-outputs*))))