1 (in-package #:topic-maps.xtm.tests)
3 (defun map-reifier-xml (iri &optional (body ""))
4 (format nil "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0' reifier='~A'>~A</topicMap>" iri body))
5 (defun map-reifier-state (map)
6 ;; Retain actual references and forward handles, including the computed inverse.
7 (list (variant-state map)
8 (loop for item in (topic-maps.tmdm::map-information-items map) collect
9 (list item (topic-maps.tmdm::canonical-item item)
10 (when (topic-maps.tmdm::reifiable-p item) (item-reifier item))
11 (when (typep item 'topic) (list (topic-reified item) (topic-maps.tmdm::%topic-forward item)))))))
12 (defun map-reifier-current-p (map)
13 (and (variants-current-p map) (topic-maps.tmdm::validate-map-reification map)
14 (topic-maps.tmdm::validate-map-item-identifiers map)
15 (let ((r (item-reifier map)))
16 (and (typep r 'topic) (eq r (canonical-topic r)) (eq map (topic-parent r))
17 (member r (topic-map-topics map) :test #'eq) (eq map (topic-reified r))))))
18 (defun map-reifier-model-data (map)
19 (let ((r (item-reifier map)))
20 (topic-maps.test-foundation::fields "identity" (variant-model-data map)
21 "reifierTopic" (when r (1+ (position r (topic-map-topics map) :test #'eq)))
22 "canonicalReifierAndInverseEQ" (if (map-reifier-current-p map) :true :false)
23 "mapItemIdentifiers" (coerce (item-identifiers map) 'vector)
24 "topicReifiedMapEQ" (coerce (mapcar (lambda (topic) (if (eq map (topic-reified topic)) :true :false))
25 (topic-map-topics map)) 'vector))))
26 (defun map-reifier-condition-data (c)
27 (if (typep c 'reification-error)
28 (topic-maps.test-foundation::fields "class" (string (type-of c))
29 "code" (string-downcase (string (reification-error-code c)))
30 "detail" (topic-maps.tmdm::reification-error-detail c) "message" (princ-to-string c)
31 "line" nil "column" nil "positionUnit" nil "documentIRI" nil "sourceKey" nil "cause" nil)
32 (negative-condition-data c)))
34 (defun map-reifier-original-contract ()
35 (let* ((id "xtm2/in/tm-reifier.xtm")
36 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
37 (raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
38 (baseline (corpus-path "xtm2/baseline/tm-reifier.xtm.cxtm")) (expected (file-bytes baseline))
39 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
40 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
41 :clauses '("XTM 2.0 4.3.1,4.3.2,4.3.4,4.4" "TMDM 5.2,5.3.4,5.3.5,6.2" "CXTM 3,4")))
42 (iri (concatenate 'string *document* "#reifier")) (map nil) (actual nil) (c nil))
43 (setf *cases* (append *cases* (list case)))
46 (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (calls 0) (same nil))
49 (setf (symbol-function 'topic-maps.xml:parse-octets)
50 (lambda (source handler) (incf calls) (setf same (eq raw source)) (funcall saved source handler)))
51 (setf map (deserialize-octets raw :document-iri *document* :source-key id))
52 (check "map-reifier-original:qualified-parser-once-original-octets-eq" (and (= 1 calls) same)))
53 (setf (symbol-function 'topic-maps.xml:parse-octets) saved)))
54 (let* ((root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
55 (r (item-reifier map)) (before (map-reifier-state map)))
56 (check "map-reifier-original:actual-source-attribute-no-topic-definitions"
57 (and (= 94 (length raw)) (equal "#reifier" (topic-maps.xtm::attr root "reifier"))
58 (null (topic-maps.xtm::elements root))))
59 (check "map-reifier-original:native-map-topic-canonical-inverse-eq"
60 (and (= 1 (length (topic-map-topics map))) (null (topic-map-associations map))
61 (eq r (first (topic-map-topics map))) (eq r (by-id map "reifier")) (map-reifier-current-p map)))
62 (check "map-reifier-original:complete-separated-identifiers-and-empty-statements"
63 (and (equal (list iri) (item-identifiers r)) (null (item-identifiers map))
64 (null (topic-subject-identifiers r)) (null (topic-subject-locators r))
65 (null (topic-names r)) (null (topic-occurrences r)) (null (topic-roles-played r))))
66 (setf actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
67 (check "map-reifier-original:source-model-handles-preserved-deterministic"
68 (and (= 128 (length expected)) (equalp raw copy) (equal before (map-reifier-state map))
69 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
70 (equal before (map-reifier-state map)) (eq r (item-reifier map)) (eq map (topic-reified r))))
71 (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:map-reifier")))
72 (check "map-reifier-original:source-key-independent-model-and-cxtm"
73 (and (equalp (map-reifier-model-data map) (map-reifier-model-data other))
74 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
75 (error (condition) (setf c condition)))
76 (record-original-result case actual expected c)
77 (setf *map-reifier-outputs* (list (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
78 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
79 "sourceText" (babel:octets-to-string raw :encoding :utf-8) "reifierLocator" iri
80 "model" (when map (map-reifier-model-data map)) "condition" (map-reifier-condition-data c)
81 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
82 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
83 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
84 "firstDifferentByte" (when actual (mismatch actual expected))
85 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case))))))
87 (check "map-reifier-original:byte-exact" (equalp actual expected))
88 (check "map-reifier-original:63-unique-case-records-one-new-positive"
89 (and (= 63 (length *cases*)) (= 55 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
90 (= 63 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))))
92 (defun map-reifier-control (label source verify)
93 (let* ((raw (bytes source)) (copy (copy-seq raw))
94 (map (deserialize-octets raw :document-iri *document* :source-key label)))
96 (check (concatenate 'string "map-reifier:" label ":canonical-native-model") (map-reifier-current-p map))
97 (let* ((before (map-reifier-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
98 (check (concatenate 'string "map-reifier:" label ":pure-deterministic-source-preserved")
99 (and (equalp raw copy) (equal before (map-reifier-state map))
100 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
101 (equal before (map-reifier-state map))))
102 (push (topic-maps.test-foundation::fields "label" label "sourceText" source "model" (map-reifier-model-data map)
103 "actualCXTM" (babel:octets-to-string actual :encoding :utf-8) "condition" nil) *map-reifier-controls*))))
104 (defun map-reifier-error-control (label source class code &optional thunk map)
105 (let* ((before (when map (map-reifier-state map)))
106 (c (condition-from (or thunk (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key label)))))
107 (actual-code (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
108 ((typep c 'reification-error) (reification-error-code c))
109 ((typep c 'item-identifier-error) (item-identifier-error-code c)))))
110 (push (topic-maps.test-foundation::fields "label" label "sourceText" source "expectedClass" (string class)
111 "expectedCode" (string-downcase (string code)) "condition" (map-reifier-condition-data c)
112 "atomicNativeOperation" (when map (if (equal before (map-reifier-state map)) :true :false))) *map-reifier-controls*)
113 (check (concatenate 'string "map-reifier-error:" label ":specific-class-code")
114 (and (eq class (type-of c)) (eq code actual-code)))
115 (when (typep c 'topic-maps.xtm:xtm-error)
116 (check (concatenate 'string "map-reifier-error:" label ":source-context-position")
117 (and (equal *document* (error-document-iri c)) (equal label (error-source-key c))
118 (integerp (error-line c)) (plusp (error-line c)) (integerp (error-column c)) (not (minusp (error-column c))))))
119 (when map (check (concatenate 'string "map-reifier-error:" label ":identities-handles-atomic")
120 (equal before (map-reifier-state map))))))
122 (defun map-reifier-tests ()
123 (dolist (row `(("absolute" "urn:map-reifier" "urn:map-reifier")
124 ("relative" "../reifiers/map" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/reifiers/map")
125 ("fragment" "#r" ,(concatenate 'string *document* "#r"))
126 ("fragment-not-topicref" "#2r/path" ,(concatenate 'string *document* "#2r/path"))
127 ("no-fragment" "other.xtm" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/other.xtm")
128 ("empty-reference" "" ,*document*) ("unicode" "urn:馬🦊" "urn:馬🦊")
129 ("percent-utf8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊")
130 ("xml-escaped" "urn:a&b" "urn:a&b")))
131 (destructuring-bind (label reference expected) row
132 (map-reifier-control label (map-reifier-xml reference)
134 (check (concatenate 'string "map-reifier:" label ":ii-only-map-identity-separated")
135 (let ((r (item-reifier map)))
136 (and (= 1 (length (topic-map-topics map))) (equal (list expected) (item-identifiers r))
137 (null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (null (item-identifiers map)))))))))
138 (map-reifier-control "map-item-identities" (map-reifier-xml "#r" "<itemIdentity href='urn:map'/><itemIdentity href='urn:map'/><topic id='r'/>")
139 (lambda (map) (check "map-reifier:map-item-identities:separate-complete-sets"
140 (and (equal '("urn:map") (item-identifiers map)) (eq (by-id map "r") (item-reifier map))
141 (equal (list (concatenate 'string *document* "#r")) (item-identifiers (item-reifier map)))))))
142 (map-reifier-control "forward-name-variant" (map-reifier-xml "#r"
143 "<topic id='r'><name><value> R & 馬🦊 </value><variant><scope><topicRef href='#s'/></scope><resourceData> V </resourceData></variant></name></topic><topic id='s'/>")
144 (lambda (map) (let* ((r (item-reifier map)) (name (first (topic-names r))) (v (first (topic-name-variants name))))
145 (check "map-reifier:forward-name-variant:canonical-parents-values-scopes"
146 (and (eq r (by-id map "r")) (eq r (topic-name-parent name)) (eq name (variant-parent v))
147 (equal " R & 馬🦊 " (topic-name-value name)) (equal " V " (variant-value v))
148 (scope-eq-p (list (by-id map "s")) (variant-scope v)))))))
149 ;; Stage only existing production operations to observe the attribute BEFORE
150 ;; later Topic definitions; the original contract above uses the public API.
151 (dolist (kind '("itemIdentity" "subjectIdentifier" "subjectLocator"))
152 (let* ((label (concatenate 'string "forward-merge-" kind))
153 (locator (if (equal kind "itemIdentity") "#r" "urn:shared"))
154 (source (map-reifier-xml "#r" (format nil "<topic id='other'><~A href='~A'/></topic><topic id='r'><~A href='~A'/></topic>" kind locator kind locator)))
155 (topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* label)
156 (root (topic-maps.xml:parse-octets (bytes source) (make-instance 'topic-maps.xtm::source-handler)))
157 (map (make-topic-map)))
158 (topic-maps.xtm::validate-source root) (topic-maps.xtm::read-map-reifier root map)
159 (let ((held (item-reifier map)))
160 (check (concatenate 'string "map-reifier:" label ":attribute-creates-before-definitions")
161 (and (= 1 (length (topic-map-topics map))) (eq map (topic-reified held))))
162 (dolist (node (topic-maps.xtm::elements root)) (topic-maps.xtm::read-topic node map))
163 (check (concatenate 'string "map-reifier:" label ":held-handle-canonical-eq")
164 (and (= 1 (length (topic-map-topics map))) (eq (canonical-topic held) (by-id map "other"))
165 (eq (canonical-topic held) (by-id map "r")) (eq (canonical-topic held) (item-reifier map))
166 (eq map (topic-reified held)) (map-reifier-current-p map)
167 (same-identifiers-p (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#other"))
168 (item-identifiers (item-reifier map)))
169 (equal (if (equal kind "subjectIdentifier") '("urn:shared") nil)
170 (topic-subject-identifiers (item-reifier map)))
171 (equal (if (equal kind "subjectLocator") '("urn:shared") nil)
172 (topic-subject-locators (item-reifier map)))))
173 (let* ((before (map-reifier-state map))
174 (forward (topic-maps.tmdm::%topic-forward held)) (current (canonical-topic held))
175 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
176 (check (concatenate 'string "map-reifier:" label ":cxtm-preserves-retained-forward-handle-and-inverse")
177 (and (equal before (map-reifier-state map)) (eq forward (topic-maps.tmdm::%topic-forward held))
178 (eq current (canonical-topic held)) (eq map (topic-reified held))
179 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
180 (equal before (map-reifier-state map)) (eq forward (topic-maps.tmdm::%topic-forward held)))))
181 (map-reifier-control label source (lambda (other)
182 (check (concatenate 'string "map-reifier:" label ":public-api-equivalent")
183 (equalp (map-reifier-model-data map) (map-reifier-model-data other))))))))
184 (let* ((map (make-topic-map)) (iri "urn:existing-ii") (r (intern-topic map :item-identifier iri))
185 (topic-maps.xtm::*document-iri* *document*)
186 (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml iri)) (make-instance 'topic-maps.xtm::source-handler)))
187 (saved (symbol-function 'intern-topic)) (calls 0))
189 (progn (setf (symbol-function 'intern-topic) (lambda (&rest args) (incf calls) (apply saved args)))
190 (topic-maps.xtm::read-map-reifier root map)
191 (check "map-reifier:existing-ii:lookup-reuses-topic-without-interning"
192 (and (zerop calls) (eq r (item-reifier map)) (eq map (topic-reified r))
193 (equal (list iri) (item-identifiers r)) (null (topic-subject-identifiers r)))))
194 (setf (symbol-function 'intern-topic) saved)))
195 (dolist (kind '(:subject-identifier :subject-locator))
196 (let* ((map (make-topic-map)) (iri "urn:only-other-category") (old (intern-topic map kind iri))
197 (topic-maps.xtm::*document-iri* *document*)
198 (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml iri)) (make-instance 'topic-maps.xtm::source-handler))))
199 (topic-maps.xtm::read-map-reifier root map)
200 (check (format nil "map-reifier:lookup:~A-is-not-ii-hit" kind)
201 (and (equal (list iri) (item-identifiers (item-reifier map)))
202 (if (eq kind :subject-identifier)
203 ;; Native intern-topic merges SI/II identity but MUST add the II.
204 (and (eq old (item-reifier map)) (= 1 (length (topic-map-topics map))))
205 (and (not (eq old (item-reifier map))) (= 2 (length (topic-map-topics map)))
206 (null (item-identifiers old)) (null (topic-reified old))))
207 (map-reifier-current-p map)))))
208 (dolist (row '(("bad-percent" "urn:%GG" :invalid-percent-escape)
209 ("truncated-percent" "urn:%A" :invalid-percent-escape)
210 ("bad-percent-utf8" "urn:%C3%28" :invalid-percent-utf8)
211 ("surrogate-percent-utf8" "urn:%ED%A0%80" :invalid-percent-utf8)
212 ("space" "urn:a b" :invalid-iri) ("decoded-control" "urn:%00" :invalid-iri)
213 ("relative-colon" "1bad:subject" :invalid-iri) ("two-fragments" "#a#b" :invalid-iri)))
214 (map-reifier-error-control (first row) (map-reifier-xml (second row)) 'xtm-rejection (third row)))
215 (dolist (row '(("extra-attribute" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0' reifier='#r' other='x'/>" :unexpected-attribute)
216 ("qualified-reifier" "<topicMap xmlns='http://www.topicmaps.org/xtm/' xmlns:x='urn:x' version='2.0' x:reifier='#r'/>" :unexpected-attribute)
217 ("missing-version" "<topicMap xmlns='http://www.topicmaps.org/xtm/' reifier='#r'/>" :missing-attribute)
218 ("invalid-version" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.1' reifier='#r'/>" :invalid-version)
219 ("wrong-namespace" "<topicMap xmlns='urn:wrong' version='2.0' reifier='#r'/>" :invalid-namespace)
220 ("identity-after-topic" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0' reifier='#r'><topic id='r'/><itemIdentity href='urn:map'/></topicMap>" :element-order)
221 ("reifier-child" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><reifier href='#r'/></topicMap>" :unexpected-element)))
222 (map-reifier-error-control (first row) (second row) 'xtm-rejection (third row)))
223 (dolist (row '(("name-with-variant-reification" "<topic id='a'><name><value>N</value><variant reifier='#r'><scope><topicRef href='#v'/></scope><resourceData>V</resourceData></variant></name></topic>" :reification)
224 ("variant-reification" "<topic id='a'><name><value>N</value><variant reifier='#v'><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant></name></topic>" :reification)
225 ("occurrence-reification" "<topic id='a'><occurrence reifier='#o'><type><topicRef href='#t'/></type><resourceData>O</resourceData></occurrence></topic>" :unsupported-construct)
226 ("association-role-reification" "<association reifier='#a'><type><topicRef href='#t'/></type><role reifier='#r'><type><topicRef href='#rt'/></type><topicRef href='#p'/></role></association>" :unsupported-construct)
227 ("mergeMap" "<mergeMap href='other.xtm'/>" :unsupported-construct)))
228 (map-reifier-error-control (first row) (map-reifier-xml "#map-r" (second row)) 'xtm-unsupported (third row)))
229 (map-reifier-error-control "map-ii-reifier-ii-collision" (map-reifier-xml "#r" "<itemIdentity href='#r'/>")
230 'item-identifier-error :duplicate-item-identifier)
231 (let* ((map (deserialize-octets (bytes (map-reifier-xml "#r" "<topic id='owner'><name><value>N</value></name></topic>")) :document-iri *document*))
232 (r (item-reifier map)) (name (first (topic-names (by-id map "owner")))))
233 (map-reifier-error-control "reuse-reifier" nil 'reification-error :reifier-conflict
234 (lambda () (set-item-reifier name r)) map)
235 (add-item-identifier name "urn:name-item")
236 (let* ((topic-maps.xtm::*document-iri* *document*)
237 (root (topic-maps.xml:parse-octets (bytes (map-reifier-xml "urn:name-item")) (make-instance 'topic-maps.xtm::source-handler))))
238 (map-reifier-error-control "existing-name-ii-collision" nil 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier
239 (lambda () (topic-maps.xtm::read-map-reifier root map)) map))
240 (let ((other (intern-topic map :item-identifier "urn:other-reifier")))
241 (set-item-reifier name other)
242 (map-reifier-error-control "merge-two-different-reified-items" nil 'reification-error :reifier-conflict
243 (lambda () (merge-topics r other)) map))
244 (check "map-reifier:conflicts:map-name-reifiers-and-inverses-preserved"
245 (and (eq r (item-reifier map)) (eq map (topic-reified r)) (eq name (topic-reified (item-reifier name)))
246 (map-reifier-current-p map))))
247 ;; Wrong native conditions are execution errors in the existing classifier.
248 (let* ((map (make-topic-map)) (r (intern-topic map :item-identifier "urn:guard"))
249 (owner (intern-topic map :item-identifier "urn:owner")) (name (add-default-name owner "N")))
250 (set-item-reifier map r)
251 (let* ((c (condition-from (lambda () (set-item-reifier name r))))
252 (case (topic-maps.test-foundation:make-test-case :id "local:reification-classifier" :category :valid :expected-outcome :cxtm-equal)))
253 (record-original-result case #(1) #(1) c)
254 (check "map-reifier:classifier:native-reification-conflict-never-positive-pass"
255 (and (eq :fail (topic-maps.test-foundation:test-case-status case))
256 (eq :execution-error (topic-maps.test-foundation:test-case-actual-outcome case))))
257 (record-negative-original-result case (first *negative-original-specs*) c *document* "local:guard")
258 (check "map-reifier:classifier:wrong-expected-condition-never-negative-pass"
259 (and (eq :fail (topic-maps.test-foundation:test-case-status case))
260 (eq :execution-error (topic-maps.test-foundation:test-case-actual-outcome case)))))))