]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-scopes.lisp
Document XTM normative coverage and remaining qualification gaps
[topic-maps.git] / tests / ctm-scopes.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-scope-checks* nil)
4 (defvar *scope-observations* nil)
5 (defvar *scope-order-witness* nil)
6 (defun scope-checks () *last-scope-checks*)
7 (defun scope-of (item)
8 (etypecase item
9 (topic-maps.tmdm:topic-name (topic-maps.tmdm:topic-name-scope item))
10 (topic-maps.tmdm:occurrence (topic-maps.tmdm:occurrence-scope item))
11 (topic-maps.tmdm:association (topic-maps.tmdm:association-scope item))))
12 (defun raw-scope (item)
13 (etypecase item
14 (topic-maps.tmdm:topic-name (topic-maps.tmdm::%topic-name-scope item))
15 (topic-maps.tmdm:occurrence (topic-maps.tmdm::%occurrence-scope item))
16 (topic-maps.tmdm:association (topic-maps.tmdm::%association-scope item))))
17 (defun (setf raw-scope) (value item)
18 (etypecase item
19 (topic-maps.tmdm:topic-name (setf (topic-maps.tmdm::%topic-name-scope item) value))
20 (topic-maps.tmdm:occurrence (setf (topic-maps.tmdm::%occurrence-scope item) value))
21 (topic-maps.tmdm:association (setf (topic-maps.tmdm::%association-scope item) value))))
22 (defun current-scoped-item (item)
23 (etypecase item
24 (topic-maps.tmdm:topic-name (topic-maps.tmdm:canonical-topic-name item))
25 (topic-maps.tmdm:occurrence (topic-maps.tmdm:canonical-occurrence item))
26 (topic-maps.tmdm:association (topic-maps.tmdm:canonical-association item))))
27 (defun scoped-items (map)
28 (append (mapcan #'topic-names (topic-map-topics map)) (all-occurrences map)
29 (topic-maps.tmdm:topic-map-associations map)))
30 (defun new-scoped-item (kind map owner type themes)
31 (ecase kind
32 (:name (topic-maps.tmdm:add-name owner "Name" :type type :scope themes))
33 (:occurrence (topic-maps.tmdm:add-occurrence owner type "Value" :scope themes))
34 (:association (topic-maps.tmdm:make-association map type (list (list type owner)) :scope themes))))
35 (defun scope-ref-invariants (map)
36 (every (lambda (item)
37 (let ((scope (raw-scope item)))
38 (and (= (length scope) (length (remove-duplicates scope :test #'eq)))
39 (every (lambda (theme)
40 (and (eq theme (topic-maps.tmdm:canonical-topic theme))
41 (member theme (topic-map-topics map) :test #'eq)
42 (eq map (topic-parent theme)))) scope)))) (scoped-items map)))
43 (defun scope-handle-state (handles)
44 (mapcar (lambda (item) (list item (current-scoped-item item) (copy-list (raw-scope item))
45 (copy-list (raw-scope (current-scoped-item item))))) handles))
46 (defun scope-observation (map)
47 (let ((topics (topic-map-topics map)))
48 (labels ((ordinal (topic) (1+ (position topic topics :test #'eq)))
49 (item (object kind parent type &optional value datatype roles)
50 (topic-maps.test-foundation::fields
51 "kind" kind "parent" parent "typeTopicOrdinal" (ordinal type)
52 "value" value "datatype" datatype
53 "scopeTopicOrdinals" (topic-maps.test-foundation::json-array (mapcar #'ordinal (scope-of object)))
54 "roles" (topic-maps.test-foundation::json-array roles))))
55 (topic-maps.test-foundation::fields
56 "topics" (topic-maps.test-foundation::json-array
57 (loop for topic in topics collect
58 (topic-maps.test-foundation::fields "ordinal" (ordinal topic)
59 "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers topic))
60 "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers topic))
61 "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators topic)))))
62 "items" (topic-maps.test-foundation::json-array
63 (append
64 (loop for owner in topics append
65 (append
66 (loop for n in (topic-names owner) collect
67 (item n "name" (ordinal (topic-name-parent n)) (topic-name-type n) (topic-name-value n)))
68 (loop for o in (topic-maps.tmdm:topic-occurrences owner) collect
69 (item o "occurrence" (ordinal (topic-maps.tmdm:occurrence-parent o))
70 (topic-maps.tmdm:occurrence-type o) (topic-maps.tmdm:occurrence-value o)
71 (topic-maps.tmdm:occurrence-datatype o)))))
72 (loop for a in (topic-maps.tmdm:topic-map-associations map) collect
73 (item a "association" (if (eq map (topic-maps.tmdm:association-parent a)) "map" "wrong")
74 (topic-maps.tmdm:association-type a) nil nil
75 (loop for r in (topic-maps.tmdm:association-roles a) collect
76 (topic-maps.test-foundation::fields
77 "parentIsAssociation" (if (eq a (topic-maps.tmdm:association-role-parent r)) :true :false)
78 "playerTopicOrdinal" (ordinal (topic-maps.tmdm:association-role-player r))
79 "typeTopicOrdinal" (ordinal (topic-maps.tmdm:association-role-type r))))))))))))
80
81 (defun scope-original-cases ()
82 (setf *scope-observations* nil)
83 (dolist (file '("name-scope" "occurrence-scope" "association-scope"))
84 (let* ((id (format nil "ctm/in/~A.ctm" file))
85 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
86 (doc (topic-maps.test-foundation:test-case-document-iri case))
87 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
88 (item (first (scoped-items map)))
89 (themes (list (local-topic map "theme1" doc) (local-topic map "theme2" doc)))
90 (before (map-state map))
91 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
92 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
93 (difference (mismatch actual expected)))
94 (check (concatenate 'string id ":theme-object-identity")
95 (and (every #'identity themes) (= 2 (length (scope-of item)))
96 (every (lambda (theme) (member theme (scope-of item) :test #'eq)) themes)))
97 (check (concatenate 'string id ":required-items-only")
98 (and (= (if (search "association" file) 5 4) (length (topic-map-topics map)))
99 (= 1 (length (scoped-items map)))))
100 (check (concatenate 'string id ":raw-scope-references") (scope-ref-invariants map))
101 (check (concatenate 'string id ":parent-type-player-identity")
102 (cond ((search "association" file)
103 (let ((role (first (topic-maps.tmdm:association-roles item))))
104 (and (eq map (topic-maps.tmdm:association-parent item))
105 (eq (local-topic map "assoctype" doc) (topic-maps.tmdm:association-type item))
106 (eq item (topic-maps.tmdm:association-role-parent role))
107 (eq (local-topic map "roletype" doc) (topic-maps.tmdm:association-role-type role))
108 (eq (local-topic map "topic" doc) (topic-maps.tmdm:association-role-player role)))))
109 ((search "occurrence" file)
110 (and (eq (local-topic map "topic" doc) (topic-maps.tmdm:occurrence-parent item))
111 (eq (local-topic map "occtype" doc) (topic-maps.tmdm:occurrence-type item))))
112 (t (and (eq (local-topic map "topic" doc) (topic-name-parent item))
113 (equal (list (default-name-type-identifier)) (topic-subject-identifiers (topic-name-type item)))))))
114 (check (concatenate 'string id ":byte-exact-cxtm") (null difference))
115 (check (concatenate 'string id ":pure-canonicalization") (equal before (map-state map)))
116 (topic-maps.test-foundation:record-result case (if difference :cxtm-mismatch :cxtm-match))
117 (setf (topic-maps.test-foundation:test-case-clauses case)
118 '("CTM 3.3.7" "CTM 3.3.9" "CTM 3.10-3.13" "TMDM 4.3" "TMDM 5.3.3-5.7"
119 "TMDM 6.1-6.6" "CXTM 3.6" "CXTM 3.8-3.9" "CXTM 3.12" "CXTM 4.4-4.11"))
120 (setf *cxtm-results* (append *cxtm-results*
121 (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string
122 (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
123 :match (null difference) :bytes (length actual)))))
124 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc
125 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
126 "actualObjects" (scope-observation map)) *scope-observations*)
127 (format t "~&SCOPE-CXTM ~A: ~A (~D bytes)~%" id (topic-maps.test-foundation:test-case-status case) (length actual))))
128 (setf *scope-observations* (nreverse *scope-observations*)))
129
130 (defun scope-api-tests ()
131 (dolist (kind '(:name :occurrence :association))
132 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
133 (a (api-topic map "a")) (b (api-topic map "b")) (input (list a b a))
134 (empty (new-scoped-item kind map owner type nil))
135 (one (new-scoped-item kind map owner type (list a)))
136 (two (new-scoped-item kind map owner type input))
137 (duplicate (new-scoped-item kind map owner type (list b a b))))
138 (check (format nil "scope-api:~A:empty-single-multiple" kind)
139 (and (null (scope-of empty)) (= 1 (length (scope-of one))) (= 2 (length (scope-of two)))))
140 (check (format nil "scope-api:~A:equal-sets-merge" kind)
141 (eq (current-scoped-item two) (current-scoped-item duplicate)))
142 (check (format nil "scope-api:~A:unequal-scopes-distinct" kind)
143 (and (= 3 (length (scoped-items map))) (not (eq (current-scoped-item empty) (current-scoped-item one)))))
144 (setf (first input) owner)
145 (let ((output (scope-of two))) (setf (first output) type))
146 (check (format nil "scope-api:~A:defensive-copy" kind)
147 (and (member a (scope-of two) :test #'eq) (member b (scope-of two) :test #'eq)
148 (not (member owner (scope-of two) :test #'eq)) (not (member type (scope-of two) :test #'eq))))
149 (let* ((foreign-map (topic-maps.tmdm:make-topic-map)) (foreign (api-topic foreign-map "foreign"))
150 (cycle (list a)) (state (map-state map)))
151 (setf (cdr cycle) cycle)
152 (loop for bad in (list a (vector a) (cons a b) cycle (list "theme") (list a foreign))
153 for code in '(:invalid-scope :invalid-scope :invalid-scope :invalid-scope :invalid-theme :foreign-theme)
154 for index from 0 do
155 (let ((condition (handler-case (progn (new-scoped-item kind map owner type bad) nil)
156 (topic-maps.tmdm:scope-error (c) c))))
157 (check (format nil "scope-api:~A:invalid-argument:~D" kind index)
158 (and condition (eq code (topic-maps.tmdm:scope-error-code condition)) (equal state (map-state map)))))))
159 (check (format nil "scope-api:~A:canonical-reference-invariants" kind) (scope-ref-invariants map))))
160 ;; Invalid scope must not synthesize a default type before rejecting the call.
161 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (before (map-state map)))
162 (check "scope-api:default-name-invalid-before-any-mutation"
163 (and (handler-case (progn (topic-maps.tmdm:add-default-name owner "Bad" :scope '("not a Topic")) nil)
164 (topic-maps.tmdm:scope-error () t)) (equal before (map-state map)))))
165 ;; Existing identifier uniqueness must distinguish unequal Occurrence scopes.
166 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
167 (a (api-topic map "a")) (b (api-topic map "b")))
168 (topic-maps.tmdm:add-occurrence owner type "Value" :scope (list a) :item-identifiers '("urn:o"))
169 (let ((state (map-state map)))
170 (check "scope-api:occurrence-identifier-collision-before-mutation"
171 (and (eq :duplicate-item-identifier (topic-maps.tmdm:occurrence-error-code
172 (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence owner type "Value"
173 :scope (list b) :item-identifiers '("urn:o"))))))
174 (equal state (map-state map)))))))
175
176 (defun scope-merge-tests ()
177 ;; Merging two themes reduces {a,b} to {c}, making two formerly distinct items equal.
178 (dolist (kind '(:name :occurrence :association))
179 (dolist (reverse '(nil t))
180 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
181 (a (api-topic map "a")) (b (api-topic map "b")) (z (api-topic map "z"))
182 (one (new-scoped-item kind map owner type (list a)))
183 (two (new-scoped-item kind map owner type (list a b)))
184 (three (new-scoped-item kind map owner type (list a b z)))
185 (roles (when (eq kind :association)
186 (append (topic-maps.tmdm:association-roles one) (topic-maps.tmdm:association-roles two))))
187 (handles (list one two three))
188 (theme (if reverse (topic-maps.tmdm:merge-topics b a) (topic-maps.tmdm:merge-topics a b)))
189 (live (current-scoped-item one)))
190 (check (format nil "scope-merge:~A:~A:cardinality-two-to-one" kind reverse)
191 (and (equal (list theme) (scope-of one)) (equal (list theme) (raw-scope live))
192 (eq theme (topic-maps.tmdm:canonical-topic a)) (eq theme (topic-maps.tmdm:canonical-topic b))))
193 (check (format nil "scope-merge:~A:~A:duplicate-item-replacement" kind reverse)
194 (and (eq live (current-scoped-item two)) (not (eq live one)) (not (eq live two))
195 (= 2 (length (scoped-items map)))))
196 (check (format nil "scope-merge:~A:~A:cardinality-three-to-two" kind reverse)
197 (and (= 2 (length (scope-of three))) (member theme (scope-of three) :test #'eq)
198 (member z (scope-of three) :test #'eq) (not (eq live (current-scoped-item three)))))
199 (check (format nil "scope-merge:~A:~A:retained-roles" kind reverse)
200 (every (lambda (r) (let ((current (topic-maps.tmdm:canonical-association-role r)))
201 (and (eq live (topic-maps.tmdm:association-role-parent r))
202 (member current (topic-maps.tmdm:association-roles live) :test #'eq)))) roles))
203 (let ((added (new-scoped-item kind map owner type (list b a b))))
204 (check (format nil "scope-merge:~A:~A:old-theme-handles-on-new-items" kind reverse)
205 (eq (current-scoped-item added) (current-scoped-item one))))
206 (let ((state (map-state map)) (old (scope-handle-state handles))
207 (role-state (loop for r in roles collect (list r (topic-maps.tmdm::%association-role-forward r)
208 (topic-maps.tmdm::%association-role-parent r)))))
209 (cxtm map) (cxtm map)
210 (check (format nil "scope-merge:~A:~A:pure-including-retired-handles" kind reverse)
211 (and (equal state (map-state map)) (equal old (scope-handle-state handles))
212 (equal role-state (loop for r in roles collect (list r (topic-maps.tmdm::%association-role-forward r)
213 (topic-maps.tmdm::%association-role-parent r)))))))
214 (check (format nil "scope-merge:~A:~A:stored-reference-integrity" kind reverse) (scope-ref-invariants map)))))
215 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
216 (a (api-topic map "theme-a")) (b (api-topic map "theme-b"))
217 (one (topic-maps.tmdm:add-occurrence owner type "Value" :scope (list a) :item-identifiers '("urn:o:one")))
218 (two (topic-maps.tmdm:add-occurrence owner type "Value" :scope (list a b) :item-identifiers '("urn:o:two"))))
219 (topic-maps.tmdm:merge-topics a b)
220 (check "scope-merge:occurrence-identifiers-unioned-without-changing-category"
221 (and (eq (current-scoped-item one) (current-scoped-item two))
222 (equal '("urn:o:one" "urn:o:two")
223 (sort (topic-maps.tmdm:occurrence-item-identifiers one) #'string<))
224 (= 1 (length (all-occurrences map)))
225 (notany (lambda (topic) (member "urn:o:one" (topic-item-identifiers topic) :test #'equal))
226 (topic-map-topics map)))))
227 ;; A theme may simultaneously be Parent, Type, Role Type and Role Player.
228 (dolist (kind '(:name :occurrence :association))
229 (let* ((map (topic-maps.tmdm:make-topic-map)) (a (api-topic map "a")) (b (api-topic map "b"))
230 (one (new-scoped-item kind map a a (list a)))
231 (two (new-scoped-item kind map a a (list a b)))
232 (roles (when (eq kind :association)
233 (append (topic-maps.tmdm:association-roles one) (topic-maps.tmdm:association-roles two))))
234 (c (topic-maps.tmdm:merge-topics a b)) (live (current-scoped-item one)))
235 (check (format nil "scope-merge:~A:simultaneous-parent-type-player-theme" kind)
236 (and (eq live (current-scoped-item two)) (equal (list c) (raw-scope live))
237 (ecase kind
238 (:name (and (eq c (topic-name-parent one)) (eq c (topic-name-type one))))
239 (:occurrence (and (eq c (topic-maps.tmdm:occurrence-parent one))
240 (eq c (topic-maps.tmdm:occurrence-type one))))
241 (:association (and (eq c (topic-maps.tmdm:association-type one))
242 (every (lambda (r) (and (eq c (topic-maps.tmdm:association-role-type r))
243 (eq c (topic-maps.tmdm:association-role-player r))
244 (eq live (topic-maps.tmdm:association-role-parent r)))) roles))))))))
245 ;; Every permutation of three theme merges exercises transitive forwarding and set collapse.
246 (dolist (order (permutations '(0 1 2)))
247 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
248 (themes (loop for i below 3 collect (api-topic map (format nil "theme:~D" i))))
249 (items (loop for kind in '(:name :occurrence :association) collect (new-scoped-item kind map owner type themes))))
250 (topic-maps.tmdm:merge-topics (nth (first order) themes) (nth (second order) themes))
251 (topic-maps.tmdm:merge-topics (nth (second order) themes) (nth (third order) themes))
252 (check (format nil "scope-transitive:~S:all-live-references" order)
253 (and (scope-ref-invariants map)
254 (every (lambda (item) (equal (list (topic-maps.tmdm:canonical-topic (first themes))) (scope-of item))) items)))))
255 ;; Scope can alias Parent/Type; default-type interning may itself merge a theme.
256 (let* ((map (topic-maps.tmdm:make-topic-map))
257 (owner (topic-maps.tmdm:intern-topic map :item-identifier (default-name-type-identifier)))
258 (name (topic-maps.tmdm:add-name owner "Self" :scope (list owner))))
259 (check "scope-default-type-interning-canonicalizes-parent-type-theme"
260 (let ((c (topic-maps.tmdm:canonical-topic owner)))
261 (and (eq c (topic-name-parent name)) (eq c (topic-name-type name))
262 (equal (list c) (raw-scope name)) (scope-ref-invariants map))))))
263
264 (defun scope-reader-tests ()
265 (dolist (text '("a - \"N\"@theme." "a t:\"V\"@theme." "t(r: a)@theme"))
266 (check (concatenate 'string "scope-reader:adjacent-punctuation:" text)
267 (let* ((map (read-text text)) (scope (scope-of (first (scoped-items map)))))
268 (equal (list (local-topic map "theme")) scope))))
269 (dolist (tail '("- \"N\"" "t:\"V\""))
270 (let* ((map (read-text (format nil "a ~A@b,a,b; ~A@a,b." tail tail))) (items (scoped-items map)))
271 (check (concatenate 'string "scope-reader:full-scope-before-item-creation:" tail)
272 (and (= 1 (length items)) (= 2 (length (scope-of (first items))))))))
273 (let ((map (read-text "t(r: a)@b,a,b t(r: a)@a,b")))
274 (check "scope-reader:association-full-scope-before-duplicate-removal" (= 1 (length (scoped-items map)))))
275 (let* ((map (read-text "t(r: a, s: b)@theme2,theme1"))
276 (a (first (topic-maps.tmdm:topic-map-associations map))))
277 (check "scope-reader:binary-association-role-and-scope-boundaries"
278 (and (= 2 (length (topic-maps.tmdm:association-roles a)))
279 (= 2 (length (scope-of a))) (scope-ref-invariants map)
280 (search "<scope>" (cxtm map)))))
281 (dolist (reference '("theme" "ex:theme" "<urn:theme>" "^<urn:theme>" "=<urn:theme>"))
282 (let* ((map (read-text (format nil "%prefix ex <urn:> a - t:\"N\"@~A." reference)))
283 (scope (scope-of (first (scoped-items map)))) (theme (first scope)))
284 (check (concatenate 'string "scope-reader:existing-reference-resolver:" reference)
285 (and (= 1 (length scope)) (eq map (topic-parent theme))
286 (cond ((char= (char reference 0) #\=) (equal '("urn:theme") (topic-subject-locators theme)))
287 ((char= (char reference 0) #\^) (equal '("urn:theme") (topic-item-identifiers theme)))
288 ((string= reference "theme") (eq theme (local-topic map "theme")))
289 (t (equal '("urn:theme") (topic-subject-identifiers theme))))))))
290 (let* ((map (read-text (format nil "a - \"N\"@#(x)# Ω,~%# theme comment~%β,Ω.")))
291 (scope (scope-of (first (scoped-items map)))))
292 (check "scope-reader:layout-unicode-and-set-identity"
293 (and (= 2 (length scope)) (member (local-topic map "Ω") scope :test #'eq)
294 (member (local-topic map "β") scope :test #'eq))))
295 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 1@theme; t: 1.0@theme; t: 2000-01-01@theme; t:\"01\"^^xsd:integer@theme."))
296 (items (all-occurrences map)))
297 (check "scope-reader:literal-and-datatype-boundaries-preserve-values"
298 (and (equal '("1" "1.0" "2000-01-01" "01") (mapcar #'topic-maps.tmdm:occurrence-value items))
299 (equal (list (topic-maps.tmdm:integer-datatype) (topic-maps.tmdm:decimal-datatype)
300 (topic-maps.tmdm:date-datatype) (topic-maps.tmdm:integer-datatype))
301 (mapcar #'topic-maps.tmdm:occurrence-datatype items))
302 (every (lambda (item) (equal (list (local-topic map "theme")) (scope-of item))) items))))
303 ;; '@' remains legal inside an IRI; never split locator content heuristically.
304 (let* ((map (read-text "a t:<https://user@example.invalid/v>@theme.")) (o (first (all-occurrences map))))
305 (check "scope-reader:iri-content-not-scope-punctuation"
306 (and (string= "https://user@example.invalid/v" (topic-maps.tmdm:occurrence-value o)) (= 1 (length (scope-of o))))))
307 (dolist (fixture '(("a - \"N\"@." :expected-scope-theme 8)
308 ("a - \"N\"@" :expected-scope-theme 8)
309 ("a - \"N\"@t,." :expected-scope-theme 10)
310 ("a - \"N\"@,t." :expected-scope-theme 8)
311 ("a - \"N\"@\"t\"." :expected-scope-theme 8)
312 ("a t:\"V\"@." :expected-scope-theme 8)
313 ("t(r: a)@" :expected-scope-theme 8)
314 ("a - \"N\"@x:t." :undefined-prefix 8)
315 ("a - \"N\"@~t." :expected-scope-theme 8)
316 ("a - \"N\"@t,~s." :expected-scope-theme 10)
317 ("a @t." :unexpected-token 2)
318 ("t(r: a@t)" :expected-role-separator 6)
319 ("a - \"N\"@t,,s." :expected-scope-theme 10)))
320 (destructuring-bind (text code position) fixture
321 (let ((e (ctm-condition text)))
322 (check (concatenate 'string "scope-diagnostic:" text)
323 (and (eq (type-of e) (if (eq code :undefined-prefix) 'undefined-prefix 'ctm-rejection))
324 (eq code (error-code e)) (= position (error-position e))
325 (= 1 (error-line e)) (= (1+ position) (error-column e)))))))
326 (let ((e (ctm-condition (format nil "a - \"N\"@t,~% ."))))
327 (check "scope-diagnostic:line-column-after-comma" (and (eq :expected-scope-theme (error-code e))
328 (= 2 (error-line e)) (= 2 (error-column e)))))
329 (dolist (text '("a - \"N\"@[-\"embedded\" ~r]." "a - \"N\"@? ~r." "a - \"N\"@t ~reifier."
330 "a - \"N\"@t (\"variant\"@s ~r)." "def tpl() a - \"N\"@t. end"))
331 (check (concatenate 'string "scope-reader:feature-boundary:" text) (cond ((search "def " text) (null (topic-map-topics (read-text text)))) ((find #\~ text) (null (ctm-condition text))) (t (typep (ctm-condition text) 'ctm-unsupported))))))
332
333 (defun scope-order-tests ()
334 (let ((reference nil)
335 (topic-maps.cxtm::*base-locator* "https://example.invalid/a/doc.ctm")
336 (topic-maps.cxtm::*locator-cache* (make-hash-table :test #'equal)))
337 ;; Four scopes: empty, {a}, {b}, {a,b}. Cardinality precedes member comparison.
338 (dolist (order (permutations '(0 1 2 3)))
339 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
340 (a (api-topic map "a")) (b (api-topic map "b")) (sets (list nil (list a) (list b) (list b a))) (handles nil))
341 (dolist (i order) (dolist (kind '(:name :occurrence :association))
342 (push (new-scoped-item kind map owner type (nth i sets)) handles)))
343 (let* ((before (map-state map)) (old (scope-handle-state handles)) (output (cxtm map)))
344 (when reference (check (format nil "scope-order:~S:construction-independent" order) (string= reference output)))
345 (setf reference output *scope-order-witness* output)
346 (check (format nil "scope-order:~S:scope-key-after-type-and-roles" order)
347 (and (zerop (topic-maps.cxtm::set-order (list a b) (list b a) #'topic-maps.cxtm::topic-order))
348 (minusp (topic-maps.cxtm::set-order (list b) (list a b) #'topic-maps.cxtm::topic-order))))
349 (check (format nil "scope-order:~S:pure-and-deterministic" order)
350 (and (string= output (cxtm map)) (equal before (map-state map)) (equal old (scope-handle-state handles))))
351 (loop for getter in (list #'topic-names #'topic-maps.tmdm:topic-occurrences)
352 for label in '(:names :occurrences) do
353 (check (format nil "scope-order:~S:~A" order label)
354 (equal '(0 1 1 2) (mapcar (lambda (i) (length (scope-of i)))
355 (topic-maps.cxtm::sorted (funcall getter owner)
356 (if (eq getter #'topic-names) #'topic-maps.cxtm::name-order #'topic-maps.cxtm::occurrence-order))))))
357 (check (format nil "scope-order:~S:association-rolePlayed-integrity" order)
358 (let ((ordered (topic-maps.cxtm::sorted (topic-maps.tmdm:topic-map-associations map) #'topic-maps.cxtm::association-order)))
359 (and (equal '(0 1 1 2) (mapcar (lambda (i) (length (scope-of i))) ordered))
360 (every (lambda (index) (search (format nil "association.~D.role.1" index) output)) '(1 2 3 4)))))))))
361 (dolist (kind '(:name :occurrence :association))
362 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (type (api-topic map "type"))
363 (a (api-topic map "a")) (old (api-topic map "old"))
364 (item (new-scoped-item kind map owner type (list a)))
365 (foreign (api-topic (topic-maps.tmdm:make-topic-map) "foreign")))
366 (topic-maps.tmdm:merge-topics a old)
367 (loop for fixture in (list (list (list old) :invalid-topic-reference)
368 (list (list foreign) :invalid-topic-reference)
369 (list (list (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm:canonical-topic a)) :invalid-set))
370 for index from 0 do
371 (let ((saved (raw-scope item)))
372 (unwind-protect
373 (progn (setf (raw-scope item) (first fixture))
374 (let ((state (map-state map)))
375 (check (format nil "scope-cxtm:~A:invalid-reference:~D:~A" kind index (second fixture))
376 (and (eq (second fixture) (topic-maps.cxtm:error-code (cxtm-condition (lambda () (cxtm map)))))
377 (equal state (map-state map))))))
378 (setf (raw-scope item) saved)))))))
379
380 (defun run-scope-tests ()
381 (run-typed-name-tests)
382 (let ((*checks* nil))
383 (scope-original-cases) (scope-api-tests) (scope-merge-tests) (scope-reader-tests) (scope-order-tests)
384 (check "scope:only-three-imported-transitions"
385 (equal '(:pass 67 :fail 2 :unsupported 0 :not-run 204)
386 (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses)))
387 (setf *last-scope-checks* (nreverse *checks*))
388 (format t "~&SCOPES-PASS: ~D checks; 3 original byte-exact comparisons; 67 PASS, 2 known FAIL, 204 NOT-RUN.~%"
389 (length *last-scope-checks*)) t))
390 (defun write-scope-report (path)
391 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
392 (topic-maps.test-foundation::emit-json-value
393 (topic-maps.test-foundation::fields "kind" "scope-evidence" "passed" (length *last-scope-checks*) "failed" 0
394 "checks" (topic-maps.test-foundation::json-array *last-scope-checks*)
395 "orderingWitnessCXTM" *scope-order-witness*
396 "cases" (topic-maps.test-foundation::json-array *scope-observations*)) out)
397 (terpri out)))