]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-mergemap.lisp
Verify CTM/XTM semantic equivalence through TMDM and CXTM (Slice 22F)
[topic-maps.git] / tests / xtm-mergemap.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 (defvar *mergemap-controls* nil)
4 (defvar *mergemap-originals* nil)
5 (defun mm-fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
6 (defun mm-freeze (value)
7 (typecase value (string (copy-seq value))
8 (cons (cons (mm-freeze (car value)) (mm-freeze (cdr value)))) (t value)))
9 (defun mm-state (map)
10 ;; Extend the existing snapshots for the native Occurrence/Association/Role
11 ;; transfer probes. Actual references/forwarding handles are retained.
12 (mm-freeze (list (map-reifier-state map)
13 (loop for i in (topic-maps.tmdm::map-information-items map) collect
14 (typecase i
15 (occurrence (list i (occurrence-parent i) (occurrence-type i) (occurrence-value i)
16 (occurrence-datatype i) (occurrence-scope i) (item-identifiers i) (item-reifier i)
17 (topic-maps.tmdm::%occurrence-forward i)))
18 (association (list i (association-parent i) (association-type i) (association-scope i)
19 (association-roles i) (topic-maps.tmdm::%association-forward i)))
20 (association-role (list i (association-role-parent i) (association-role-type i)
21 (association-role-player i) (topic-maps.tmdm::%association-role-forward i))))))))
22 (defun mm-current-p (map)
23 (and (topic-maps.tmdm::validate-import-map map)
24 (every (lambda (i) (and (eq i (topic-maps.tmdm::canonical-item i))
25 (eq map (topic-maps.tmdm::item-topic-map i))))
26 (topic-maps.tmdm::map-information-items map))
27 (every (lambda (a) (every (lambda (r)
28 (and (eq a (association-role-parent r))
29 (member r (topic-roles-played (association-role-player r)) :test #'eq))) (association-roles a)))
30 (topic-map-associations map))))
31 (defun mm-cxtm (map &optional (doc *document*))
32 (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
33 (defun mm-model (map)
34 (mm-fields "identity" (variant-reifier-model-data map)
35 "occurrences" (coerce (loop for topic in (topic-map-topics map) append
36 (loop for o in (topic-occurrences topic) collect (mm-fields
37 "value" (occurrence-value o) "datatype" (occurrence-datatype o)
38 "itemIdentifiers" (coerce (item-identifiers o) 'vector)))) 'vector)
39 "canonicalNativeReferencesEQ" (if (mm-current-p map) :true :false)))
40 (defun mm-condition (c)
41 (when c
42 (let ((data (map-reifier-condition-data c)))
43 (when (typep c 'topic-maps.xtm:xtm-resource-error)
44 (setf (cdr (assoc "cause" (topic-maps.test-foundation::json-object-members data) :test #'equal))
45 (mm-condition (topic-maps.xtm:error-cause c))))
46 (when (typep c 'map-import-error)
47 (setf (cdr (assoc "code" (topic-maps.test-foundation::json-object-members data) :test #'equal))
48 (string-downcase (string (map-import-error-code c)))))
49 data)))
50 (defun mm-local (map doc id)
51 (find-if (lambda (topic) (member (concatenate 'string doc "#" id) (item-identifiers topic) :test #'equal))
52 (topic-map-topics map)))
53 (defun mm-child-iri (&optional (name "child.xtm") (doc *document*))
54 (let ((topic-maps.xtm::*document-iri* doc) (topic-maps.xtm::*source-key* "test:resolve"))
55 (topic-maps.xtm::resolve-iri name doc nil)))
56 (defun mm-directive (href) (format nil "<mergeMap href='~A'/>" href))
57 (defun mm-call (label source resources verify &key (doc *document*) (key label) (depth 32) augment)
58 "Observe the real qualified parser and TMDM importer without changing their results.
59 AUGMENT is explicit native-only test setup for constructs outside XML support."
60 (let* ((raw (if (stringp source) (bytes source) source)) (copies (mapcar (lambda (r) (copy-seq (second r))) resources))
61 (root-copy (copy-seq raw)) (calls nil) (parses nil) (imports nil)
62 (saved-parser (symbol-function 'topic-maps.xml:parse-octets))
63 (saved-import (symbol-function 'import-map-content)) (map nil))
64 (unwind-protect
65 (progn
66 (setf (symbol-function 'topic-maps.xml:parse-octets)
67 (lambda (input &rest args)
68 (push (list topic-maps.xtm::*document-iri* topic-maps.xtm::*source-key* input) parses)
69 (apply saved-parser input args)))
70 (setf (symbol-function 'import-map-content)
71 (lambda (target child)
72 (when augment (funcall augment child))
73 (let ((before (mm-state child)) (cx (mm-cxtm child doc)))
74 (multiple-value-bind (result resolve) (funcall saved-import target child)
75 (check (concatenate 'string "mm:" label ":source-preserved:" (write-to-string (length imports)))
76 (and (equal before (mm-state child)) (equalp cx (mm-cxtm child doc))))
77 (push (list child resolve before cx) imports)
78 (values result resolve)))))
79 (setf map (deserialize-octets raw :document-iri doc :source-key key :max-resource-depth depth
80 :resource-resolver (lambda (iri &key purpose from-document-iri notation)
81 (push (list iri purpose from-document-iri notation) calls)
82 (let ((r (assoc iri resources :test #'equal)))
83 (values (second r) (third r)))))))
84 (setf (symbol-function 'topic-maps.xml:parse-octets) saved-parser
85 (symbol-function 'import-map-content) saved-import))
86 (setf calls (nreverse calls) parses (nreverse parses) imports (nreverse imports))
87 (check (concatenate 'string "mm:" label ":raw-octets-qualified-parser")
88 (and (eq raw (third (first parses)))
89 (= (length parses) (1+ (length calls)))
90 (every (lambda (p) (if (equal (first p) doc) (eq (third p) raw)
91 (let ((r (assoc (first p) resources :test #'equal)))
92 (and r (equal (or (third r) (first r)) (second p))
93 (equalp (second r) (third p))
94 (or (not (typep (second r) '(vector (unsigned-byte 8)))) (eq (second r) (third p))))))) parses)))
95 (funcall verify map imports calls)
96 (check (concatenate 'string "mm:" label ":canonical-native-references-EQ") (mm-current-p map))
97 (dolist (record imports)
98 (check (format nil "mm:~A:retained-source-~D" label (position record imports :test #'eq))
99 (and (equal (third record) (mm-state (first record))) (equalp (fourth record) (mm-cxtm (first record) doc)))))
100 (let ((before (mm-state map)) (cx (mm-cxtm map doc)))
101 (check (concatenate 'string "mm:" label ":pure-cxtm-and-source-bytes")
102 (and (equalp cx (mm-cxtm map doc)) (equal before (mm-state map)) (equalp raw root-copy)
103 (every #'equalp copies (mapcar #'second resources))))
104 (push (mm-fields "label" label "sourceText" (babel:octets-to-string raw :encoding :utf-8)
105 "resolverCalls" (coerce (mapcar (lambda (c) (apply #'mm-fields
106 (list "resourceIRI" (first c) "purpose" (string-downcase (string (second c)))
107 "fromDocumentIRI" (third c) "notation" (fourth c)))) calls) 'vector)
108 "qualifiedParserCalls" (length parses) "sourceMapsPreserved" :true
109 "model" (mm-model map) "actualCXTM" (babel:octets-to-string cx :encoding :utf-8)
110 "condition" nil) *mergemap-controls*))
111 map))
112 (defun mm-positive-tests ()
113 (dolist (href '("child.xtm" "https://xtm-tests.example.invalid/absolute.xtm" "../%E9%A6%AC.xtm" "child.xtm?mode=one"))
114 (let* ((child (mm-child-iri href)) (raw (bytes (document "<topic id='topic'/>&#xA;"))))
115 (mm-call (concatenate 'string "href:" href)
116 (document (concatenate 'string (mm-directive href) "<topic id='topic'/><topic id='typed'><instanceOf><topicRef href='#type'/></instanceOf></topic>"))
117 (list (list child raw "same-source-label-for-all-documents"))
118 (lambda (map imports calls)
119 (check (concatenate 'string "mm:exact-resolver-arguments:" href)
120 (equal calls (list (list child :mergemap *document* "http://psi.topicmaps.org/iso13250/xtm"))))
121 (let* ((root-topic (by-id map "topic")) (child-topic (mm-local map child "topic"))
122 (record (first imports)) (source-topic (first (topic-map-topics (first record)))))
123 (check (concatenate 'string "mm:document-local-II-and-resolve-EQ:" href)
124 (and root-topic child-topic (not (eq root-topic child-topic))
125 (eq child-topic (funcall (second record) source-topic))
126 (not (eq source-topic child-topic))
127 (equal (list (concatenate 'string child "#topic")) (item-identifiers child-topic))
128 (= 1 (length (topic-map-associations map))))))))))
129 (let* ((child (mm-child-iri)) (raw (bytes (document "<topic id='a'><subjectIdentifier href='urn:shared'/><name><value> 馬 &amp; A </value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='urn:custom'> V </resourceData></variant></name></topic>")))
130 (source (document (concatenate 'string (mm-directive "child.xtm") "<topic id='b'><subjectIdentifier href='urn:shared'/><name><value>Local</value></name></topic>")))
131 (map (mm-call "SI-merge" source (list (list child raw "source-one"))
132 (lambda (map imports calls) (declare (ignore calls))
133 (let* ((owner (by-si map "urn:shared")) (record (first imports))
134 (old (find "urn:shared" (topic-map-topics (first record)) :key (lambda (tpc) (first (topic-subject-identifiers tpc))) :test #'equal)))
135 (check "mm:SI-merge:imported-handle-resolves-after-later-local-merge"
136 (and (eq owner (by-id map "b")) (eq owner (mm-local map child "a"))
137 (eq owner (funcall (second record) old)) (= 2 (length (topic-names owner)))
138 (equal " V " (variant-value (first (all-variants map))))
139 (equal "urn:custom" (variant-datatype (first (all-variants map))))))))))
140 (other (mm-call "source-key-independent" source (list (list child raw "source-two"))
141 (lambda (&rest ignored) (declare (ignore ignored))) :key "independent-root-key")))
142 (check "mm:source-key-independent:identical-model-cxtm" (equalp (mm-cxtm map) (mm-cxtm other))))
143 (dolist (kind '("subjectLocator" "itemIdentity"))
144 (let ((child (mm-child-iri)))
145 (mm-call (concatenate 'string "merge-identity:" kind)
146 (document (format nil "~A<topic id='local'><~A href='urn:identity'/></topic>" (mm-directive "child.xtm") kind))
147 (list (list child (bytes (document (format nil "<topic id='child'><~A href='urn:identity'/></topic>" kind))) nil))
148 (lambda (map imports calls) (declare (ignore calls))
149 (check (concatenate 'string "mm:merge-identity-EQ:" kind)
150 (and (= 1 (length (topic-map-topics map))) (eq (by-id map "local") (mm-local map child "child"))
151 (eq (by-id map "local") (funcall (second (first imports)) (first (topic-map-topics (first (first imports))))))))))))
152 (let* ((a (mm-child-iri "a.xtm")) (b (mm-child-iri "b.xtm")) (c (mm-child-iri "nested/c.xtm"))
153 (raw-a (bytes (document (concatenate 'string (mm-directive "nested/c.xtm") "<topic id='a'/>"))))
154 (raw-b (bytes (document "<topic id='b'/>"))) (raw-c (bytes (document "<topic id='c'/>"))))
155 (mm-call "nested-multiple-repeated"
156 (document (concatenate 'string (mm-directive "a.xtm") (mm-directive "b.xtm") (mm-directive "a.xtm") "<topic id='root'/>"))
157 (list (list a raw-a "a") (list b raw-b "b") (list c raw-c "c"))
158 (lambda (map imports calls)
159 (check "mm:nested:document-order-no-global-visited-skip"
160 (and (= 5 (length imports)) (= 4 (length (topic-map-topics map)))
161 (equal (mapcar #'first calls) (list a c b a c))
162 (equal (mapcar #'third calls) (list *document* a *document* *document* a)))))))
163 ;; Public reentrant calls are independent, even inside a caller's resolver.
164 (let* ((raw (bytes (document "<topic id='a'/>"))) (observed nil)
165 (map (deserialize-octets (bytes (document (mm-directive "child.xtm"))) :document-iri *document*
166 :resource-resolver (lambda (&rest args) (declare (ignore args))
167 (setf observed (deserialize-octets raw :document-iri *document*)) (values raw nil)))))
168 (check "mm:reentrant-public-call-context-independent"
169 (and observed (= 1 (length (topic-map-topics map))) (= 1 (length (topic-map-topics observed)))))))
170
171 (defun mm-rich-test ()
172 (let* ((child (mm-child-iri))
173 (raw (bytes (map-reifier-xml "#map-r" "<itemIdentity href='urn:child-map'/><topic id='owner'><subjectIdentifier href='urn:owner'/><instanceOf><topicRef href='#type'/></instanceOf><name reifier='#name-r'><itemIdentity href='urn:name'/><scope><topicRef href='#base'/></scope><value>N</value><variant reifier='#variant-r'><itemIdentity href='urn:variant'/><scope><topicRef href='#extra'/></scope><resourceData datatype='urn:value-type'>V</resourceData></variant></name></topic>"))))
174 (mm-call "rich-native-content"
175 (map-reifier-xml "#root-r" (concatenate 'string "<itemIdentity href='urn:root-map'/>" (mm-directive "child.xtm") "<topic id='local'><subjectIdentifier href='urn:owner'/></topic>"))
176 (list (list child raw "rich-child"))
177 (lambda (map imports calls) (declare (ignore calls))
178 (let* ((record (first imports)) (source (first record)) (resolve (second record))
179 (owner (by-si map "urn:owner")) (name (first (topic-names owner))) (v (first (topic-name-variants name)))
180 (occ (first (topic-occurrences owner)))
181 (as (find "urn:association" (topic-map-associations map) :test (lambda (id i) (member id (item-identifiers i) :test #'equal)))))
182 (check "mm:rich:map-metadata-separated-source-reifier-topic-imported-only"
183 (and (equal '("urn:root-map") (item-identifiers map))
184 (equal '("urn:child-map") (item-identifiers source))
185 (eq (by-id map "root-r") (item-reifier map)) (eq map (topic-reified (item-reifier map)))
186 (not (eq (item-reifier map) (funcall resolve (item-reifier source))))
187 (null (topic-reified (funcall resolve (item-reifier source))))
188 (eq source (topic-reified (item-reifier source)))))
189 (check "mm:rich:all-topic-name-variant-occurrence-role-identities-EQ"
190 (and (eq owner (by-id map "local")) (eq owner (topic-name-parent name)) (eq name (variant-parent v))
191 (eq owner (occurrence-parent occ)) (equal "42" (occurrence-value occ))
192 (equal "urn:occurrence-type" (occurrence-datatype occ))
193 (equal '("urn:occurrence") (item-identifiers occ))
194 (equal '("urn:name") (item-identifiers name)) (equal '("urn:variant") (item-identifiers v))
195 (scope-eq-p (variant-scope v) (list (mm-local map child "base") (mm-local map child "extra")))
196 (eq v (topic-reified (item-reifier v))) (eq name (topic-reified (item-reifier name)))
197 (eq occ (topic-reified (item-reifier occ)))
198 (eq map (association-parent as)) (eq as (topic-reified (item-reifier as)))
199 (some (lambda (role) (and (eq owner (association-role-player role))
200 (equal '("urn:role") (item-identifiers role))
201 (eq role (topic-reified (item-reifier role))))) (association-roles as))))
202 (check "mm:rich:all-import-resolutions-current-not-shared"
203 (every (lambda (item) (let ((target (funcall resolve item)))
204 (and (not (eq item target)) (eq map (topic-maps.tmdm::item-topic-map target))
205 (eq target (topic-maps.tmdm::canonical-item target)))))
206 (rest (topic-maps.tmdm::map-information-items source))))))
207 :augment (lambda (source)
208 ;; Native test setup after real XML deserialization, before the real
209 ;; importer. This does not implement XML occurrences/explicit associations.
210 (let* ((owner (by-si source "urn:owner")) (type (mm-local source child "type"))
211 (theme (mm-local source child "extra"))
212 (occ (add-occurrence owner type "42" :datatype "urn:occurrence-type" :scope (list theme)))
213 (as (make-association source type (list (list type owner) (list type theme)) :scope (list theme)))
214 (role (find owner (association-roles as) :key #'association-role-player :test #'eq)))
215 (loop for item in (list occ as role) for id in '("occurrence" "association" "role") do
216 (add-item-identifier item (concatenate 'string "urn:" id))
217 (set-item-reifier item (intern-topic source :item-identifier (concatenate 'string "urn:reifier:" id)))))))))
218
219 (defun mm-nested-supported-features ()
220 (let* ((mid (mm-child-iri "folder/mid.xtm")) (leaf (mm-child-iri "馬.xtm"))
221 (raw (bytes (document "<topic id='owner'><name reifier='#nr'><itemIdentity href='urn:nested-name'/><type><topicRef href='#type'/></type><scope><topicRef href='#base'/></scope><value> 馬 &amp; Name </value><variant reifier='#vr'><itemIdentity href='urn:nested-variant'/><scope><topicRef href='#extra'/></scope><resourceData> V &amp; text </resourceData></variant><variant><scope><topicRef href='#extra'/></scope><resourceRef href='../sound'/></variant><variant><scope><topicRef href='#extra'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyURI'>../%E9%A6%AC</resourceData></variant><variant><scope><topicRef href='#extra'/></scope><resourceData datatype='urn:unknown'>literal%GG</resourceData></variant></name></topic>") :utf-16be t)))
222 (mm-call "nested-UTF16-supported-elements" (document (mm-directive "folder/mid.xtm"))
223 (list (list mid (bytes (document (mm-directive "../%E9%A6%AC.xtm"))) "mid") (list leaf raw "leaf-UTF16"))
224 (lambda (map imports calls) (declare (ignore imports))
225 (let* ((owner (mm-local map leaf "owner")) (name (first (topic-names owner)))
226 (variants (topic-name-variants name)) (v (find " V & text " variants :key #'variant-value :test #'equal)))
227 (check "mm:nested:UTF16-names-variants-values-scopes-types-and-identifiers"
228 (and (equal (mapcar #'first calls) (list mid leaf))
229 (equal " 馬 & Name " (topic-name-value name)) (eq owner (topic-name-parent name))
230 (eq (mm-local map leaf "type") (topic-name-type name))
231 (equal '("urn:nested-name") (item-identifiers name))
232 (equal '("urn:nested-variant") (item-identifiers v))
233 (eq v (topic-reified (mm-local map leaf "vr"))) (eq name (topic-reified (mm-local map leaf "nr")))
234 (= 4 (length variants))
235 (every (lambda (i) (scope-eq-p (variant-scope i) (list (mm-local map leaf "base") (mm-local map leaf "extra")))) variants)
236 (find "literal%GG" variants :key #'variant-value :test #'equal)
237 (= 2 (count (iri-datatype) variants :key #'variant-datatype :test #'equal))))))))
238 (let* ((child (mm-child-iri))
239 (raw (bytes (map-reifier-xml "#child-r" "<itemIdentity href='urn:child-map-metadata'/><topic id='child-r'><subjectIdentifier href='urn:map-reifier-shared'/></topic>"))))
240 (mm-call "map-reifier-topic-identity-merging"
241 (map-reifier-xml "#root-r" (concatenate 'string "<itemIdentity href='urn:target-map-metadata'/>" (mm-directive "child.xtm") "<topic id='root-r'><subjectIdentifier href='urn:map-reifier-shared'/></topic>"))
242 (list (list child raw "source-map"))
243 (lambda (map imports calls) (declare (ignore calls))
244 (let* ((record (first imports)) (source (first record)) (r (item-reifier map)))
245 (check "mm:map-reifier:canonical-identity-merge-does-not-import-source-map-properties"
246 (and (eq r (by-id map "root-r")) (eq r (mm-local map child "child-r"))
247 (eq r (funcall (second record) (item-reifier source))) (eq map (topic-reified r))
248 (eq source (topic-reified (item-reifier source)))
249 (equal '("urn:target-map-metadata") (item-identifiers map))
250 (equal '("urn:child-map-metadata") (item-identifiers source)))))))))
251
252 (define-condition mm-resolver-denied (error) ())
253 (defun mm-error (label raw class code &key resolver (depth 32) (doc *document*)
254 (expected-doc doc) (expected-key label))
255 (let ((c (condition-from (lambda ()
256 (deserialize-octets (if (stringp raw) (bytes raw) raw) :document-iri doc :source-key label
257 :resource-resolver resolver :max-resource-depth depth)))))
258 (check (concatenate 'string "mm-error:" label ":specific-class-code")
259 (and (eq (type-of c) class) (eq code (error-code c))))
260 (check (concatenate 'string "mm-error:" label ":context-and-position")
261 (and (equal expected-doc (error-document-iri c)) (equal expected-key (error-source-key c))
262 (or (and (integerp (error-line c)) (integerp (error-column c)))
263 (and (typep c 'xtm-xml-error) (null (error-line c)) (null (error-column c))))))
264 (push (mm-fields "label" label "sourceText" (when (stringp raw) raw)
265 "condition" (mm-condition c) "expectedClass" (string class) "expectedCode" (string-downcase (string code)))
266 *mergemap-controls*)
267 c))
268 (defun mm-negative-tests ()
269 (let* ((text (document (mm-directive "child.xtm"))) (child (mm-child-iri)) (calls 0)
270 (resolver (lambda (&rest args) (declare (ignore args)) (incf calls) nil)))
271 (mm-error "missing-resolver" text 'xtm-unsupported :resource-resolver-required)
272 (mm-error "declined-resource" text 'topic-maps.xtm:xtm-resource-error :resource-unavailable :resolver resolver)
273 (check "mm-error:resolver-decline-called-once" (= 1 calls))
274 (let* ((denied (make-condition 'mm-resolver-denied))
275 (c (mm-error "denied-resource" text 'topic-maps.xtm:xtm-resource-error :resource-resolver-failure
276 :resolver (lambda (&rest args) (declare (ignore args)) (error denied)))))
277 (check "mm-error:resolver-denial-original-cause-EQ" (eq denied (topic-maps.xtm:error-cause c))))
278 (dolist (spec '(("not bytes" "k") (#(256) "k") (#(-1) "k") (#(1 2) 42) ((1 2) "k")))
279 (mm-error (format nil "invalid-result:~S" spec) text 'topic-maps.xtm:xtm-resource-error :invalid-resource-result
280 :resolver (lambda (&rest args) (declare (ignore args)) (values (first spec) (second spec)))))
281 (dolist (row (list (list "bad-XML" (bytes "<a><b></a>") 'xtm-xml-error :xml-not-well-formed)
282 (list "bad-UTF8" (concatenate '(vector (unsigned-byte 8)) (bytes "<r>") #(195 40) (bytes "</r>")) 'xtm-xml-error :xml-not-well-formed)
283 (list "empty-octets" (make-array 0 :element-type '(unsigned-byte 8)) 'xtm-xml-error :xml-not-well-formed)
284 (list "DOCTYPE" (bytes (concatenate 'string "<!DOCTYPE topicMap SYSTEM 'file:///must-not-open'>" (document ""))) 'xtm-xml-error :doctype-forbidden)
285 (list "bad-version" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.1'/>") 'xtm-rejection :invalid-version)
286 (list "bad-namespace" (bytes "<topicMap version='2.0'/>") 'xtm-rejection :invalid-namespace)
287 (list "unknown-structure" (bytes (document "<unknown/>")) 'xtm-rejection :unexpected-element)
288 (list "anyType-unimplemented" (bytes (document (datatype-body (datatype-resource "http://www.w3.org/2001/XMLSchema#anyType" "<x xmlns='urn:x'>V</x>")))) 'xtm-unsupported :resource-data-datatype)
289 (list "occurrence-XML-still-unsupported" (bytes (document "<topic id='a'><occurrence><type><topicRef href='#t'/></type><resourceData>V</resourceData></occurrence></topic>")) 'xtm-unsupported :unsupported-construct)))
290 (destructuring-bind (label raw class code) row
291 (mm-error label text class code :expected-doc child :expected-key "child-source"
292 :resolver (lambda (&rest args) (declare (ignore args)) (values raw "child-source")))))
293 (setf calls 0)
294 (dolist (row '(("fragment" "<mergeMap href='child.xtm#topic'/>" :mergemap-fragment)
295 ("empty-fragment" "<mergeMap href='child.xtm#'/>" :mergemap-fragment)
296 ("escaped-fragment" "<mergeMap href='child.xtm%23topic'/>" :mergemap-fragment)
297 ("bad-percent" "<mergeMap href='child%GG'/>" :invalid-percent-escape)
298 ("bad-percent-UTF8" "<mergeMap href='child%C3%28'/>" :invalid-percent-utf8)
299 ("missing-href" "<mergeMap/>" :missing-attribute)
300 ("extra-attribute" "<mergeMap href='child.xtm' other='x'/>" :unexpected-attribute)
301 ("nonempty-element" "<mergeMap href='child.xtm'><topic id='a'/></mergeMap>" :unexpected-child)
302 ("non-whitespace" "<mergeMap href='child.xtm'>x</mergeMap>" :unexpected-text)
303 ("after-topic" "<topic id='a'/><mergeMap href='child.xtm'/>" :element-order)
304 ("identity-after-import" "<mergeMap href='child.xtm'/><itemIdentity href='urn:late'/>" :element-order)))
305 (mm-error (first row) (document (second row)) 'xtm-rejection (third row) :resolver resolver))
306 (check "mm-error:invalid-syntax-before-any-resolution" (zerop calls))
307 (mm-error "depth-zero-policy" text 'xtm-unsupported :resource-depth-limit :depth 0 :resolver resolver)
308 (mm-error "immediate-cycle-policy" (document (mm-directive "")) 'xtm-unsupported :cyclic-mergemap :resolver resolver)
309 (check "mm-error:root-cycle-and-depth-policy-before-fetch" (zerop calls))
310 (let ((c (mm-error "indirect-cycle-policy" text 'xtm-unsupported :cyclic-mergemap
311 :expected-doc child :expected-key "loop-child"
312 :resolver (lambda (&rest args) (declare (ignore args)) (incf calls)
313 (values (bytes (document (mm-directive *document*))) "loop-child")))))
314 (check "mm-error:indirect-cycle-one-fetch-no-normative-PASS"
315 (and (= 1 calls) (null (find "xtm2/in/mergemap-loop.xtm" *cases* :test #'equal :key #'topic-maps.test-foundation:test-case-id))
316 (eq :cyclic-mergemap (error-code c)))))
317 (mm-error "acyclic-depth-policy" text 'xtm-unsupported :resource-depth-limit :depth 1
318 :expected-doc child :expected-key "depth-child"
319 :resolver (lambda (&rest args) (declare (ignore args))
320 (values (bytes (document (mm-directive "grand.xtm"))) "depth-child"))))
321 ;; Generic vector octets are specialized only in storage, never text-decoded.
322 (let ((child (mm-child-iri)))
323 (mm-call "generic-octet-vector" (document (mm-directive "child.xtm"))
324 (list (list child (map 'vector #'identity (bytes (document "<topic id='a'/>"))) nil))
325 (lambda (map imports calls) (declare (ignore imports calls))
326 (check "mm:generic-octets-fallback-key" (not (null (mm-local map child "a"))))))))
327
328 (defun mm-import-conflict-tests ()
329 (dolist (kind '("identifier" "reifier"))
330 (let* ((saved (symbol-function 'import-map-content)) (native nil) (target nil) (source nil) (tb nil) (sb nil)
331 (body (if (equal kind "identifier")
332 "<itemIdentity href='urn:collision'/><mergeMap href='child.xtm'/>"
333 "<mergeMap href='child.xtm'/>"))
334 (parent (if (equal kind "identifier") (document body) (map-reifier-xml "#r" body)))
335 (child-raw (bytes (document (if (equal kind "identifier")
336 "<topic id='a'><itemIdentity href='urn:collision'/></topic>"
337 (format nil "<topic id='a'><name reifier='~A#r'><value>N</value></name></topic>" *document*)))))
338 (c nil))
339 (unwind-protect
340 (progn
341 (setf (symbol-function 'import-map-content)
342 (lambda (a b)
343 (setf target a source b tb (mm-state a) sb (mm-state b))
344 (handler-case (funcall saved a b) (error (e) (setf native e) (error e)))))
345 (setf c (mm-error (concatenate 'string "native-import-conflict:" kind) parent
346 'topic-maps.xtm:xtm-resource-error :mergemap-import-failure
347 :resolver (lambda (&rest args) (declare (ignore args)) (values child-raw "conflicting-child")))))
348 (setf (symbol-function 'import-map-content) saved))
349 (check (concatenate 'string "mm-error:native-import-cause-and-atomic-graphs:" kind)
350 (and (eq native (topic-maps.xtm:error-cause c))
351 (if (equal kind "identifier")
352 (and (typep native 'item-identifier-error) (eq :duplicate-item-identifier (item-identifier-error-code native)))
353 (and (typep native 'reification-error) (eq :reifier-conflict (reification-error-code native))))
354 (equal tb (mm-state target)) (equal sb (mm-state source)))))))
355
356 (defun mm-original-observations ()
357 (let* ((id "xtm2/in/mergemap-itemid.xtm")
358 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
359 (raw (file-bytes (corpus-path id))) (child (mm-child-iri "mergemap-itemid.sub"))
360 (child-raw (file-bytes (corpus-path "xtm2/in/mergemap-itemid.sub")))
361 (expected (file-bytes (corpus-path "xtm2/baseline/mergemap-itemid.xtm.cxtm")))
362 (map (mm-call "original:mergemap-itemid" raw (list (list child child-raw "xtm2/in/mergemap-itemid.sub"))
363 (lambda (map imports calls)
364 (check "mm-original:exact-resource-and-map-item-identity-separation"
365 (and (= 148 (length raw)) (= 151 (length child-raw))))
366 (check "mm-original:only-two-document-local-topics-no-map-metadata"
367 (and (equal calls (list (list child :mergemap *document* "http://psi.topicmaps.org/iso13250/xtm")))
368 (= 2 (length (topic-map-topics map))) (null (topic-map-associations map))
369 (null (item-identifiers map)) (null (item-reifier map))
370 (not (eq (by-id map "topic") (mm-local map child "topic")))
371 (equal (list (concatenate 'string child "#the-topic-map")) (item-identifiers (first (first imports)))))))))
372 (actual (mm-cxtm map)) (text (babel:octets-to-string expected :encoding :utf-8))
373 (topic-start (search "<topic number=" text))
374 (without-map-metadata (concatenate 'string (format nil "<topicMap>~%") (subseq text topic-start))))
375 (check "mm-original:exact-baseline-difference-is-root-map-II-only"
376 (and (= 314 (length expected)) (not (equalp actual expected))
377 (equal without-map-metadata (babel:octets-to-string actual :encoding :utf-8))))
378 (check "mm-original:registered-status-remains-NOT-RUN"
379 (and (= 72 (length *cases*)) (null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))
380 (push (mm-fields "id" id "documentIRI" *document* "sourceBytes" (length raw)
381 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
382 "childIRI" child "childBytes" (length child-raw) "childHex" (format nil "~{~2,'0X~}" (coerce child-raw 'list))
383 "model" (mm-model map) "targetMapItemIdentifiers" (coerce (item-identifiers map) 'vector)
384 "sourceMapItemIdentifiers" (vector (concatenate 'string child "#the-topic-map"))
385 "actualCXTM" (babel:octets-to-string actual :encoding :utf-8) "expectedCXTM" text
386 "actualBytes" (length actual) "expectedBytes" (length expected) "firstDifferentByte" (mismatch actual expected)
387 "condition" nil "byteExact" :false "onlyBaselineRootMapItemIdentifiersDiffer" :true
388 "registeredStatus" "not-run" "registeredAsPASS" :false) *mergemap-originals*))
389 (let* ((id "xtm2/in/mergemap-loop.xtm")
390 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
391 (child (mm-child-iri "mergemap-loop.sub")) (calls 0)
392 (c (mm-error "original-cycle-policy-not-normative-PASS" (file-bytes (corpus-path id))
393 'xtm-unsupported :cyclic-mergemap :expected-doc child :expected-key "xtm2/in/mergemap-loop.sub"
394 :resolver (lambda (iri &rest args) (declare (ignore args)) (incf calls)
395 (assert (equal child iri))
396 (values (file-bytes (corpus-path "xtm2/in/mergemap-loop.sub")) "xtm2/in/mergemap-loop.sub")))))
397 (check "mm-original:cycle-finite-one-fetch-no-registration" (and (= 1 calls) (= 72 (length *cases*))))
398 (push (mm-fields "id" id "documentIRI" *document* "condition" (mm-condition c)
399 "registeredStatus" "not-run" "registeredAsPASS" :false "normativeCycleDecision" nil "resolverCalls" calls) *mergemap-originals*)))
400 (defun mergemap-tests ()
401 (setf *mergemap-controls* nil *mergemap-originals* nil)
402 (mm-positive-tests) (mm-rich-test) (mm-nested-supported-features) (mm-negative-tests) (mm-import-conflict-tests) (mm-original-observations)
403 (setf *mergemap-controls* (nreverse *mergemap-controls*) *mergemap-originals* (nreverse *mergemap-originals*)))