]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-variants.lisp
Implement XTM association and role core with original conformance evidence
[topic-maps.git] / tests / ctm-variants.lisp
1 (defpackage #:topic-maps.variant.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:read-text #:api-topic #:cxtm #:map-state
4 #:ctm-condition #:local-topic #:binary-file #:permutations #:run-scope-tests)
5 (:export #:run-tests #:variant-checks #:write-variant-report))
6 (in-package #:topic-maps.variant.tests)
7
8 (defvar *last-checks* nil)
9 (defvar *observations* nil)
10 (defvar *ordering-witness* nil)
11 (defun variant-checks () *last-checks*)
12 (defun all-variants (map)
13 (loop for topic in (topic-map-topics map) append
14 (loop for name in (topic-names topic) append (topic-name-variants name))))
15 (defun condition-from (thunk)
16 (handler-case (progn (funcall thunk) nil) (error (c) c)))
17 (defun handle-state (handles)
18 ;; Old objects matter too: serialization must not compress their forward chains.
19 (mapcar (lambda (v) (list v (topic-maps.tmdm::%variant-forward v)
20 (topic-maps.tmdm::%variant-parent v)
21 (copy-list (topic-maps.tmdm::%variant-scope v)))) handles))
22 (defun variant-invariants (map)
23 (every (lambda (topic)
24 (every (lambda (name)
25 (every (lambda (v)
26 (and (eq v (canonical-variant v)) (eq name (variant-parent v))
27 (eq name (topic-maps.tmdm::%variant-parent v))
28 (topic-maps.tmdm::variant-scope-valid-p (variant-scope v) (topic-name-scope name))
29 (every (lambda (theme) (and (eq theme (canonical-topic theme))
30 (eq map (topic-parent theme))
31 (member theme (topic-map-topics map) :test #'eq)))
32 (topic-maps.tmdm::%variant-scope v)))) (topic-name-variants name)))
33 (topic-names topic))) (topic-map-topics map)))
34 (defun observe-map (map)
35 (let ((topics (topic-map-topics map)))
36 (labels ((ordinal (topic) (1+ (position topic topics :test #'eq)))
37 (themes (scope) (topic-maps.test-foundation::json-array (mapcar #'ordinal scope))))
38 (topic-maps.test-foundation::fields
39 "topics" (topic-maps.test-foundation::json-array
40 (loop for topic in topics collect
41 (topic-maps.test-foundation::fields "ordinal" (ordinal topic)
42 "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers topic))
43 "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers topic))
44 "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators topic)))))
45 "names" (topic-maps.test-foundation::json-array
46 (loop for topic in topics append
47 (loop for name in (topic-names topic) collect
48 (topic-maps.test-foundation::fields "owner" (ordinal topic) "value" (topic-name-value name)
49 "type" (ordinal (topic-name-type name)) "scope" (themes (topic-name-scope name))
50 "variants" (topic-maps.test-foundation::json-array
51 (loop for v in (topic-name-variants name) collect
52 (topic-maps.test-foundation::fields "value" (variant-value v) "datatype" (variant-datatype v)
53 "scope" (themes (variant-scope v))
54 "parentIsName" (if (eq name (variant-parent v)) :true :false)
55 "rawParentIsName" (if (eq name (topic-maps.tmdm::%variant-parent v)) :true :false))))))))))))
56
57 (defun original-cases ()
58 (setf *observations* nil)
59 (dolist (file '("variant" "variants" "variant-duplicate"))
60 (let* ((id (format nil "ctm/in/~A.ctm" file))
61 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
62 (doc (topic-maps.test-foundation:test-case-document-iri case))
63 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
64 :document-iri doc :source-name id))
65 (owner (local-topic map "topic" doc)) (name (first (topic-names owner)))
66 (expected-count (if (string= file "variants") 2 1))
67 (before (map-state map))
68 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
69 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
70 (check (concatenate 'string id ":exact-items-and-references")
71 (and (= (+ 2 expected-count) (length (topic-map-topics map)))
72 (= 1 (length (topic-names owner))) (= expected-count (length (all-variants map)))
73 (= 1 (loop for topic in (topic-map-topics map) sum (length (topic-names topic))))
74 (null (topic-map-associations map)) (every (lambda (topic) (null (topic-occurrences topic))) (topic-map-topics map))
75 (string= "Name" (topic-name-value name)) (null (topic-name-scope name))
76 (equal (list (default-name-type-identifier)) (topic-subject-identifiers (topic-name-type name)))
77 (variant-invariants map)))
78 (loop for v in (topic-name-variants name) for text in '("name" "nome")
79 for theme in '("theme" "theme2") do
80 (check (format nil "~A:~A:value-datatype-theme-identity" id text)
81 (and (string= text (variant-value v)) (string= (string-datatype) (variant-datatype v))
82 (equal (list (local-topic map theme doc)) (variant-scope v))
83 (null (variant-item-identifiers v)))))
84 (check (concatenate 'string id ":byte-exact-baseline") (equalp actual expected))
85 (check (concatenate 'string id ":serialization-pure") (equal before (map-state map)))
86 (topic-maps.test-foundation:record-result case :cxtm-match)
87 (setf (topic-maps.test-foundation:test-case-clauses case)
88 '("CTM 3.4.1" "CTM 3.11" "CTM 3.12" "TMDM 5.5" "TMDM 6.3" "TMDM 6.4"
89 "CXTM 3.6" "CXTM 3.7" "CXTM 3.12" "CXTM 3.14" "CXTM 3.16" "CXTM 3.19" "CXTM 4.8"))
90 (setf topic-maps.ctm.tests::*cxtm-results*
91 (append topic-maps.ctm.tests::*cxtm-results*
92 (list (list :id id :base doc :actual (cxtm map doc) :expected
93 (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case)
94 :external-format :utf-8)
95 :match t :bytes (length actual)))))
96 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc
97 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)
98 :external-format :utf-8)
99 "actualObjects" (observe-map map)) *observations*)
100 (format t "~&VARIANT-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
101 (let* ((id "ctm/invalid/variant-no-scope.ctm")
102 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
103 (e (condition-from (lambda () (topic-maps.ctm:deserialize-file
104 (topic-maps.test-foundation:test-case-source-pathname case)
105 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id)))))
106 (check "variant-no-scope:original-specific-rejection-position"
107 (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection)
108 (eq :expected-variant-scope (topic-maps.ctm:error-code e))
109 (= 28 (topic-maps.ctm:error-position e))
110 (= 3 (topic-maps.ctm:error-line e)) (= 22 (topic-maps.ctm:error-column e))))
111 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string e)))
112 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.12" "TMDM 5.5")))
113 (setf *observations* (nreverse *observations*)))
114
115 (defun api-tests ()
116 (let* ((map (make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
117 (p (api-topic map "p")) (a (api-topic map "a")) (b (api-topic map "b"))
118 (name (add-name owner "N" :type type :scope (list p)))
119 (input (list p a a)) (one (add-variant name "V" :scope input))
120 (same (add-variant name "V" :scope (list a p)))
121 (different-scope (add-variant name "V" :scope (list p b)))
122 (different-type (add-variant name "V" :datatype "urn:datatype" :scope (list a p))))
123 (check "variant-api:sets-deduplicate-and-replacement-handles"
124 (and (eq (canonical-variant one) same) (not (eq one same))
125 (= 3 (length (topic-name-variants name)))
126 (not (eq same different-scope)) (not (eq same different-type)) (variant-invariants map)))
127 (setf (first input) owner)
128 (let ((output (variant-scope one))) (setf (first output) type))
129 (let ((output (topic-name-variants name))) (setf (first output) nil))
130 (check "variant-api:defensive-set-copies" (and (equal (list p a) (variant-scope one))
131 (every #'identity (topic-name-variants name))))
132 (let* ((foreign (api-topic (make-topic-map) "foreign")) (cycle (list a)))
133 (setf (cdr cycle) cycle)
134 (dolist (fixture (list (list nil :invalid-variant-scope) (list (list a) :invalid-variant-scope)
135 (list (list p) :invalid-variant-scope) (list (list foreign) :foreign-theme)
136 (list (list "a") :invalid-theme) (list (cons p a) :invalid-scope)
137 (list cycle :invalid-scope)))
138 (let* ((before (map-state map)) (e (condition-from (lambda () (add-variant name "X" :scope (first fixture))))))
139 (check (format nil "variant-api:~A:~D:atomic" (second fixture) (length topic-maps.ctm.tests::*checks*))
140 (and (typep e (if (eq (second fixture) :invalid-variant-scope) 'variant-error 'scope-error))
141 (eq (second fixture) (if (typep e 'variant-error) (variant-error-code e) (scope-error-code e)))
142 (equal before (map-state map)))))))
143 (dolist (fixture (list (list "x" "relative" :invalid-datatype)
144 (list "x" (integer-datatype) :invalid-integer-value)
145 (list "x" (decimal-datatype) :invalid-decimal-value)
146 (list "0000-01-01" (date-datatype) :invalid-date-value)
147 (list "x" (datetime-datatype) :invalid-datetime-value)
148 (list "relative" (iri-datatype) :relative-uri-value)))
149 (let* ((before (map-state map))
150 (e (condition-from (lambda () (add-variant name (first fixture) :datatype (second fixture) :scope (list p a))))))
151 (check (format nil "variant-api:~A:~A:literal-atomic" (second fixture) (third fixture))
152 (and (typep e 'variant-error) (eq (third fixture) (variant-error-code e)) (equal before (map-state map))))))
153 (let ((other-name (add-name owner "Other" :type type :scope (list p))))
154 (check "variant-api:different-parent-prevents-merging"
155 (not (eq (canonical-variant one) (add-variant other-name "V" :scope (list p a))))))
156 (let* ((n (add-name owner "Empty scope" :type type)) (before (map-state map)))
157 (check "variant-api:empty-parent-still-needs-nonempty-variant"
158 (and (typep (condition-from (lambda () (add-variant n "V"))) 'variant-error)
159 (equal before (map-state map)))))
160 (dolist (thunk (list (lambda () (add-variant name nil :scope (list p a)))
161 (lambda () (add-variant name "V" :datatype nil :scope (list p a)))
162 (lambda () (add-variant owner "V" :scope (list p a)))))
163 (let ((before (map-state map)))
164 (check (format nil "variant-api:type-error:~D" (length topic-maps.ctm.tests::*checks*))
165 (and (typep (condition-from thunk) 'type-error) (equal before (map-state map))))))))
166
167 (defun merge-tests ()
168 (let* ((map (make-topic-map)) (o (api-topic map "o")) (type1 (api-topic map "type1"))
169 (type2 (api-topic map "type2")) (theme (api-topic map "theme"))
170 (n1 (add-name o "N" :type type1)) (n2 (add-name o "N" :type type2))
171 (v1 (add-variant n1 "one" :scope (list theme)))
172 (v2 (add-variant n2 "two" :scope (list theme))))
173 (merge-topics type1 type2)
174 (check "variant-merge:name-union-preserves-distinct-children-and-stored-parents"
175 (and (= 1 (length (topic-names o))) (= 2 (length (all-variants map)))
176 (eq (canonical-topic-name n1) (canonical-topic-name n2))
177 (eq (canonical-topic-name n1) (variant-parent v1))
178 (eq (canonical-topic-name n2) (variant-parent v2))
179 (variant-invariants map))))
180 (dolist (kind '(:owner :type :name-scope :variant-scope))
181 (dolist (reverse '(nil t))
182 (let* ((map (make-topic-map)) (o (api-topic map "owner")) (t1 (api-topic map "type"))
183 (a (api-topic map "a")) (b (api-topic map "b")) (x (api-topic map "x"))
184 (o2 (if (eq kind :owner) (api-topic map "owner2") o))
185 (t2 (if (eq kind :type) (api-topic map "type2") t1))
186 (n1 (add-name o "N" :type t1 :scope (list a)))
187 (n2 (add-name o2 "N" :type t2 :scope (list (if (eq kind :name-scope) b a))))
188 (v1 (add-variant n1 "V" :scope (list a x)))
189 (v2 (add-variant n2 "V" :scope (list (if (eq kind :name-scope) b a)
190 (if (eq kind :variant-scope) b x))))
191 (pair (ecase kind (:owner (list o o2)) (:type (list t1 t2))
192 (:name-scope (list a b)) (:variant-scope (list x b)))))
193 (when reverse (setf pair (reverse pair)))
194 (apply #'merge-topics pair)
195 (check (format nil "variant-merge:~A:~A:parents-and-forward-handles" kind reverse)
196 (and (eq (canonical-topic-name n1) (canonical-topic-name n2))
197 (eq (canonical-variant v1) (canonical-variant v2))
198 (= 1 (length (all-variants map)))
199 (eq (canonical-topic-name n1) (variant-parent v1))
200 (eq (canonical-topic o) (topic-name-parent (variant-parent v2)))
201 (variant-invariants map)))
202 (let ((before (map-state map)) (handles (handle-state (list v1 v2))))
203 (check (format nil "variant-merge:~A:~A:pure-output-and-old-handles" kind reverse)
204 (and (string= (cxtm map) (cxtm map)) (equal before (map-state map))
205 (equal handles (handle-state (list v1 v2)))))))))
206 ;; Parent scope loses a duplicate theme but retains an additional Variant theme.
207 (let* ((map (make-topic-map)) (o (api-topic map "o")) (a (api-topic map "a"))
208 (b (api-topic map "b")) (x (api-topic map "x"))
209 (n (add-name o "N" :scope (list a b))) (v (add-variant n "V" :scope (list a b x)))
210 (c (merge-topics a b)))
211 (check "variant-merge:scope-cardinality-reduction-remains-valid"
212 (and (equal (list c) (topic-name-scope n)) (= 2 (length (variant-scope v)))
213 (member c (variant-scope v) :test #'eq) (member x (variant-scope v) :test #'eq)
214 (variant-invariants map)))
215 (let ((again (add-variant n "V" :scope (list b a x))))
216 (check "variant-merge:retained-theme-handles-at-api-boundary" (eq again (canonical-variant v)))))
217 (dolist (kind '(:explicit :item-identifier :subject-identifier :subject-locator))
218 (let* ((map (make-topic-map)) (o (api-topic map "o")) (p (api-topic map "p")) (x (api-topic map "x"))
219 (n (add-name o "N" :scope (list p))) (v (add-variant n "V" :scope (list p x))))
220 (unless (eq kind :explicit) (add-topic-identifier p kind "urn:shared"))
221 (let* ((before (map-state map)) (handles (handle-state (list v)))
222 (e (condition-from (lambda () (if (eq kind :explicit) (merge-topics p x)
223 (add-topic-identifier x kind "urn:shared"))))))
224 (check (format nil "variant-merge:~A:collapse-rejected-before-any-mutation" kind)
225 (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e))
226 (equal before (map-state map)) (equal handles (handle-state (list v)))
227 (eq p (canonical-topic p)) (eq x (canonical-topic x)) (variant-invariants map))))))
228 ;; Global preflight must catch a transitive collapse before the first pair merge.
229 (let* ((map (make-topic-map)) (o (api-topic map "o")) (p (api-topic map "p"))
230 (x (api-topic map "x")) (y (api-topic map "y"))
231 (n (add-name o "N" :scope (list p))))
232 (add-variant n "V" :scope (list p x y))
233 (setf (topic-maps.tmdm::%topic-subject-identifiers p) '("urn:one")
234 (topic-maps.tmdm::%topic-subject-identifiers x) '("urn:one" "urn:two")
235 (topic-maps.tmdm::%topic-subject-identifiers y) '("urn:two"))
236 (let ((before (map-state map)) (e (condition-from (lambda () (normalize-topic-map map)))))
237 (check "variant-merge:transitive-normalization-atomic"
238 (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e))
239 (equal before (map-state map)))))))
240
241 (defun reader-tests ()
242 (let* ((map (read-text "a - kind:\"N\"@p (\"V\"@x,p,x)(\"W\"@y)."))
243 (n (first (topic-names (local-topic map "a")))) (vs (topic-name-variants n)))
244 (check "variant-reader:explicit-vs-resulting-scope"
245 (and (= 5 (length (topic-map-topics map))) (= 2 (length vs))
246 (eq (local-topic map "kind") (topic-name-type n))
247 (equal (list (local-topic map "p")) (topic-name-scope n))
248 (every (lambda (v) (= 2 (length (variant-scope v)))) vs)
249 (member (local-topic map "x") (variant-scope (first vs)) :test #'eq)
250 (member (local-topic map "p") (variant-scope (second vs)) :test #'eq)
251 (variant-invariants map))))
252 (let* ((map (read-text "%prefix x <urn:> a - \"N\" (\"01\"^^x:datatype @x:theme)(x:value @x:theme)."))
253 (vs (all-variants map)))
254 (check "variant-reader:datatype-and-uri-never-synthesized-topics"
255 (and (= 3 (length (topic-map-topics map)))
256 (equal '("01" "urn:value") (mapcar #'variant-value vs))
257 (equal (list "urn:datatype" (iri-datatype)) (mapcar #'variant-datatype vs)))))
258 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a - \"N\" (1@t)(\"01\"^^xsd:integer@t)(1.0@t)(2000-01-01@t)(2000-01-01T24:00:00Z@t)."))
259 (vs (all-variants map)) (before (map-state map)))
260 (check "variant-reader:shared-literal-semantics-and-distinct-lexical-equality"
261 (and (= 5 (length vs))
262 (equal '("1" "01" "1.0" "2000-01-01" "2000-01-02T00:00:00Z") (mapcar #'variant-value vs))
263 (equal (list (integer-datatype) (integer-datatype) (decimal-datatype) (date-datatype) (datetime-datatype))
264 (mapcar #'variant-datatype vs))
265 (search "<value>1</value>" (cxtm map)) (equal before (map-state map)) (= 5 (length (all-variants map))))))
266 (dolist (fixture '(("a - \"N\" (\"V\")." :expected-variant-scope)
267 ("a - \"N\" (\"V\"@)." :expected-scope-theme)
268 ("a - \"N\" (\"V\"@t,)." :expected-scope-theme)
269 ("a - \"N\" (\"V\"@t." :missing-variant-close)
270 ("a - \"N\" (@t)." :expected-variant-value)
271 ("a - \"N\"@t (\"V\"@t)." :invalid-variant-scope)
272 ("a t:\"V\" (\"bad\"@t)." :unexpected-variant)
273 ("a - \"N\" (\"V\"@x:t)." :undefined-prefix)
274 ("a - \"N\" (\"V\"@t); - \"N\" (\"W\"@t)." nil)))
275 (let ((e (ctm-condition (first fixture))))
276 (check (concatenate 'string "variant-reader:diagnostic:" (first fixture))
277 (if (null (second fixture)) (null e)
278 (and (eq (type-of e) (if (eq (second fixture) :undefined-prefix)
279 'topic-maps.ctm:undefined-prefix 'topic-maps.ctm:ctm-rejection))
280 (eq (second fixture) (topic-maps.ctm:error-code e))
281 (<= 0 (topic-maps.ctm:error-position e) (length (first fixture)))
282 (= 1 (topic-maps.ctm:error-line e))
283 (= (1+ (topic-maps.ctm:error-position e)) (topic-maps.ctm:error-column e)))))))
284 (let ((e (ctm-condition (format nil "a - \"N\" (~% \"V\")."))))
285 (check "variant-reader:missing-scope-exact-line-column"
286 (and (eq :expected-variant-scope (topic-maps.ctm:error-code e))
287 (= 2 (topic-maps.ctm:error-line e)) (= 5 (topic-maps.ctm:error-column e)))))
288 (let ((e (ctm-condition "a - \"N\"@p (\"V\"@x). p <urn:same>. x <urn:same>.")))
289 (check "variant-reader:identity-triggered-collapse-is-specific-ctm-rejection"
290 (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection)
291 (eq :variant-scope-collapse (topic-maps.ctm:error-code e))
292 (plusp (topic-maps.ctm:error-position e)))))
293 (let* ((map (read-text "a - \"N\" (\"one\"@t); - \"N\" (\"two\"@t)."))
294 (n (first (topic-names (local-topic map "a")))))
295 (check "variant-reader:duplicate-name-preserves-all-children"
296 (and (= 1 (length (topic-names (local-topic map "a")))) (= 2 (length (topic-name-variants n))))))
297 (dolist (file '("variant-reifier" "variant-scope-embedded" "variant-scope-embedded2"
298 "variant-scope-embedded3" "variant-scope-embedded4"))
299 (let* ((case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus*
300 :id (format nil "ctm/in/~A.ctm" file))))
301 (e (condition-from (lambda () (topic-maps.ctm:deserialize-file
302 (topic-maps.test-foundation:test-case-source-pathname case)
303 :document-iri (topic-maps.test-foundation:test-case-document-iri case))))))
304 (check (concatenate 'string "variant-reader:reference-contract-remains-unexecuted:" file)
305 (and (null e)
306 (eq :not-run (topic-maps.test-foundation:test-case-status case)))))))
307
308 (defun ordering-tests ()
309 (let ((reference nil))
310 (dolist (order (permutations '(0 1 2 3 4)))
311 (let* ((map (make-topic-map)) (o (api-topic map "owner")) (type (api-topic map "type"))
312 (a (api-topic map "a")) (b (api-topic map "b"))
313 (n (add-name o "N" :type type))
314 (specs (list (list "A" (string-datatype) (list a))
315 (list "V" "urn:a" (list b)) (list "V" "urn:b" (list a))
316 (list "V" "urn:b" (list b a)) (list "urn:0" (iri-datatype) (list a)))))
317 (dolist (i order)
318 (let ((spec (nth i specs))) (add-variant n (first spec) :datatype (second spec) :scope (third spec))))
319 (let ((before (map-state map)) (output (cxtm map)))
320 (when reference (check (format nil "variant-order:~S:construction-independent" order) (string= reference output)))
321 (setf reference output *ordering-witness* output)
322 (check (format nil "variant-order:~S:deterministic-and-mutation-free" order)
323 (and (string= output (cxtm map)) (equal before (map-state map))))))))
324 (let* ((map (make-topic-map)) (o (api-topic map "o")) (a (api-topic map "a"))
325 (n1 (add-name o "A")) (n2 (add-name o "Z"))
326 (v1 (add-variant n1 "V" :scope (list a))) (v2 (add-variant n2 "V" :scope (list a))))
327 (let ((topic-maps.cxtm::*base-locator* "https://example.invalid/a/doc.ctm")
328 (topic-maps.cxtm::*locator-cache* (make-hash-table :test #'equal)))
329 (check "variant-order:parent-name-final-comparison-key" (minusp (topic-maps.cxtm::variant-order v1 v2))))
330 (check "variant-order:numbering-restarts-in-each-name"
331 (= 2 (loop with xml = (cxtm map) with start = 0 for pos = (search "<variant number=\"1\">" xml :start2 start)
332 while pos count pos do (setf start (1+ pos))))))
333 (let* ((map (read-text "a - \"N\" (\"V\"@t).")) (v (first (all-variants map)))
334 (n (variant-parent v)) (saved (variant-scope v)))
335 (dolist (scope (list nil (list (first saved) (first saved))
336 (list (api-topic (make-topic-map) "foreign"))))
337 (unwind-protect
338 (progn (setf (topic-maps.tmdm::%variant-scope v) scope)
339 (let ((before (map-state map)))
340 (check (format nil "variant-cxtm:invalid-scope:~D:refused-without-mutation" (length scope))
341 (and (typep (condition-from (lambda () (cxtm map))) 'topic-maps.cxtm:cxtm-error)
342 (equal before (map-state map))))))
343 (setf (topic-maps.tmdm::%variant-scope v) saved)))
344 (let ((old (add-variant n "V" :scope saved)))
345 (declare (ignore old))
346 (let ((current (topic-name-variants n)))
347 (unwind-protect
348 (progn (setf (topic-maps.tmdm::%topic-name-variants n) (list v))
349 (check "variant-cxtm:obsolete-variant-in-live-set-rejected"
350 (eq :invalid-parent (topic-maps.cxtm:error-code
351 (condition-from (lambda () (cxtm map)))))))
352 (setf (topic-maps.tmdm::%topic-name-variants n) current))))))
353
354 (defun run-tests ()
355 (run-scope-tests)
356 (let ((topic-maps.ctm.tests::*checks* nil))
357 (original-cases) (api-tests) (merge-tests) (reader-tests) (ordering-tests)
358 (check "variant:only-four-original-contract-transitions"
359 (equal '(:pass 71 :fail 2 :unsupported 0 :not-run 200)
360 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
361 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
362 (format t "~&VARIANTS-PASS: ~D checks; 3 original byte-exact comparisons, 1 specific rejection; 71 PASS, 2 known FAIL, 200 NOT-RUN.~%"
363 (length *last-checks*)) t))
364 (defun write-variant-report (path)
365 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
366 (topic-maps.test-foundation::emit-json-value
367 (topic-maps.test-foundation::fields "kind" "variant-evidence" "passed" (length *last-checks*) "failed" 0
368 "checks" (topic-maps.test-foundation::json-array *last-checks*) "orderingWitnessCXTM" *ordering-witness*
369 "cases" (topic-maps.test-foundation::json-array *observations*)) out)
370 (terpri out)))