]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-prefixes.lisp
Support XTM topic name reification (Slice 21M)
[topic-maps.git] / tests / ctm-prefixes.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defparameter *prefix-positive-ids*
4 '("occurrence-explicit-datatype" "occurrence-datatype-unknown"
5 "prefix-duplicate" "prefix-duplicate2" "prefix-multiline"))
6 (defvar *last-prefix-checks* nil)
7 (defvar *prefix-observations* nil)
8 (defun prefix-checks () *last-prefix-checks*)
9
10 (defun exercise-prefix-cases ()
11 (setf *prefix-observations* nil)
12 (dolist (name *prefix-positive-ids*)
13 (let* ((id (format nil "ctm/in/~A.ctm" name))
14 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
15 (doc (topic-maps.test-foundation:test-case-document-iri case))
16 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
17 :document-iri doc :source-name id))
18 (state (map-state map))
19 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
20 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
21 (different (mismatch actual expected))
22 (occurrence (first (all-occurrences map))))
23 (setf (topic-maps.test-foundation:test-case-clauses case)
24 '("CTM 3.2" "CTM 3.3.3-3.3.7" "CTM 3.4.1" "CTM 3.10" "CTM 3.16.1"
25 "TMDM 4.3-4.4" "TMDM 5.3" "TMDM 5.6" "CXTM 3.8" "CXTM 3.14"
26 "CXTM 3.16" "CXTM 3.18-3.20" "CXTM 4.3-4.6" "CXTM 4.9"))
27 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
28 :diagnostics (when different (list (format nil "First mismatch byte ~D, actual ~D bytes, expected ~D bytes."
29 different (length actual) (length expected)))))
30 (check (concatenate 'string name ":byte-exact") (null different))
31 (check (concatenate 'string name ":pure") (equal state (map-state map)))
32 (check (concatenate 'string name ":no-extra-items")
33 (and (null (topic-maps.tmdm:topic-map-associations map))
34 (notany #'topic-names (topic-map-topics map))
35 (= (if occurrence 2 (if (string= name "prefix-duplicate2") 2 1)) (length (topic-map-topics map)))))
36 (if occurrence
37 (progn
38 (check (concatenate 'string name ":parent-type-references")
39 (and (eq (local-topic map "topic" doc) (topic-maps.tmdm::%occurrence-parent occurrence))
40 (eq (local-topic map "occtype" doc) (topic-maps.tmdm::%occurrence-type occurrence))
41 (= 1 (length (all-occurrences map)))))
42 (check (concatenate 'string name ":lexical-value-and-datatype")
43 (and (string= (if (string= name "occurrence-explicit-datatype") "Occurrence" "BOGUS")
44 (topic-maps.tmdm:occurrence-value occurrence))
45 (string= (if (string= name "occurrence-explicit-datatype") (topic-maps.tmdm:string-datatype) "http://example.com/bogus")
46 (topic-maps.tmdm:occurrence-datatype occurrence))))
47 (check (concatenate 'string name ":datatype-is-no-topic")
48 (every (lambda (topic) (and (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic))
49 (= 1 (length (topic-item-identifiers topic))))) (topic-map-topics map))))
50 (check (concatenate 'string name ":subject-identifiers-only")
51 (equal (if (string= name "prefix-duplicate2") '("http://psi.example.org/fake" "http://psi.example.org/fake2")
52 '("http://psi.example.org/fake"))
53 (loop for topic in (topic-map-topics map) collect
54 (when (and (null (topic-item-identifiers topic)) (null (topic-subject-locators topic))
55 (= 1 (length (topic-subject-identifiers topic))))
56 (first (topic-subject-identifiers topic)))))))
57 (setf *cxtm-results* (append *cxtm-results*
58 (list (list :id id :base doc :actual (cxtm map doc)
59 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
60 :match (null different) :bytes (length actual)))))
61 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass"
62 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
63 "actualObjects" (occurrence-observation map)) *prefix-observations*)
64 (format t "~&PREFIX-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
65 (dolist (entry '(("prefix-collision" topic-maps.ctm:prefix-collision :prefix-collision 43 2 9)
66 ("prefix-undefined2" undefined-prefix :undefined-prefix 136 6 15)))
67 (destructuring-bind (name class code position line column) entry
68 (let* ((id (format nil "ctm/invalid/~A.ctm" name))
69 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
70 (doc (topic-maps.test-foundation:test-case-document-iri case))
71 (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8))
72 (condition (handler-case
73 (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
74 :document-iri doc :source-name id) nil)
75 (ctm-error (c) c))))
76 (check (concatenate 'string name ":specific-rejection")
77 (and (typep condition class) (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported))
78 (eq code (error-code condition))))
79 (check (concatenate 'string name ":source-position")
80 (and (= position (error-position condition)) (= line (error-line condition)) (= column (error-column condition))
81 (string= id (topic-maps.ctm:error-source condition))
82 (eq :character (topic-maps.ctm:error-position-unit condition))))
83 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.3.4" "CTM 3.16.1"))
84 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition)))
85 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass" "ctmSource" source
86 "conditionClass" (symbol-name (type-of condition)) "code" (symbol-name (error-code condition))
87 "position" (error-position condition) "line" (error-line condition) "column" (error-column condition)
88 "source" (topic-maps.ctm:error-source condition) "positionUnit" "character") *prefix-observations*)
89 (format t "~&PREFIX-REJECTION ~A: ~A~%" id condition))))
90 (setf *prefix-observations* (nreverse *prefix-observations*))
91 (check "slice6:36-passes-237-unexecuted"
92 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses)
93 '(:pass 36 :fail 0 :unsupported 0 :not-run 237))))
94
95 (defun prefix-semantic-tests ()
96 (dolist (reference '("ex:Ä" "ex:0123" "ex:12a-b" "ex:a.b" "ex:a-b"))
97 (let* ((text (format nil "%prefix ex <urn:base:> ~A. <urn:base:~A>." reference (subseq reference 3)))
98 (topics (topic-map-topics (read-text text))))
99 (check (concatenate 'string "qname-equals-explicit-iri:" reference)
100 (and (= 1 (length topics)) (equal (list (concatenate 'string "urn:base:" (subseq reference 3)))
101 (topic-subject-identifiers (first topics)))))))
102 (let ((map (read-text "%prefix ex <../vocab/> ex:A. =ex:B. ^ex:C.")))
103 (check "prefix-relative-binding-resolved-against-document" (equal '("https://example.invalid/vocab/A") (topic-subject-identifiers (first-topic map))))
104 (check "qname-subject-locator-distinct" (equal '("https://example.invalid/vocab/B") (topic-subject-locators (second (topic-map-topics map)))))
105 (check "qname-item-identifier-distinct" (equal '("https://example.invalid/vocab/C") (topic-item-identifiers (third (topic-map-topics map))))))
106 (let ((map (read-text "%prefix ex <urn:percent:%2520> ex:A.")))
107 (check "qname-binding-not-percent-decoded-twice" (equal '("urn:percent:%20A") (topic-subject-identifiers (first-topic map)))))
108 (check "prefix-terminal-does-not-require-extra-layout"
109 (equal '("urn:ex:a") (topic-subject-identifiers (first-topic (read-text "%prefixex<urn:ex:>ex:a.")))))
110 (check "prefix-directives-create-no-information-items" (null (topic-map-topics (read-text "%prefix ex <urn:ex:>"))))
111 (check "identical-resolved-binding-permitted" (= 1 (length (topic-map-topics (read-text "%prefix ex <../vocab/> %prefix ex <https://example.invalid/vocab/> ex:a.")))))
112 (check "prefix-binding-case-preserved" (equal '("urn:Upper:A") (topic-subject-identifiers (first-topic (read-text "%prefix EX <urn:Upper:> EX:A.")))))
113 (dolist (text '("%prefix ex <urn:ex:> EX:a." "ex:a. %prefix ex <urn:ex:>" "ex:a." "a t:\"v\"^^xsd:string."))
114 (check (concatenate 'string "prefix-is-case-sensitive-ordered-and-local:" text)
115 (typep (ctm-condition text) 'undefined-prefix)))
116 (check "numeric-leading-qname-local-cannot-contain-dots"
117 (typep (ctm-condition "%prefix ex <urn:ex:> ex:12.3.") 'ctm-rejection))
118 (read-text "%prefix private <urn:private:> private:a.")
119 (check "prefix-does-not-leak-between-reads" (typep (ctm-condition "private:a.") 'undefined-prefix))
120 (let* ((map (read-text (format nil "%prefix~%# comment~%ex~%#( comment )#~%<urn:ex:> ex:a."))))
121 (check "prefix-multiline-comments-layout" (equal '("urn:ex:a") (topic-subject-identifiers (first-topic map)))))
122 (let* ((map (read-text "%prefix ex <urn:ex:> ex:rel(ex:role: ex:player)"))
123 (a (first (topic-maps.tmdm:topic-map-associations map)))
124 (r (first (topic-maps.tmdm:association-roles a))))
125 (check "qname-association-type" (equal '("urn:ex:rel") (topic-subject-identifiers (topic-maps.tmdm:association-type a))))
126 (check "qname-role-type" (equal '("urn:ex:role") (topic-subject-identifiers (topic-maps.tmdm:association-role-type r))))
127 (check "qname-role-player" (equal '("urn:ex:player") (topic-subject-identifiers (topic-maps.tmdm:association-role-player r)))))
128 (let* ((map (read-text "%prefix ex <urn:ex:> a ex:type: ex:value.")) (o (first (all-occurrences map))))
129 (check "qname-iri-value-not-interned" (and (= 2 (length (topic-map-topics map))) (string= "urn:ex:value" (topic-maps.tmdm:occurrence-value o))))
130 (check "qname-iri-value-datatype" (string= (topic-maps.tmdm:iri-datatype) (topic-maps.tmdm:occurrence-datatype o))))
131 (dolist (entry (list (list "%prefix" :expected-prefix-name 7)
132 (list "%prefix ex" :expected-prefix-iri 10)
133 (list "%prefix ex other" :expected-prefix-iri 11)
134 (list "%prefix ex <urn:ex:> %prefix new ex:a" :expected-prefix-iri 33)
135 (list "%prefix ex <urn:one:> %prefix ex <urn:two:>" :prefix-collision 30)
136 (list "a t:\"v\"^^." :expected-datatype-iri 9)
137 (list "a t:\"v\"^ ^<urn:d>." :invalid-datatype-marker 7)
138 (list "a t:\"file.html\"^^<http://www.w3.org/2001/XMLSchema#anyURI>." :relative-uri-value 4)))
139 (destructuring-bind (text code position) entry
140 (let ((c (ctm-condition text)))
141 (check (concatenate 'string "prefix-datatype-diagnostic:" text)
142 (and (typep c 'ctm-rejection) (eq code (error-code c)) (= position (error-position c))
143 (= 1 (error-line c)) (= (1+ position) (error-column c)))))))
144 (let ((c (ctm-condition (format nil "%prefix ex <urn:one:>~C~C%prefix ex <urn:two:>" #\Return #\Newline))))
145 (check "prefix-collision-crlf-location" (and (typep c 'topic-maps.ctm:prefix-collision) (= 2 (error-line c)) (= 9 (error-column c)))))
146 (check "prefix:Include-requires-explicit-resolver"
147 (let ((c (ctm-condition "%include <other.ctm>")))
148 (and (typep c 'ctm-unsupported) (eq :resource-resolver-required (error-code c)))))
149 (check "prefix:version-1.0-prolog-supported"
150 (let ((map (read-text "%version 1.0")))
151 (and (null (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map)))))
152 (let* ((map (read-text "%prefix ex <urn:ex:> a - ex:type: \"Name\"."))
153 (name (first (topic-names (first-topic map)))))
154 (check "prefix-qname-name-type-supported"
155 (equal '("urn:ex:type") (topic-subject-identifiers (topic-name-type name))))))
156
157
158 (defun explicit-datatype-tests ()
159 (dolist (datatype '("urn:datatype:custom" "http://example.org/types/custom" "http://www.w3.org/2001/XMLSchema#string"))
160 (let* ((text (format nil "%prefix d <~A> a t:\" 00042 %41 file.html \\u00C4 \\n\"^^d:value." datatype))
161 (map (read-text text)) (o (first (all-occurrences map)))
162 (value (format nil " 00042 %41 file.html Ä ~%")) (state (map-state map)) (out (cxtm map)))
163 (check (concatenate 'string "explicit-arbitrary-datatype:" datatype) (string= (concatenate 'string datatype "value") (topic-maps.tmdm:occurrence-datatype o)))
164 (check (concatenate 'string "explicit-lexical-content:" datatype) (string= value (topic-maps.tmdm:occurrence-value o)))
165 (check (concatenate 'string "unknown-datatype-other-cxtm:" datatype) (search (concatenate 'string "<value>" value "</value>") out))
166 (check (concatenate 'string "datatype-not-base-normalized:" datatype) (search (concatenate 'string "<datatype>" datatype "value</datatype>") (cxtm map datatype)))
167 (check (concatenate 'string "datatype-not-topic-and-map-pure:" datatype) (and (= 2 (length (topic-map-topics map))) (equal state (map-state map))))))
168 (check "explicit-datatype-permits-layout-around-marker"
169 (string= "urn:d" (topic-maps.tmdm:occurrence-datatype
170 (first (all-occurrences (read-text "a t: \"v\" ^^ <urn:d>."))))))
171 (let* ((map (read-text "a t:\"v\"^^<../datatype>.")) (o (first (all-occurrences map))))
172 (check "relative-datatype-iri-is-resolved" (string= "https://example.invalid/datatype" (topic-maps.tmdm:occurrence-datatype o))))
173 (let* ((map (read-text "a t:\"https://example.invalid/a/doc.ctm#z\"^^<http://www.w3.org/2001/XMLSchema#anyURI>."))
174 (o (first (all-occurrences map))))
175 (check "explicit-absolute-anyuri-stored-unchanged" (string= "https://example.invalid/a/doc.ctm#z" (topic-maps.tmdm:occurrence-value o)))
176 (check "explicit-anyuri-cxtm-locator-rule" (search "<value>#z</value>" (cxtm map))))
177 (dolist (name '("integer" "decimal" "date" "dateTime"))
178 (let* ((dt (concatenate 'string "http://www.w3.org/2001/XMLSchema#" name))
179 (value (cond ((equal name "date") "2000-01-01") ((equal name "dateTime") "2000-01-01T00:00:00.1200Z") (t "00042")))
180 (map (read-text (format nil "a t:\"~A\"^^<~A>." value dt))) (o (first (all-occurrences map)))
181 (state (map-state map)) (c (cxtm-condition (lambda () (cxtm map)))))
182 (check (concatenate 'string "excluded-normalization-model-preserves:" name)
183 (and (string= dt (topic-maps.tmdm:occurrence-datatype o)) (string= value (topic-maps.tmdm:occurrence-value o))))
184 (check (concatenate 'string name "-explicit-cxtm-now-supported")
185 (and (null c) (search (cond ((string= name "integer") "<value>42</value>")
186 ((string= name "decimal") "<value>42.0</value>")
187 ((string= name "dateTime") "<value>2000-01-01T00:00:00.12Z</value>")
188 (t "<value>2000-01-01</value>")) (cxtm map))
189 (equal state (map-state map))))))
190 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t:\"Same\"; t:\"Same\"^^xsd:string.")))
191 (check "implicit-explicit-string-datatypes-have-identical-equality" (= 1 (length (all-occurrences map)))))
192 (let* ((value (concatenate 'string "<&>" (map 'string #'code-char '(101 769))))
193 (map (read-text (format nil "a <urn:datatype>: \"~A\"^^<urn:datatype>." value)))
194 (o (first (all-occurrences map))) (state (map-state map)) (out (cxtm map)))
195 (check "datatype-equals-type-iri-without-extra-topic"
196 (and (= 2 (length (topic-map-topics map)))
197 (equal '("urn:datatype") (topic-subject-identifiers (topic-maps.tmdm:occurrence-type o)))
198 (string= "urn:datatype" (topic-maps.tmdm:occurrence-datatype o))))
199 (check "other-datatype-xml-escaping-and-nfc"
200 (search (concatenate 'string "<value>&lt;&amp;&gt;" (string (code-char 233)) "</value>") out))
201 (check "other-datatype-model-retains-decomposed-lexical-value"
202 (and (string= value (topic-maps.tmdm:occurrence-value o)) (equal state (map-state map)))))
203 ;; Unknown datatype equality remains lexical. No value-space interpretation.
204 (let* ((map (read-text "a t:\"01\"^^<urn:d>; t:\"1\"^^<urn:d>; t:\"01\"^^<urn:e>; t:\"01\"^^<urn:d>.")))
205 (check "datatype-and-lexical-value-control-duplicates" (= 3 (length (all-occurrences map))))))
206
207 (defun prefix-order-and-merge-tests ()
208 (let ((reference nil))
209 (dolist (order (permutations '(0 1 2 3)))
210 (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t")))
211 (dolist (i order)
212 (topic-maps.tmdm:add-occurrence a typ (if (= i 3) "z" "Same") :datatype (nth i '("urn:a" "urn:b" "urn:c" "urn:a"))))
213 (let ((state (map-state map)) (out (cxtm map)))
214 (unless reference (setf reference out))
215 (check (format nil "explicit-datatype-construction-order:~{~D~}" order) (string= reference out))
216 (check (format nil "explicit-datatype-order-pure:~{~D~}" order) (equal state (map-state map))))))
217 (check "occurrences-sort-by-datatype-after-value"
218 (< (search "<datatype>urn:a</datatype>" reference) (search "<datatype>urn:b</datatype>" reference)
219 (search "<datatype>urn:c</datatype>" reference))))
220 (let* ((map (read-text "%prefix ex <urn:ex:> a ex:r:\"Same\"^^ex:d. b ex:s:\"Same\"^^ex:d."))
221 (handles (all-occurrences map)) (a (local-topic map "a")) (b (local-topic map "b"))
222 (r (topic-maps.tmdm:occurrence-type (first handles))) (s (topic-maps.tmdm:occurrence-type (second handles))))
223 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
224 (check "qualified-occurrences-merge-by-parent-type-value-datatype"
225 (and (= 1 (length (all-occurrences map)))
226 (eq (topic-maps.tmdm:canonical-occurrence (first handles)) (topic-maps.tmdm:canonical-occurrence (second handles)))))
227 (let* ((live (topic-maps.tmdm:canonical-occurrence (first handles))) (state (map-state map)) (retained (occurrence-handle-state handles)))
228 (check "qualified-merge-raw-parent-type-canonical"
229 (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a))
230 (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r))
231 (string= "urn:ex:d" (topic-maps.tmdm:occurrence-datatype live))))
232 (cxtm map)
233 (check "qualified-canonicalization-preserves-retained-handles" (and (equal state (map-state map)) (equal retained (occurrence-handle-state handles)))))))
234
235 (defun run-prefix-tests ()
236 (run-occurrence-tests)
237 (let ((*checks* nil))
238 (exercise-prefix-cases) (prefix-semantic-tests) (explicit-datatype-tests) (prefix-order-and-merge-tests)
239 (setf *last-prefix-checks* (nreverse *checks*))
240 (format t "~&PREFIX-DATATYPES-PASS: ~D checks; 5 new byte-exact baselines, 2 specific rejections; 36 imported PASS, 237 NOT-RUN.~%" (length *last-prefix-checks*)) t))
241 (defun write-prefix-report (path)
242 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
243 (topic-maps.test-foundation::emit-json-value
244 (topic-maps.test-foundation::fields "kind" "prefix-and-explicit-datatype-evidence"
245 "passed" (length *last-prefix-checks*) "failed" 0
246 "checks" (topic-maps.test-foundation::json-array *last-prefix-checks*)
247 "cases" (topic-maps.test-foundation::json-array *prefix-observations*)) out)
248 (terpri out)))