]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-templates.lisp
test(ctm): register seven reviewed negative Slice 20B contracts
[topic-maps.git] / tests / ctm-templates.lisp
1 (defpackage #:topic-maps.template.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:read-text #:cxtm #:binary-file)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:export #:run-tests #:template-checks #:write-template-report))
6 (in-package #:topic-maps.template.tests)
7 (defvar *last-checks* nil)
8 (defvar *observations* nil)
9 (defvar *rejections* nil)
10 (defparameter *originals*
11 '(
12 "association-scope-embedded3"
13 "association-scope-embedded4"
14 "name-scope-embedded3"
15 "name-scope-embedded4"
16 "occurrence-scope-embedded3"
17 "occurrence-scope-embedded4"
18 "prefix-template-scope"
19 "template-ambiguous-parameter"
20 "template-ambiguous-parameter2"
21 "template-ambiguous-parameter3"
22 "template-ambiguous-parameter4"
23 "template-assoc-embedded-topic"
24 "template-duplicate-name"
25 "template-forward-ref"
26 "template-invocation-embedded"
27 "template-invocation-embedded2"
28 "template-invocation-noargs-isa"
29 "template-invocation-noargs2"
30 "template-invocation-prefix-notation"
31 "template-invocation-twice"
32 "template-invocation-wildcard-named"
33 "template-invocation-wildcard-named2"
34 "template-invocation-wildcard-named3"
35 "template-invocation-wildcard-named4"
36 "template-invocation-wildcard-named5"
37 "template-invocation-wildcard-named6"
38 "template-invocation-wildcard"
39 "template-item-identifier"
40 "template-item-identifier2"
41 "template-item-identifier4"
42 "template-nested-occtype"
43 "template-nested-occtype2"
44 "template-noop"
45 "template-noop2"
46 "template-noop3"
47 "template-references"
48 "template-simple"
49 "template-subject-identifier"
50 "template-subject-locator"
51 "template-subject-locator2"
52 "template-subject-locator4"
53 "template-topic-nested"
54 "template-topic-nested2"
55 "template-typed-value"
56 "template-wildcard-named"
57 "template-wildcard-named2"
58 "template-wildcard-named3"
59 "template-wildcard-named4"
60 "template-wildcard"
61 "template-wildcard2"
62 "tmlab-tmql-issue-48"
63 "topic-embedded-template-invocation"
64 "topic-identifier-eq-template-name"
65 "topic-template-invocation-unary"
66 "topic-template-invocation"
67 "variant-scope-embedded3"
68 "variant-scope-embedded4"
69 ))
70 (defparameter *negative-originals*
71 '(("embedded-topic-template" topic-maps.ctm:invalid-template-body :invalid-template-body)
72 ("occurrence-explicit-datatype-as-parameter" topic-maps.ctm:invalid-template-body :variable-datatype)
73 ("prefix-template-scope" topic-maps.ctm:invalid-template-body :invalid-template-body)
74 ("template-bad-type" topic-maps.ctm:invalid-template-argument :invalid-template-argument)
75 ("template-missing-args" topic-maps.ctm:template-arity-error :template-arity)
76 ("template-missing-def" topic-maps.ctm:undefined-template :undefined-template)
77 ("template-name-collision" topic-maps.ctm:duplicate-template :duplicate-template)
78 ("tm-reifier-in-template" topic-maps.ctm:invalid-template-body :invalid-template-body)
79 ("variable-undefined" topic-maps.ctm:unbound-template-variable :variable-outside-template)
80 ("variable-undefined2" topic-maps.ctm:unbound-template-variable :variable-outside-template)))
81 (defun template-checks () *last-checks*)
82 (defun named (map local) (topic-maps.arity.tests::named-topic map local))
83 (defun invariant (map) (topic-maps.relationship.tests::valid-model-p map))
84 (defun state (map held) (topic-maps.reification.tests::state map held))
85 (defun handles (map) (copy-list (topic-maps.tmdm::map-information-items map)))
86 (defun yes (value) (if value :true :false))
87 (defun observe (map)
88 (let ((topics (topic-map-topics map)))
89 (flet ((ref (topic) (1+ (position topic topics :test #'eq))))
90 (topic-maps.test-foundation::fields
91 "topicCount" (length topics) "associationCount" (length (topic-map-associations map))
92 "topics" (topic-maps.test-foundation::json-array
93 (loop for topic in topics collect
94 (topic-maps.test-foundation::fields
95 "ordinal" (ref topic)
96 "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers topic))
97 "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers topic))
98 "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators topic))
99 "names" (topic-maps.test-foundation::json-array
100 (loop for name in (topic-names topic) collect (topic-maps.test-foundation::fields
101 "value" (topic-name-value name) "parent" (ref (topic-name-parent name)) "type" (ref (topic-name-type name))
102 "scope" (topic-maps.test-foundation::json-array (mapcar #'ref (topic-name-scope name)))
103 "variantCount" (length (topic-name-variants name)))))
104 "occurrences" (topic-maps.test-foundation::json-array
105 (loop for occurrence in (topic-occurrences topic) collect (topic-maps.test-foundation::fields
106 "value" (occurrence-value occurrence) "datatype" (occurrence-datatype occurrence)
107 "parent" (ref (occurrence-parent occurrence)) "type" (ref (occurrence-type occurrence))
108 "scope" (topic-maps.test-foundation::json-array (mapcar #'ref (occurrence-scope occurrence)))))))))))))
109 (defun definition-tests ()
110 (let* ((text "def tpl($t) $t - \"N\"@?x (\"V\"@?y). assoc(r: ?z) later($t) end")
111 (parser (topic-maps.ctm::make-parser (topic-maps.ctm::make-lexer text) "https://example.invalid/d" "urn:wild" 19)))
112 ;; If definition checking accidentally invokes an identity operation, fail.
113 (let ((saved (symbol-function 'topic-maps.tmdm:intern-topic)))
114 (unwind-protect
115 (progn (setf (symbol-function 'topic-maps.tmdm:intern-topic) (lambda (&rest args) (declare (ignore args)) (error "Definition mutated TMDM")))
116 (topic-maps.ctm::read-template-definition parser))
117 (setf (symbol-function 'topic-maps.tmdm:intern-topic) saved)))
118 (let* ((definition (gethash '("tpl" 1) (topic-maps.ctm::parser-templates parser)))
119 (body (topic-maps.ctm::template-definition-body definition)))
120 (check "definition:stored-ordered-parameters" (equal '("t") (topic-maps.ctm::template-definition-parameters definition)))
121 (check "definition:grammar-checked-statement-kinds" (equal '(:topic :association :invocation) (mapcar #'topic-maps.ctm::ctm-statement-kind body)))
122 (check "definition:no-wildcard-counter-effects" (= 19 (topic-maps.ctm::parser-wildcard-counter parser)))
123 (check "definition:no-wildcard-bindings" (zerop (hash-table-count (topic-maps.ctm::parser-wildcards parser))))
124 (dolist (statement body)
125 (dolist (token (topic-maps.ctm::ctm-statement-tokens statement))
126 (check (format nil "syntax:original-source-range:~D" (topic-maps.ctm::token-position token))
127 (<= 0 (topic-maps.ctm::token-position token) (topic-maps.ctm::token-end token) (length text)))))))
128 (multiple-value-bind (map counter)
129 (topic-maps.ctm:deserialize-string "def unused() ?. ?x - \"N\". end ?." :document-iri "urn:doc" :wildcard-iri "urn:w" :wildcard-counter 7)
130 (check "definition:unused-body-creates-no-information-items-or-counter-gaps"
131 (and (= counter 8) (= 1 (length (topic-map-topics map))) (equal '("urn:w#$__8") (topic-item-identifiers (first (topic-map-topics map)))))))
132 (let ((map (read-text "def f() a. end def f($t) $t. end f() f(b) f.")))
133 (check "definition:overload-and-independent-topic-namespace" (and (= 3 (length (topic-map-topics map))) (named map "f")))))
134
135 (defun binding-tests ()
136 (let ((definition "def name($t,$n) $t - $n. end "))
137 (check "binding:global-and-tail-models-equivalent"
138 (string= (cxtm (read-text (concatenate 'string definition "name(a,\"N\")")))
139 (cxtm (read-text (concatenate 'string definition "a name(\"N\")."))))))
140 (let* ((map (read-text "def iri($i) a occ: $i. end iri(<urn:value>)"))
141 (a (named map "a")) (occ (first (topic-occurrences a))))
142 (check "binding:IRI-value-does-not-create-a-Subject-Identifier-Topic"
143 (and (= 2 (length (topic-map-topics map))) (every (lambda (tpc) (null (topic-subject-identifiers tpc))) (topic-map-topics map))))
144 (check "binding:IRI-value-datatype-and-parent-Type-EQ"
145 (and (string= "urn:value" (occurrence-value occ)) (string= (iri-datatype) (occurrence-datatype occ))
146 (eq a (occurrence-parent occ)) (eq (named map "occ") (occurrence-type occ)))))
147 (let* ((map (read-text "def iri($i) $i occ: $i. end iri(<urn:both>)"))
148 (topic (find "urn:both" (topic-map-topics map) :test (lambda (x y) (member x (topic-subject-identifiers y) :test #'string=)))))
149 (check "binding:one-IRI-used-in-both-contexts"
150 (and topic (string= "urn:both" (occurrence-value (first (topic-occurrences topic)))) (invariant map))))
151 (let* ((prefix "%prefix p <urn:%2525:> ")
152 (direct (read-text (concatenate 'string prefix "p:thing occ: p:thing.")))
153 (expanded (read-text (concatenate 'string prefix "def f($i) $i occ:$i. end f(p:thing)"))))
154 (check "binding:QName-locator-is-resolved-once-without-double-decoding"
155 (string= (cxtm direct) (cxtm expanded))))
156 (dolist (mark '("=" "^"))
157 (let* ((map (read-text (format nil "def f($t) $t - \"N\". end f(~A<urn:marked>)" mark)))
158 (topic (find-if #'topic-names (topic-map-topics map))))
159 (check (concatenate 'string "binding:explicit-identity-kind-preserved:" mark)
160 (and (null (topic-subject-identifiers topic))
161 (equal '("urn:marked") (if (string= mark "=") (topic-subject-locators topic) (topic-item-identifiers topic)))))))
162 (dolist (pair '(("+0001" "1" "integer") ("-.30" "-0.3" "decimal")
163 ("2000-02-29" "2000-02-29" "date")
164 ("2000-01-01T00:00:00Z" "2000-01-01T00:00:00Z" "dateTime")
165 ("\"+0001\"^^<http://www.w3.org/2001/XMLSchema#integer>" "+0001" "integer")
166 ("\"lexical\"^^<urn:unknown>" "lexical" "unknown")))
167 (destructuring-bind (literal expected datatype) pair
168 (let* ((map (read-text (format nil "def f($v) a occ: $v. end f(~A)" literal)))
169 (occ (first (topic-occurrences (named map "a")))))
170 (check (concatenate 'string "binding:existing-literal-reader-policy:" literal)
171 (and (string= expected (occurrence-value occ))
172 (string= (if (string= datatype "unknown") "urn:unknown"
173 (concatenate 'string "http://www.w3.org/2001/XMLSchema#" datatype)) (occurrence-datatype occ))
174 (eq (occurrence-parent occ) (named map "a")) (invariant map))))))
175 (let* ((map (read-text "def outer($t) inner($t) $t - \"outer\". end def inner($x) $x - \"inner\". end a outer(); occ: \"after\"."))
176 (a (named map "a")))
177 (check "binding:nested-frame-restoration-and-Tail-owner"
178 (and (= 2 (length (topic-names a))) (string= "after" (occurrence-value (first (topic-occurrences a)))) (invariant map))))
179 (let ((map (read-text "%prefix p <urn:p:> def f($t) $t. end f(p:r) f(p: r)")))
180 (check "dispatch:QName-argument-versus-Role-declaration"
181 (and (find "urn:p:r" (topic-map-topics map) :test (lambda (x y) (member x (topic-subject-identifiers y) :test #'string=)))
182 (= 1 (length (topic-map-associations map))) (invariant map))))
183 (let ((map (read-text "def f($t) $t - \"N\". end f([ - \"E\" ])")))
184 (check "binding:Embedded-argument-is-one-Topic-with-both-Names"
185 (and (= 2 (length (topic-map-topics map))) (= 2 (length (topic-names (first (topic-map-topics map))))))))
186 (let* ((map (read-text "def f($o,$nt,$t,$e,$r,$v) $o - $nt : \"N\" @ $t ~ $r ($v @ $e). end f(a,nt,theme,extra,reifier,\"V\")"))
187 (name (first (topic-names (named map "a")))) (variant (first (topic-name-variants name))))
188 (check "binding:Name-Type-Theme-Variant-and-Reifier-are-actual-objects"
189 (and (eq (topic-name-type name) (named map "nt"))
190 (eq (first (topic-name-scope name)) (named map "theme"))
191 (eq (item-reifier name) (named map "reifier"))
192 (eq (topic-reified (named map "reifier")) name)
193 (eq name (variant-parent variant)) (string= "V" (variant-value variant))
194 (every (lambda (tpc) (member tpc (variant-scope variant) :test #'eq))
195 (list (named map "theme") (named map "extra"))) (invariant map))))
196 (let* ((map (read-text "def f($a,$r,$p,$t,$ar,$rr) $a($r : $p ~ $rr) @ $t ~ $ar end f(at,rt,p,theme,ar,rr) f(at,rt,p,theme,ar,rr)"))
197 (association (first (topic-map-associations map))) (role (first (association-roles association))))
198 (check "binding:all-Association-reference-positions-and-duplicate-normalization"
199 (and (= 1 (length (topic-map-associations map))) (= 1 (length (association-roles association)))
200 (eq (named map "at") (association-type association)) (eq (named map "rt") (association-role-type role))
201 (eq (named map "p") (association-role-player role)) (eq association (association-role-parent role))
202 (eq (named map "theme") (first (association-scope association)))
203 (eq (named map "ar") (item-reifier association)) (eq (named map "rr") (item-reifier role)) (invariant map)))))
204
205 (defun wildcard-tests ()
206 (dolist (argument '("?" "[-\"Embedded\"]"))
207 (multiple-value-bind (map counter)
208 (topic-maps.ctm:deserialize-string
209 (format nil "def f($t) a(r: $t, s: $t) end f(~A) f(~A)" argument argument)
210 :document-iri "https://example.invalid/doc" :wildcard-iri "urn:wild" :wildcard-counter 30)
211 (let ((associations (topic-map-associations map)))
212 (check (concatenate 'string "wildcard:one-allocation-per-argument-and-distinct-calls:" argument)
213 (and (= 32 counter) (= 2 (length associations))
214 (not (eq (association-role-player (first (association-roles (first associations))))
215 (association-role-player (first (association-roles (second associations))))))))
216 (dolist (a associations)
217 (check (format nil "wildcard:repeated-argument-is-EQ:~A:~A" argument (topic-item-identifiers (association-role-player (first (association-roles a)))))
218 (apply #'eq (mapcar #'association-role-player (association-roles a))))))))
219 (multiple-value-bind (map counter)
220 (topic-maps.ctm:deserialize-string
221 "?x - \"global\". def outer() ?x - \"outer\". inner(?x) ?x - \"outer again\". end def inner($t) $t - \"passed\". ?x - \"inner\". end outer() outer() ?x - \"global again\"."
222 :document-iri "urn:document" :wildcard-iri "urn:wild" :wildcard-counter 8)
223 (check "wildcard:nested-call-frames-share-only-counter"
224 (and (= 13 counter)
225 (equal '(2 3 1 3 1)
226 (loop for topic in (topic-map-topics map) when (topic-item-identifiers topic) collect (length (topic-names topic))))))
227 (check "wildcard:document-and-wildcard-IRIs-remain-distinct"
228 (every (lambda (topic) (every (lambda (id) (search "urn:wild#$__" id)) (topic-item-identifiers topic))) (topic-map-topics map)))))
229
230 (defun merge-tests ()
231 ;; Keep actual handles while execution merges Topics and all structural items.
232 (let* ((map (read-text "a. b. theme. extra.")) (a (named map "a")) (b (named map "b"))
233 (theme (named map "theme")) (extra (named map "extra"))
234 (names (list (add-name a "N" :type a :scope (list theme)) (add-name b "N" :type b :scope (list theme))))
235 (variants (mapcar (lambda (n) (add-variant n "V" :scope (list theme extra))) names))
236 (occurrences (list (add-occurrence a a "O" :scope (list theme)) (add-occurrence b b "O" :scope (list theme))))
237 (associations (list (make-association map a (list (list a a)) :scope (list theme))
238 (make-association map b (list (list b b)) :scope (list theme))))
239 (roles (mapcar (lambda (x) (first (association-roles x))) associations))
240 (pairs (list names variants occurrences associations roles)))
241 (loop for pair in pairs for index from 1 do
242 (loop for item in pair for side from 1 do
243 (add-item-identifier item (format nil "urn:item:~D:~D" index side))
244 (set-item-reifier item (intern-topic map :item-identifier (format nil "urn:reifier:~D:~D" index side)))))
245 (let* ((held (handles map))
246 (text "def merge($left,$right) $left ^<#b>. $right - \"After\". end a merge(b); after: \"Tail\".")
247 (parser (topic-maps.ctm::make-parser (topic-maps.ctm::make-lexer text) "https://example.invalid/a/doc.ctm" "urn:w" 0)))
248 (topic-maps.ctm::read-template-definition parser)
249 (topic-maps.ctm::read-statement parser map)
250 (let ((current (canonical-topic a)))
251 (check "merge:bindings-and-continued-Tail-use-canonical-owner"
252 (and (eq current (canonical-topic b)) (not (eq current a))
253 (find "After" (topic-names current) :test #'string= :key #'topic-name-value)
254 (find "Tail" (topic-occurrences current) :test #'string= :key #'occurrence-value))))
255 (loop for pair in pairs for index from 1 do
256 (let ((left (topic-maps.tmdm::canonical-item (first pair))) (right (topic-maps.tmdm::canonical-item (second pair))))
257 (check (format nil "merge:retained-canonical-handles:~D" index) (eq left right))
258 (check (format nil "merge:identifiers-and-Reifiers-survive:~D" index)
259 (and (= 2 (length (item-identifiers left))) (item-reifier left) (eq left (topic-reified (item-reifier left)))))))
260 (check "merge:all-stored-references-and-Variant-invariants" (invariant map))
261 (let ((before (state map held)) (out (cxtm map)))
262 (check "merge:CXTM-pure-and-retained-forward-slots-unchanged"
263 (and (equal before (state map held)) (string= out (cxtm map)))))))
264 (dolist (mode '(:core :variant))
265 (let* ((map (read-text "a. b. owner.")) (a (named map "a")) (b (named map "b")))
266 (ecase mode
267 (:core (add-topic-identifier a :subject-identifier "http://psi.topicmaps.org/iso13250/model/type")
268 (add-topic-identifier b :subject-identifier "http://psi.topicmaps.org/iso13250/model/instance"))
269 (:variant (add-variant (add-name (named map "owner") "N" :scope (list a)) "V" :scope (list a b))))
270 (let* ((held (handles map)) (before (state map held))
271 (text "def f($t) $t ^<#b>. end f(a)")
272 (parser (topic-maps.ctm::make-parser (topic-maps.ctm::make-lexer text) "https://example.invalid/a/doc.ctm" "urn:w" 0)))
273 (topic-maps.ctm::read-template-definition parser)
274 (let ((condition (condition-from (lambda () (topic-maps.ctm::read-statement parser map)))))
275 (check (format nil "merge:existing-atomic-conflict-policy-preserved:~A" mode)
276 (and (typep condition (if (eq mode :core) 'topic-maps.ctm:ctm-rejection 'variant-error))
277 (equal before (state map held)) (invariant map)
278 (null (topic-maps.ctm::parser-bindings parser)))))))))
279
280 (defun rejection-check (label text class code)
281 (let ((condition (condition-from (lambda () (read-text text)))))
282 (check (concatenate 'string label ":condition-and-code")
283 (and (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition))))
284 (check (concatenate 'string label ":source-position")
285 (and (<= 0 (topic-maps.ctm:error-position condition) (length text))
286 (plusp (topic-maps.ctm:error-line condition)) (plusp (topic-maps.ctm:error-column condition))))
287 condition))
288 (defun diagnostic-tests ()
289 (dolist (fixture '(("f()" topic-maps.ctm:undefined-template :undefined-template)
290 ("def f($a) $a. end f()" topic-maps.ctm:template-arity-error :template-arity)
291 ("def f() a. end def f() b. end" topic-maps.ctm:duplicate-template :duplicate-template)
292 ("def f() $unbound. end" topic-maps.ctm:unbound-template-variable :unbound-template-variable)
293 ("a occ:$x." topic-maps.ctm:unbound-template-variable :variable-outside-template)
294 ("def f($t) $t. end f(1)" topic-maps.ctm:invalid-template-argument :invalid-template-argument)
295 ("def f($n) a - $n. end f(1)" topic-maps.ctm:invalid-template-argument :invalid-template-argument)
296 ("def f($i) a occ:$i. end f(=<urn:i>)" topic-maps.ctm:invalid-template-argument :invalid-template-argument)
297 ("def f($i) a ^$i. end f(local)" topic-maps.ctm:invalid-template-argument :invalid-template-argument)
298 ("def f($unused) end f(ex:missing)" topic-maps.ctm:undefined-prefix :undefined-prefix)
299 ("def f() %prefix p <urn:p:> end" topic-maps.ctm:invalid-template-body :invalid-template-body)
300 ("def f() a - . end" topic-maps.ctm:invalid-template-body :invalid-template-body)
301 ("def f() a(<urn:r>:<urn:p>) end" topic-maps.ctm:invalid-template-body :role-separator-whitespace)
302 ("def f($t) a occ:\"v\"^^$t. end" topic-maps.ctm:invalid-template-body :variable-datatype)
303 ("def f() later() end f() def later() a. end" topic-maps.ctm:undefined-template :undefined-template)))
304 (destructuring-bind (text class code) fixture (rejection-check (concatenate 'string "diagnostic:" text) text class code)))
305 (let* ((text (format nil "def outer($x)~% inner($x)~%end~%def inner($t)~% $t - \"N\".~%end~%outer(42)"))
306 (condition (rejection-check "diagnostic:nested-context" text 'topic-maps.ctm:invalid-template-argument :invalid-template-argument))
307 (contexts (topic-maps.ctm:error-context condition)))
308 (check "diagnostic:original-Definition-and-Invocation-positions"
309 (and (= (search "$t -" text) (topic-maps.ctm:error-position condition)) (= 2 (length contexts))
310 (= (search "def inner" text) (getf (first contexts) :definition-position))
311 (= (search "inner($x)" text) (getf (first contexts) :invocation-position))
312 (= (search "outer(42)" text) (getf (second contexts) :invocation-position)))))
313 (let ((topic-maps.ctm::*template-depth-limit* 3))
314 (let ((c (condition-from (lambda () (read-text "def f() f() end f()")))))
315 (check "diagnostic:resource-limit-is-UNSUPPORTED-not-normative-rejection"
316 (and (eq (type-of c) 'topic-maps.ctm:ctm-unsupported) (eq :template-depth-limit (topic-maps.ctm:error-code c))))))
317 (let ((condition (condition-from (lambda () (read-text "%mergemap <other> <urn:ctm>")))))
318 (check "diagnostic:unknown-mergemap-notation"
319 (and (typep condition 'topic-maps.ctm:ctm-rejection)
320 (eq :unknown-mergemap-notation (topic-maps.ctm:error-code condition)))))
321 (dolist (text '("def f() a - \"\"\"N\"\"\". end" "%include <other>"))
322 (check (if (search "%include" text) "diagnostic:Include-requires-explicit-resolver"
323 (concatenate 'string "diagnostic:unchanged-excluded-feature:" text))
324 (let ((c (condition-from (lambda () (read-text text)))))
325 (and (typep c 'topic-maps.ctm:ctm-unsupported)
326 (or (not (search "%include" text)) (eq :resource-resolver-required (topic-maps.ctm:error-code c))))))))
327
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 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
335 (held (handles map)) (before (state map held))
336 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
337 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
338 (check (concatenate 'string id ":TMDM-object-and-reference-invariants") (invariant map))
339 (check (concatenate 'string id ":byte-exact-original-CXTM") (equalp actual expected))
340 (check (concatenate 'string id ":deterministic-mutation-free-CXTM")
341 (and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal before (state map held))))
342 (topic-maps.test-foundation:record-result case :cxtm-match)
343 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.14" "CTM 3.15" "CTM 3.3.6-3.3.8" "TMDM 5.3-5.8" "TMDM 6.1-6.7" "CXTM 3.4-3.19" "CXTM 4.6-4.11"))
344 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
345 (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)))))
346 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "model" (observe map)) *observations*)
347 (format t "~&TEMPLATE-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
348 (dolist (fixture *negative-originals*)
349 (destructuring-bind (file class code) fixture
350 (let* ((id (format nil "ctm/invalid/~A.ctm" file))
351 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
352 (source (topic-maps.test-foundation:test-case-source-pathname case))
353 (text (uiop:read-file-string source))
354 (condition (condition-from (lambda () (topic-maps.ctm:deserialize-file source
355 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id)))))
356 (check (concatenate 'string id ":specific-condition-code-and-source")
357 (and (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition))
358 (equal id (topic-maps.ctm:error-source condition))))
359 (check (concatenate 'string id ":meaningful-original-position")
360 (and (<= 0 (topic-maps.ctm:error-position condition) (length text))
361 (plusp (topic-maps.ctm:error-line condition)) (plusp (topic-maps.ctm:error-column condition))))
362 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition)))
363 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.14" "CTM 3.15"))
364 (push (topic-maps.test-foundation::fields "id" id "class" (symbol-name class) "code" (symbol-name code)
365 "position" (topic-maps.ctm:error-position condition) "line" (topic-maps.ctm:error-line condition)
366 "column" (topic-maps.ctm:error-column condition) "source" (topic-maps.ctm:error-source condition)
367 "context" (prin1-to-string (topic-maps.ctm:error-context condition))) *rejections*)
368 (format t "~&TEMPLATE-REJECTION ~A: PASS (~A)~%" id code))))
369 (setf *observations* (nreverse *observations*) *rejections* (nreverse *rejections*)))
370
371 (defun run-tests ()
372 (topic-maps.relationship.tests:run-tests)
373 (let ((topic-maps.ctm.tests::*checks* nil))
374 (definition-tests) (binding-tests) (wildcard-tests) (merge-tests) (diagnostic-tests) (original-cases)
375 (check "templates:executed-original-contracts-only"
376 (equal '(:pass 187 :fail 2 :unsupported 0 :not-run 84)
377 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
378 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
379 (format t "~&TEMPLATES-PASS: ~D checks; 57 byte-exact originals, 10 specific rejections; 187 PASS, 2 known FAIL, 84 NOT-RUN.~%" (length *last-checks*)) t))
380 (defun write-template-report (path)
381 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
382 (topic-maps.test-foundation::emit-json-value
383 (topic-maps.test-foundation::fields "kind" "template-execution-evidence" "passed" (length *last-checks*) "failed" 0
384 "checks" (topic-maps.test-foundation::json-array *last-checks*)
385 "cases" (topic-maps.test-foundation::json-array *observations*)
386 "rejections" (topic-maps.test-foundation::json-array *rejections*)) out)
387 (terpri out)))