]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-variants.lisp
test: prove CTM/XTM anyType equivalence (slice 22T)
[topic-maps.git] / tests / xtm-variants.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 (defparameter *variant-original-specs*
4 '(("variant" 3 nil ("command") 1)
5 ("variant-duplicate" 3 nil ("command") 2)
6 ("variant-scope-duplicate" 3 nil ("command") 1)
7 ("variant-scope-multiple" 4 nil ("command" "silly") 1)
8 ("variant-inherit" 4 ("english") ("english" "command") 1)))
9 (defparameter *variant-negative-spec*
10 '("variant-missing-scope-duplicate" variant-error :invalid-variant-scope
11 "Variant scope must properly contain its Parent Name scope." nil nil "XTM 2.0 4.12,4.13; TMDM 5.5: proper superset of Parent Name scope"))
12
13 (defun all-variants (map)
14 (loop for topic in (topic-map-topics map) append
15 (loop for name in (topic-names topic) append (topic-name-variants name))))
16 (defun variants-current-p (map)
17 (and (names-current-p map)
18 (every (lambda (topic)
19 (every (lambda (name)
20 (every (lambda (v)
21 (and (eq v (canonical-variant v)) (eq name (variant-parent v))
22 (> (length (variant-scope v)) (length (topic-name-scope name)))
23 (subsetp (topic-name-scope name) (variant-scope v) :test #'eq)
24 (every (lambda (theme) (and (eq theme (canonical-topic theme))
25 (member theme (topic-map-topics map) :test #'eq))) (variant-scope v)))) (topic-name-variants name)))
26 (topic-names topic))) (topic-map-topics map))))
27 (defun variant-state (map)
28 (list (name-state map)
29 (loop for v in (all-variants map) collect
30 (list v (variant-parent v) (variant-value v) (variant-datatype v) (variant-scope v)
31 (item-identifiers v) (item-reifier v) (topic-maps.tmdm::%variant-forward v)))))
32 (defun variant-model-data (map)
33 (let ((topics (topic-map-topics map))
34 (names (loop for topic in (topic-map-topics map) append (topic-names topic))))
35 (topic-maps.test-foundation::fields "nameModel" (name-model-data map)
36 "canonicalVariantReferences" (if (variants-current-p map) :true :false)
37 "variants" (coerce (loop for v in (all-variants map) collect
38 (topic-maps.test-foundation::fields "value" (variant-value v) "datatype" (variant-datatype v)
39 "parentName" (1+ (position (variant-parent v) names :test #'eq))
40 "scope" (coerce (mapcar (lambda (theme) (1+ (position theme topics :test #'eq))) (variant-scope v)) 'vector)
41 "itemIdentifiers" (coerce (item-identifiers v) 'vector) "reifier" (when (item-reifier v) (1+ (position (item-reifier v) topics :test #'eq))))) 'vector))))
42
43 (defun variant-original-model-checks (spec map raw)
44 (destructuring-bind (id count name-scope-ids variant-scope-ids source-count) spec
45 (let* ((owner (by-id map "topic")) (name (first (topic-names owner))) (v (first (topic-name-variants name)))
46 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
47 (source-name (first (topic-maps.xtm::elements (first (topic-maps.xtm::elements root)))))
48 (source-variants (remove-if-not (lambda (node) (equal "variant" (topic-maps.xtm::node-name node))) (topic-maps.xtm::elements source-name)))
49 (label (concatenate 'string "variant-original:" id)))
50 (check (concatenate 'string label ":original-textual-subset")
51 (and (= source-count (length source-variants))
52 (every (lambda (node) (let ((kids (topic-maps.xtm::elements node)))
53 (and (null (topic-maps.xtm::node-attributes node)) (= 2 (length kids))
54 (equal "scope" (topic-maps.xtm::node-name (first kids)))
55 (equal "resourceData" (topic-maps.xtm::node-name (second kids)))
56 (null (topic-maps.xtm::node-attributes (second kids)))
57 (equal '("TOPIC!") (topic-maps.xtm::node-children (second kids)))))) source-variants)))
58 (check (concatenate 'string label ":native-counts-and-parent-name-eq")
59 (and (= count (length (topic-map-topics map))) (null (topic-map-associations map))
60 (= 1 (loop for topic in (topic-map-topics map) sum (length (topic-names topic))))
61 (= 1 (length (topic-name-variants name))) (= 1 (length (all-variants map)))
62 (eq owner (topic-name-parent name)) (eq name (canonical-topic-name name))
63 (eq name (variant-parent v)) (eq v (canonical-variant v))
64 (eq (by-si map (default-name-type-identifier)) (topic-name-type name))))
65 (check (concatenate 'string label ":value-datatype-and-scope-union")
66 (and (equal "Topic" (topic-name-value name)) (equal "TOPIC!" (variant-value v))
67 (equal (string-datatype) (variant-datatype v))
68 (scope-eq-p (topic-name-scope name) (mapcar (lambda (id) (by-id map id)) name-scope-ids))
69 (scope-eq-p (variant-scope v) (mapcar (lambda (id) (by-id map id)) variant-scope-ids))
70 (variants-current-p map)))
71 (check (concatenate 'string label ":identities-and-excluded-metadata")
72 (and (null (item-identifiers map)) (null (item-identifiers name)) (null (item-reifier name))
73 (null (item-identifiers v)) (null (item-reifier v))
74 (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
75 (every (lambda (id) (let ((theme (by-id map id)))
76 (and (equal (list (concatenate 'string *document* "#" id)) (item-identifiers theme))
77 (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme))))) variant-scope-ids)
78 (topic-maps.tmdm::validate-map-item-identifiers map))))))
79
80 (defun variant-original-contracts ()
81 (setf *variant-outputs* nil)
82 (dolist (spec *variant-original-specs*)
83 (let* ((name (first spec)) (id (format nil "xtm2/in/~A.xtm" name))
84 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
85 (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
86 (raw (file-bytes source)) (copy (copy-seq raw)) (expected (file-bytes baseline))
87 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
88 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
89 :clauses '("XTM 2.0 4.12,4.13,4.16" "TMDM 5.5,6.2,6.3,6.4" "CXTM 3,4")))
90 (map nil) (actual nil) (c nil) (before nil))
91 (setf *cases* (append *cases* (list case)))
92 (handler-case (setf map (deserialize-octets raw :document-iri *document* :source-key id)
93 before (variant-state map)
94 actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
95 (error (condition) (setf c condition)))
96 ;; A reader or model failure can never satisfy this positive contract.
97 (when (null c)
98 (handler-case (variant-original-model-checks spec map raw) (error (condition) (setf c condition))))
99 (record-original-result case actual expected c)
100 (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
101 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
102 "sourceText" (babel:octets-to-string raw :encoding :utf-8)
103 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
104 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
105 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
106 "firstDifferentByte" (when actual (mismatch actual expected))
107 "condition" (negative-condition-data c) "model" (when map (variant-model-data map))
108 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *variant-outputs*)
109 (when c (error c))
110 (check (concatenate 'string "variant-original:" name ":byte-exact") (equalp actual expected))
111 (check (concatenate 'string "variant-original:" name ":source-preserved") (equalp raw copy))
112 (check (concatenate 'string "variant-original:" name ":pure") (equal before (variant-state map)))
113 (check (concatenate 'string "variant-original:" name ":deterministic")
114 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
115 (check (concatenate 'string "variant-original:" name ":repeat-pure") (equal before (variant-state map)))
116 (let ((other (deserialize-octets raw :document-iri *document* :source-key "different-name-provenance")))
117 (check (concatenate 'string "variant-original:" name ":source-key-only-provenance")
118 (and (equalp (variant-model-data map) (variant-model-data other))
119 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
120 (setf *variant-outputs* (nreverse *variant-outputs*))
121 (check "variant-originals:55-unique-case-records"
122 (and (= 55 (length *cases*)) (= 48 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
123 (= 55 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
124
125 (defun variant-negative-original ()
126 (let* ((spec *variant-negative-spec*) (id "xtm2/invalid/variant-missing-scope-duplicate.xtm")
127 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
128 (raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
129 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
130 (source-name (first (topic-maps.xtm::elements (first (topic-maps.xtm::elements root)))))
131 (kids (topic-maps.xtm::elements source-name)) (v (third kids))
132 (scope (first (topic-maps.xtm::elements v)))
133 (case (topic-maps.test-foundation:make-test-case :id id :category :invalid :source-pathname (corpus-path id)
134 :document-iri iri :expected-outcome :ctm-rejection :clauses (list (seventh spec))))
135 (c (condition-from (lambda () (deserialize-octets raw :document-iri iri :source-key id)))))
136 (check "variant-negative:original-equal-scopes-and-textual-resource"
137 (and (= 3 (length kids)) (equal "scope" (topic-maps.xtm::node-name (first kids)))
138 (equal "variant" (topic-maps.xtm::node-name v))
139 (equal "#command" (topic-maps.xtm::attr (first (topic-maps.xtm::elements (first kids))) "href"))
140 (equal "#command" (topic-maps.xtm::attr (first (topic-maps.xtm::elements scope)) "href"))
141 (equal "resourceData" (topic-maps.xtm::node-name (second (topic-maps.xtm::elements v))))))
142 (record-negative-original-result case spec c iri id)
143 (setf *cases* (append *cases* (list case)))
144 (setf *variant-negative-outputs* (list (topic-maps.test-foundation::fields
145 "id" id "documentIRI" iri "sourceKey" id "sourceBytes" (length raw)
146 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
147 "condition" (negative-condition-data c) "sourceVariantElementPosition"
148 (topic-maps.test-foundation::fields "line" (topic-maps.xtm::node-line v) "column" (topic-maps.xtm::node-column v)
149 "unit" "approximate-SAX-element-position-not-condition-position")
150 "intendedCauseReached" (if (intended-negative-p spec c iri id) :true :false)
151 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case))))))
152 (check "variant-negative:intended-native-scope-condition" (intended-negative-p spec c iri id))
153 (check "variant-negative:source-preserved" (equalp raw copy))
154 (check "variant-originals:56-unique-case-records"
155 (and (= 56 (length *cases*)) (= 48 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
156 (= 56 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))))
157
158 (defun scope-xml (ids)
159 (format nil "<scope>~{~A~}</scope>" (mapcar (lambda (id) (format nil "<topicRef href='#~A'/>" id)) ids)))
160 (defun variant-xml (value themes)
161 (format nil "<variant>~A<resourceData>~A</resourceData></variant>" (scope-xml themes) value))
162 (defun variant-name-xml (name-themes variants &optional ii)
163 (format nil "<name>~A~A<value>N</value>~A</name>"
164 (if ii (format nil "<itemIdentity href='~A'/>" ii) "")
165 (if name-themes (scope-xml name-themes) "") variants))
166 (defun variant-control (label body verify)
167 (let* ((map (read-body body)) (before (variant-state map)))
168 (funcall verify map)
169 (check (concatenate 'string "variant-control:" label ":canonical-and-proper-scope") (variants-current-p map))
170 (let ((actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
171 (check (concatenate 'string "variant-control:" label ":pure") (equal before (variant-state map)))
172 (push (topic-maps.test-foundation::fields "label" label "kind" "positive-XTM"
173 "sourceText" (document body) "model" (variant-model-data map)
174 "actualCXTM" (babel:octets-to-string actual :encoding :utf-8)) *variant-controls*))))
175 (defun variant-error-control (label body class code)
176 (let* ((key (concatenate 'string "variant-syntax:" label)) (source (ss-unsupported-source (document body) code label))
177 (c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key key)))))
178 (check (concatenate 'string key ":class-code")
179 (and (eq class (type-of c)) (eq code (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
180 ((typep c 'variant-error) (variant-error-code c)) ((typep c 'item-identifier-error) (item-identifier-error-code c))))))
181 (when (typep c 'topic-maps.xtm:xtm-error)
182 (check (concatenate 'string key ":context-position")
183 (and (equal key (error-source-key c)) (equal *document* (error-document-iri c))
184 (plusp (error-line c)) (plusp (error-column c)))))
185 (push (topic-maps.test-foundation::fields "label" label "kind" "negative-or-unsupported-XTM"
186 "sourceText" source "documentIRI" *document* "sourceKey" key "condition" (negative-condition-data c)) *variant-controls*)))
187
188 (defun variant-text-controls ()
189 (dolist (row (list (list "simple" "V" "V") (list "empty" "" "")
190 (list "unicode-no-normalization" "馬籠🦊é" "馬籠🦊é")
191 (list "escaping-once" "&amp;lt; &lt; &amp; &#x1F98A;" "&lt; < & 🦊")
192 (list "whitespace" (format nil " A~C B~% C " #\Tab) (format nil " A~C B~% C " #\Tab))
193 (list "line-endings" (format nil "A~C~CB~CC&#xD;D" #\Return #\Newline #\Return) (format nil "A~%B~%C~CD" #\Return))
194 (list "sax-chunks" "A<![CDATA[<&]]>&amp;B<!--ignored--><?p ignored?>C" "A<&&BC")))
195 (variant-control (first row) (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (variant-xml (second row) '("s"))))
196 (lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (topic-name-variants name))))
197 (check (concatenate 'string "variant-text:" (first row))
198 (and (equal (third row) (variant-value v)) (equal (string-datatype) (variant-datatype v))
199 (eq name (variant-parent v)) (scope-eq-p (variant-scope v) (list (by-id map "s")))))))))
200 (variant-control "explicit-xsd-string"
201 "<topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#string'>V</resourceData></variant></name></topic>"
202 (lambda (map) (check "variant:explicit-string-equals-default"
203 (let ((other (read-body (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (variant-xml "V" '("s")))))))
204 (and (equalp (variant-model-data map) (variant-model-data other))
205 (equalp (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
206 (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
207 (variant-control "inherited-union-duplicates-forward"
208 (format nil "<topic id='a'>~A</topic><topic id='base'/><topic id='s'/><topic id='t'/>"
209 (variant-name-xml '("base") (concatenate 'string (variant-xml "V" '("base" "s" "t" "s")) (variant-xml "V" '("t" "s")))))
210 (lambda (map) (let ((name (first (topic-names (by-id map "a")))))
211 (check "variant:union-set-order-and-normalization"
212 (and (= 1 (length (topic-name-variants name)))
213 (scope-eq-p (variant-scope (first (topic-name-variants name))) (list (by-id map "base") (by-id map "s") (by-id map "t"))))))))
214 (variant-control "different-values-and-scopes"
215 (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil
216 (concatenate 'string (variant-xml "V" '("s")) (variant-xml "V " '("s")) (variant-xml "V" '("t")))))
217 (lambda (map) (check "variant:different-properties-stay-distinct" (= 3 (length (all-variants map))))))
218 (dolist (kind '("subjectIdentifier" "itemIdentity"))
219 (variant-control (concatenate 'string "reference-hit:" kind)
220 (format nil "<topic id='s'><~A href='https://themes.invalid/map#theme'/></topic><topic id='a'><name><value>N</value><variant><scope><topicRef href='https://themes.invalid/map#theme'/></scope><resourceData>V</resourceData></variant></name></topic>" kind)
221 (lambda (map) (check (concatenate 'string "variant:reference-hit-no-extra-II:" kind)
222 (and (scope-eq-p (variant-scope (first (all-variants map))) (list (by-id map "s")))
223 (same-identifiers-p (item-identifiers (by-id map "s"))
224 (append (list (concatenate 'string *document* "#s")) (when (equal kind "itemIdentity") '("https://themes.invalid/map#theme"))))))))))
225 (defun variant-merge-controls (&optional (literal (lambda (themes) (variant-xml "V" themes))) (prefix "variant-merge:") verify)
226 (dolist (target '("parent" "variant-theme" "name-theme"))
227 (dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity"))
228 (let* ((label (format nil "~A~A:~A" prefix target kind)) (old-names nil) (old-variants nil) (old-themes nil)
229 (first-v (funcall literal '("extra"))) (second-v first-v) (body nil) (*name-controls* nil))
230 (labels ((topic-source (id locator content)
231 (format nil "<topic id='~A'>~A~A</topic>" id
232 (if (equal kind "itemIdentity") "" (format nil "<~A href='~A'/>" kind locator)) content))
233 (bridge (a b)
234 (format nil "<topic id='bridge'><~A href='~A'/><~A href='~A'/></topic>" kind
235 (if (equal kind "itemIdentity") (concatenate 'string "#" a) "urn:one") kind
236 (if (equal kind "itemIdentity") (concatenate 'string "#" b) "urn:two"))))
237 (setf body
238 (cond
239 ((equal target "parent")
240 (concatenate 'string
241 (topic-source "a" "urn:one" (variant-name-xml '("base") first-v "urn:n1"))
242 (topic-source "b" "urn:two" (variant-name-xml '("base") second-v "urn:n2")) (bridge "a" "b")))
243 ((equal target "variant-theme")
244 (concatenate 'string
245 (format nil "<topic id='a'>~A</topic>" (variant-name-xml '("base")
246 (concatenate 'string (funcall literal '("x")) (funcall literal '("y"))) "urn:n1"))
247 (topic-source "x" "urn:one" "") (topic-source "y" "urn:two" "") (bridge "x" "y")))
248 (t
249 (concatenate 'string (format nil "<topic id='a'>~A~A</topic>"
250 (variant-name-xml '("x") first-v "urn:n1") (variant-name-xml '("y") second-v "urn:n2"))
251 (topic-source "x" "urn:one" "") (topic-source "y" "urn:two" "") (bridge "x" "y")))))
252 (staged-name-control label body
253 (lambda (map step)
254 (if (equal target "parent")
255 (when (<= step 2)
256 (let ((name (first (topic-names (by-id map (if (= step 1) "a" "b"))))))
257 (push name old-names) (push (first (topic-name-variants name)) old-variants)))
258 (when (= step 1)
259 (setf old-names (topic-names (by-id map "a")))
260 (setf old-variants (loop for name in old-names append (topic-name-variants name)))
261 (setf old-themes (list (by-id map "x") (by-id map "y")))))
262 (when (= step (if (equal target "parent") 2 3))
263 (check (concatenate 'string label ":distinct-before")
264 (and (= 2 (length old-variants)) (not (eq (first old-variants) (second old-variants)))
265 (every (lambda (v) (eq v (canonical-variant v))) old-variants)))))
266 (lambda (map)
267 (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (topic-name-variants name)))
268 (before (variant-state map))
269 (themes (cond ((equal target "parent") (list (by-id map "base") (by-id map "extra")))
270 ((equal target "variant-theme") (list (by-id map "base") (by-id map "bridge")))
271 (t (list (by-id map "bridge") (by-id map "extra"))))))
272 (check (concatenate 'string label ":canonical-parent-name-variant-handles")
273 (and (= 1 (length (topic-names owner))) (= 1 (length (all-variants map)))
274 (every (lambda (old) (eq name (canonical-topic-name old))) old-names)
275 (every (lambda (old) (and (eq v (canonical-variant old)) (eq name (variant-parent old))
276 (scope-eq-p (variant-scope old) themes))) old-variants)
277 (if (equal target "parent") (eq owner (by-id map "b"))
278 (every (lambda (old) (eq (by-id map "bridge") (canonical-topic old))) old-themes))
279 (same-identifiers-p (item-identifiers name)
280 (if (equal target "variant-theme") '("urn:n1") '("urn:n1" "urn:n2")))
281 (variants-current-p map)))
282 (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
283 (when verify (funcall verify label map))
284 (check (concatenate 'string label ":cxtm-pure-retains-handles") (equal before (variant-state map)))
285 (push (topic-maps.test-foundation::fields "label" label "kind" "staged-native-reader-control"
286 "sourceText" (document body) "model" (variant-model-data map)) *variant-controls*)))))))))
287
288 (defun variant-scope-error-controls ()
289 (dolist (row '(("equal" ("base") ("base")) ("explicit-subset" ("base" "s") ("base"))
290 ("duplicate-adds-no-theme" ("base") ("base" "base"))))
291 (variant-error-control (first row)
292 (format nil "<topic id='a'>~A</topic>" (variant-name-xml (second row) (variant-xml "V" (third row))))
293 'variant-error :invalid-variant-scope))
294 (variant-error-control "SI-alias-adds-no-theme"
295 "<topic id='base'><subjectIdentifier href='https://themes.invalid/map#base'/></topic><topic id='a'><name><scope><topicRef href='#base'/></scope><value>N</value><variant><scope><topicRef href='https://themes.invalid/map#base'/></scope><resourceData>V</resourceData></variant></name></topic>"
296 'variant-error :invalid-variant-scope)
297 (variant-error-control "forward-merge-collapses-proper-scope"
298 (format nil "<topic id='a'>~A</topic><topic id='base'><itemIdentity href='#extra'/></topic>"
299 (variant-name-xml '("base") (variant-xml "V" '("extra")))) 'variant-error :variant-scope-collapse)
300 (variant-error-control "topicRef-conflicts-with-name-II"
301 "<topic id='a'><name><itemIdentity href='#s'/><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant></name></topic>"
302 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier)
303 (dolist (kind '(:subject-identifier :subject-locator :item-identifier))
304 (let* ((element (ecase kind (:subject-identifier "subjectIdentifier") (:subject-locator "subjectLocator") (:item-identifier "itemIdentity")))
305 (body (format nil "<topic id='base'><~A href='urn:base'/></topic><topic id='extra'><~A href='urn:extra'/></topic><topic id='a'>~A</topic>"
306 element element (variant-name-xml '("base") (variant-xml "V" '("extra")))))
307 (map (read-body body)) (v (first (all-variants map))) (before (variant-state map))
308 (c (condition-from (lambda () (add-topic-identifier (by-id map "extra") kind "urn:base")))))
309 (check (format nil "variant-collapse:~A:specific-atomic-retained" kind)
310 (and (eq 'variant-error (type-of c)) (eq :variant-scope-collapse (variant-error-code c))
311 (equal before (variant-state map)) (eq v (canonical-variant v)) (variants-current-p map)))
312 (push (topic-maps.test-foundation::fields "label" (format nil "collapse-~A" kind) "kind" "native-TMDM-collapse-atomicity"
313 "sourceText" (document body) "condition" (negative-condition-data c) "modelUnchanged" :true
314 "model" (variant-model-data map)) *variant-controls*))))
315
316 (defun variant-syntax-controls ()
317 (dolist (row '(("empty-variant" "<variant/>" :missing-variant-scope)
318 ("missing-scope" "<variant><resourceData>V</resourceData></variant>" :missing-variant-scope)
319 ("missing-resource" "<variant><scope><topicRef href='#s'/></scope></variant>" :missing-variant-resource)
320 ("empty-scope" "<variant><scope/><resourceData>V</resourceData></variant>" :empty-scope)
321 ("scope-wrong-child" "<variant><scope><value>N</value></scope><resourceData>V</resourceData></variant>" :unexpected-element)
322 ("scope-text" "<variant><scope>bad</scope><resourceData>V</resourceData></variant>" :unexpected-text)
323 ("multiple-scopes" "<variant><scope><topicRef href='#s'/></scope><scope><topicRef href='#t'/></scope><resourceData>V</resourceData></variant>" :duplicate-variant-scope)
324 ("scope-after-resource" "<variant><scope><topicRef href='#s'/></scope><resourceData>V</resourceData><scope><topicRef href='#t'/></scope></variant>" :element-order)
325 ("multiple-resources" "<variant><scope><topicRef href='#s'/></scope><resourceData>A</resourceData><resourceData>B</resourceData></variant>" :variant-resource-cardinality)
326 ("mixed-resources" "<variant><scope><topicRef href='#s'/></scope><resourceData>A</resourceData><resourceRef href='urn:x'/></variant>" :variant-resource-cardinality)
327 ("variant-attribute" "<variant other='x'><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant>" :unexpected-attribute)
328 ("data-attribute" "<variant><scope><topicRef href='#s'/></scope><resourceData other='x'>V</resourceData></variant>" :unexpected-attribute)
329 ("XTM-markup-in-data" "<variant><scope><topicRef href='#s'/></scope><resourceData><value>V</value></resourceData></variant>" :unexpected-child)
330 ("unknown-child" "<variant><other/><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant>" :unexpected-element)
331 ("text-in-variant" "<variant>bad<scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant>" :unexpected-text)
332 ("wrong-namespace" "<variant xmlns='urn:other'><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant>" :invalid-namespace)
333 ("bad-href" "<variant><scope><topicRef href='#2bad'/></scope><resourceData>V</resourceData></variant>" :invalid-topic-ref)
334 ("resourceRef-missing-href" "<variant><scope><topicRef href='#s'/></scope><resourceRef/></variant>" :missing-attribute)))
335 (variant-error-control (first row) (format nil "<topic id='a'><name><value>N</value>~A</name></topic>" (second row)) 'xtm-rejection (third row)))
336 (dolist (row '(("resourceData-anyType" "<variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant>" :resource-resolver-required)
337 ("reified-itemIdentity-excluded-datatype" "<variant reifier='#r'><itemIdentity href='urn:v'/><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant>" :resource-resolver-required)
338 ("reifier-excluded-datatype" "<variant reifier='#r'><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant>" :resource-resolver-required)
339 ("datatype" "<variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>V</resourceData></variant>" :resource-resolver-required)
340 ("embedded-XML" "<variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'><b xmlns='urn:other'>V</b></resourceData></variant>" :resource-resolver-required)))
341 (variant-error-control (first row) (format nil "<topic id='a'><name><value>N</value>~A</name></topic>" (second row)) 'xtm-unsupported (third row))))
342
343 (defun variant-tests ()
344 (setf *variant-controls* nil)
345 (variant-text-controls) (variant-merge-controls) (variant-scope-error-controls) (variant-syntax-controls)
346 (setf *variant-controls* (nreverse *variant-controls*)))
347
348 (defun variant-merge-original-model-checks (map raw)
349 (let* ((owner (by-id map "topic")) (other (by-id map "topic2")) (theme (by-id map "theme"))
350 (type (by-si map (default-name-type-identifier)))
351 (names (topic-names owner)) (first-name (find "Topic" names :key #'topic-name-value :test #'equal))
352 (second-name (find "Topic 2" names :key #'topic-name-value :test #'equal))
353 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
354 (source-topics (topic-maps.xtm::elements root)))
355 (check "variant-merge-original:source-shared-item-identifier-and-values"
356 (and (= 2 (length source-topics))
357 (equal '("topic" "topic2") (mapcar (lambda (node) (topic-maps.xtm::attr node "id")) source-topics))
358 (every (lambda (node) (equal "http://example.org/#topic"
359 (topic-maps.xtm::attr (first (topic-maps.xtm::elements node)) "href"))) source-topics)))
360 (check "variant-merge-original:counts-and-canonical-topic-eq"
361 (and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map))
362 (eq owner other) (eq owner (canonical-topic owner))
363 (= 2 (length names)) (= 2 (length (all-variants map))) (variants-current-p map)))
364 (check "variant-merge-original:exact-identifier-sets"
365 (and (same-identifiers-p (item-identifiers owner)
366 (list (concatenate 'string *document* "#topic") (concatenate 'string *document* "#topic2") "http://example.org/#topic"))
367 (null (topic-subject-identifiers owner)) (null (topic-subject-locators owner))
368 (equal (list (concatenate 'string *document* "#theme")) (item-identifiers theme))
369 (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme))
370 (null (item-identifiers type)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type))
371 (null (topic-subject-locators type)) (null (item-identifiers map))
372 (null (topic-names type)) (null (topic-names theme))
373 (topic-maps.tmdm::validate-map-item-identifiers map)))
374 (check "variant-merge-original:two-distinct-names-with-canonical-parent-and-type"
375 (and first-name second-name (not (eq first-name second-name))
376 (every (lambda (name) (and (eq name (canonical-topic-name name)) (eq owner (topic-name-parent name))
377 (eq type (topic-name-type name)) (null (topic-name-scope name))
378 (null (item-identifiers name)) (null (item-reifier name)))) names)))
379 (dolist (row '(("Topic" "TOPIC!") ("Topic 2" "TOPIC2!")))
380 (let* ((name (find (first row) names :key #'topic-name-value :test #'equal)) (vs (topic-name-variants name)) (v (first vs)))
381 (check (concatenate 'string "variant-merge-original:variant-parent-value-datatype-scope:" (first row))
382 (and (= 1 (length vs)) (eq v (canonical-variant v)) (eq name (variant-parent v))
383 (equal (second row) (variant-value v)) (equal (string-datatype) (variant-datatype v))
384 (scope-eq-p (variant-scope v) (list theme)) (null (item-identifiers v)) (null (item-reifier v))))))
385 (check "variant-merge-original:variants-stay-distinct"
386 (not (eq (first (topic-name-variants first-name)) (first (topic-name-variants second-name)))))))
387
388 (defun variant-merge-original-retained-handles (raw expected-map expected-cxtm)
389 ;; Same qualified source tree and reader operations as STAGED-NAME-CONTROL,
390 ;; using the full unchanged original octets, including their document root.
391 (let* ((topic-maps.xtm::*document-iri* *document*)
392 (topic-maps.xtm::*source-key* "xtm2/in/merge-itemid-with-variants.xtm")
393 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
394 (map (make-topic-map)) (old-owner nil) (old-name nil) (old-variant nil) (old-theme nil) (stages nil))
395 (topic-maps.xtm::validate-source root)
396 (loop for node in (topic-maps.xtm::elements root) for step from 1 do
397 (topic-maps.xtm::read-topic node map)
398 (when (= step 1)
399 (setf old-owner (by-id map "topic") old-name (first (topic-names old-owner))
400 old-variant (first (topic-name-variants old-name)) old-theme (by-id map "theme"))
401 (check "variant-merge-original:retained-first-topic-before-merge"
402 (and (= 1 (length (topic-names old-owner))) (= 1 (length (all-variants map)))
403 (eq old-owner (canonical-topic old-owner)) (eq old-name (variant-parent old-variant)))))
404 (push (topic-maps.test-foundation::fields "afterTopic" step "model" (variant-model-data map)) stages))
405 (let* ((owner (by-id map "topic2")) (name (find "Topic" (topic-names owner) :key #'topic-name-value :test #'equal))
406 (v (first (topic-name-variants name))) (before (variant-state map)))
407 (check "variant-merge-original:retained-topic-name-variant-theme-eq-after-merge"
408 (and (not (eq old-owner owner)) (eq owner (canonical-topic old-owner)) (eq owner (by-id map "topic"))
409 (eq name old-name) (eq name (canonical-topic-name old-name)) (eq owner (topic-name-parent old-name))
410 (eq v old-variant) (eq v (canonical-variant old-variant)) (eq name (variant-parent old-variant))
411 (eq old-theme (by-id map "theme")) (scope-eq-p (variant-scope old-variant) (list old-theme))
412 (variants-current-p map)))
413 (check "variant-merge-original:staged-original-equals-public-api-model-and-bytes"
414 (and (equalp (variant-model-data expected-map) (variant-model-data map))
415 (equalp expected-cxtm (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
416 (equal before (variant-state map)))))
417 (coerce (nreverse stages) 'vector)))
418
419 (defun variant-merge-original-contract ()
420 (let* ((id "xtm2/in/merge-itemid-with-variants.xtm")
421 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
422 (source (corpus-path id)) (baseline (corpus-path "xtm2/baseline/merge-itemid-with-variants.xtm.cxtm"))
423 (raw (file-bytes source)) (copy (copy-seq raw)) (expected (file-bytes baseline))
424 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
425 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
426 :clauses '("XTM 2.0 4.5,4.6,4.10,4.12,4.13,4.16" "TMDM 5.3.5,5.4,5.5,6.2" "CXTM 3,4")))
427 (map nil) (actual nil) (c nil) (before nil) (stages nil))
428 (setf *cases* (append *cases* (list case)))
429 (handler-case
430 (progn
431 (setf map (deserialize-octets raw :document-iri *document* :source-key id) before (variant-state map)
432 actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
433 (variant-merge-original-model-checks map raw)
434 (setf stages (variant-merge-original-retained-handles raw map actual)))
435 (error (condition) (setf c condition)))
436 (record-original-result case actual expected c)
437 (setf *variant-merge-outputs* (list (topic-maps.test-foundation::fields
438 "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw)
439 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
440 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
441 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
442 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
443 "firstDifferentByte" (when actual (mismatch actual expected)) "condition" (negative-condition-data c)
444 "model" (when map (variant-model-data map)) "originalReaderStages" stages
445 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case))))))
446 (when c (error c))
447 (check "variant-merge-original:byte-exact" (equalp actual expected))
448 (check "variant-merge-original:source-preserved" (equalp raw copy))
449 (check "variant-merge-original:pure" (equal before (variant-state map)))
450 (check "variant-merge-original:deterministic" (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
451 (check "variant-merge-original:repeat-pure" (equal before (variant-state map)))
452 (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:variant-merge-provenance")))
453 (check "variant-merge-original:source-key-only-provenance"
454 (and (equalp (variant-model-data map) (variant-model-data other))
455 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))
456 (check "variant-merge-original:57-unique-case-records"
457 (and (= 57 (length *cases*)) (= 49 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
458 (= 57 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))))