]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-association-arity.lisp
Qualify pinned CXML encodings with reproducible local patches
[topic-maps.git] / tests / ctm-association-arity.lisp
1 (defpackage #:topic-maps.arity.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:read-text #:cxtm #:binary-file #:association-invariants)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:import-from #:topic-maps.identifier.tests #:si)
6 (:export #:run-tests #:arity-checks #:write-arity-report))
7 (in-package #:topic-maps.arity.tests)
8
9 (defvar *last-checks* nil)
10 (defvar *original* nil)
11 (defvar *witnesses* nil)
12 (defun arity-checks () *last-checks*)
13 (defun named-topic (map name)
14 (or (find (concatenate 'string "https://example.invalid/a/doc.ctm#" name)
15 (topic-map-topics map) :test (lambda (id topic) (member id (topic-item-identifiers topic) :test #'string=)))
16 (error "Missing fixture Topic ~A" name)))
17 (defun role-with (association type player)
18 (find-if (lambda (r) (and (eq (canonical-topic type) (association-role-type r))
19 (eq (canonical-topic player) (association-role-player r))))
20 (association-roles association)))
21 (defun state (map handles) (topic-maps.reification.tests::state map handles))
22 (defun valid-model-p (map) (topic-maps.reification.tests::invariants map))
23 (defun witness (label map)
24 (push (topic-maps.test-foundation::fields "label" label
25 "roleCount" (loop for a in (topic-map-associations map) sum (length (association-roles a)))
26 "actualCXTM" (cxtm map)) *witnesses*))
27
28 (defun cardinality-tests ()
29 (dolist (n '(1 2 3 4 32 64))
30 (let* ((text (format nil "a(~{~A~^, ~})"
31 (loop for i from 1 to n collect (format nil "r~D: p~D" i i))))
32 (map (read-text text)) (a (first (topic-map-associations map)))
33 (before (state map (cons a (association-roles a)))))
34 (check (format nil "arity:~D:exact-items" n)
35 (and (= (1+ (* 2 n)) (length (topic-map-topics map)))
36 (= 1 (length (topic-map-associations map))) (= n (length (association-roles a)))
37 (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic))))
38 (topic-map-topics map))))
39 (check (format nil "arity:~D:type-and-parent-EQ" n)
40 (and (eq map (association-parent a)) (eq (named-topic map "a") (association-type a))))
41 (loop for i from 1 to n do
42 (let* ((type (named-topic map (format nil "r~D" i))) (player (named-topic map (format nil "p~D" i)))
43 (r (role-with a type player)))
44 (check (format nil "arity:~D:role:~D:actual-EQ-references" n i)
45 (and r (eq a (association-role-parent r))
46 (eq r (first (topic-roles-played player)))
47 (eq r (canonical-association-role r))
48 (eq type (topic-maps.tmdm::%association-role-type r))
49 (eq player (topic-maps.tmdm::%association-role-player r))))))
50 (check (format nil "arity:~D:deterministic-and-pure" n)
51 (and (string= (cxtm map) (cxtm map)) (equal before (state map (cons a (association-roles a)) ))
52 (association-invariants map)))
53 (witness (format nil "arity:~D" n) map))))
54
55 (defun permutations (items)
56 (if (null items) (list nil)
57 (loop for item in items append
58 (mapcar (lambda (rest) (cons item rest)) (permutations (remove item items :count 1 :test #'equal))))))
59 (defun permutation-tests ()
60 (let ((reference nil))
61 (loop for order in (permutations '("r1: p1" "r2: p2" "r3: p3" "r4: p4")) for index from 1 do
62 ;; Source multiplicity has no significance, even when >2 declarations
63 ;; normalize to fewer than three Role items.
64 (let* ((source (format nil "a(~{~A~^, ~})" (append order (list (first order) (first order)))))
65 (map (read-text source)) (a (first (topic-map-associations map))) (output (cxtm map)))
66 (unless reference (setf reference output))
67 (check (format nil "permutation:~D:source-order-and-duplicates" index)
68 (and (= 4 (length (association-roles a))) (string= reference output) (valid-model-p map))))))
69 (dolist (n '(1 2))
70 (let* ((roles (loop for i below 12 collect (format nil "r: p~D" (mod i n))))
71 (map (read-text (format nil "a(~{~A~^, ~})" roles))))
72 (check (format nil "duplicates:12-declarations-become-~D-roles" n)
73 (and (= n (length (association-roles (first (topic-map-associations map)))))
74 (valid-model-p map)))))
75 (let* ((map (read-text "a(r: p ~x, s: q, t: z, r: p ~y, r: p ~x) @theme ~ar"))
76 (a (first (topic-map-associations map)))
77 (r (role-with a (named-topic map "r") (named-topic map "p"))))
78 (check "duplicates:source-role-reifiers-merge-on-the-same-role"
79 (and (= 3 (length (association-roles a)))
80 (eq (named-topic map "x") (named-topic map "y"))
81 (eq (named-topic map "x") (item-reifier r)) (eq r (topic-reified (named-topic map "y")))
82 (eq a (topic-reified (named-topic map "ar")))
83 (equal (association-scope a) (list (named-topic map "theme"))) (valid-model-p map)))
84 (witness "duplicate-role-reifiers-and-scope" map)))
85
86 (defun reference-tests ()
87 (let* ((map (read-text (format nil "%prefix ex http://example.org/~%a(ex:r: ex:p, ^<urn:r2>: =<urn:p2>, [-\"R\"]: [-\"P\"], ?r: ?p, ?r: ?p) @?theme ~~?ar")))
88 (a (first (topic-map-associations map)))
89 (roles (association-roles a)))
90 (check "references:qname-item-id-subject-locator-embedded-and-named-wildcards"
91 (and (= 4 (length roles)) (= 1 (length (association-scope a))) (item-reifier a) (valid-model-p map)))
92 (check "references:identity-categories-not-conflated"
93 (some (lambda (r) (and (equal '("urn:r2") (topic-item-identifiers (association-role-type r)))
94 (equal '("urn:p2") (topic-subject-locators (association-role-player r)))
95 (null (topic-subject-identifiers (association-role-player r))))) roles))
96 (witness "all-existing-topic-reference-forms" map))
97 (let* ((map (read-text "a(r: ?, r: ?, r: ?, r: ?)")) (a (first (topic-map-associations map))))
98 (check "references:anonymous-wildcards-remain-distinct-players"
99 (and (= 4 (length (association-roles a)))
100 (= 4 (length (remove-duplicates (mapcar #'association-role-player (association-roles a)) :test #'eq)))
101 (valid-model-p map)))))
102
103 (defun merge-fixture (axis)
104 (read-text (ecase axis
105 (:player "a(r: p, r: q, s: z) @u ~ar1 a(r: p, s: z) @u ~ar2")
106 (:role-type "a(r: p, s: p, t: z) @u ~ar1 a(r: p, t: z) @u ~ar2")
107 (:association-type "a(r: p, s: q, t: z) @u ~ar1 b(r: p, s: q, t: z) @u ~ar2")
108 (:scope "a(r: p, s: q, t: z) @u,v ~ar1 a(r: p, s: q, t: z) @u ~ar2"))))
109 (defun merge-tests ()
110 (dolist (axis '(:player :role-type :association-type :scope))
111 (dolist (via-identifier '(nil t))
112 (let ((reference nil))
113 (dolist (reverse-order '(nil t))
114 (let* ((map (merge-fixture axis)) (associations (topic-map-associations map))
115 (roles (loop for a in associations append (association-roles a)))
116 (metadata (loop for r in roles for i from 1 collect
117 (let ((id (format nil "urn:arity:role:~D" i)) (reifier (si map (format nil "arity:reifier:~D" i))))
118 (add-item-identifier r id) (set-item-reifier r reifier) (list r id reifier))))
119 (ar1 (named-topic map "ar1")) (ar2 (named-topic map "ar2"))
120 (pair (ecase axis (:player '("p" "q")) (:role-type '("r" "s"))
121 (:association-type '("a" "b")) (:scope '("u" "v"))))
122 (left (named-topic map (first pair))) (right (named-topic map (second pair)))
123 (handles (append (topic-map-topics map) associations roles))
124 (label (format nil "merge:~A:identifier=~A:reverse=~A" axis via-identifier reverse-order)))
125 (loop for a in associations for i from 1 do (add-item-identifier a (format nil "urn:arity:association:~D" i)))
126 (when reverse-order (rotatef left right))
127 (if via-identifier
128 (progn (add-topic-identifier left :subject-identifier "urn:arity:join")
129 (add-topic-identifier right :subject-identifier "urn:arity:join"))
130 (merge-topics left right))
131 (let* ((a (first (topic-map-associations map))) (expected-roles (if (member axis '(:player :role-type)) 2 3)))
132 (check (concatenate 'string label ":association-and-role-collapse")
133 (and (= 1 (length (topic-map-associations map))) (= expected-roles (length (association-roles a)))
134 (every (lambda (old) (and (not (eq old a)) (eq a (canonical-association old)))) associations)))
135 (check (concatenate 'string label ":association-identifiers-and-reifiers")
136 (and (equal '("urn:arity:association:1" "urn:arity:association:2") (sort (item-identifiers a) #'string<))
137 (eq (canonical-topic ar1) (canonical-topic ar2))
138 (eq (canonical-topic ar1) (item-reifier a)) (eq a (topic-reified ar2))))
139 (check (concatenate 'string label ":canonical-scope-and-topic-refs")
140 (and (equal (list (named-topic map "u")) (association-scope a)) (valid-model-p map)))
141 (loop for (old id reifier) in metadata for i from 1 do
142 (let ((r (canonical-association-role old)))
143 (check (format nil "~A:role:~D:identifier-reifier-parent-and-inverse" label i)
144 (and (not (eq r old)) (member r (association-roles a) :test #'eq)
145 (member id (item-identifiers r) :test #'string=)
146 (eq (canonical-topic reifier) (item-reifier r)) (eq r (topic-reified reifier))
147 (eq a (association-role-parent old))
148 (member r (topic-roles-played (association-role-player old)) :test #'eq)))))
149 ;; Force another Association/Role replacement to exercise chains
150 ;; of old handles, not only one-step forwarding.
151 (let ((next (make-association map (association-type a)
152 (mapcar (lambda (r) (list (association-role-type r) (association-role-player r)))
153 (reverse (association-roles a))) :scope (association-scope a))))
154 (check (concatenate 'string label ":further-replacements-resolve-old-handles")
155 (and (not (eq a next)) (every (lambda (old) (eq next (canonical-association old))) associations)
156 (every (lambda (r) (eq next (association-role-parent r))) roles) (valid-model-p map))))
157 (let* ((before (state map handles)) (output (cxtm map)))
158 (unless reference (setf reference output))
159 (check (concatenate 'string label ":merge-order-independent-pure-cxtm")
160 (and (string= reference output) (string= output (cxtm map)) (equal before (state map handles)))))
161 (when (and (not via-identifier) (not reverse-order)) (witness label map)))))))))
162
163 (defun syntax-tests ()
164 (dolist (fixture '(("a()" :undefined-template 0) ("a(r: )" :expected-role-player 5)
165 ("a(r: p,)" :expected-role 7) ("a(r: p" :missing-association-close 6)
166 ("a(r: p, s: q, t: )" :expected-role-player 17)
167 ("a(r: p, s: q, t: z,)" :expected-role 19)
168 ("a(r: p, s: q, t: z" :missing-association-close 18)))
169 (destructuring-bind (text code position) fixture
170 (let ((c (condition-from (lambda () (read-text text)))))
171 (check (concatenate 'string "syntax:specific-rejection-and-position:" text)
172 (and (typep c 'topic-maps.ctm:ctm-rejection) (eq code (topic-maps.ctm:error-code c))
173 (= position (topic-maps.ctm:error-position c)) (= 1 (topic-maps.ctm:error-line c))
174 (= (1+ position) (topic-maps.ctm:error-column c)))))))
175 (let* ((text (format nil "a(r: p,~% s: q,~% t: )")) (c (condition-from (lambda () (read-text text)))))
176 (check "syntax:third-role-multiline-position"
177 (and (eq (type-of c) 'topic-maps.ctm:ctm-rejection)
178 (eq :expected-role-player (topic-maps.ctm:error-code c))
179 (= (position #\) text :from-end t) (topic-maps.ctm:error-position c))
180 (= 3 (topic-maps.ctm:error-line c)) (= 5 (topic-maps.ctm:error-column c)))))
181 (let ((c (condition-from (lambda () (read-text "a(r: p, s: q, bad:r: z)")))))
182 (check "syntax:third-role-undefined-prefix-is-specific"
183 (and (eq (type-of c) 'topic-maps.ctm:undefined-prefix)
184 (eq :undefined-prefix (topic-maps.ctm:error-code c)) (= 14 (topic-maps.ctm:error-position c)))))
185 (dolist (text '("a isa b." "a ako b."))
186 (check (concatenate 'string "boundary:17B-now-deserialized:" text)
187 (let* ((map (read-text text)) (a (first (topic-map-associations map))))
188 (and (= 2 (length (association-roles a))) (valid-model-p map))))))
189
190 (defun original-case ()
191 (let* ((id "ctm/in/association-duplicate-role.ctm")
192 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
193 (doc (topic-maps.test-foundation:test-case-document-iri case))
194 (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)))
195 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
196 (a (first (topic-map-associations map))) (roles (association-roles a)) (before (state map (cons a roles)))
197 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
198 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
199 (check "original:exact-seven-topics-one-association-three-roles"
200 (and (= 7 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map)))
201 (= 3 (length roles)) (valid-model-p map)))
202 (check "original:1157-byte-exact-unmodified-baseline"
203 (and (= 1157 (length expected) (length actual)) (equalp expected actual)))
204 (check "original:serialization-preserves-objects-and-handles" (equal before (state map (cons a roles))))
205 (topic-maps.test-foundation:record-result case :cxtm-match)
206 (setf (topic-maps.test-foundation:test-case-clauses case)
207 '("CTM 3.13" "TMDM 5.7" "TMDM 5.8" "TMDM 6.6" "TMDM 6.7" "CXTM 3.9" "CXTM 3.10" "CXTM 4.10" "CXTM 4.11"))
208 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
209 (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case))
210 :match t :bytes (length actual)))))
211 (setf *original* (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" source
212 "topicCount" (length (topic-map-topics map)) "associationCount" (length (topic-map-associations map))
213 "typeIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-type a)))
214 "roles" (topic-maps.test-foundation::json-array
215 (loop for r in roles collect (topic-maps.test-foundation::fields
216 "typeIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-role-type r)))
217 "playerIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-role-player r)))
218 "parentIsAssociation" (if (eq a (association-role-parent r)) :true :false)
219 "roleIsCanonical" (if (eq r (canonical-association-role r)) :true :false)
220 "inverseContainsRole" (if (member r (topic-roles-played (association-role-player r)) :test #'eq) :true :false))))))
221 (format t "~&ARITY-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
222
223 (defun run-tests ()
224 (topic-maps.reification.tests:run-tests)
225 (let ((topic-maps.ctm.tests::*checks* nil))
226 (setf *witnesses* nil)
227 (cardinality-tests) (permutation-tests) (reference-tests) (merge-tests) (syntax-tests) (original-case)
228 (check "arity:only-one-imported-contract-transition"
229 (equal '(:pass 107 :fail 2 :unsupported 0 :not-run 164)
230 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
231 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*) *witnesses* (nreverse *witnesses*))
232 (format t "~&ASSOCIATION-ARITY-PASS: ~D checks; one new original CTM/CXTM contract; 107 PASS, 2 known FAIL, 164 NOT-RUN.~%" (length *last-checks*)) t))
233 (defun write-arity-report (path)
234 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
235 (topic-maps.test-foundation::emit-json-value
236 (topic-maps.test-foundation::fields "kind" "association-arity-evidence" "passed" (length *last-checks*) "failed" 0
237 "checks" (topic-maps.test-foundation::json-array *last-checks*) "original" *original*
238 "witnesses" (topic-maps.test-foundation::json-array *witnesses*)) out)
239 (terpri out)))