]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-name-scopes.lisp
test(xtm): register variant item-identity merge original
[topic-maps.git] / tests / xtm-name-scopes.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 (defparameter *scope-original-specs*
4 '(("name-scope" 3 "Emne" :default ("norwegian"))
5 ("name-scope-multiple" 4 "Emne" :default ("norwegian" "danish"))
6 ("name-scope-duplicate" 3 "Emne" :default ("norwegian"))
7 ("name-scope-duplicate-merged" 3 "Emne" :default ("no" "norwegian"))
8 ("name-type-scope" 3 "Emner" :plural ("norwegian"))))
9
10 (defun scope-eq-p (actual expected)
11 (let ((expected (remove-duplicates expected :test #'eq)))
12 (and (= (length actual) (length expected))
13 (every (lambda (theme) (member theme actual :test #'eq)) expected))))
14
15 (defun scope-original-model-checks (spec map)
16 (destructuring-bind (id count value type-key theme-ids) spec
17 (let* ((owner (by-id map "topic")) (names (topic-names owner)) (name (first names))
18 (type (if (eq type-key :default) (by-si map (default-name-type-identifier))
19 (find-if (lambda (topic) (member "http://psi.ontopia.net/xtm/basename/#plural"
20 (topic-item-identifiers topic) :test #'equal)) (topic-map-topics map))))
21 (themes (mapcar (lambda (id) (by-id map id)) theme-ids))
22 (label (concatenate 'string "scope-original:" id)))
23 (check (concatenate 'string label ":counts")
24 (and (= count (length (topic-map-topics map))) (null (topic-map-associations map))
25 (= 1 (loop for topic in (topic-map-topics map) sum (length (topic-names topic))))))
26 (check (concatenate 'string label ":parent-type-value-eq")
27 (and owner type (= 1 (length names)) (eq name (canonical-topic-name name))
28 (eq owner (topic-name-parent name)) (eq type (topic-name-type name))
29 (equal value (topic-name-value name))))
30 (check (concatenate 'string label ":canonical-theme-set-eq")
31 (and (every #'identity themes) (scope-eq-p (topic-name-scope name) themes) (names-current-p map)))
32 (check (concatenate 'string label ":no-excluded-properties")
33 (and (null (topic-name-variants name)) (null (item-identifiers name)) (null (item-reifier name))))
34 (check (concatenate 'string label ":exact-topic-identities")
35 (and (equal (list (concatenate 'string *document* "#topic")) (topic-item-identifiers owner))
36 (null (topic-subject-identifiers owner)) (null (topic-subject-locators owner))
37 (every (lambda (theme)
38 (and (null (topic-subject-identifiers theme)) (null (topic-subject-locators theme))
39 (same-identifiers-p (topic-item-identifiers theme)
40 (loop for id in theme-ids when (eq theme (by-id map id)) collect (concatenate 'string *document* "#" id))))) themes)
41 (if (eq type-key :default)
42 (and (null (topic-item-identifiers type))
43 (equal (list (default-name-type-identifier)) (topic-subject-identifiers type)))
44 (and (equal '("http://psi.ontopia.net/xtm/basename/#plural") (topic-item-identifiers type))
45 (null (topic-subject-identifiers type)))))))))
46
47 (defun scope-original-contracts ()
48 (setf *scope-outputs* nil)
49 (dolist (spec *scope-original-specs*)
50 (let* ((name (first spec)) (id (format nil "xtm2/in/~A.xtm" name))
51 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
52 (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
53 (raw (file-bytes source)) (copy (copy-seq raw)) (expected (file-bytes baseline))
54 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
55 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
56 :clauses '("XTM 2.0 4.2,4.10,4.11,4.13,4.14,4.20" "TMDM 5.3.3,5.4,6.2,6.3" "CXTM 3,4")))
57 (map nil) (actual nil) (c nil) (before nil))
58 (setf *cases* (append *cases* (list case)))
59 (handler-case (setf map (deserialize-octets raw :document-iri *document* :source-key id)
60 before (name-state map)
61 actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
62 (error (condition) (setf c condition)))
63 ;; A reader or model failure can never satisfy this positive contract.
64 (when (null c)
65 (handler-case (scope-original-model-checks spec map) (error (condition) (setf c condition))))
66 (record-original-result case actual expected c)
67 (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
68 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
69 "sourceText" (babel:octets-to-string raw :encoding :utf-8)
70 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
71 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
72 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
73 "firstDifferentByte" (when actual (mismatch actual expected))
74 "condition" (negative-condition-data c) "model" (when map (name-model-data map))
75 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *scope-outputs*)
76 (when c (error c))
77 (check (concatenate 'string "scope-original:" name ":byte-exact") (equalp actual expected))
78 (check (concatenate 'string "scope-original:" name ":source-preserved") (equalp raw copy))
79 (check (concatenate 'string "scope-original:" name ":pure") (equal before (name-state map)))
80 (check (concatenate 'string "scope-original:" name ":deterministic")
81 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
82 (check (concatenate 'string "scope-original:" name ":repeat-pure") (equal before (name-state map)))
83 (let ((other (deserialize-octets raw :document-iri *document* :source-key "different-name-provenance")))
84 (check (concatenate 'string "scope-original:" name ":source-key-only-provenance")
85 (and (equalp (name-model-data map) (name-model-data other))
86 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
87 (setf *scope-outputs* (nreverse *scope-outputs*))
88 (check "scope-originals:48-unique-case-records"
89 (and (= 48 (length *cases*)) (= 41 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
90 (= 48 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
91
92 ;; These wrappers keep all counterprobes in the existing harness and report
93 ;; format, while retaining the historical Name-controls collection separately.
94 (defun scope-control (label body verify)
95 (let ((*name-controls* nil))
96 (name-control (concatenate 'string "scope:" label) body verify)
97 (setf *scope-controls* (append *scope-controls* (reverse *name-controls*)))))
98 (defun scope-error-control (label body class code)
99 (let ((*name-controls* nil))
100 (name-error-control (concatenate 'string "scope:" label) body class code)
101 (setf *scope-controls* (append *scope-controls* (reverse *name-controls*)))))
102 (defun staged-scope-control (label body after-step verify)
103 (let ((*name-controls* nil))
104 (staged-name-control (concatenate 'string "scope:" label) body after-step verify)
105 (setf *scope-controls* (append *scope-controls* (reverse *name-controls*)))))
106
107 (defun scope-positive-controls ()
108 (scope-control "single-forward" "<topic id='a'><name><scope><topicRef href='#s'/></scope><value>N</value></name></topic><topic id='s'/>"
109 (lambda (map) (check "scope:single-forward-eq"
110 (scope-eq-p (topic-name-scope (first (topic-names (by-id map "a")))) (list (by-id map "s"))))))
111 (scope-control "set-order-and-duplicates"
112 "<topic id='a'><name><scope><topicRef href='#s'/><topicRef href='#t'/><topicRef href='#s'/></scope><value>N</value></name><name><scope><topicRef href='#t'/><topicRef href='#s'/></scope><value>N</value></name></topic>"
113 (lambda (map) (let ((names (topic-names (by-id map "a"))))
114 (check "scope:set-order-duplicate-name-collapse"
115 (and (= 1 (length names)) (scope-eq-p (topic-name-scope (first names)) (list (by-id map "s") (by-id map "t"))))))))
116 (scope-control "different-and-unconstrained"
117 "<topic id='a'><name><scope><topicRef href='#s'/></scope><value>N</value></name><name><scope><topicRef href='#t'/></scope><value>N</value></name><name><value>N</value></name></topic>"
118 (lambda (map) (let* ((names (topic-names (by-id map "a"))) (types (mapcar #'topic-name-type names)))
119 (check "scope:different-sets-do-not-merge"
120 (and (= 3 (length names)) (= 3 (length (remove-duplicates names :test #'eq)))
121 (every (lambda (type) (eq type (first types))) types)
122 (scope-eq-p (topic-name-scope (first names)) (list (by-id map "s")))
123 (scope-eq-p (topic-name-scope (second names)) (list (by-id map "t")))
124 (null (topic-name-scope (third names))))))))
125 (scope-control "type-theme-same"
126 "<topic id='a'><name><type><topicRef href='#s'/></type><scope><topicRef href='#s'/></scope><value>N</value></name><name><type><topicRef href='#s'/></type><value>N</value></name></topic>"
127 (lambda (map) (let ((names (topic-names (by-id map "a"))) (theme (by-id map "s")))
128 (check "scope:type-and-scope-separate-properties"
129 (and (= 2 (length names)) (eq theme (topic-name-type (first names)))
130 (eq theme (first (topic-name-scope (first names))))
131 (eq theme (topic-name-type (second names))) (null (topic-name-scope (second names))))))))
132 (dolist (kind '("subjectIdentifier" "itemIdentity"))
133 (scope-control (concatenate 'string "reference-hit:" kind)
134 (format nil "<topic id='s'><~A href='https://themes.invalid/map#theme'/></topic><topic id='a'><name><scope><topicRef href='https://themes.invalid/map#theme'/></scope><value>N</value></name></topic>" kind)
135 (lambda (map)
136 (let* ((theme (by-id map "s")) (scope (topic-name-scope (first (topic-names (by-id map "a"))))))
137 (check (concatenate 'string "scope:reference-hit-no-extra-II:" kind)
138 (and (eq theme (first scope)) (= 1 (length scope)) (= 3 (length (topic-map-topics map)))
139 (same-identifiers-p (topic-item-identifiers theme)
140 (append (list (concatenate 'string *document* "#s"))
141 (when (equal kind "itemIdentity") '("https://themes.invalid/map#theme"))))))))))
142 (scope-control "subject-locator-is-not-reference-hit"
143 "<topic id='s'><subjectLocator href='https://themes.invalid/map#theme'/></topic><topic id='a'><name><scope><topicRef href='https://themes.invalid/map#theme'/></scope><value>N</value></name></topic>"
144 (lambda (map) (let ((theme (first (topic-name-scope (first (topic-names (by-id map "a")))))))
145 (check "scope:SL-only-reference-creates-II-placeholder"
146 (and (not (eq theme (by-id map "s"))) (= 4 (length (topic-map-topics map)))
147 (equal '("https://themes.invalid/map#theme") (topic-item-identifiers theme)))))))
148 (scope-control "percent-utf8-external-no-fetch"
149 "<topic id='a'><name><scope><topicRef href='https://unfetched.invalid/map#th%C3%A8me'/></scope><value>N</value></name></topic>"
150 (lambda (map) (check "scope:percent-utf8-existing-IRI-path"
151 (equal '("https://unfetched.invalid/map#thème")
152 (topic-item-identifiers (first (topic-name-scope (first (topic-names (by-id map "a"))))))))))
153 (scope-control "owner-theme"
154 "<topic id='a'><name><scope><topicRef href='#a'/></scope><value>N</value></name></topic>"
155 (lambda (map) (check "scope:owner-can-be-theme-eq"
156 (eq (by-id map "a") (first (topic-name-scope (first (topic-names (by-id map "a")))))))))
157 (scope-control "default-type-is-theme"
158 "<topic id='s'><subjectIdentifier href='http://psi.topicmaps.org/iso13250/model/topic-name'/></topic><topic id='a'><name><scope><topicRef href='#s'/></scope><value>N</value></name></topic>"
159 (lambda (map) (let ((name (first (topic-names (by-id map "a")))))
160 (check "scope:default-type-theme-eq"
161 (and (= 2 (length (topic-map-topics map))) (eq (by-id map "s") (topic-name-type name))
162 (eq (topic-name-type name) (first (topic-name-scope name))))))))
163 (scope-control "parent-merge-scoped-names"
164 "<topic id='a'><name><scope><topicRef href='#s'/><topicRef href='#t'/></scope><value>N</value></name></topic><topic id='b'><name><scope><topicRef href='#t'/><topicRef href='#s'/></scope><value>N</value></name></topic><topic id='bridge'><itemIdentity href='#a'/><itemIdentity href='#b'/></topic>"
165 (lambda (map) (check "scope:parent-merge-set-equality"
166 (and (eq (by-id map "a") (by-id map "b")) (= 1 (length (topic-names (by-id map "a")))))))))
167
168 (defun scope-theme-merge-controls ()
169 (dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity"))
170 (let ((themes nil) (names nil) (duplicate-name nil) (label (concatenate 'string "theme-merge:" kind)))
171 (staged-scope-control label
172 (concatenate 'string
173 "<topic id='a'><name><scope><topicRef href='#s1'/></scope><value>N</value></name><name><scope><topicRef href='#s2'/></scope><value>N</value></name><name><scope><topicRef href='#s1'/><topicRef href='#s2'/><topicRef href='#s1'/></scope><value>D</value></name></topic>"
174 (if (equal kind "itemIdentity")
175 "<topic id='s1'/><topic id='s2'/><topic id='bridge'><itemIdentity href='#s1'/><itemIdentity href='#s2'/></topic>"
176 (format nil "<topic id='s1'><~A href='urn:one'/></topic><topic id='s2'><~A href='urn:two'/></topic><topic id='bridge'><~A href='urn:one'/><~A href='urn:two'/></topic>" kind kind kind kind)))
177 (lambda (map step)
178 (when (= step 1)
179 (setf themes (list (by-id map "s1") (by-id map "s2"))
180 names (subseq (topic-names (by-id map "a")) 0 2)
181 duplicate-name (third (topic-names (by-id map "a")))))
182 (when (= step 3)
183 (check (concatenate 'string "scope:" label ":distinct-before")
184 (and (= 3 (length (topic-names (by-id map "a"))))
185 (not (eq (canonical-topic (first themes)) (canonical-topic (second themes))))
186 (not (eq (first names) (second names))) (= 2 (length (topic-name-scope duplicate-name)))))))
187 (lambda (map)
188 (let* ((theme (by-id map "bridge")) (owner (by-id map "a"))
189 (name (find "N" (topic-names owner) :key #'topic-name-value :test #'equal))
190 (before (name-state map)))
191 (check (concatenate 'string "scope:" label ":canonical-themes-and-retained-names-eq")
192 (and (= 2 (length (topic-names owner)))
193 (every (lambda (old) (eq theme (canonical-topic old))) themes)
194 (every (lambda (old) (and (eq name (canonical-topic-name old))
195 (eq owner (topic-name-parent old)) (scope-eq-p (topic-name-scope old) (list theme)))) names)
196 (scope-eq-p (topic-name-scope duplicate-name) (list theme))))
197 (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
198 (check (concatenate 'string "scope:" label ":cxtm-pure-retained-handles")
199 (and (equal before (name-state map))
200 (every (lambda (old) (eq name (canonical-topic-name old))) names)))))))))
201
202 (defun scope-syntax-controls ()
203 (dolist (spec '(("empty" "<scope/>" :empty-scope)
204 ("whitespace-only" "<scope> </scope>" :empty-scope)
205 ("text-without-topicRef" "<scope>text</scope>" :unexpected-text)
206 ("wrong-child" "<scope><subjectIdentifier href='urn:s'/></scope>" :unexpected-element)
207 ("attribute" "<scope other='x'><topicRef href='#s'/></scope>" :unexpected-attribute)
208 ("multiple" "<scope><topicRef href='#s'/></scope><scope><topicRef href='#t'/></scope>" :duplicate-name-scope)
209 ("before-later-type" "<scope><topicRef href='#s'/></scope><type><topicRef href='#t'/></type>" :element-order)
210 ("missing-href" "<scope><topicRef/></scope>" :missing-attribute)
211 ("empty-href" "<scope><topicRef href=''/></scope>" :invalid-topic-ref)
212 ("no-fragment" "<scope><topicRef href='urn:s'/></scope>" :invalid-topic-ref)
213 ("invalid-fragment" "<scope><topicRef href='#2bad'/></scope>" :invalid-topic-ref)
214 ("bad-percent" "<scope><topicRef href='#s%GG'/></scope>" :invalid-percent-escape)
215 ("bad-utf8" "<scope><topicRef href='#s%FF'/></scope>" :invalid-percent-utf8)
216 ("bad-IRI" "<scope><topicRef href='#s t'/></scope>" :invalid-iri)
217 ("ref-attribute" "<scope><topicRef href='#s' other='x'/></scope>" :unexpected-attribute)
218 ("nested-ref" "<scope><topicRef href='#s'><value>N</value></topicRef></scope>" :unexpected-child)
219 ("namespace" "<scope xmlns='urn:foreign'><topicRef href='#s'/></scope>" :invalid-namespace)
220 ("ref-namespace" "<scope><topicRef xmlns='urn:foreign' href='#s'/></scope>" :invalid-namespace)))
221 (scope-error-control (first spec) (format nil "<topic id='a'><name>~A<value>N</value></name></topic>" (second spec)) 'xtm-rejection (third spec)))
222 (scope-error-control "after-value" "<topic id='a'><name><value>N</value><scope><topicRef href='#s'/></scope></name></topic>" 'xtm-rejection :element-order)
223 (dolist (spec '(("variant-resourceRef-stays-unsupported" "<name><scope><topicRef href='#s'/></scope><value>N</value><variant><scope><topicRef href='#v'/></scope><resourceRef href='urn:excluded'/></variant></name>" :unsupported-construct)
224 ("name-reifier-stays-unsupported" "<name reifier='#r'><scope><topicRef href='#s'/></scope><value>N</value></name>" :reification)))
225 (scope-error-control (first spec) (format nil "<topic id='a'>~A</topic>" (second spec)) 'xtm-unsupported (third spec)))
226 (scope-error-control "occurrence-stays-unsupported" "<topic id='a'><occurrence><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></occurrence></topic>" 'xtm-unsupported :unsupported-construct)
227 (scope-error-control "association-stays-unsupported" "<association><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope><role><type><topicRef href='#r'/></type><topicRef href='#a'/></role></association>" 'xtm-unsupported :unsupported-construct))
228
229 (defun scope-tests ()
230 (setf *scope-controls* nil)
231 (scope-positive-controls) (scope-theme-merge-controls) (scope-syntax-controls))