1 (defpackage #:topic-maps.mergemap.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:cxtm #:binary-file)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:export #:run-tests #:mergemap-checks #:write-mergemap-report))
6 (in-package #:topic-maps.mergemap.tests)
7 (defvar *last-checks* nil)
8 (defvar *observations* nil)
9 (defvar *rejections* nil)
10 (defparameter *notation* "http://psi.topicmaps.org/iso13250/ctm")
11 (defparameter *doc* "https://example.invalid/maps/main.ctm")
12 (defparameter *originals* '("mergemap" "mergemap-syntax-qname" "mergemap-template" "mergemap-tm-reifier"))
13 (defun mergemap-checks () *last-checks*)
15 (assert (every (lambda (c) (< (char-code c) 128)) text))
16 (map '(vector (unsigned-byte 8)) #'char-code text))
17 (defun directive (resource) (format nil "%mergemap <~A> <~A>" resource *notation*))
18 (defun resources (entries)
19 "Finite table of (absolute IRI bytes source-key); no filesystem/HTTP fallback."
21 (values (lambda (iri &key purpose from-document-iri notation)
22 (let ((entry (assoc iri entries :test #'string=)))
23 (push (topic-maps.test-foundation::fields "resourceIRI" iri "purpose" (symbol-name purpose)
24 "fromDocumentIRI" from-document-iri "notation" notation
25 "sourceKey" (if entry (third entry) nil)) calls)
26 (when entry (values (second entry) (third entry)))))
27 (lambda () (reverse calls)))))
28 (defun original-resources ()
29 (resources (loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries topic-maps.ctm.tests::*cxtm-corpus*)
30 collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id)))))
31 (defun read-map (text &rest keys)
32 (apply #'topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name "memory:main" keys))
33 (defun local (map iri)
34 (find-if (lambda (topic) (member iri (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))
36 (find-if (lambda (topic) (member iri (topic-subject-identifiers topic) :test #'string=)) (topic-map-topics map)))
37 (defun valid (map) (topic-maps.reference.tests::references-current-p map))
38 (defun observe (map) (topic-maps.template.tests::observe map))
39 (defun call-with-import-observer (observer thunk)
40 ;; Temporary instrumentation in the fresh test process only. Every invocation
41 ;; delegates to the actual Slice 19A operation; no alternate model/import path.
42 (let ((saved (symbol-function 'topic-maps.tmdm:import-map-content)))
44 (progn (setf (symbol-function 'topic-maps.tmdm:import-map-content)
45 (lambda (target source) (funcall observer saved target source)))
47 (setf (symbol-function 'topic-maps.tmdm:import-map-content) saved))))
48 (defun rejection (label text class code &rest keys)
49 (let ((c (condition-from (lambda () (apply #'read-map text keys)))))
50 (check (concatenate 'string label ":specific-condition")
51 (and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c))))
52 (check (concatenate 'string label ":source-position")
53 (and (stringp (topic-maps.ctm:error-source c)) (integerp (topic-maps.ctm:error-position c))
54 (not (minusp (topic-maps.ctm:error-position c))))) c))
55 (defun resolution-tests ()
56 (dolist (base '("https://example.invalid/a/main.ctm" "https://example.invalid/b/main.ctm"))
57 (let* ((resource (concatenate 'string (subseq base 0 (1+ (position #\/ base :from-end t))) "child.ctm"))
58 (text (format nil "~A~%topic." (directive "child.ctm"))))
59 (multiple-value-bind (resolver calls) (resources (list (list resource (ascii "topic.") "memory:child")))
60 (let* ((map (topic-maps.ctm:deserialize-string text :document-iri base :resource-resolver resolver))
61 (request (first (funcall calls))))
62 (check (concatenate 'string "resolution:Document-IRI:" base)
63 (and (= 2 (length (topic-map-topics map))) (local map (concatenate 'string base "#topic"))
64 (local map (concatenate 'string resource "#topic")) (valid map)))
65 (check (concatenate 'string "resolution:resolver-arguments:" base)
66 (and (= 1 (length (funcall calls)))
67 (equal (topic-maps.test-foundation::json-object-members (topic-maps.test-foundation::fields "resourceIRI" resource "purpose" "MERGEMAP"
68 "fromDocumentIRI" base "notation" *notation* "sourceKey" "memory:child"))
69 (topic-maps.test-foundation::json-object-members request))))))))
70 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "t.") "/local/path/is-only-a-source-key")))
71 (let ((map (read-map (directive "urn:child") :resource-resolver resolver)))
72 (check "resolution:absolute-resource-IRI-and-source-key-are-independent"
73 (and (local map "urn:child#t") (not (local map "/local/path/is-only-a-source-key#t")) (= 1 (length (funcall calls)))))))
74 (multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" (ascii "t.") "child")))
75 (let ((map (read-map (format nil "%prefix r <./>~%%prefix n <http://psi.topicmaps.org/iso13250/>~%%mergemap r:child.ctm n:ctm")
76 :resource-resolver resolver)))
77 (check "resolution:resource-and-notation-QNames"
78 (and (local map "https://example.invalid/maps/child.ctm#t") (= 1 (length (funcall calls)))))))
79 ;; Strict existing UTF-8 decoding, including a BOM and multibyte content.
80 (let ((bytes (concatenate '(vector (unsigned-byte 8)) #(239 187 191) (ascii "t - \"") #(195 169) (ascii "\"."))))
81 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" bytes "utf8:child")))
82 (declare (ignore calls))
83 (let* ((before (copy-seq bytes)) (map (read-map (directive "urn:child") :resource-resolver resolver))
84 (name (first (topic-names (local map "urn:child#t")))))
85 (check "resolution:strict-UTF8-BOM-Unicode-and-input-bytes-preserved"
86 (and (string= (string (code-char #xe9)) (topic-name-value name)) (equalp before bytes)))))))
87 (defun environment-tests ()
88 (let* ((resource "https://example.invalid/maps/child.ctm")
89 (child "%prefix p <urn:child:> def f() p:t - \"Child\". end f()")
90 (parent (format nil "%prefix p <urn:parent:> def f() p:t - \"Parent\". end~%~A~%f()" (directive "child.ctm"))))
91 (multiple-value-bind (resolver calls) (resources (list (list resource (ascii child) "env:child")))
92 (declare (ignore calls))
93 (let ((map (read-map parent :resource-resolver resolver)))
94 (check "environment:independent-prefixes-and-template-registers"
95 (and (si map "urn:parent:t") (si map "urn:child:t")
96 (string= "Parent" (topic-name-value (first (topic-names (si map "urn:parent:t")))))
97 (string= "Child" (topic-name-value (first (topic-names (si map "urn:child:t"))))) (valid map))))))
98 (dolist (fixture (list (list "p:t." (format nil "%prefix p <urn:parent:>~%~A" (directive "child.ctm")) 'topic-maps.ctm:undefined-prefix :undefined-prefix t)
99 (list "%prefix p <urn:child:>" (format nil "~A~%p:t." (directive "child.ctm")) 'topic-maps.ctm:undefined-prefix :undefined-prefix nil)
100 (list "f()" (format nil "def f() t. end~%~A" (directive "child.ctm")) 'topic-maps.ctm:undefined-template :undefined-template t)
101 (list "def f() t. end" (format nil "~A~%f()" (directive "child.ctm")) 'topic-maps.ctm:undefined-template :undefined-template nil)))
102 (destructuring-bind (child parent class code child-error-p) fixture
103 (multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" (ascii child) "env:child")))
104 (let ((c (rejection (format nil "environment:isolation:~A:~A" code child-error-p) parent class code :resource-resolver resolver)))
105 (check (format nil "environment:isolation:~A:~A:correct-failure-source" code child-error-p)
106 (and (= 1 (length (funcall calls))) (string= (if child-error-p "env:child" "memory:main") (topic-maps.ctm:error-source c))))))))
107 ;; A child is freshly processed for each occurrence. No child Wildcard state,
108 ;; aliases or counter updates enter the parent environment.
109 (multiple-value-bind (resolver calls)
110 (resources (list (list "https://example.invalid/maps/child.ctm" (ascii "?x - \"C\". ?x. ? - \"A\".") "wild:child")))
111 (multiple-value-bind (map counter)
112 (read-map (format nil "?x. ~A~%~A~%?." (directive "child.ctm") (directive "child.ctm"))
113 :resource-resolver resolver :wildcard-iri "urn:parent-wild" :wildcard-counter 40)
114 (check "environment:own-Document-Wildcard-IRI-and-counter"
115 (and (= counter 42) (local map "urn:parent-wild#$__41.x") (local map "urn:parent-wild#$__42")
116 (local map "https://example.invalid/maps/child.ctm#$__1.x")
117 (local map "https://example.invalid/maps/child.ctm#$__2")
118 (= 5 (length (topic-map-topics map))) (= 2 (length (funcall calls))) (valid map))))))
119 (defun traversal-tests ()
120 (multiple-value-bind (resolver calls)
121 (resources (list (list "https://example.invalid/maps/sub/child.ctm"
122 (ascii (format nil "a. ~A~%b." (directive "../grand.ctm"))) "nested:child")
123 (list "https://example.invalid/maps/grand.ctm" (ascii "g.") "nested:grand")))
124 (let ((map (read-map (format nil "before. ~A~%after." (directive "sub/child.ctm")) :resource-resolver resolver)))
125 (check "traversal:nested-resources-relative-to-their-own-Document-IRIs"
126 (and (= 5 (length (topic-map-topics map))) (local map "https://example.invalid/maps/grand.ctm#g")
127 (equal (mapcar (lambda (c) (cdr (assoc "fromDocumentIRI" (topic-maps.test-foundation::json-object-members c) :test #'string=))) (funcall calls))
128 (list *doc* "https://example.invalid/maps/sub/child.ctm")) (valid map)))))
129 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "~ child-map") "source:child")))
130 (let ((map (read-map (format nil "~A~%~~ parent-map" (directive "urn:child")) :resource-resolver resolver)))
131 (check "traversal:caller-map-Reifier-remains-independent-after-leading-directive"
132 (and (eq (local map (concatenate 'string *doc* "#parent-map")) (item-reifier map))
133 (null (topic-reified (local map "urn:child#child-map"))) (= 1 (length (funcall calls)))))))
134 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "t.") "child")))
135 (let ((observed nil))
136 (call-with-import-observer
137 (lambda (saved target source)
138 (setf observed (and (local target (concatenate 'string *doc* "#before"))
139 (null (local target (concatenate 'string *doc* "#after")))
140 (local source "urn:child#t")))
141 (funcall saved target source))
142 (lambda () (read-map (format nil "before. ~A~%after." (directive "urn:child")) :resource-resolver resolver)))
143 (check "traversal:directive-executed-immediately-before-caller-continues" (and observed (= 1 (length (funcall calls))))))))
144 (defun diagnostic-tests ()
145 (rejection "diagnostic:missing-resource" "%mergemap" 'topic-maps.ctm:ctm-rejection :expected-mergemap-resource)
146 (rejection "diagnostic:missing-notation" "%mergemap <child> topic." 'topic-maps.ctm:ctm-rejection :expected-mergemap-notation)
147 (rejection "diagnostic:unknown-notation" "%mergemap <child> <urn:unknown>" 'topic-maps.ctm:ctm-rejection :unknown-mergemap-notation)
148 (rejection "diagnostic:XTM-explicit-resolver-required" "%mergemap <child> <http://psi.topicmaps.org/iso13250/xtm>" 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
149 (rejection "diagnostic:no-implicit-resource-access" (directive "file:///private/never-open-this") 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
150 (multiple-value-bind (resolver calls) (resources nil)
151 (rejection "diagnostic:resolver-declines-missing-resource" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :resource-unavailable :resource-resolver resolver)
152 (check "diagnostic:missing-resource-request-recorded" (= 1 (length (funcall calls)))))
153 (rejection "diagnostic:resolver-error" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :resource-resolver-failure
154 :resource-resolver (lambda (&rest args) (declare (ignore args)) (error "resource denied")))
155 (dolist (bad (list "not bytes" #(256) '()))
156 (rejection (format nil "diagnostic:invalid-resolver-result:~S" bad) (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error
157 (if bad :invalid-resource-result :resource-unavailable)
158 :resource-resolver (lambda (&rest args) (declare (ignore args)) (values bad "bad"))))
159 (rejection "diagnostic:invalid-source-key" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :invalid-resource-result
160 :resource-resolver (lambda (&rest args) (declare (ignore args)) (values #(97 46) 42)))
161 (dolist (fixture (list (list #(255) :invalid-utf8 :byte) (list (ascii "a - .") :expected-name-value :character)))
162 (destructuring-bind (bytes code unit) fixture
163 (multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" bytes "invalid:child")))
164 (let* ((text (format nil "before.~%~A" (directive "child.ctm")))
165 (c (rejection (format nil "diagnostic:imported-source:~A" code) text 'topic-maps.ctm:ctm-rejection code :resource-resolver resolver))
166 (frame (first (topic-maps.ctm:error-context c))))
167 (check (format nil "diagnostic:imported-source:~A:both-source-locations" code)
168 (and (string= "invalid:child" (topic-maps.ctm:error-source c)) (eq unit (topic-maps.ctm:error-position-unit c))
169 (eq :mergemap (getf frame :purpose)) (= 8 (getf frame :position))
170 (string= "memory:main" (getf frame :source)) (string= *doc* (getf frame :document-iri))
171 (string= "invalid:child" (getf frame :resource-source))
172 (search "memory:main" (princ-to-string c)) (search "invalid:child" (princ-to-string c))
173 (= 1 (length (funcall calls)))))))))
174 (multiple-value-bind (resolver calls)
175 (resources (list (list "urn:a" (ascii (directive "urn:b")) "cycle:a")
176 (list "urn:b" (ascii (directive "urn:a")) "cycle:b")))
177 (let ((c (rejection "diagnostic:active-resource-cycle" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :cyclic-mergemap :resource-resolver resolver)))
178 (check "diagnostic:cycle-has-complete-context-without-completed-resource-cache"
179 (and (= 3 (length (topic-maps.ctm:error-context c))) (= 2 (length (funcall calls)))))))
180 (multiple-value-bind (resolver calls) (resources (list (list "urn:a" (ascii (directive "urn:b")) "depth:a")))
181 (let ((c (rejection "diagnostic:resource-depth-policy" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :resource-depth-limit
182 :resource-resolver resolver :max-resource-depth 1)))
183 (check "diagnostic:depth-limit-is-not-normative-rejection" (and (= 1 (length (funcall calls))) (= 2 (length (topic-maps.ctm:error-context c))))))))
184 (defun resolver-policy-tests ()
185 (rejection "policy:invalid-resolver" "a." 'topic-maps.ctm:ctm-resource-error :invalid-resource-resolver :resource-resolver 42)
186 (dolist (bad '(-1 1/2))
187 (rejection (format nil "policy:invalid-depth:~A" bad) "a." 'topic-maps.ctm:ctm-resource-error :invalid-resource-depth-limit :max-resource-depth bad))
188 (multiple-value-bind (resolver calls) (resources nil)
189 (rejection "policy:zero-depth-without-retrieval" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :resource-depth-limit
190 :resource-resolver resolver :max-resource-depth 0)
191 (check "policy:zero-depth-does-not-call-resolver" (null (funcall calls)))
192 (rejection "policy:XTM-explicit-resolver-before-retrieval" "%mergemap <urn:a> <http://psi.topicmaps.org/iso13250/xtm>"
193 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
194 (check "policy:XTM-without-resolver-does-not-fetch" (null (funcall calls))))
195 (multiple-value-bind (resolver calls) (resources (list (list "urn:empty" #() nil)))
196 (let ((map (read-map (format nil "a. ~A~%b." (directive "urn:empty")) :resource-resolver resolver)))
197 (check "policy:empty-resource-accepted-and-caller-continues" (and (= 2 (length (topic-map-topics map))) (= 1 (length (funcall calls))) (valid map)))))
198 (multiple-value-bind (resolver calls) (resources (list (list "urn:invalid" (ascii "a - .") nil)))
199 (declare (ignore calls))
200 (let ((c (rejection "policy:NIL-source-key-defaults-to-resource-IRI" (directive "urn:invalid") 'topic-maps.ctm:ctm-rejection
201 :expected-name-value :resource-resolver resolver)))
202 (check "policy:diagnostic-key-does-not-replace-Document-IRI" (string= "urn:invalid" (topic-maps.ctm:error-source c))))))
204 (defun identity-tests ()
205 (loop for pair in '(("<urn:shared>" "<urn:shared>") ("^<urn:shared>" "^<urn:shared>")
206 ("<urn:shared>" "^<urn:shared>") ("=<urn:shared>" "=<urn:shared>"))
208 (let* ((parent (format nil "a ~A; - <urn:type>: \"N\".~%~A" (first pair) (directive "urn:child")))
209 (child (format nil "b ~A; - <urn:type>: \"N\"." (second pair))))
210 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "identity:child")))
211 (declare (ignore calls))
213 (let* ((map (call-with-import-observer
214 (lambda (saved target source)
215 (setf old (local target (concatenate 'string *doc* "#a")))
216 (funcall saved target source))
217 (lambda () (read-map parent :resource-resolver resolver))))
218 (owner (local map "urn:child#b")))
219 (check (format nil "identity:~D:existing-rules-merge-Topics-and-dependent-Names" index)
220 (and (eq owner (canonical-topic old)) (not (eq owner old))
221 (eq owner (local map (concatenate 'string *doc* "#a")))
222 (= 1 (length (topic-names owner))) (= 2 (length (topic-map-topics map))) (valid map)))))))))
224 (defun model-tests ()
225 (let* ((child "~ map-r owner <urn:owner>; - type: \"N\" @theme ~name-r (\"V\" @extra ~variant-r); type: \"O\" @theme ~occ-r. assoc(role: owner ~role-r) @theme ~assoc-r")
226 (parent (format nil "local <urn:owner>. ~A~%local - type: \"N\" @<urn:theme>." (directive "urn:child"))))
227 ;; Use the actual source/target objects and resolver returned by Slice 19A.
228 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "model:child")))
229 (declare (ignore calls))
231 (let ((map (call-with-import-observer
232 (lambda (saved target source)
233 (add-item-identifier source "urn:model:source-map-only")
234 (loop for item in (rest (topic-maps.import.tests::handles source)) for i from 1
235 when (topic-maps.tmdm::reifiable-p item) do
236 (add-item-identifier item (format nil "urn:model:item:~D" i)))
237 (let* ((held (topic-maps.import.tests::handles source))
238 (before (topic-maps.import.tests::state source held)) (xml (cxtm source)))
239 (multiple-value-bind (result resolve) (funcall saved target source)
240 (check "model:one-real-TMDM-import-with-unchanged-source"
241 (and (eq target result) (not (eq target source))
242 (equal before (topic-maps.import.tests::state source held)) (string= xml (cxtm source))))
243 (check "model:all-six-kind-Identifier-metadata-and-root-exclusion"
244 (and (null (item-identifiers target))
245 (every (lambda (item) (subsetp (item-identifiers item) (item-identifiers (funcall resolve item)) :test #'string=)) (rest held))))
246 (let* ((p (funcall resolve (local source "urn:child#owner")))
247 (name (funcall resolve (first (topic-names (local source "urn:child#owner"))))))
249 (check "model:canonical-Parent-Type-Player-Scope-Variant-and-Reifier-EQ"
250 (and (eq p (topic-name-parent name))
251 (eq (funcall resolve (local source "urn:child#type")) (topic-name-type name))
252 (eq name (variant-parent (first (topic-name-variants name))))
253 (eq (local target "urn:child#theme") (first (topic-name-scope name)))
254 (eq p (occurrence-parent (first (topic-occurrences p))))
255 (eq p (association-role-player (first (association-roles (first (topic-map-associations target))))))
256 (eq name (topic-reified (item-reifier name)))
257 (null (topic-reified (funcall resolve (item-reifier source)))) (valid target))))
258 (values result resolve))))
259 (lambda () (read-map parent :resource-resolver resolver)))))
260 (check "model:caller-retained-handle-and-canonical-target-after-continuation"
261 (and (eq seen (si map "urn:owner")) (valid map)))
262 (let ((snapshot (topic-maps.import.tests::state map (topic-maps.import.tests::handles map))))
263 (check "model:deterministic-mutation-free-CXTM"
264 (and (string= (cxtm map) (cxtm map)) (equal snapshot (topic-maps.import.tests::state map (topic-maps.import.tests::handles map))))))))))
267 (list "core" "a <http://psi.topicmaps.org/iso13250/model/type>; ^<urn:bridge>."
268 "b <http://psi.topicmaps.org/iso13250/model/instance>; ^<urn:bridge>." :core-subject-conflict)
269 (list "reifier" "a - \"One\" ~<urn:r>." "b - \"Two\" ~<urn:r>." :reifier-conflict)
270 (list "identifiers" "a ^<urn:collision>." "b <urn:child>; <urn:type>: \"O\"." :duplicate-item-identifier)
271 (list "variant" "a <urn:a>; <urn:b>." "o - \"N\" @<urn:a> (\"V\" @<urn:b>)." :variant-scope-collapse)))
272 (destructuring-bind (label before child code) fixture
273 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "conflict:child")))
274 (declare (ignore calls))
275 (let ((intercepted nil))
276 (let ((condition (condition-from (lambda ()
277 (call-with-import-observer
278 (lambda (saved target source)
279 (when (string= label "identifiers")
280 (add-item-identifier (first (topic-occurrences (si source "urn:child"))) "urn:collision"))
281 (let* ((th (topic-maps.import.tests::handles target)) (sh (topic-maps.import.tests::handles source))
282 (tb (topic-maps.import.tests::state target th)) (sb (topic-maps.import.tests::state source sh))
283 (tx (cxtm target)) (sx (cxtm source)) (c (condition-from (lambda () (funcall saved target source)))))
284 (check (format nil "model:atomic-~A:both-real-graphs-and-CXTM-unchanged" label)
285 (and c (equal tb (topic-maps.import.tests::state target th)) (equal sb (topic-maps.import.tests::state source sh))
286 (string= tx (cxtm target)) (string= sx (cxtm source))))
287 (setf intercepted t) (error c)))
288 (lambda () (read-map (format nil "~A~%~A" before (directive "urn:child")) :resource-resolver resolver)))))))
289 (check (format nil "model:atomic-~A:caller-diagnostic" label)
290 (and intercepted (typep condition 'topic-maps.ctm:ctm-rejection)
291 (eq code (topic-maps.ctm:error-code condition))
292 (string= "memory:main" (topic-maps.ctm:error-source condition))
293 (string= "conflict:child" (getf (first (topic-maps.ctm:error-context condition)) :resource-source))))))))))
294 (defun repeated-role-tests ()
295 (let* ((child "<urn:assoc>(<urn:role>: <urn:player> ~<urn:role-reifier>) ~<urn:association-reifier>")
296 (text (format nil "~A~%~A" (directive "urn:child") (directive "urn:child")))
297 (old-association nil) (old-role nil) (imports 0))
298 (multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "repeat:child")))
300 (call-with-import-observer
301 (lambda (saved target source)
303 (let* ((a (first (topic-map-associations source))) (r (first (association-roles a))))
304 ;; Metadata supplied through the model API, not CTM syntax.
305 (add-item-identifier a (format nil "urn:association-copy:~D" imports))
306 (add-item-identifier r (format nil "urn:role-copy:~D" imports))
307 (let* ((held (topic-maps.import.tests::handles source)) (before (topic-maps.import.tests::state source held)) (xml (cxtm source)))
308 (multiple-value-bind (result resolve) (funcall saved target source)
309 (check (format nil "repeat-roles:~D:source-preserved" imports)
310 (and (equal before (topic-maps.import.tests::state source held)) (string= xml (cxtm source))))
311 (when (= imports 1) (setf old-association (funcall resolve a) old-role (funcall resolve r)))
312 (check (format nil "repeat-roles:~D:resolver-canonical-Association-and-Role-EQ" imports)
313 (and (eq (canonical-association old-association) (funcall resolve a))
314 (eq (canonical-association-role old-role) (funcall resolve r))))
315 (values result resolve)))))
316 (lambda () (read-map text :resource-resolver resolver)))))
317 (let* ((a (first (topic-map-associations map))) (r (first (association-roles a))))
318 (check "repeat-roles:repeated-directive-and-retained-forwarding-handles"
319 (and (= 2 imports) (= 2 (length (funcall calls))) (= 1 (length (topic-map-associations map)))
320 (= 1 (length (association-roles a)))
321 (not (eq a old-association)) (eq a (canonical-association old-association))
322 (not (eq r old-role)) (eq r (canonical-association-role old-role))))
323 (check "repeat-roles:Identifier-union-Reifier-and-Parent-EQ"
324 (and (= 2 (length (item-identifiers a))) (= 2 (length (item-identifiers r)))
325 (eq a (association-role-parent r)) (eq a (topic-reified (item-reifier a)))
326 (eq r (topic-reified (item-reifier r))) (valid map))))))))
328 (defun original-cases ()
329 (setf *observations* nil *rejections* nil)
330 (dolist (file *originals*)
331 (let* ((id (format nil "ctm/in/~A.ctm" file))
332 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
333 (doc (topic-maps.test-foundation:test-case-document-iri case)))
334 (multiple-value-bind (resolver calls) (original-resources)
335 (let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
336 :document-iri doc :source-name id :resource-resolver resolver))
337 (held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held))
338 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
339 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
340 (check (concatenate 'string id ":complete-model-and-reference-invariants") (valid map))
341 (check (concatenate 'string id ":original-byte-exact-CXTM") (equalp actual expected))
342 (check (concatenate 'string id ":deterministic-and-pure-CXTM")
343 (and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal before (topic-maps.import.tests::state map held))))
344 (if (string= file "mergemap-tm-reifier")
345 (check "original:map-Reifier-edge-excluded-content-Topic-retained"
346 (and (= 1 (length (topic-map-topics map))) (null (item-reifier map))
347 (null (topic-reified (first (topic-map-topics map))))))
348 (check (concatenate 'string id ":separate-document-local-subjects-without-Include-aliases")
349 (and (= 7 (length (topic-map-topics map))) (= 2 (length (topic-map-associations map)))
350 (local map (concatenate 'string doc "#lars"))
351 (= 1 (length (topic-item-identifiers (local map (concatenate 'string doc "#lars"))))))))
352 (topic-maps.test-foundation:record-result case :cxtm-match)
353 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "CTM 3.2" "CTM 3.3.3-3.3.8" "TMDM 5.1-5.8" "TMDM 6.1-6.7" "CXTM 3.18"))
354 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
355 (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case)) :match t :bytes (length actual)))))
356 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))
357 "requests" (topic-maps.test-foundation::json-array (funcall calls)) "model" (observe map) "byteCount" (length actual)) *observations*)
358 (format t "~&MERGEMAP-CXTM ~A: PASS (~D bytes)~%" id (length actual))))))
359 (dolist (fixture '(("mergemap-missing-syntax-iri" topic-maps.ctm:ctm-rejection :expected-mergemap-notation 0)
360 ("mergemap-template" topic-maps.ctm:undefined-template :undefined-template 1)
361 ("mergemap-unknown-syntax" topic-maps.ctm:ctm-rejection :unknown-mergemap-notation 0)))
362 (destructuring-bind (file class code requests) fixture
363 (let* ((id (format nil "ctm/invalid/~A.ctm" file))
364 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))))
365 (multiple-value-bind (resolver calls) (original-resources)
366 (let* ((text (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)))
367 (c (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
368 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver)))))
369 (check (concatenate 'string id ":specific-rejection-from-intended-cause")
370 (and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c)) (= requests (length (funcall calls)))
371 (string= id (topic-maps.ctm:error-source c)) (<= 0 (topic-maps.ctm:error-position c) (length text))))
372 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string c)))
373 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "CTM 3.15"))
374 (push (topic-maps.test-foundation::fields "id" id "class" (symbol-name class) "code" (symbol-name code)
375 "source" (topic-maps.ctm:error-source c) "position" (topic-maps.ctm:error-position c)
376 "line" (topic-maps.ctm:error-line c) "column" (topic-maps.ctm:error-column c)
377 "requests" (topic-maps.test-foundation::json-array (funcall calls))) *rejections*)
378 (format t "~&MERGEMAP-REJECTION ~A: PASS (~A)~%" id code))))))
379 (setf *observations* (nreverse *observations*) *rejections* (nreverse *rejections*)))
381 (topic-maps.import.tests:run-tests)
382 (let ((topic-maps.ctm.tests::*checks* nil))
383 (resolution-tests) (environment-tests) (traversal-tests) (diagnostic-tests) (resolver-policy-tests) (identity-tests) (model-tests) (repeated-role-tests) (original-cases)
384 (check "mergemap:only-seven-new-contracts-registered"
385 (equal '(:pass 194 :fail 2 :unsupported 0 :not-run 77)
386 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
387 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
388 (format t "~&MERGEMAP-PASS: ~D checks; four byte-exact originals, three specific rejections.~%" (length *last-checks*)) t))
389 (defun write-mergemap-report (path)
390 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
391 (topic-maps.test-foundation::emit-json-value
392 (topic-maps.test-foundation::fields "kind" "ctm-mergemap-execution-evidence" "passed" (length *last-checks*) "failed" 0
393 "checks" (topic-maps.test-foundation::json-array *last-checks*)
394 "cases" (topic-maps.test-foundation::json-array *observations*)
395 "rejections" (topic-maps.test-foundation::json-array *rejections*)) out)