]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-identity.lisp
Integrate qualified XML anyType values into the XTM reader
[topic-maps.git] / tests / ctm-identity.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *checks* nil)
4 (defvar *last-checks* nil)
5 (defvar *observations* nil)
6 (defun observations () *observations*)
7 (defparameter *positive-ids*
8 '("ctm/in/simple.ctm" "ctm/in/subject-identifier.ctm" "ctm/in/subject-locator.ctm"
9 "ctm/in/item-identifier.ctm" "ctm/in/name.ctm"))
10
11 (defmacro check (label assertion)
12 `(progn (unless ,assertion (error "Semantic check failed: ~A" ,label))
13 (push ,label *checks*)))
14
15 (defun read-text (text &optional (document "https://example.invalid/a/doc.ctm"))
16 (deserialize-string text :document-iri document :source-name "independent-fixture"))
17 (defun first-topic (map) (first (topic-map-topics map)))
18 (defun rejection (text &optional (document "https://example.invalid/a/doc.ctm"))
19 ;; Catch only a real CTM rejection; unsupported and generic errors propagate.
20 (handler-case (progn (read-text text document) nil) (ctm-rejection (e) e)))
21 (defun unsupported-p (text)
22 (handler-case (progn (read-text text) nil) (ctm-unsupported () t)))
23
24 (defun verify-item-invariants (map)
25 (let ((topics (topic-map-topics map)))
26 (and
27 (every (lambda (topic)
28 (and (eq map (topic-parent topic))
29 (or (topic-item-identifiers topic) (topic-subject-identifiers topic)
30 (topic-subject-locators topic))
31 (every (lambda (name)
32 (and (typep name 'topic-maps.tmdm:topic-name)
33 (eq topic (topic-name-parent name))
34 (member (topic-name-type name) topics :test #'eq)
35 (stringp (topic-name-value name))))
36 (topic-names topic)))) topics)
37 (= (length topics) (length (remove-duplicates topics :test #'eq))))))
38
39 (defun verify-selected-map (id document map)
40 (let* ((topics (topic-map-topics map)) (topic (first topics))
41 (ii (topic-item-identifiers topic)) (si (topic-subject-identifiers topic))
42 (sl (topic-subject-locators topic))
43 (named (string= id "ctm/in/name.ctm")))
44 (check (concatenate 'string id ":item-invariants") (verify-item-invariants map))
45 (check (concatenate 'string id ":topic-count") (= (length topics) (if named 2 1)))
46 (cond
47 ((or named (string= id "ctm/in/simple.ctm"))
48 (check (concatenate 'string id ":local-item-identity")
49 (and (equal ii (list (concatenate 'string document "#topic"))) (null si) (null sl))))
50 ((string= id "ctm/in/subject-identifier.ctm")
51 (check "selected-subject-identifier-only"
52 (and (equal si '("http://psi.example.org/fake")) (null ii) (null sl))))
53 ((string= id "ctm/in/subject-locator.ctm")
54 (check "selected-subject-locator-only"
55 (and (equal sl '("http://www.example.org/resource")) (null ii) (null si))))
56 ((string= id "ctm/in/item-identifier.ctm")
57 (check "selected-explicit-item-identifier-only"
58 (and (equal ii '("http://www.example.org/#topic")) (null si) (null sl))))
59 (t (error "Unreviewed selected case ~A" id)))
60 (if named
61 (let* ((names (topic-names topic)) (name (first names)) (type (second topics)))
62 (check "selected-name-is-one-information-item" (= 1 (length names)))
63 (check "selected-name-value" (string= "Name" (topic-name-value name)))
64 (check "selected-name-type-is-the-second-topic" (eq type (topic-name-type name)))
65 (check "selected-default-name-type-identification"
66 (and (equal (topic-subject-identifiers type) (list (default-name-type-identifier)))
67 (null (topic-item-identifiers type)) (null (topic-subject-locators type))
68 (null (topic-names type)))))
69 (check (concatenate 'string id ":no-unwarranted-name-or-type-topic") (null (topic-names topic))))
70 map))
71
72 (defun exercise-corpus (&optional (corpus (topic-maps.test-foundation:discover-corpus)))
73 "Return original corpus plus separate real semantic observations; no CXTM output."
74 (let ((results nil))
75 (dolist (id *positive-ids*)
76 (let* ((case (first (topic-maps.test-foundation:select-cases corpus :id id)))
77 (document (topic-maps.test-foundation:test-case-document-iri case))
78 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
79 :document-iri document :source-name id)))
80 (verify-selected-map id document map)
81 ;; Do not change the imported CXTM equality contract or result status.
82 (check (concatenate 'string id ":canonical-contract-remains-not-run")
83 (and (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
84 (eq :not-run (topic-maps.test-foundation:test-case-status case))
85 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
86 (push (list :id id :document-iri document :status :pass :topic-map map) results)))
87 (let* ((id "ctm/invalid/prefix-undefined.ctm")
88 (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
89 (condition
90 (handler-case
91 (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
92 :document-iri (topic-maps.test-foundation:test-case-document-iri case)
93 :source-name id)
94 nil)
95 (undefined-prefix (e) e))))
96 (check "pinned-prefix-rejected-for-correct-reason"
97 (and condition (eq :undefined-prefix (error-code condition))))
98 (check "pinned-prefix-source-position"
99 (and (= 1 (error-position condition)) (= 2 (error-line condition)) (= 1 (error-column condition))))
100 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition)))
101 (check "only-pinned-rejection-is-imported-pass"
102 (equal (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)
103 '(:pass 1 :fail 0 :unsupported 0 :not-run 272))))
104 (values corpus (nreverse results))))
105
106 (defun run-tests ()
107 (let ((*checks* nil))
108 (multiple-value-bind (corpus observations) (exercise-corpus)
109 (declare (ignore corpus)) (setf *observations* observations))
110 (let* ((a (read-text "same." "https://example.invalid/a.ctm"))
111 (b (read-text "same." "https://example.invalid/b.ctm")))
112 (check "local-identifier-uses-explicit-document"
113 (equal (topic-item-identifiers (first-topic a)) '("https://example.invalid/a.ctm#same")))
114 (check "different-document-iris-different-item-identities"
115 (not (equal (topic-item-identifiers (first-topic a)) (topic-item-identifiers (first-topic b)))))
116 (check "local-identifiers-do-not-assert-subject-identity"
117 (and (not (eq (first-topic a) (first-topic b)))
118 (null (topic-subject-identifiers (first-topic a))) (null (topic-subject-locators (first-topic b))))))
119 (dolist (text '("same. same." "<https://example.invalid/id>. <https://example.invalid/id>."
120 "=<https://example.invalid/id>. =<https://example.invalid/id>."
121 "^<https://example.invalid/id>. ^<https://example.invalid/id>."))
122 (check (concatenate 'string "same-document-reference-reuses-topic:" text)
123 (= 1 (length (topic-map-topics (read-text text))))))
124 (let ((map (read-text "^<https://example.invalid/id>. <https://example.invalid/id>.")))
125 (check "tmdm-item-subject-identifier-overlap-reuses-topic" (= 1 (length (topic-map-topics map))))
126 (check "overlap-keeps-both-identity-properties"
127 (and (equal (topic-item-identifiers (first-topic map)) '("https://example.invalid/id"))
128 (equal (topic-subject-identifiers (first-topic map)) '("https://example.invalid/id"))
129 (null (topic-subject-locators (first-topic map))))))
130 (let ((map (read-text "^<https://example.invalid/id>. =<https://example.invalid/id>.")))
131 (check "item-identity-does-not-equal-subject-locator" (= 2 (length (topic-map-topics map)))))
132 (let ((map (read-text "<https://example.invalid/id>. =<https://example.invalid/id>.")))
133 (check "subject-identifier-does-not-equal-subject-locator" (= 2 (length (topic-map-topics map)))))
134 (let* ((map (read-text "a - \"One\"; - \"One\". b - \"Two\". a - \"Three\"."))
135 (a (first-topic map)) (b (third (topic-map-topics map))))
136 (check "one-shared-default-type-no-extra-items" (= 3 (length (topic-map-topics map))))
137 (check "equal-unscoped-name-items-coalesce" (= 2 (length (topic-names a))))
138 (check "different-name-values-remain-distinct"
139 (equal (mapcar #'topic-name-value (topic-names a)) '("One" "Three")))
140 (check "name-type-and-parent-object-identity"
141 (and (eq (topic-name-type (first (topic-names a))) (topic-name-type (first (topic-names b))))
142 (eq a (topic-name-parent (first (topic-names a)))))))
143 (let ((map (read-text "<http://psi.topicmaps.org/iso13250/model/topic-name>. a - \"Name\".")))
144 (check "existing-default-type-reused" (= 2 (length (topic-map-topics map))))
145 (check "type-is-existing-original-topic"
146 (eq (first-topic map) (topic-name-type (first (topic-names (second (topic-map-topics map))))))))
147 (let* ((map (read-text "ÄΩ - \"München🙂\".")) (topic (first-topic map)))
148 (check "unicode-identifier-preserved" (search "#ÄΩ" (first (topic-item-identifiers topic))))
149 (check "unicode-name-preserved" (string= "München🙂" (topic-name-value (first (topic-names topic))))))
150 (check "case-sensitive-local-identifiers" (= 2 (length (topic-map-topics (read-text "Topic. topic.")))))
151 (check "internal-dots-remain-in-identifiers"
152 (search "#one..two" (first (topic-item-identifiers (first-topic (read-text "one..two."))))))
153 (check "comment-layout-no-extra-topics"
154 (= 1 (length (topic-map-topics (read-text (format nil "# comment~%a. #( nested #( ignored )# )#"))))))
155 (check "plain-unwrapped-absolute-iri"
156 (equal (topic-subject-identifiers (first-topic (read-text "https://example.invalid/path.")))
157 '("https://example.invalid/path")))
158 (check "relative-iri-resolution"
159 (equal (topic-subject-identifiers (first-topic (read-text "<../object#s>.")))
160 '("https://example.invalid/object#s")))
161 (check "same-document-fragment-resolution"
162 (equal (topic-item-identifiers (first-topic (read-text "^<#topic>.")))
163 '("https://example.invalid/a/doc.ctm#topic")))
164 (check "percent-utf8-iri-decoding"
165 (equal (topic-subject-identifiers (first-topic (read-text "<https://example.invalid/%C3%A4>.")))
166 '("https://example.invalid/ä")))
167 (check "absolute-iri-case-preserved"
168 (equal (topic-subject-identifiers (first-topic (read-text "<HTTPS://Example.invalid/Case>.")))
169 '("HTTPS://Example.invalid/Case")))
170 (check "document-iri-required" (eq :invalid-document-iri (error-code (rejection "a." nil))))
171 (check "relative-document-iri-rejected" (eq :invalid-document-iri (error-code (rejection "a." "local.ctm"))))
172 (check "fragment-document-iri-rejected"
173 (eq :invalid-document-iri (error-code (rejection "a." "https://example.invalid/doc#x"))))
174 (check "missing-terminator-rejected" (eq :missing-period (error-code (rejection "a"))))
175 (check "missing-tail-separator-rejected" (eq :expected-separator (error-code (rejection "a - \"x\" - \"y\"."))))
176 (check "unbalanced-string-rejected" (eq :unterminated-string (error-code (rejection "a - \"oops"))))
177 (check "invalid-string-escape-rejected" (eq :invalid-string-escape (error-code (rejection "a - \"\\q\"."))))
178 (check "unicode-escapes-decoded"
179 (string= "Ä🙂" (topic-name-value (first (topic-names (first-topic (read-text "a - \"\\u00C4\\U01F642\".")))))))
180 (let ((condition (rejection (format nil "a.~% ex:fake."))))
181 (check "undefined-prefix-condition-class" (typep condition 'undefined-prefix))
182 (check "error-line-column" (and (= 2 (error-line condition)) (= 3 (error-column condition)))))
183 (check "identity-feature-boundary:a isa person."
184 (let ((map (read-text "a isa person.")))
185 (= 1 (length (topic-maps.tmdm:topic-map-associations map)))))
186 (dolist (text '("def tpl() a. end" "%include <other.ctm>"
187 "? - \"anonymous\" ~r." "a - \"\"\"triple\"\"\"."))
188 (check (if (search "%include" text) "identity:Include-requires-explicit-resolver"
189 (concatenate 'string "identity-feature-boundary:" text))
190 (cond ((search "def " text) (null (topic-map-topics (read-text text))))
191 ((search "%include" text)
192 (handler-case (progn (read-text text) nil)
193 (ctm-unsupported (c) (eq :resource-resolver-required (error-code c)))))
194 ((find #\~ text) (typep (read-text text) 'topic-maps.tmdm:topic-map))
195 (t (string= "triple" (topic-name-value (first (topic-names (first-topic (read-text text))))))))))
196 (check "scope-on-bare-topic-rejected"
197 (eq :unexpected-token (error-code (rejection "a @theme."))))
198 (check "scoped-name-now-deserialized"
199 (= 1 (length (topic-maps.tmdm:topic-name-scope
200 (first (topic-names (first-topic (read-text "a - \"name\" @theme."))))))))
201 (check "association-now-deserialized"
202 (= 1 (length (topic-maps.tmdm:topic-map-associations (read-text "relation(role: a)")))))
203 (check "empty-document-has-no-accidental-type-topic"
204 (null (topic-map-topics (read-text ""))))
205 (let ((map (deserialize-file (asdf:system-relative-pathname "topic-maps" "tests/fixtures/ctm/utf8-bom.ctm")
206 :document-iri "https://example.invalid/unicode.ctm")))
207 (check "utf8-file-and-bom-decoded"
208 (and (equal (topic-item-identifiers (first-topic map)) '("https://example.invalid/unicode.ctm#Ž"))
209 (string= "🙂" (topic-name-value (first (topic-names (first-topic map))))))))
210 (let ((condition (handler-case
211 (deserialize-file (asdf:system-relative-pathname "topic-maps" "tests/fixtures/ctm/invalid-utf8.ctm")
212 :document-iri "https://example.invalid/invalid.ctm")
213 (ctm-rejection (e) e))))
214 (check "malformed-utf8-is-specific-rejection" (eq :invalid-utf8 (error-code condition))))
215 (check "file-io-error-not-ctm-rejection"
216 (handler-case
217 (progn (deserialize-file (asdf:system-relative-pathname "topic-maps" "tests/fixtures/ctm/absent.ctm")
218 :document-iri "https://example.invalid/absent.ctm") nil)
219 (ctm-rejection () nil)
220 (file-error () t)))
221 (check "typed-name-explicitly-supported" (verify-item-invariants (read-text "a - fullname:\"Name\".")))
222 ;; CTM 3.11 requires a string, with or without an explicit type.
223 ;; CTM 3.3.1 permits layout between the type separator and that string.
224 (dolist (fixture '(("a - ." ctm-rejection :expected-name-value 4)
225 ("a -" ctm-rejection :expected-name-value 3)))
226 (destructuring-bind (text class code position) fixture
227 (let ((condition (handler-case (progn (read-text text) nil)
228 (ctm-error (e) e))))
229 (check (concatenate 'string "name-classification-class:" text)
230 (and condition (eq class (type-of condition))))
231 (check (concatenate 'string "name-classification-code:" text)
232 (eq code (error-code condition)))
233 (check (concatenate 'string "name-classification-position:" text)
234 (= position (error-position condition)))
235 (check (concatenate 'string "name-classification-source:" text)
236 (and (= 1 (error-line condition))
237 (= (1+ position) (error-column condition))
238 (eq :character (topic-maps.ctm:error-position-unit condition))
239 (string= "independent-fixture" (topic-maps.ctm:error-source condition))))
240 (check (concatenate 'string "name-classification-diagnostic:" text)
241 (search (if (eq class 'ctm-rejection) "quoted name value" "Typed names")
242 (princ-to-string condition))))))
243 (dolist (text '("a - type:\"Name\"." "a - type: \"Name\"."))
244 (let* ((map (read-text text)) (owner (first-topic map))
245 (name (first (topic-names owner))) (type (topic-name-type name)))
246 (check (concatenate 'string "name-classification-supported:" text) (typep name 'topic-maps.tmdm:topic-name))
247 (check (concatenate 'string "name-classification-value:" text) (string= "Name" (topic-name-value name)))
248 (check (concatenate 'string "name-classification-parent:" text) (eq owner (topic-name-parent name)))
249 (check (concatenate 'string "name-classification-type:" text)
250 (equal '("https://example.invalid/a/doc.ctm#type") (topic-item-identifiers type)))
251 (check (concatenate 'string "name-classification-no-extra-topic:" text) (= 2 (length (topic-map-topics map))))))
252 ;; A type-like token alone must not turn a malformed name into UNSUPPORTED.
253 (check "name-type-without-separator-is-rejected"
254 (eq :expected-name-type-separator (error-code (rejection "a - type \"Name\"."))))
255 (check "typed-name-without-value-is-rejected"
256 (eq :expected-name-value (error-code (rejection "a - type:."))))
257 (check "typed-name-layout-supported"
258 (verify-item-invariants (read-text (format nil "a - type:~%#( layout )# \"Name\"."))))
259 (check "additional-identity-now-deserialized"
260 (let* ((map (read-text "a <https://example.invalid/alias>.")) (topic (first-topic map)))
261 (and (= 1 (length (topic-map-topics map)))
262 (equal (topic-subject-identifiers topic) '("https://example.invalid/alias"))
263 (equal (topic-item-identifiers topic) '("https://example.invalid/a/doc.ctm#a")))))
264 (dolist (ending (list (string #\Return) (format nil "~C~C" #\Return #\Newline)))
265 (let ((condition (rejection (concatenate 'string "a." ending "ex:fake."))))
266 (check (if (= 1 (length ending)) "cr-error-line" "crlf-error-line")
267 (and (= 2 (error-line condition)) (= 1 (error-column condition))))))
268 (check "empty-delimited-iri-rejected" (eq :empty-iri (error-code (rejection "<>."))))
269 (check "invalid-relative-colon-rejected" (eq :invalid-iri (error-code (rejection "<1bad:thing>."))))
270 (check "multiple-fragments-rejected" (eq :invalid-iri (error-code (rejection "<https://example.invalid/#a#b>."))))
271 (dolist (pair '(("g:h" "g:h") ("g" "http://a/b/c/g")
272 ("./g" "http://a/b/c/g") ("../g" "http://a/b/g")
273 ("//g" "http://g") ("?y" "http://a/b/c/d;p?y")
274 ("#s" "http://a/b/c/d;p?q#s") ("." "http://a/b/c/")
275 (".." "http://a/b/") ("../../g" "http://a/g")
276 ("/./g" "http://a/g") ("g/../h" "http://a/b/c/h")))
277 (check (concatenate 'string "iri-resolution:" (first pair))
278 (equal (topic-subject-identifiers
279 (first-topic (read-text (format nil "<~A>." (first pair)) "http://a/b/c/d;p?q")))
280 (list (second pair)))))
281 (check "reader-does-not-depend-on-canonicalizer"
282 (equal (asdf:system-depends-on (asdf:find-system "topic-maps/ctm"))
283 '("topic-maps/tmdm")))
284 (setf *last-checks* (nreverse *checks*))
285 (format t "~&CTM-IDENTITY-SEMANTICS-PASS: ~D checks, 5 imported semantic cases, required prefix rejection; no CXTM equality passes.~%"
286 (length *last-checks*))
287 t))