1 (in-package #:topic-maps.xtm.tests)
3 (defvar *registered-mergemap-outputs* nil)
4 (defparameter *registered-mergemap-originals*
5 '("mergemap-merge" "mergemap-ontopia-issue-28-2" "mergemap-ontopia-issue-28"
6 "mergemap-tm-reifier" "mergemap"))
7 (defun mm-registered-model-checks (name map child imports)
8 (let* ((source (first (first imports))) (resolve (second (first imports)))
9 (owner (by-id map "topic"))
10 (type-case (equal name "mergemap-merge"))
11 (identity-case (search "ontopia-issue-28" name))
12 (reifier-case (equal name "mergemap-tm-reifier")))
13 (check (concatenate 'string "mm-registered:" name ":canonical-import-mapper-and-source-separation")
14 (and (not (eq source map)) (mm-current-p source) (mm-current-p map)
16 (let ((target (funcall resolve item)))
17 (and (not (eq item target)) (eq target (topic-maps.tmdm::canonical-item target))
18 (eq map (topic-maps.tmdm::item-topic-map target)))))
19 (remove source (topic-maps.tmdm::map-information-items source) :test #'eq))
20 (null (item-identifiers map)) (null (item-reifier map))
21 (null (item-identifiers source))
22 (eq reifier-case (not (null (item-reifier source))))))
23 (let ((c (condition-from (lambda () (funcall resolve source)))))
24 (check (concatenate 'string "mm-registered:" name ":source-map-item-is-not-imported-content")
25 (and (typep c 'map-import-error) (eq :not-imported (map-import-error-code c)))))
26 (check (concatenate 'string "mm-registered:" name ":no-unrequested-characteristics")
27 (and (null (all-occurrences map)) (null (all-variants map))
28 (every (lambda (topic) (and (null (topic-subject-locators topic)) (null (topic-reified topic))))
29 (topic-map-topics map))
30 (every (lambda (n) (and (null (topic-name-scope n)) (null (item-identifiers n)) (null (item-reifier n))))
31 (name-reifier-names map))))
34 (let* ((type (mm-local map child "type")) (name-item (first (topic-names type)))
35 (a (first (topic-map-associations map)))
36 (instance-role (find (core map "instance") (association-roles a) :key #'association-role-type :test #'eq))
37 (type-role (find (core map "type") (association-roles a) :key #'association-role-type :test #'eq)))
38 (check "mm-registered:mergemap-merge:exact-type-instance-and-name-EQ"
39 (and (= 6 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map)))
40 (= 2 (length (association-roles a))) (= 1 (length (name-reifier-names map)))
41 (eq (association-type a) (core map "type-instance")) (null (association-scope a))
42 (eq owner (association-role-player instance-role)) (eq type (association-role-player type-role))
43 (eq a (association-role-parent instance-role)) (eq a (association-role-parent type-role))
44 (member instance-role (topic-roles-played owner) :test #'eq)
45 (member type-role (topic-roles-played type) :test #'eq)
46 (eq type (topic-name-parent name-item)) (equal "Type" (topic-name-value name-item))
47 (eq (by-si map (default-name-type-identifier)) (topic-name-type name-item))
48 (equal (list (concatenate 'string child "#type")) (item-identifiers type))
49 (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))))))
51 (check (concatenate 'string "mm-registered:" name ":exact-cross-SI-II-identity-and-retained-identifiers")
52 (and (= 1 (length (topic-map-topics map))) (eq owner (mm-local map child "sub-topic"))
53 (null (topic-map-associations map)) (null (name-reifier-names map))
54 (same-identifiers-p (item-identifiers owner)
55 (list (concatenate 'string *document* "#topic") (concatenate 'string child "#sub-topic")
56 "http://www.example.org/ontopia-issue-28"))
57 (same-identifiers-p (topic-subject-identifiers owner)
58 '("http://www.example.org/ontopia-issue-28" "ftp://www.example.org/ontopia-issue-28")))))
60 (let ((imported (mm-local map child (if reifier-case "reifier" "topic"))))
61 (check (concatenate 'string "mm-registered:" name ":two-document-local-topics-no-root-metadata")
62 (and (= 2 (length (topic-map-topics map))) (not (eq owner imported))
63 (null (topic-map-associations map)) (null (name-reifier-names map))
64 (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
65 (equal (list (concatenate 'string child (if reifier-case "#reifier" "#topic"))) (item-identifiers imported))
66 (every (lambda (topic) (null (topic-subject-identifiers topic))) (topic-map-topics map))
67 (or (not reifier-case)
68 (and (eq source (topic-reified (item-reifier source)))
69 (eq imported (funcall resolve (item-reifier source))) (null (topic-reified imported)))))))))))
70 (defun mm-registered-read (label raw child child-raw child-key key verify)
71 ;; Reuse MM-CALL's actual public reader/import path, with passive context trace.
72 (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (parses nil) (map nil))
75 (setf (symbol-function 'topic-maps.xml:parse-octets)
76 (lambda (input &rest args)
77 (push (mm-fields "documentIRI" topic-maps.xtm::*document-iri*
78 "sourceKey" topic-maps.xtm::*source-key* "bytes" (length input)
79 "originalRootOctetsEQ" (if (eq raw input) :true :false)
80 "originalChildOctetsEQ" (if (eq child-raw input) :true :false)) parses)
81 (apply saved input args)))
82 (setf map (mm-call label raw (list (list child child-raw child-key)) verify :key key)))
83 (setf (symbol-function 'topic-maps.xml:parse-octets) saved))
84 (values map (coerce (nreverse parses) 'vector))))
85 (defun registered-mergemap-original-contracts ()
86 (setf *registered-mergemap-outputs* nil)
87 (let ((previous (copy-list *cases*)) (executed nil)
88 (requested (mapcar (lambda (name) (format nil "xtm2/in/~A.xtm" name)) *registered-mergemap-originals*)))
89 (dolist (name *registered-mergemap-originals*)
90 (let* ((id (format nil "xtm2/in/~A.xtm" name))
91 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
92 (child-name (concatenate 'string name ".sub")) (child (mm-child-iri child-name))
93 (child-id (concatenate 'string "xtm2/in/" child-name))
94 (raw (file-bytes (corpus-path id))) (child-raw (file-bytes (corpus-path child-id)))
95 (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (expected (file-bytes baseline))
96 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
97 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
98 :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")))
99 (map nil) (actual nil) (condition nil) (parses nil) (trace nil) (source-model nil))
100 (check (concatenate 'string "mm-registered:" name ":original-not-already-registered")
101 (null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))
102 (setf *cases* (append *cases* (list case)))
104 (let ((*mergemap-controls* nil))
105 (multiple-value-setq (map parses)
106 (mm-registered-read (concatenate 'string "registered:" name) raw child child-raw child-id id
107 (lambda (target imports calls)
108 (check (concatenate 'string "mm-registered:" name ":exact-resolver-context")
109 (equal calls (list (list child :mergemap *document* "http://psi.topicmaps.org/iso13250/xtm"))))
110 (setf source-model (mm-model (first (first imports))))
111 (mm-registered-model-checks name target child imports))))
112 (setf trace (first *mergemap-controls*) actual (mm-cxtm map))
113 (multiple-value-bind (other other-parses)
114 (mm-registered-read (concatenate 'string "independent:" name) raw child child-raw "diagnostic:child" "diagnostic:parent"
115 (lambda (target imports calls) (declare (ignore imports calls))
116 (check (concatenate 'string "mm-registered:" name ":repeated-native-EQ") (mm-current-p target))))
117 ;; The repeated read has different keys and exactly the same Document IRIs.
118 (check (concatenate 'string "mm-registered:" name ":source-keys-independent-of-identities")
119 (and (equalp (mm-model map) (mm-model other)) (equalp actual (mm-cxtm other))
120 (= 2 (length other-parses))))))
121 (error (c) (setf condition c)))
122 (record-original-result case actual expected condition)
124 (push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "childDocumentIRI" child "childSourceKey" child-id
125 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
126 "childBytes" (length child-raw) "childHex" (format nil "~{~2,'0X~}" (coerce child-raw 'list))
127 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
128 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
129 "firstDifferentByte" (when actual (mismatch actual expected)) "qualifiedParserTrace" parses
130 "importTrace" trace "sourceModel" source-model "targetModel" (when map (mm-model map))
131 "condition" (mm-condition condition) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *registered-mergemap-outputs*)
132 (when condition (error condition))
133 (check (concatenate 'string "mm-registered:" name ":complete-original-CXTM-byte-exact") (equalp actual expected))))
134 (check "mm-registered:exact-requested-set-once-previous-cases-preserved"
135 (and (same-identifiers-p requested executed)
136 (every (lambda (case) (and (member case *cases* :test #'eq)
137 (eq :pass (topic-maps.test-foundation:test-case-status case)))) previous)
138 (every (lambda (id) (= 1 (count id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) requested)
139 (same-identifiers-p requested (mapcar #'topic-maps.test-foundation:test-case-id
140 (set-difference *cases* previous :test #'eq)))))
141 (setf *registered-mergemap-outputs* (nreverse *registered-mergemap-outputs*))))