]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-typed-names.lisp
feat(tmdm): readers for the TMDM 7.2/7.3 relationships; tests for both default name...
[topic-maps.git] / tests / ctm-typed-names.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-typed-name-checks* nil)
4 (defvar *typed-name-observations* nil)
5 (defun typed-name-checks () *last-typed-name-checks*)
6 (defun name-handle-state (names)
7 (mapcar (lambda (name)
8 (list name (topic-maps.tmdm::%topic-name-forward name)
9 (topic-maps.tmdm::%topic-name-parent name)
10 (topic-maps.tmdm::%topic-name-type name)
11 (topic-maps.tmdm::%topic-name-value name))) names))
12
13 (defun typed-name-original-cases ()
14 (setf *typed-name-observations* nil)
15 (dolist (file '("name" "name2" "name-type" "name-duplicate"))
16 (let* ((id (format nil "ctm/in/~A.ctm" file))
17 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
18 (doc (topic-maps.test-foundation:test-case-document-iri case))
19 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
20 :document-iri doc :source-name id))
21 (owner (local-topic map "topic" doc)) (names (topic-names owner))
22 (name (first names)) (typed (string= file "name-type"))
23 (type (if typed (local-topic map "nametype" doc)
24 (find (default-name-type-identifier) (topic-map-topics map)
25 :key (lambda (topic) (first (topic-subject-identifiers topic))) :test #'equal))))
26 (check (concatenate 'string id ":name-count") (= 1 (length names)))
27 (check (concatenate 'string id ":value") (string= "Name" (topic-name-value name)))
28 (check (concatenate 'string id ":parent-identity") (eq owner (topic-name-parent name)))
29 (check (concatenate 'string id ":type-identity") (and type (eq type (topic-name-type name))))
30 (check (concatenate 'string id ":raw-references") (raw-reference-invariants map))
31 (check (concatenate 'string id ":only-required-items")
32 (and (= 2 (length (topic-map-topics map)))
33 (= 1 (count-if #'topic-names (topic-map-topics map)))
34 (null (all-occurrences map)) (null (topic-maps.tmdm:topic-map-associations map))))
35 (check (concatenate 'string id ":type-identifiers")
36 (if typed
37 (and (equal (list (concatenate 'string doc "#nametype")) (topic-item-identifiers type))
38 (null (topic-subject-identifiers type)) (null (topic-subject-locators type)))
39 (and (equal (list (default-name-type-identifier)) (topic-subject-identifiers type))
40 (null (topic-item-identifiers type)) (null (topic-subject-locators type)))))
41 (let* ((before (map-state map)) (handles (name-handle-state names))
42 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
43 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
44 (difference (mismatch actual expected)))
45 (topic-maps.test-foundation:record-result case (if difference :cxtm-mismatch :cxtm-match)
46 :diagnostics (when difference (list (format nil "First different byte: ~D." difference))))
47 (setf (topic-maps.test-foundation:test-case-clauses case)
48 (union '("CTM 3.3.6" "CTM 3.3.7" "CTM 3.11" "TMDM 5.4" "TMDM 6.2" "TMDM 6.3"
49 "CXTM 3.6" "CXTM 4.7")
50 (topic-maps.test-foundation:test-case-clauses case) :test #'equal))
51 (check (concatenate 'string id ":byte-exact-cxtm") (null difference))
52 (check (concatenate 'string id ":serialization-is-pure")
53 (and (equal before (map-state map)) (equal handles (name-handle-state names))))
54 ;; Existing comparisons retain their result objects/order. Add only the
55 ;; two contracts not previously executed by the retained Slice 11 suite.
56 (unless (find id *cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal)
57 (setf *cxtm-results*
58 (append *cxtm-results* (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)
60 :external-format :utf-8)
61 :match (null difference) :bytes (length actual)))))))
62 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc
63 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)
64 :external-format :utf-8)
65 "actualObjects" (model-observation map)) *typed-name-observations*)
66 (format t "~&TYPED-NAME-CXTM ~A: PASS~%" id)))
67 (setf *typed-name-observations* (nreverse *typed-name-observations*)))
68
69 (defun typed-name-api-tests ()
70 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner"))
71 (a (api-topic map "type-a")) (b (api-topic map "type-b"))
72 (na (topic-maps.tmdm:add-name owner "Name" :type a))
73 (nb (topic-maps.tmdm:add-name owner "Name" :type b)))
74 (check "typed-api:explicit-type-no-default" (= 3 (length (topic-map-topics map))))
75 (check "typed-api:same-value-different-type-distinct" (and (not (eq na nb)) (= 2 (length (topic-names owner)))))
76 (check "typed-api:equal-properties-reuse-name" (eq na (topic-maps.tmdm:add-name owner "Name" :type a)))
77 (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared-type")
78 (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared-type")
79 (let ((current (topic-maps.tmdm:canonical-topic-name na)) (type (topic-maps.tmdm:canonical-topic a)))
80 (check "typed-api:type-identity-causes-name-merging" (= 1 (length (topic-names owner))))
81 (check "typed-api:retained-name-handles-resolve" (and (not (eq na current)) (not (eq nb current))
82 (eq current (topic-maps.tmdm:canonical-topic-name nb))))
83 (check "typed-api:stored-name-type-is-canonical" (eq type (topic-maps.tmdm::%topic-name-type current)))
84 (check "typed-api:stored-name-parent-is-canonical" (eq owner (topic-maps.tmdm::%topic-name-parent current)))
85 (check "typed-api:old-type-handle-reused" (eq current (topic-maps.tmdm:add-name owner "Name" :type b)))
86 (let ((before (map-state map)) (handles (name-handle-state (list na nb current))))
87 (cxtm map)
88 (check "typed-api:serialization-preserves-retained-handles"
89 (and (equal before (map-state map)) (equal handles (name-handle-state (list na nb current))))))))
90 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner"))
91 (name (topic-maps.tmdm:add-name owner "Default")) (type (topic-name-type name)))
92 (check "typed-api:default-wrapper-compatible" (eq name (topic-maps.tmdm:add-default-name owner "Default")))
93 (check "typed-api:explicit-default-reuses-name" (eq name (topic-maps.tmdm:add-name owner "Default" :type type)))
94 (check "typed-api:default-only-two-topics" (= 2 (length (topic-map-topics map)))))
95 (let* ((map (topic-maps.tmdm:make-topic-map))
96 (owner (topic-maps.tmdm:intern-topic map :item-identifier (default-name-type-identifier)))
97 (name (topic-maps.tmdm:add-name owner "self")))
98 (check "typed-api:default-identity-merges-owner-correctly"
99 (and (= 1 (length (topic-map-topics map)))
100 (eq (topic-name-parent name) (topic-name-type name)) (raw-reference-invariants map))))
101 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner"))
102 (other (topic-maps.tmdm:make-topic-map)) (foreign (api-topic other "foreign")))
103 (dolist (bad (list foreign nil 42))
104 (let ((before (map-state map)) (other-before (map-state other)))
105 (check (format nil "typed-api:invalid-type-refused:~A" (type-of bad))
106 (handler-case (progn (topic-maps.tmdm:add-name owner "Name" :type bad) nil) (error () t)))
107 (check (format nil "typed-api:invalid-type-no-mutation:~A" (type-of bad))
108 (and (equal before (map-state map)) (equal other-before (map-state other))))))))
109
110 (defun typed-name-merge-order-tests ()
111 (let ((reference nil))
112 (dolist (order '(:parents-first :types-first))
113 (let* ((map (topic-maps.tmdm:make-topic-map)) (a (api-topic map "a")) (b (api-topic map "b"))
114 (r (api-topic map "r")) (s (api-topic map "s"))
115 (handles (loop for owner in (list a b) append
116 (loop for type in (list r s) collect (topic-maps.tmdm:add-name owner "Name" :type type)))))
117 (if (eq order :parents-first)
118 (progn (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s))
119 (progn (topic-maps.tmdm:merge-topics r s) (topic-maps.tmdm:merge-topics a b)))
120 (let* ((owner (topic-maps.tmdm:canonical-topic a)) (type (topic-maps.tmdm:canonical-topic r))
121 (name (first (topic-names owner))) (before (map-state map))
122 (state (name-handle-state handles)) (out (cxtm map)))
123 (check (format nil "typed-merge:all-handles:~A" order)
124 (every (lambda (h) (eq name (topic-maps.tmdm:canonical-topic-name h))) handles))
125 (check (format nil "typed-merge:single-live-name:~A" order) (= 1 (length (topic-names owner))))
126 (check (format nil "typed-merge:raw-references:~A" order)
127 (and (eq owner (topic-maps.tmdm::%topic-name-parent name))
128 (eq type (topic-maps.tmdm::%topic-name-type name)) (raw-reference-invariants map)))
129 (check (format nil "typed-merge:serialization-pure:~A" order)
130 (and (equal before (map-state map)) (equal state (name-handle-state handles))))
131 (when reference (check "typed-merge:order-independent-cxtm" (string= reference out)))
132 (setf reference out))))))
133
134 (defun typed-name-reader-tests ()
135 (dolist (text (list "a - type:\"Name\"." "a - type: \"Name\"."
136 (format nil "a - type:~%#( layout )# \"Name\".")
137 "a - type :\"Name\"."))
138 (let* ((map (read-text text)) (owner (local-topic map "a")) (name (first (topic-names owner))))
139 (check (concatenate 'string "typed-reader:local-type:" text)
140 (and (eq (local-topic map "type") (topic-name-type name))
141 (eq owner (topic-name-parent name)) (= 2 (length (topic-map-topics map)))))))
142 (dolist (pair '(("<urn:type>" :subject-identifier "urn:type")
143 ("<../type>" :subject-identifier "https://example.invalid/type")
144 ("ex:type" :subject-identifier "urn:ex:type")
145 ("^<urn:type>" :item-identifier "urn:type")
146 ("=<urn:type>" :subject-locator "urn:type")))
147 (destructuring-bind (reference kind identifier) pair
148 (let* ((map (read-text (format nil "%prefix ex <urn:ex:> a - ~A: \"Name\"." reference)))
149 (name (first (topic-names (local-topic map "a")))) (type (topic-name-type name)))
150 (check (format nil "typed-reader:identity-category:~A" reference)
151 (equal (list identifier) (ecase kind
152 (:subject-identifier (topic-subject-identifiers type))
153 (:item-identifier (topic-item-identifiers type))
154 (:subject-locator (topic-subject-locators type)))))
155 (check (format nil "typed-reader:no-default:~A" reference)
156 (and (= 2 (length (topic-map-topics map)))
157 (notany (lambda (topic) (member (default-name-type-identifier)
158 (topic-subject-identifiers topic) :test #'equal))
159 (topic-map-topics map)))))))
160 (let* ((map (read-text "a - a:\"Name\"; - a:\"Name\"; - a:\"name\"."))
161 (owner (local-topic map "a")))
162 (check "typed-reader:self-type-case-preserved-duplicate-removed"
163 (and (= 1 (length (topic-map-topics map))) (= 2 (length (topic-names owner)))
164 (every (lambda (n) (eq owner (topic-name-type n))) (topic-names owner)))))
165 (dolist (text '("a - ex:type:\"Name\"." "a - ." "a -" "a - type:." "a - type \"Name\"."))
166 (let* ((condition (ctm-condition text)) (undefined (search "ex:type" text))
167 (separator (string= text "a - type \"Name\"."))
168 (position (if undefined 4 (if separator 9 (if (string= text "a - type:.") 9 (if (string= text "a -") 3 4))))))
169 (check (concatenate 'string "typed-reader:diagnostic:" text)
170 (and (eq (type-of condition) (if undefined 'undefined-prefix 'ctm-rejection))
171 (eq (error-code condition) (if undefined :undefined-prefix
172 (if separator :expected-name-type-separator :expected-name-value)))
173 (= position (error-position condition)) (= 1 (error-line condition))
174 (= (1+ position) (error-column condition))))))
175 (let* ((map (read-text "a - type:\"Name\" @theme.")) (name (first (topic-names (local-topic map "a")))))
176 (check "typed-reader:scope-now-supported"
177 (and (eq (local-topic map "type") (topic-name-type name))
178 (equal (list (local-topic map "theme")) (topic-maps.tmdm:topic-name-scope name)))))
179 (dolist (text '("a - [-\"fullname\" ~r]:\"Name\"."
180 "a - type:\"Name\" ~reifier." "a - type:\"Name\" (\"variant\" @theme ~r)."))
181 (check (concatenate 'string "typed-reader:feature-boundary:" text) (if (find #\~ text) (null (ctm-condition text)) (typep (ctm-condition text) 'ctm-unsupported)))))
182
183 (defun typed-name-order-tests ()
184 (let ((reference nil))
185 (dolist (order (permutations '(0 1 2)))
186 (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner"))
187 (a (api-topic map "a")) (z (api-topic map "z")))
188 (dolist (i order)
189 (topic-maps.tmdm:add-name owner (if (= i 2) "Z" "Name") :type (if (= i 1) z a)))
190 (let* ((before (map-state map)) (out (cxtm map)))
191 (check (format nil "typed-order:value-then-type:~S" order)
192 (< (search "<type topicref=\"1\">" out) (search "<type topicref=\"3\">" out)
193 (search "<value>Z</value>" out)))
194 (when reference (check (format nil "typed-order:construction-independent:~S" order) (string= reference out)))
195 (check (format nil "typed-order:pure:~S" order) (equal before (map-state map)))
196 (setf reference out))))))
197
198 (defun run-typed-name-tests ()
199 (run-timezone-tests)
200 (let ((*checks* nil))
201 (typed-name-original-cases) (typed-name-api-tests) (typed-name-merge-order-tests)
202 (typed-name-reader-tests) (typed-name-order-tests)
203 (check "typed-names:only-two-new-imported-passes"
204 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses)
205 '(:pass 64 :fail 2 :unsupported 0 :not-run 207)))
206 (setf *last-typed-name-checks* (nreverse *checks*))
207 (format t "~&TYPED-NAMES-PASS: ~D checks; four original comparisons; 64 PASS, 2 known FAIL, 207 NOT-RUN.~%"
208 (length *last-typed-name-checks*)) t))
209
210 (defun write-typed-name-report (path)
211 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
212 (topic-maps.test-foundation::emit-json-value
213 (topic-maps.test-foundation::fields "kind" "typed-name-evidence"
214 "passed" (length *last-typed-name-checks*) "failed" 0
215 "checks" (topic-maps.test-foundation::json-array *last-typed-name-checks*)
216 "cases" (topic-maps.test-foundation::json-array *typed-name-observations*)) out)
217 (terpri out)))