]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-associations.lisp
feat(xtm): support resource references on name variants
[topic-maps.git] / tests / ctm-associations.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-association-checks* nil)
4 (defparameter *association-files*
5 '("association.ctm" "association-binary.ctm" "association-binary-duplicate.ctm"
6 "association-type-sid.ctm" "association-with-comment.ctm" "association-with-comment2.ctm"
7 "association-with-comment3.ctm" "association-with-comment4.ctm"))
8
9 (defun association-invariants (map)
10 (let* ((topics (topic-map-topics map)) (associations (topic-maps.tmdm:topic-map-associations map))
11 (roles (loop for a in associations append (topic-maps.tmdm:association-roles a))))
12 (and (raw-reference-invariants map)
13 (every (lambda (a)
14 (and (eq a (topic-maps.tmdm:canonical-association a))
15 (eq map (topic-maps.tmdm::%association-parent a))
16 (member (topic-maps.tmdm::%association-type a) topics :test #'eq)
17 (topic-maps.tmdm:association-roles a)
18 (every (lambda (r)
19 (and (eq r (topic-maps.tmdm:canonical-association-role r))
20 (eq a (topic-maps.tmdm::%association-role-parent r))
21 (member (topic-maps.tmdm::%association-role-type r) topics :test #'eq)
22 (member (topic-maps.tmdm::%association-role-player r) topics :test #'eq)))
23 (topic-maps.tmdm:association-roles a)))) associations)
24 (= (length roles) (length (remove-duplicates roles :test #'eq)))
25 (every (lambda (topic)
26 (let ((played (topic-maps.tmdm:topic-roles-played topic))
27 (expected (remove-if-not (lambda (r) (eq topic (topic-maps.tmdm::%association-role-player r))) roles)))
28 (and (= (length played) (length expected))
29 (= (length played) (length (remove-duplicates played :test #'eq)))
30 (every (lambda (r) (member r expected :test #'eq)) played)
31 (every (lambda (r) (member r played :test #'eq)) expected)))) topics))))
32
33 (defun exercise-association-corpus ()
34 (let ((corpus (topic-maps.test-foundation:discover-corpus)) (observations nil))
35 (dolist (file *association-files*)
36 (let* ((id (concatenate 'string "ctm/in/" file))
37 (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
38 (document (topic-maps.test-foundation:test-case-document-iri case))
39 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
40 :document-iri document :source-name id))
41 (binary (member file '("association-binary.ctm" "association-binary-duplicate.ctm") :test #'string=))
42 (association (first (topic-maps.tmdm:topic-map-associations map))))
43 (check (concatenate 'string id ":topic-count") (= (length (topic-map-topics map)) (if binary 5 3)))
44 (check (concatenate 'string id ":association-count") (= 1 (length (topic-maps.tmdm:topic-map-associations map))))
45 (check (concatenate 'string id ":role-count") (= (length (topic-maps.tmdm:association-roles association)) (if binary 2 1)))
46 (check (concatenate 'string id ":actual-information-items")
47 (and (typep association 'topic-maps.tmdm:association)
48 (every (lambda (r) (typep r 'topic-maps.tmdm:association-role)) (topic-maps.tmdm:association-roles association))))
49 (check (concatenate 'string id ":raw-references-and-roles-played") (association-invariants map))
50 (check (concatenate 'string id ":no-unwarranted-name-items") (every (lambda (topic) (null (topic-names topic))) (topic-map-topics map)))
51 (check (concatenate 'string id ":canonical-contract-not-run")
52 (and (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
53 (eq :not-run (topic-maps.test-foundation:test-case-status case))
54 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
55 (push (list :id id :document-iri document :topic-map map) observations)))
56 (nreverse observations)))
57
58 (defun api-topic (map name) (topic-maps.tmdm:intern-topic map :item-identifier (concatenate 'string "urn:" name)))
59
60 (defun run-association-tests ()
61 (let ((*checks* nil))
62 (exercise-association-corpus)
63 ;; Acceptance matrix: every nonempty subset of TYPE, ROLE TYPE and PLAYER
64 ;; refers to A; B's later identity causes A/B replacement after construction.
65 (loop for mask from 1 to 7 do
66 (let* ((map (topic-maps.tmdm:make-topic-map)) (a (api-topic map "a")) (b (api-topic map "b"))
67 (x (api-topic map "x")) (y (api-topic map "y")) (z (api-topic map "z"))
68 (at (if (logbitp 0 mask) a x)) (rt (if (logbitp 1 mask) a y)) (p (if (logbitp 2 mask) a z))
69 (association (topic-maps.tmdm:make-association map at (list (list rt p))))
70 (role (first (topic-maps.tmdm:association-roles association))))
71 (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared")
72 (let ((c (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared")))
73 (check (format nil "acceptance-type-reference:~D" mask)
74 (eq (if (logbitp 0 mask) c x) (topic-maps.tmdm::%association-type association)))
75 (check (format nil "acceptance-role-type-reference:~D" mask)
76 (eq (if (logbitp 1 mask) c y) (topic-maps.tmdm::%association-role-type role)))
77 (check (format nil "acceptance-player-reference:~D" mask)
78 (eq (if (logbitp 2 mask) c z) (topic-maps.tmdm::%association-role-player role)))
79 (check (format nil "acceptance-retained-handles:~D" mask)
80 (and (eq association (first (topic-maps.tmdm:topic-map-associations map)))
81 (eq role (first (topic-maps.tmdm:association-roles association)))
82 (eq c (topic-maps.tmdm:canonical-topic a)) (eq c (topic-maps.tmdm:canonical-topic b))))
83 (check (format nil "acceptance-model-integrity:~D" mask) (association-invariants map))
84 (check (format nil "acceptance-inverse-through-old-topic:~D" mask)
85 (equal (topic-maps.tmdm:topic-roles-played a) (topic-maps.tmdm:topic-roles-played c))))))
86 (let* ((map (read-text "a - \"Name\".")) (a (first-topic map))
87 (type (second (topic-map-topics map))) (name (first (topic-names a)))
88 (association (topic-maps.tmdm:make-association map type (list (list a type))))
89 (role (first (topic-maps.tmdm:association-roles association)))
90 (c (topic-maps.tmdm:merge-topics a type)))
91 (check "combined-name-parent-reference" (eq c (topic-maps.tmdm::%topic-name-parent name)))
92 (check "combined-name-type-reference" (eq c (topic-maps.tmdm::%topic-name-type name)))
93 (check "combined-association-type-reference" (eq c (topic-maps.tmdm::%association-type association)))
94 (check "combined-role-type-and-player-references"
95 (and (eq c (topic-maps.tmdm::%association-role-type role)) (eq c (topic-maps.tmdm::%association-role-player role))))
96 (check "combined-information-item-integrity"
97 (and (= 1 (length (topic-map-topics map))) (= 1 (length (topic-names c)))
98 (= 1 (length (topic-maps.tmdm:topic-map-associations map))) (association-invariants map))))
99 (let* ((map (read-text "a(r: a) b."))
100 (a (first-topic map)) (r (second (topic-map-topics map))) (b (third (topic-map-topics map)))
101 (association (first (topic-maps.tmdm:topic-map-associations map)))
102 (role (first (topic-maps.tmdm:association-roles association))))
103 ;; All three reference properties converge to one Topic through two merges.
104 (let ((ab (topic-maps.tmdm:merge-topics a b)))
105 (let ((c (topic-maps.tmdm:merge-topics ab r)))
106 (check "association-transitive-three-reference-kinds"
107 (and (eq c (topic-maps.tmdm:association-type association))
108 (eq c (topic-maps.tmdm:association-role-type role))
109 (eq c (topic-maps.tmdm:association-role-player role))))
110 (check "self-referential-association-no-extra-items" (= 1 (length (topic-map-topics map))))
111 (check "self-referential-association-raw-invariants" (association-invariants map)))))
112 (dolist (text '("a(r: p1, r: p2)" "a(r1: p, r2: p)"))
113 (let* ((map (read-text text)) (association (first (topic-maps.tmdm:topic-map-associations map)))
114 (roles (topic-maps.tmdm:association-roles association)) (ra (first roles)) (rb (second roles))
115 (player-change (string= text "a(r: p1, r: p2)")))
116 (topic-maps.tmdm:merge-topics
117 (if player-change (topic-maps.tmdm:association-role-player ra) (topic-maps.tmdm:association-role-type ra))
118 (if player-change (topic-maps.tmdm:association-role-player rb) (topic-maps.tmdm:association-role-type rb)))
119 (let ((role (first (topic-maps.tmdm:association-roles association))))
120 (check (concatenate 'string "late-role-merge-new-item:" text)
121 (and (= 1 (length (topic-maps.tmdm:association-roles association))) (not (eq role ra)) (not (eq role rb))))
122 (check (concatenate 'string "late-role-merge-handles:" text)
123 (and (eq role (topic-maps.tmdm:canonical-association-role ra)) (eq role (topic-maps.tmdm:canonical-association-role rb))))
124 (check (concatenate 'string "late-role-merge-inverse:" text)
125 (= 1 (length (topic-maps.tmdm:topic-roles-played (topic-maps.tmdm:association-role-player role)))))
126 (check (concatenate 'string "late-role-merge-integrity:" text) (association-invariants map)))))
127 (let* ((map (read-text "a(r: p) b(r: p)"))
128 (associations (topic-maps.tmdm:topic-map-associations map)) (aa (first associations)) (ab (second associations))
129 (ra (first (topic-maps.tmdm:association-roles aa))) (rb (first (topic-maps.tmdm:association-roles ab))))
130 (check "equal-role-signatures-different-parents-stay-separate"
131 (and (not (eq ra rb)) (= 2 (length (topic-maps.tmdm:topic-roles-played (topic-maps.tmdm:association-role-player ra))))))
132 (let ((type (topic-maps.tmdm:merge-topics (topic-maps.tmdm:association-type aa) (topic-maps.tmdm:association-type ab))))
133 (let ((c (first (topic-maps.tmdm:topic-map-associations map))))
134 (check "late-association-merge-new-item" (and (= 1 (length (topic-maps.tmdm:topic-map-associations map))) (not (eq c aa)) (not (eq c ab))))
135 (check "retained-association-handles-resolve"
136 (and (eq c (topic-maps.tmdm:canonical-association aa)) (eq c (topic-maps.tmdm:canonical-association ab))))
137 (check "retained-role-parents-after-association-merge"
138 (and (eq c (topic-maps.tmdm:association-role-parent ra)) (eq c (topic-maps.tmdm:association-role-parent rb))))
139 (check "discarded-role-handle-resolves-to-live-role"
140 (eq (topic-maps.tmdm:canonical-association-role ra) (topic-maps.tmdm:canonical-association-role rb)))
141 (let* ((next (api-topic map "next")) (merged (topic-maps.tmdm:merge-topics type next)))
142 (check "obsolete-association-getters-follow-further-topic-merges"
143 (and (eq merged (topic-maps.tmdm:association-type aa)) (eq merged (topic-maps.tmdm:association-type ab)))))
144 (check "late-association-merge-raw-invariants" (association-invariants map)))))
145 (let ((map (read-text "a(r1: p1, r2: p2) a(r2: p2, r1: p1)")))
146 (check "association-equality-ignores-role-order" (= 1 (length (topic-maps.tmdm:topic-map-associations map))))
147 (check "role-order-inverse-and-parent-integrity" (association-invariants map)))
148 (let* ((map (read-text "a(r: p, r: p)")) (association (first (topic-maps.tmdm:topic-map-associations map))))
149 (check "duplicate-role-in-binary-declaration-normalized" (= 1 (length (topic-maps.tmdm:association-roles association)))))
150 (let* ((map (read-text "a. b.")) (a (first-topic map)) (b (second (topic-map-topics map)))
151 (c (topic-maps.tmdm:merge-topics a b))
152 (association (topic-maps.tmdm:make-association map a (list (list b a))))
153 (role (first (topic-maps.tmdm:association-roles association))))
154 (check "constructor-resolves-obsolete-topic-handles"
155 (and (eq c (topic-maps.tmdm::%association-type association)) (eq c (topic-maps.tmdm::%association-role-type role))
156 (eq c (topic-maps.tmdm::%association-role-player role)))))
157 (let* ((map (read-text "a.")) (other (read-text "b.")) (a (first-topic map)) (b (first-topic other)))
158 (loop for fixture in (list (list b (list (list a a))) (list a (list (list b a))) (list a (list (list a b))) (list a nil))
159 for i from 1 do
160 (check (format nil "constructor-rejects-invalid-map-or-empty-role:~D" i)
161 (handler-case (progn (topic-maps.tmdm:make-association map (first fixture) (second fixture)) nil) (error () t))))
162 (check "invalid-construction-is-atomic" (and (null (topic-maps.tmdm:topic-map-associations map)) (null (topic-maps.tmdm:topic-map-associations other)))))
163 (dolist (text '("a()" "a(r: )" "a(r p)" "a(r: p" "a(r: p,)" "a(r: p s: q)" "a(<urn:r>:<urn:p>)"))
164 (check (concatenate 'string "malformed-association-is-rejection:" text) (typep (rejection text) 'ctm-rejection)))
165 (check "role-separator-needs-layout"
166 (eq :role-separator-whitespace (error-code (rejection "a(<urn:r>:<urn:p>)"))))
167 (let ((condition (rejection (format nil "a(r p)"))))
168 (check "role-syntax-diagnostic-position"
169 (and (eq :missing-template-close (error-code condition)) (= 4 (error-position condition))
170 (= 1 (error-line condition)) (= 5 (error-column condition)))))
171 (let ((condition (rejection "a(r: ex:p)")))
172 (check "association-undefined-prefix-remains-specific" (and (typep condition 'undefined-prefix) (eq :undefined-prefix (error-code condition)))))
173 (check "scoped-association-now-deserialized"
174 (= 1 (length (topic-maps.tmdm:association-scope
175 (first (topic-maps.tmdm:topic-map-associations (read-text "a(r: p) @theme")))))))
176 (dolist (text '("a(r: p) ~reifier" "a(r: p, s: q, t: z)"))
177 (check (concatenate 'string "association-feature-boundary:" text)
178 (association-invariants (read-text text))))
179 (check "comment-can-separate-role-colon"
180 (association-invariants (read-text "a(r#( separator )#:p)")))
181 (check "delimited-iri-role-references"
182 (association-invariants (read-text "<urn:a>(<urn:r>: <urn:p>)")))
183 (run-association-graph-checks)
184 (setf *last-association-checks* (nreverse *checks*))
185 (format t "~&CTM-ASSOCIATIONS-SEMANTICS-PASS: ~D checks, 8 pinned cases; no CXTM equality passes.~%" (length *last-association-checks*))
186 t))