(defpackage #:topic-maps.arity.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.ctm.tests #:check #:read-text #:cxtm #:binary-file #:association-invariants) (:import-from #:topic-maps.variant.tests #:condition-from) (:import-from #:topic-maps.identifier.tests #:si) (:export #:run-tests #:arity-checks #:write-arity-report)) (in-package #:topic-maps.arity.tests) (defvar *last-checks* nil) (defvar *original* nil) (defvar *witnesses* nil) (defun arity-checks () *last-checks*) (defun named-topic (map name) (or (find (concatenate 'string "https://example.invalid/a/doc.ctm#" name) (topic-map-topics map) :test (lambda (id topic) (member id (topic-item-identifiers topic) :test #'string=))) (error "Missing fixture Topic ~A" name))) (defun role-with (association type player) (find-if (lambda (r) (and (eq (canonical-topic type) (association-role-type r)) (eq (canonical-topic player) (association-role-player r)))) (association-roles association))) (defun state (map handles) (topic-maps.reification.tests::state map handles)) (defun valid-model-p (map) (topic-maps.reification.tests::invariants map)) (defun witness (label map) (push (topic-maps.test-foundation::fields "label" label "roleCount" (loop for a in (topic-map-associations map) sum (length (association-roles a))) "actualCXTM" (cxtm map)) *witnesses*)) (defun cardinality-tests () (dolist (n '(1 2 3 4 32 64)) (let* ((text (format nil "a(~{~A~^, ~})" (loop for i from 1 to n collect (format nil "r~D: p~D" i i)))) (map (read-text text)) (a (first (topic-map-associations map))) (before (state map (cons a (association-roles a))))) (check (format nil "arity:~D:exact-items" n) (and (= (1+ (* 2 n)) (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= n (length (association-roles a))) (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic)))) (topic-map-topics map)))) (check (format nil "arity:~D:type-and-parent-EQ" n) (and (eq map (association-parent a)) (eq (named-topic map "a") (association-type a)))) (loop for i from 1 to n do (let* ((type (named-topic map (format nil "r~D" i))) (player (named-topic map (format nil "p~D" i))) (r (role-with a type player))) (check (format nil "arity:~D:role:~D:actual-EQ-references" n i) (and r (eq a (association-role-parent r)) (eq r (first (topic-roles-played player))) (eq r (canonical-association-role r)) (eq type (topic-maps.tmdm::%association-role-type r)) (eq player (topic-maps.tmdm::%association-role-player r)))))) (check (format nil "arity:~D:deterministic-and-pure" n) (and (string= (cxtm map) (cxtm map)) (equal before (state map (cons a (association-roles a)) )) (association-invariants map))) (witness (format nil "arity:~D" n) map)))) (defun permutations (items) (if (null items) (list nil) (loop for item in items append (mapcar (lambda (rest) (cons item rest)) (permutations (remove item items :count 1 :test #'equal)))))) (defun permutation-tests () (let ((reference nil)) (loop for order in (permutations '("r1: p1" "r2: p2" "r3: p3" "r4: p4")) for index from 1 do ;; Source multiplicity has no significance, even when >2 declarations ;; normalize to fewer than three Role items. (let* ((source (format nil "a(~{~A~^, ~})" (append order (list (first order) (first order))))) (map (read-text source)) (a (first (topic-map-associations map))) (output (cxtm map))) (unless reference (setf reference output)) (check (format nil "permutation:~D:source-order-and-duplicates" index) (and (= 4 (length (association-roles a))) (string= reference output) (valid-model-p map)))))) (dolist (n '(1 2)) (let* ((roles (loop for i below 12 collect (format nil "r: p~D" (mod i n)))) (map (read-text (format nil "a(~{~A~^, ~})" roles)))) (check (format nil "duplicates:12-declarations-become-~D-roles" n) (and (= n (length (association-roles (first (topic-map-associations map))))) (valid-model-p map))))) (let* ((map (read-text "a(r: p ~x, s: q, t: z, r: p ~y, r: p ~x) @theme ~ar")) (a (first (topic-map-associations map))) (r (role-with a (named-topic map "r") (named-topic map "p")))) (check "duplicates:source-role-reifiers-merge-on-the-same-role" (and (= 3 (length (association-roles a))) (eq (named-topic map "x") (named-topic map "y")) (eq (named-topic map "x") (item-reifier r)) (eq r (topic-reified (named-topic map "y"))) (eq a (topic-reified (named-topic map "ar"))) (equal (association-scope a) (list (named-topic map "theme"))) (valid-model-p map))) (witness "duplicate-role-reifiers-and-scope" map))) (defun reference-tests () (let* ((map (read-text (format nil "%prefix ex http://example.org/~%a(ex:r: ex:p, ^: =, [-\"R\"]: [-\"P\"], ?r: ?p, ?r: ?p) @?theme ~~?ar"))) (a (first (topic-map-associations map))) (roles (association-roles a))) (check "references:qname-item-id-subject-locator-embedded-and-named-wildcards" (and (= 4 (length roles)) (= 1 (length (association-scope a))) (item-reifier a) (valid-model-p map))) (check "references:identity-categories-not-conflated" (some (lambda (r) (and (equal '("urn:r2") (topic-item-identifiers (association-role-type r))) (equal '("urn:p2") (topic-subject-locators (association-role-player r))) (null (topic-subject-identifiers (association-role-player r))))) roles)) (witness "all-existing-topic-reference-forms" map)) (let* ((map (read-text "a(r: ?, r: ?, r: ?, r: ?)")) (a (first (topic-map-associations map)))) (check "references:anonymous-wildcards-remain-distinct-players" (and (= 4 (length (association-roles a))) (= 4 (length (remove-duplicates (mapcar #'association-role-player (association-roles a)) :test #'eq))) (valid-model-p map))))) (defun merge-fixture (axis) (read-text (ecase axis (:player "a(r: p, r: q, s: z) @u ~ar1 a(r: p, s: z) @u ~ar2") (:role-type "a(r: p, s: p, t: z) @u ~ar1 a(r: p, t: z) @u ~ar2") (:association-type "a(r: p, s: q, t: z) @u ~ar1 b(r: p, s: q, t: z) @u ~ar2") (:scope "a(r: p, s: q, t: z) @u,v ~ar1 a(r: p, s: q, t: z) @u ~ar2")))) (defun merge-tests () (dolist (axis '(:player :role-type :association-type :scope)) (dolist (via-identifier '(nil t)) (let ((reference nil)) (dolist (reverse-order '(nil t)) (let* ((map (merge-fixture axis)) (associations (topic-map-associations map)) (roles (loop for a in associations append (association-roles a))) (metadata (loop for r in roles for i from 1 collect (let ((id (format nil "urn:arity:role:~D" i)) (reifier (si map (format nil "arity:reifier:~D" i)))) (add-item-identifier r id) (set-item-reifier r reifier) (list r id reifier)))) (ar1 (named-topic map "ar1")) (ar2 (named-topic map "ar2")) (pair (ecase axis (:player '("p" "q")) (:role-type '("r" "s")) (:association-type '("a" "b")) (:scope '("u" "v")))) (left (named-topic map (first pair))) (right (named-topic map (second pair))) (handles (append (topic-map-topics map) associations roles)) (label (format nil "merge:~A:identifier=~A:reverse=~A" axis via-identifier reverse-order))) (loop for a in associations for i from 1 do (add-item-identifier a (format nil "urn:arity:association:~D" i))) (when reverse-order (rotatef left right)) (if via-identifier (progn (add-topic-identifier left :subject-identifier "urn:arity:join") (add-topic-identifier right :subject-identifier "urn:arity:join")) (merge-topics left right)) (let* ((a (first (topic-map-associations map))) (expected-roles (if (member axis '(:player :role-type)) 2 3))) (check (concatenate 'string label ":association-and-role-collapse") (and (= 1 (length (topic-map-associations map))) (= expected-roles (length (association-roles a))) (every (lambda (old) (and (not (eq old a)) (eq a (canonical-association old)))) associations))) (check (concatenate 'string label ":association-identifiers-and-reifiers") (and (equal '("urn:arity:association:1" "urn:arity:association:2") (sort (item-identifiers a) #'string<)) (eq (canonical-topic ar1) (canonical-topic ar2)) (eq (canonical-topic ar1) (item-reifier a)) (eq a (topic-reified ar2)))) (check (concatenate 'string label ":canonical-scope-and-topic-refs") (and (equal (list (named-topic map "u")) (association-scope a)) (valid-model-p map))) (loop for (old id reifier) in metadata for i from 1 do (let ((r (canonical-association-role old))) (check (format nil "~A:role:~D:identifier-reifier-parent-and-inverse" label i) (and (not (eq r old)) (member r (association-roles a) :test #'eq) (member id (item-identifiers r) :test #'string=) (eq (canonical-topic reifier) (item-reifier r)) (eq r (topic-reified reifier)) (eq a (association-role-parent old)) (member r (topic-roles-played (association-role-player old)) :test #'eq))))) ;; Force another Association/Role replacement to exercise chains ;; of old handles, not only one-step forwarding. (let ((next (make-association map (association-type a) (mapcar (lambda (r) (list (association-role-type r) (association-role-player r))) (reverse (association-roles a))) :scope (association-scope a)))) (check (concatenate 'string label ":further-replacements-resolve-old-handles") (and (not (eq a next)) (every (lambda (old) (eq next (canonical-association old))) associations) (every (lambda (r) (eq next (association-role-parent r))) roles) (valid-model-p map)))) (let* ((before (state map handles)) (output (cxtm map))) (unless reference (setf reference output)) (check (concatenate 'string label ":merge-order-independent-pure-cxtm") (and (string= reference output) (string= output (cxtm map)) (equal before (state map handles))))) (when (and (not via-identifier) (not reverse-order)) (witness label map))))))))) (defun syntax-tests () (dolist (fixture '(("a()" :undefined-template 0) ("a(r: )" :expected-role-player 5) ("a(r: p,)" :expected-role 7) ("a(r: p" :missing-association-close 6) ("a(r: p, s: q, t: )" :expected-role-player 17) ("a(r: p, s: q, t: z,)" :expected-role 19) ("a(r: p, s: q, t: z" :missing-association-close 18))) (destructuring-bind (text code position) fixture (let ((c (condition-from (lambda () (read-text text))))) (check (concatenate 'string "syntax:specific-rejection-and-position:" text) (and (typep c 'topic-maps.ctm:ctm-rejection) (eq code (topic-maps.ctm:error-code c)) (= position (topic-maps.ctm:error-position c)) (= 1 (topic-maps.ctm:error-line c)) (= (1+ position) (topic-maps.ctm:error-column c))))))) (let* ((text (format nil "a(r: p,~% s: q,~% t: )")) (c (condition-from (lambda () (read-text text))))) (check "syntax:third-role-multiline-position" (and (eq (type-of c) 'topic-maps.ctm:ctm-rejection) (eq :expected-role-player (topic-maps.ctm:error-code c)) (= (position #\) text :from-end t) (topic-maps.ctm:error-position c)) (= 3 (topic-maps.ctm:error-line c)) (= 5 (topic-maps.ctm:error-column c))))) (let ((c (condition-from (lambda () (read-text "a(r: p, s: q, bad:r: z)"))))) (check "syntax:third-role-undefined-prefix-is-specific" (and (eq (type-of c) 'topic-maps.ctm:undefined-prefix) (eq :undefined-prefix (topic-maps.ctm:error-code c)) (= 14 (topic-maps.ctm:error-position c))))) (dolist (text '("a isa b." "a ako b.")) (check (concatenate 'string "boundary:17B-now-deserialized:" text) (let* ((map (read-text text)) (a (first (topic-map-associations map)))) (and (= 2 (length (association-roles a))) (valid-model-p map)))))) (defun original-case () (let* ((id "ctm/in/association-duplicate-role.ctm") (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))) (doc (topic-maps.test-foundation:test-case-document-iri case)) (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))) (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id)) (a (first (topic-map-associations map))) (roles (association-roles a)) (before (state map (cons a roles))) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))) (check "original:exact-seven-topics-one-association-three-roles" (and (= 7 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= 3 (length roles)) (valid-model-p map))) (check "original:1157-byte-exact-unmodified-baseline" (and (= 1157 (length expected) (length actual)) (equalp expected actual))) (check "original:serialization-preserves-objects-and-handles" (equal before (state map (cons a roles)))) (topic-maps.test-foundation:record-result case :cxtm-match) (setf (topic-maps.test-foundation:test-case-clauses case) '("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")) (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results* (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case)) :match t :bytes (length actual))))) (setf *original* (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" source "topicCount" (length (topic-map-topics map)) "associationCount" (length (topic-map-associations map)) "typeIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-type a))) "roles" (topic-maps.test-foundation::json-array (loop for r in roles collect (topic-maps.test-foundation::fields "typeIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-role-type r))) "playerIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers (association-role-player r))) "parentIsAssociation" (if (eq a (association-role-parent r)) :true :false) "roleIsCanonical" (if (eq r (canonical-association-role r)) :true :false) "inverseContainsRole" (if (member r (topic-roles-played (association-role-player r)) :test #'eq) :true :false)))))) (format t "~&ARITY-CXTM ~A: PASS (~D bytes)~%" id (length actual)))) (defun run-tests () (topic-maps.reification.tests:run-tests) (let ((topic-maps.ctm.tests::*checks* nil)) (setf *witnesses* nil) (cardinality-tests) (permutation-tests) (reference-tests) (merge-tests) (syntax-tests) (original-case) (check "arity:only-one-imported-contract-transition" (equal '(:pass 107 :fail 2 :unsupported 0 :not-run 164) (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses))) (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*) *witnesses* (nreverse *witnesses*)) (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)) (defun write-arity-report (path) (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value (topic-maps.test-foundation::fields "kind" "association-arity-evidence" "passed" (length *last-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-checks*) "original" *original* "witnesses" (topic-maps.test-foundation::json-array *witnesses*)) out) (terpri out)))