1 (defpackage #:topic-maps.include.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 (:import-from #:topic-maps.mergemap.tests #:ascii #:resources #:original-resources #:local #:si #:valid #:observe #:call-with-import-observer)
6 (:export #:run-tests #:include-checks #:write-include-report))
7 (in-package #:topic-maps.include.tests)
8 (defparameter *doc* "https://example.invalid/maps/main.ctm")
9 (defparameter *child* "https://example.invalid/maps/child.ctm")
10 (defparameter *originals* '("include" "include-prefix" "include-prefix2" "include-template" "include-template-wildcard" "include-tm-reifier" "include-topicref" "include-wildcard" "include-wildcard2"))
11 (defvar *last-checks* nil)
12 (defvar *observations* nil)
13 (defvar *rejections* nil)
14 (defun include-checks () *last-checks*)
15 (defun read-map (text &rest keys)
16 (apply #'topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name "memory:main" keys))
17 (defun with-child (child parent &rest keys)
18 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii child) "memory:child")))
19 (declare (ignore calls)) (apply #'read-map parent :resource-resolver resolver keys)))
20 (defun ids= (topic expected)
21 (and topic (equal (sort (item-identifiers topic) #'string<) (sort (copy-list expected) #'string<))))
22 (defun rejected (label text class code &rest keys)
23 (let ((c (condition-from (lambda () (apply #'read-map text keys)))))
24 (check (concatenate 'string "include:" label ":specific-condition")
25 (and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c))))
26 (check (concatenate 'string "include:" label ":source-position")
27 (and (integerp (topic-maps.ctm:error-position c)) (>= (topic-maps.ctm:error-position c) 0))) c))
29 (let* ((map (with-child "topic <urn:subject>; =<urn:located>; occ: \"child.ctm#payload\"^^<urn:datatype>."
30 "before. %include <child.ctm> topic - \"After\"."))
31 (owner (local map (concatenate 'string *doc* "#topic")))
32 (occ (first (topic-occurrences owner))))
33 (check "alias:local-Topic-two-IIs-and-caller-reference-EQ"
34 (and (ids= owner (list (concatenate 'string *doc* "#topic") (concatenate 'string *child* "#topic")))
35 (eq owner (si map "urn:subject")) (= 1 (length (topic-names owner))) (valid map)))
36 (check "alias:SI-SL-Occurrence-value-and-datatype-unchanged"
37 (and (equal '("urn:subject") (topic-subject-identifiers owner)) (equal '("urn:located") (topic-subject-locators owner))
38 (string= "child.ctm#payload" (occurrence-value occ)) (string= "urn:datatype" (occurrence-datatype occ))
39 (eq owner (occurrence-parent occ))
40 (ids= (occurrence-type occ) (list (concatenate 'string *doc* "#occ") (concatenate 'string *child* "#occ"))))))
41 (let ((map (with-child "^<child.ctmish#x>. ^<child.ctm>. ^<child.ctm#x%2525>." "%include <child.ctm>")))
42 (check "alias:literal-prefix-no-fragment-boundary-assumption"
43 (ids= (local map "https://example.invalid/maps/ish#x") (list (concatenate 'string *child* "ish#x") "https://example.invalid/maps/ish#x")))
44 (check "alias:empty-suffix-resolves-to-parent-Document-IRI"
45 (ids= (local map *doc*) (list *doc* *child*)))
46 (check "alias:existing-percent-encoded-locator-is-not-decoded-again"
47 (ids= (local map (concatenate 'string *doc* "#x%25"))
48 (list (concatenate 'string *doc* "#x%25") (concatenate 'string *child* "#x%25")))))
49 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii "t.") "source:key-not-an-IRI-base")))
50 (let* ((map (read-map "%prefix r <./> %include r:child.ctm" :resource-resolver resolver))
51 (request (topic-maps.test-foundation::json-object-members (first (funcall calls)))))
52 (check "resolver:purpose-Include-notation-NIL-resolved-IRI-and-source-key"
53 (and (ids= (local map (concatenate 'string *doc* "#t"))
54 (list (concatenate 'string *child* "#t") (concatenate 'string *doc* "#t")))
55 (= 1 (length (funcall calls)))
56 (equal request (topic-maps.test-foundation::json-object-members
57 (topic-maps.test-foundation::fields "resourceIRI" *child* "purpose" "INCLUDE" "fromDocumentIRI" *doc* "notation" nil "sourceKey" "source:key-not-an-IRI-base")))))))
58 (let ((doc "https://example.invalid/maps/xy") (child "https://example.invalid/maps/x"))
59 (multiple-value-bind (resolver calls) (resources (list (list child (ascii "t.") "snapshot:child")))
60 (declare (ignore calls))
61 (let ((map (topic-maps.ctm:deserialize-string "%include <x>" :document-iri doc :resource-resolver resolver)))
62 (check "alias:single-snapshot-pass-even-when-new-alias-matches-child-prefix"
63 (ids= (local map (concatenate 'string doc "#t"))
64 (list (concatenate 'string child "#t") (concatenate 'string doc "#t")))))))
66 (call-with-import-observer
67 (lambda (saved target source)
68 (setf seen (and (local target (concatenate 'string *doc* "#before"))
69 (null (local target (concatenate 'string *doc* "#after")))))
70 (funcall saved target source))
71 (lambda () (with-child "t." "before. %include <child.ctm> after.")))
72 (check "include:immediate-processing-before-caller-continuation" seen)))
73 (defun wildcard-tests ()
74 (multiple-value-bind (map counter)
75 (with-child "?x. ?x. ?." "?x. %include <child.ctm> ?x. ?. %include <child.ctm> ?."
76 :wildcard-iri "urn:root-wild" :wildcard-counter 7)
77 (check "wildcard:counter-passed-returned-and-parent-cache-preserved"
78 (and (= counter 14) (= 7 (length (topic-map-topics map))) (valid map)))
79 (dolist (id (list "urn:root-wild#$__8.x" (concatenate 'string *doc* "#$__9.x")
80 (concatenate 'string *doc* "#$__10") "urn:root-wild#$__11"
81 (concatenate 'string *doc* "#$__12.x") (concatenate 'string *doc* "#$__13") "urn:root-wild#$__14"))
82 (check (concatenate 'string "wildcard:exact-distinct-identity:" id) (ids= (local map id) (list id)))))
83 (let ((child "def f($x) $x occ: \"V\". ?body - \"B\". ?body. end"))
84 (multiple-value-bind (map counter)
85 (with-child child "%include <child.ctm> f(?argument) f(?argument) f([-\"Embedded\"])" :wildcard-counter 10)
86 (let ((argument (local map (concatenate 'string *doc* "#$__11.argument")))
87 (embedded (local map (concatenate 'string *doc* "#$__14"))))
88 (check "wildcard:Template-local-names-and-once-evaluated-arguments"
89 (and (= counter 15) (local map (concatenate 'string *doc* "#$__12.body"))
90 (local map (concatenate 'string *doc* "#$__13.body")) (local map (concatenate 'string *doc* "#$__15.body"))
91 (= 1 (length (topic-occurrences argument))) (eq argument (occurrence-parent (first (topic-occurrences argument))))
92 (string= "Embedded" (topic-name-value (first (topic-names embedded))))
93 (eq embedded (occurrence-parent (first (topic-occurrences embedded)))) (valid map)))))))
94 (defun template-tests ()
95 (let* ((child (format nil "%prefix p <urn:child:>~%def f($x)~% $x p:type: \"V\".~%end~%f(child)"))
96 (parent (format nil "%prefix p <urn:caller:>~%%include <child.ctm>~%f(owner)")))
97 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii child) "definition:child")))
98 (declare (ignore calls))
99 (multiple-value-bind (map counter parser)
100 (topic-maps.ctm::deserialize-context parent :document-iri *doc* :source-name "invocation:main" :resource-resolver resolver)
101 (let* ((definition (gethash '("f" 1) (topic-maps.ctm::parser-templates parser)))
102 (body (first (topic-maps.ctm::template-definition-body definition)))
103 (token (first (topic-maps.ctm::ctm-statement-tokens body)))
104 (owner (local map (concatenate 'string *doc* "#owner"))))
105 (check "template:source-text-Document-IRI-and-original-token-range-preserved"
106 (and (= counter 0) (string= child (topic-maps.ctm::template-definition-source-text definition))
107 (string= "definition:child" (topic-maps.ctm::template-definition-source-name definition))
108 (string= *child* (topic-maps.ctm::template-definition-document-iri definition))
109 (= (search "def f" child) (topic-maps.ctm::template-definition-position definition))
110 (= (search "$x p:type" child) (topic-maps.ctm::token-position token))))
111 (check "template:caller-Prefix-bindings-and-Document-IRI-not-definition-closure"
112 (and owner (eq (si map "urn:caller:type") (occurrence-type (first (topic-occurrences owner))))
113 (si map "urn:child:type") (local map (concatenate 'string *doc* "#child"))
114 (not (local map (concatenate 'string *child* "#owner"))) (valid map)))))))
115 (let* ((child "def outer($x) inner($x) end")
116 (parent (format nil "%include <child.ctm>~%def inner($t)~% $t - \"N\".~%end~%outer(owner)"))
117 (map (with-child child parent)))
118 (check "template:forward-lookup-at-Invocation-and-nested-imported-execution"
119 (string= "N" (topic-name-value (first (topic-names (local map (concatenate 'string *doc* "#owner"))))))))
120 (let ((map (with-child "def f() end def f($x) $x. end" "%include <child.ctm> f() f(owner) f.")))
121 (check "template:overloads-and-independent-Topic-namespace" (and (= 2 (length (topic-map-topics map))) (valid map))))
122 (let* ((child (format nil "~%def f($x)~% $x missing:type: \"V\".~%end"))
123 (parent (format nil "%include <child.ctm>~%f(owner)")))
124 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii child) "definition:child")))
125 (declare (ignore calls))
126 (let* ((c (rejected "template:body-error-source" parent 'topic-maps.ctm:undefined-prefix :undefined-prefix :resource-resolver resolver))
127 (frame (first (topic-maps.ctm:error-context c))))
128 (check "template:body-error-original-source-position-and-Invocation-frame"
129 (and (string= "definition:child" (topic-maps.ctm:error-source c))
130 (= (search "missing:type" child) (topic-maps.ctm:error-position c)) (= 3 (topic-maps.ctm:error-line c))
131 (string= "definition:child" (getf frame :definition-source))
132 (string= *child* (getf frame :definition-document-iri))
133 (string= "memory:main" (getf frame :invocation-source))
134 (string= *doc* (getf frame :invocation-document-iri))
135 (= (search "f(owner)" parent) (getf frame :invocation-position)))))))
136 (let* ((child (format nil "~%def outer($x)~% inner($x)~%end"))
137 (parent (format nil "%include <child.ctm>~%def inner($t)~% $t - \"N\".~%end~%outer(42)")))
138 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii child) "definition:outer")))
139 (declare (ignore calls))
140 (let* ((c (rejected "template:nested-body-error" parent 'topic-maps.ctm:invalid-template-argument :invalid-template-argument :resource-resolver resolver))
141 (frames (topic-maps.ctm:error-context c)))
142 (check "template:nested-source-chain-and-restored-parent-definition-source"
143 (and (= 2 (length frames)) (string= "memory:main" (topic-maps.ctm:error-source c))
144 (= (search "$t -" parent) (topic-maps.ctm:error-position c))
145 (string= "memory:main" (getf (first frames) :definition-source))
146 (string= "definition:outer" (getf (first frames) :invocation-source))
147 (= (search "inner($x)" child) (getf (first frames) :invocation-position))
148 (string= "definition:outer" (getf (second frames) :definition-source))
149 (= (search "outer(42)" parent) (getf (second frames) :invocation-position)))))))
150 (let* ((grand "https://example.invalid/maps/grand.ctm") (text "%include <child.ctm> f(owner)"))
151 (multiple-value-bind (resolver calls)
152 (resources (list (list *child* (ascii "%include <grand.ctm>") "source:middle")
153 (list grand (ascii "def f($x) $x type: \"V\". end") "source:grand")))
154 (multiple-value-bind (map counter parser)
155 (topic-maps.ctm::deserialize-context text :document-iri *doc* :source-name "source:root" :resource-resolver resolver)
156 (let* ((definition (gethash '("f" 1) (topic-maps.ctm::parser-templates parser)))
157 (owner (local map (concatenate 'string *doc* "#owner"))))
158 (check "template:nested-transport-preserves-original-source-and-caller-identity"
159 (and (= counter 0) (= 2 (length (funcall calls)))
160 (string= grand (topic-maps.ctm::template-definition-document-iri definition))
161 (string= "source:grand" (topic-maps.ctm::template-definition-source-name definition))
162 (ids= (occurrence-type (first (topic-occurrences owner))) (list (concatenate 'string *doc* "#type"))) (valid map)))))))
163 (check "API:public-deserialization-still-returns-exactly-two-values"
164 (= 2 (length (multiple-value-list (read-map "def unused() t. end"))))))
165 (defun model-tests ()
166 (let* ((body "owner <urn:owner>; - type: \"N\" @theme ~<urn:nr> (\"V\" @extra ~<urn:vr>); type: \"O\" @theme ~<urn:or>. assoc(role: owner ~<urn:rr>) @theme ~<urn:ar>")
167 (child (concatenate 'string "~ child-map " body))
168 (old-topic nil) (old-name nil) (old-variant nil) (old-occ nil) (old-a nil) (old-r nil))
169 (let ((map (call-with-import-observer
170 (lambda (saved target source)
171 (setf old-topic (si target "urn:owner") old-name (first (topic-names old-topic))
172 old-variant (first (topic-name-variants old-name)) old-occ (first (topic-occurrences old-topic))
173 old-a (first (topic-map-associations target)) old-r (first (association-roles old-a)))
174 (add-item-identifier source "urn:source-map-only")
175 (loop for item in (rest (topic-maps.import.tests::handles source)) for i from 1
176 when (topic-maps.tmdm::reifiable-p item) do (add-item-identifier item (format nil "urn:metadata:~D" i)))
177 (let* ((held (topic-maps.import.tests::handles source)) (state (topic-maps.import.tests::state source held)))
178 (multiple-value-bind (result resolve) (funcall saved target source)
179 (check "model:source-preserving-real-import-and-root-Identifiers-excluded"
180 (and (equal state (topic-maps.import.tests::state source held)) (null (item-identifiers result))
181 (every (lambda (item) (subsetp (item-identifiers item) (item-identifiers (funcall resolve item)) :test #'string=)) (rest held))))
182 (values result resolve))))
183 (lambda () (with-child child (format nil "~A %include <child.ctm>" body))))))
184 (let* ((owner (si map "urn:owner")) (name (first (topic-names owner))) (variant (first (topic-name-variants name)))
185 (occ (first (topic-occurrences owner))) (a (first (topic-map-associations map))) (r (first (association-roles a))))
186 (check "model:alias-driven-merge-retains-all-six-canonical-handles"
187 (and (not (eq old-topic owner)) (eq owner (canonical-topic old-topic))
188 (eq name (canonical-topic-name old-name)) (eq variant (canonical-variant old-variant))
189 (eq occ (canonical-occurrence old-occ)) (eq a (canonical-association old-a)) (eq r (canonical-association-role old-r))))
190 (check "model:Parent-Type-Scope-Player-and-Reifier-references-EQ"
191 (and (= 1 (length (topic-names owner))) (= 1 (length (topic-occurrences owner))) (= 1 (length (topic-map-associations map)))
192 (eq owner (topic-name-parent name)) (eq name (variant-parent variant)) (eq owner (occurrence-parent occ))
193 (eq (local map (concatenate 'string *doc* "#type")) (topic-name-type name))
194 (eq (topic-name-type name) (occurrence-type occ))
195 (eq (local map (concatenate 'string *doc* "#theme")) (first (topic-name-scope name)))
196 (eq owner (association-role-player r)) (eq a (association-role-parent r))
197 (every (lambda (item) (eq item (topic-reified (item-reifier item)))) (list name variant occ a r))
198 (null (topic-reified (local map (concatenate 'string *doc* "#child-map")))) (valid map))))
199 (let* ((held (topic-maps.import.tests::handles map)) (state (topic-maps.import.tests::state map held)))
200 (check "model:CXTM-deterministic-and-mutation-free-for-retained-handles"
201 (and (string= (cxtm map) (cxtm map)) (equal state (topic-maps.import.tests::state map held))))))))
202 (defun atomic-tests ()
203 (dolist (collision '(nil t))
204 (let* ((text (if collision "def f() before. end %include <child.ctm>" "%include <child.ctm>"))
205 (parser (topic-maps.ctm::make-parser (topic-maps.ctm::make-lexer text) *doc* "urn:w" 9))
206 (map (topic-maps.ctm:deserialize-string "a <http://psi.topicmaps.org/iso13250/model/type>; ^<urn:bridge>." :document-iri *doc*))
207 (child (if collision "def f() x. end ?." "def added() x. end ?. b <http://psi.topicmaps.org/iso13250/model/instance>; ^<urn:bridge>.")))
208 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii child) "atomic:child")))
209 (declare (ignore calls))
210 (setf (topic-maps.ctm::parser-resource-resolver parser) resolver)
211 (let ((topic-maps.ctm::*source-name* "memory:main") (topic-maps.ctm::*source-text* text))
212 (when collision (topic-maps.ctm::read-template-definition parser))
213 (let* ((held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held))
214 (registry (hash-table-count (topic-maps.ctm::parser-templates parser)))
215 (c (condition-from (lambda () (topic-maps.ctm::read-directive parser map)))))
216 (check (format nil "atomic:~A:Map-Counter-and-Template-registry-unchanged" collision)
217 (and (eq (if collision :duplicate-template :core-subject-conflict) (topic-maps.ctm:error-code c))
218 (equal before (topic-maps.import.tests::state map held))
219 (= 9 (topic-maps.ctm::parser-wildcard-counter parser))
220 (= registry (hash-table-count (topic-maps.ctm::parser-templates parser)))))))))))
221 (defun diagnostic-tests ()
222 (rejected "diagnostic:missing-resource" "%include" 'topic-maps.ctm:ctm-rejection :expected-include-resource)
223 (rejected "diagnostic:no-implicit-access" "%include <file:///private/never-read>" 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
224 (rejected "diagnostic:resolver-declined" "%include <child.ctm>" 'topic-maps.ctm:ctm-resource-error :resource-unavailable :resource-resolver (lambda (&rest args) (declare (ignore args)) nil))
225 (rejected "diagnostic:resolver-error" "%include <child.ctm>" 'topic-maps.ctm:ctm-resource-error :resource-resolver-failure :resource-resolver (lambda (&rest args) (declare (ignore args)) (error "denied")))
226 (dolist (bad '("text" #(256)))
227 (rejected (format nil "diagnostic:invalid-bytes:~S" bad) "%include <child.ctm>" 'topic-maps.ctm:ctm-resource-error :invalid-resource-result
228 :resource-resolver (lambda (&rest args) (declare (ignore args)) bad)))
229 (dolist (fixture '((#(255) :invalid-utf8) (#(97) :missing-period)))
230 (let ((c (rejected (format nil "diagnostic:child:~S" (second fixture)) "%include <child.ctm>" 'topic-maps.ctm:ctm-rejection (second fixture)
231 :resource-resolver (lambda (&rest args) (declare (ignore args)) (values (first fixture) "bad:child")))))
232 (check (format nil "diagnostic:child:~S:correct-source" (second fixture)) (string= "bad:child" (topic-maps.ctm:error-source c)))))
233 (multiple-value-bind (resolver calls) (resources (list (list *child* (ascii "%include <main.ctm>") "cycle:child")))
234 (let ((c (rejected "diagnostic:active-cycle" "%include <child.ctm>" 'topic-maps.ctm:ctm-unsupported :cyclic-include :resource-resolver resolver)))
235 (check "diagnostic:cycle-context-and-no-second-fetch" (and (= 1 (length (funcall calls))) (= 2 (length (topic-maps.ctm:error-context c)))))))
236 (multiple-value-bind (resolver calls) (resources nil)
237 (rejected "diagnostic:depth-limit" "%include <child.ctm>" 'topic-maps.ctm:ctm-unsupported :resource-depth-limit :resource-resolver resolver :max-resource-depth 0)
238 (check "diagnostic:depth-limit-no-fetch" (null (funcall calls))))
239 (let* ((bytes (concatenate '(vector (unsigned-byte 8)) #(239 187 191) (ascii "t - \"") #(195 169) (ascii "\".")))
240 (before (copy-seq bytes)))
241 (multiple-value-bind (resolver calls) (resources (list (list *child* bytes "BOM:child")))
242 (declare (ignore calls))
243 (let ((map (read-map "%include <child.ctm>" :resource-resolver resolver)))
244 (check "include:strict-UTF8-BOM-and-Unicode-without-byte-mutation"
245 (and (equalp before bytes) (string= "é" (topic-name-value (first (topic-names (local map (concatenate 'string *doc* "#t"))))))))))))
246 (defun call-with-context-observer (observer thunk)
247 (let ((saved (symbol-function 'topic-maps.ctm::deserialize-context)))
250 (setf (symbol-function 'topic-maps.ctm::deserialize-context)
251 (lambda (text &rest keys)
252 (multiple-value-bind (map counter parser) (apply saved text keys)
253 (funcall observer keys map counter parser)
254 (values map counter parser))))
256 (setf (symbol-function 'topic-maps.ctm::deserialize-context) saved))))
257 (defun call-with-alias-observer (observer thunk)
258 (let ((saved (symbol-function 'topic-maps.ctm::add-include-aliases)))
261 (setf (symbol-function 'topic-maps.ctm::add-include-aliases)
262 (lambda (map child parent position)
263 (let ((before (observe map)))
264 (funcall saved map child parent position)
265 (funcall observer child parent before (observe map)) map)))
267 (setf (symbol-function 'topic-maps.ctm::add-include-aliases) saved))))
268 (defun discrepancy (file)
269 (cond ((string= file "include-template") "Known normative discrepancy: CTM 3.14 / 3.15 / 3.16.2; declaration creates no Topics, imported body executes in caller context, no later definition-source alias rule. Baseline adds include-template.sub#occtype.")
270 ((string= file "include-wildcard2") "Known normative discrepancy: CTM 3.16.2; immediate-parent Document IRI initializes child Wildcard IRI and each Include level retains original identifiers. Baseline omits include-wildcard2.sub#$__1.foo.")))
271 (defun original-cases ()
272 (setf *observations* nil *rejections* nil)
273 (dolist (file *originals*)
274 (let* ((id (format nil "ctm/in/~A.ctm" file))
275 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
276 (doc (topic-maps.test-foundation:test-case-document-iri case)) (environments nil) (transfers nil))
277 (multiple-value-bind (resolver calls) (original-resources)
278 (multiple-value-bind (map counter)
279 (call-with-context-observer
280 (lambda (keys map counter parser)
281 (push (topic-maps.test-foundation::fields
282 "documentIRI" (getf keys :document-iri) "wildcardIRI" (getf keys :wildcard-iri (getf keys :document-iri))
283 "initialCounter" (getf keys :wildcard-counter 0) "finalCounter" counter
284 "templateCount" (hash-table-count (topic-maps.ctm::parser-templates parser)) "model" (observe map)) environments))
286 (call-with-alias-observer
287 (lambda (child parent before after)
288 (push (topic-maps.test-foundation::fields "childDocumentIRI" child "parentDocumentIRI" parent "before" before "after" after) transfers))
289 (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
290 :document-iri doc :source-name id :resource-resolver resolver)))))
291 (let* ((held (topic-maps.import.tests::handles map)) (state (topic-maps.import.tests::state map held))
292 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
293 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
294 (match (equalp actual expected)))
295 (check (concatenate 'string id ":model-and-references") (valid map))
296 (check (concatenate 'string id ":actual-original-comparison") (eq match (null (discrepancy file))))
297 (check (concatenate 'string id ":deterministic-and-pure")
298 (and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal state (topic-maps.import.tests::state map held))))
299 (when (string= file "include-template")
300 (let* ((owner (local map (concatenate 'string doc "#topic"))) (occ (first (topic-occurrences owner))))
301 (check "original:template-no-eager-Topic-or-future-definition-alias"
302 (and (= 2 (length (topic-map-topics map))) (= counter 0) (eq owner (occurrence-parent occ))
303 (ids= (occurrence-type occ) (list (concatenate 'string doc "#occtype")))
304 (string= "Test" (occurrence-value occ)) (string= (string-datatype) (occurrence-datatype occ))))))
305 (when (string= file "include-wildcard2")
306 (let* ((b (topic-maps.ctm::resolve-iri "include-wildcard2.sub" doc 0))
307 (owner (local map (concatenate 'string doc "#$__1.foo")))); original B II must remain
308 (check "original:nested-Wildcard-counter-and-both-retained-identifiers"
309 (and (= counter 3) (= 4 (length (topic-map-topics map)))
310 (ids= owner (list (concatenate 'string doc "#$__1.foo") (concatenate 'string b "#$__1.foo")))
311 (string= "Sub Sub" (topic-name-value (first (topic-names owner))))
312 (local map (concatenate 'string doc "#$__2.foo")) (local map (concatenate 'string doc "#$__3.foo"))))))
313 (topic-maps.test-foundation:record-result case (if match :cxtm-match :cxtm-mismatch)
314 :diagnostics (when (not match) (list (discrepancy file)
315 (format nil "First different byte ~D; actual ~D bytes, expected ~D bytes." (mismatch actual expected) (length actual) (length expected)))))
316 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.2" "CTM 3.3.6" "CTM 3.3.8" "CTM 3.14" "CTM 3.15" "CTM 3.16.2" "TMDM 5-6" "CXTM 3.13" "CXTM 3.18"))
317 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
318 (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 match :bytes (length actual)))))
319 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))
320 "requests" (topic-maps.test-foundation::json-array (funcall calls)) "counter" counter "model" (observe map)
321 "environments" (topic-maps.test-foundation::json-array (reverse environments))
322 "aliasTransfers" (topic-maps.test-foundation::json-array (reverse transfers))
323 "byteCount" (length actual) "expectedByteCount" (length expected) "baselineMatch" (if match :true :false)
324 "discrepancy" (discrepancy file)) *observations*)
325 (format t "~&INCLUDE-CXTM ~A: ~A (~D/~D bytes)~%" id (if match :PASS :FAIL) (length actual) (length expected)))))))
326 (dolist (fixture '(("include-prefix" topic-maps.ctm:undefined-prefix :undefined-prefix "ctm/invalid/include-prefix.ctm")
327 ("include-prefix2" topic-maps.ctm:undefined-prefix :undefined-prefix "ctm/invalid/include-prefix2.sub")
328 ("include-xtm" topic-maps.ctm:ctm-rejection :invalid-iri "ctm/invalid/include-xtm.sub")))
329 (destructuring-bind (file class code source) fixture
330 (let* ((id (format nil "ctm/invalid/~A.ctm" file))
331 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))))
332 (multiple-value-bind (resolver calls) (original-resources)
333 (let ((c (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
334 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver)))))
335 (check (concatenate 'string id ":intended-rejection-source-and-request")
336 (and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c))
337 (string= source (topic-maps.ctm:error-source c)) (= 1 (length (funcall calls)))))
338 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string c)))
339 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.2" "CTM 3.3.4"))
340 (push (topic-maps.test-foundation::fields "id" id "class" (symbol-name class) "code" (symbol-name code) "source" (topic-maps.ctm:error-source c)
341 "position" (topic-maps.ctm:error-position c) "line" (topic-maps.ctm:error-line c) "column" (topic-maps.ctm:error-column c)
342 "requests" (topic-maps.test-foundation::json-array (funcall calls))) *rejections*)
343 (format t "~&INCLUDE-REJECTION ~A: PASS (~A)~%" id code))))))
344 (setf *observations* (nreverse *observations*) *rejections* (nreverse *rejections*)))
346 (topic-maps.mergemap.tests:run-tests)
347 (let ((topic-maps.ctm.tests::*checks* nil))
348 (alias-tests) (wildcard-tests) (template-tests) (model-tests) (atomic-tests) (diagnostic-tests) (original-cases)
349 (check "include:twelve-contracts-only-ten-PASS-two-actual-FAIL"
350 (equal '(:pass 204 :fail 4 :unsupported 0 :not-run 65)
351 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
352 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
353 (format t "~&INCLUDE-PASS: ~D checks; seven byte-exact originals, three rejections, two explicit baseline FAILs.~%" (length *last-checks*)) t))
354 (defun write-include-report (path)
355 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
356 (topic-maps.test-foundation::emit-json-value
357 (topic-maps.test-foundation::fields "kind" "ctm-include-execution-evidence" "passed" (length *last-checks*) "failed" 0
358 "checks" (topic-maps.test-foundation::json-array *last-checks*) "cases" (topic-maps.test-foundation::json-array *observations*)
359 "rejections" (topic-maps.test-foundation::json-array *rejections*)) out) (terpri out)))