(defpackage #:topic-maps.reference.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.ctm.tests #:check #:read-text #:api-topic #:cxtm #:map-state #:ctm-condition #:binary-file #:local-topic) (:import-from #:topic-maps.variant.tests #:condition-from #:handle-state) (:export #:run-tests #:reference-checks #:write-reference-report)) (in-package #:topic-maps.reference.tests) (defparameter *originals* '(("wildcard" 1) ("wildcard-named" 1) ("wildcard-named2" 3) ("wildcard-named-merge" 1) ("association-scope-embedded" 2) ("association-scope-embedded2" 3) ("association-type-embedded" 1) ("association-type-embedded-merge" 2) ("embedded-semicolon" 1) ("name-scope-embedded" 2) ("name-scope-embedded2" 3) ("name-type-embedded" 1) ("nested-twice" 2) ("occurrence-scope-embedded" 2) ("occurrence-scope-embedded2" 3) ("occurrence-type-embedded" 1) ("role-player-embedded" 1) ("role-type-embedded" 1) ("subject-identifier-embedded" 1) ("variant-scope-embedded" 3) ("variant-scope-embedded2" 4))) (defvar *last-checks* nil) (defvar *observations* nil) (defun reference-checks () *last-checks*) (defun by-si (map iri) (find-if (lambda (topic) (member iri (topic-subject-identifiers topic) :test #'string=)) (topic-map-topics map))) (defun by-label (map label) (find-if (lambda (topic) (find label (topic-names topic) :key #'topic-name-value :test #'string=)) (topic-map-topics map))) (defun references-current-p (map) (let ((topics (topic-map-topics map))) (labels ((current (topic) (and (eq topic (canonical-topic topic)) (eq map (topic-parent topic)) (member topic topics :test #'eq)))) (and (every #'current topics) (topic-maps.ctm.tests::scope-ref-invariants map) (topic-maps.variant.tests::variant-invariants map) (every (lambda (topic) (and (every (lambda (n) (and (eq n (canonical-topic-name n)) (eq topic (topic-maps.tmdm::%topic-name-parent n)) (current (topic-maps.tmdm::%topic-name-type n)))) (topic-names topic)) (every (lambda (o) (and (eq o (canonical-occurrence o)) (eq topic (topic-maps.tmdm::%occurrence-parent o)) (current (topic-maps.tmdm::%occurrence-type o)))) (topic-occurrences topic)))) topics) (every (lambda (a) (and (eq a (canonical-association a)) (eq map (association-parent a)) (current (topic-maps.tmdm::%association-type a)) (every (lambda (r) (and (eq r (canonical-association-role r)) (eq a (topic-maps.tmdm::%association-role-parent r)) (current (topic-maps.tmdm::%association-role-type r)) (current (topic-maps.tmdm::%association-role-player r)))) (association-roles a)))) (topic-map-associations map)))))) (defun original-cases () (setf *observations* nil) (dolist (fixture *originals*) (let* ((id (format nil "ctm/in/~A.ctm" (first fixture))) (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))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id) (let* ((before (map-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))) (check (concatenate 'string id ":normative-counter") (= counter (second fixture))) (check (concatenate 'string id ":actual-and-stored-reference-identity") (references-current-p map)) (check (concatenate 'string id ":all-topics-identified") (every (lambda (topic) (or (topic-item-identifiers topic) (topic-subject-identifiers topic) (topic-subject-locators topic))) (topic-map-topics map))) (check (concatenate 'string id ":byte-exact-cxtm") (equalp actual expected)) (check (concatenate 'string id ":deterministic-and-mutation-free") (and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal before (map-state map)))) (topic-maps.test-foundation:record-result case :cxtm-match) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.2" "CTM 3.3.6-3.3.9" "CTM 3.9-3.13" "TMDM 5.1" "TMDM 5.3-5.8" "TMDM 6.1-6.7" "CXTM 3.4-3.21" "CXTM 4.3-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) :external-format :utf-8) :match t :bytes (length actual))))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "wildcardIRI" doc "finalCounter" counter "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "scopedObjects" (topic-maps.ctm.tests::scope-observation map) "nameObjects" (topic-maps.variant.tests::observe-map map)) *observations*) (format t "~&REFERENCE-CXTM ~A: PASS (~D bytes), counter ~D~%" id (length actual) counter))))) (let* ((id "ctm/invalid/embedded-topic-global-scope.ctm") (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))) (text (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))) (e (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id))))) (check "embedded-global:original-specific-rejection-and-position" (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq :embedded-topic-at-top-level (topic-maps.ctm:error-code e)) (= (length text) (topic-maps.ctm:error-position e)) (= 1 (topic-maps.ctm:error-line e)) (= (1+ (length text)) (topic-maps.ctm:error-column e)))) (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string e))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.3.7" "CTM 3.5" "CTM 3.8"))) (setf *observations* (nreverse *observations*))) (defun wildcard-tests () (let ((doc "https://example.invalid/doc.ctm") (wild "https://identity.example.invalid/wild.ctm")) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string "?. ?foo. local - [-\"Type\"]: \"N\" @?foo; t: @?. ?foo." :document-iri doc :wildcard-iri wild :wildcard-counter 10) (let* ((topics (topic-map-topics map)) (local (local-topic map "local" doc)) (name (first (topic-names local))) (o (first (topic-occurrences local))) (foo (find (concatenate 'string wild "#$__12.foo") topics :test #'string= :key (lambda (x) (first (topic-item-identifiers x)))))) (check "wildcards:document-and-wildcard-iri-are-independent" (and (= 14 counter) (equal (list (concatenate 'string wild "#$__13")) (topic-item-identifiers (topic-name-type name))) (eq foo (first (topic-name-scope name))) (equal (list (concatenate 'string wild "#$__14")) (topic-item-identifiers (first (occurrence-scope o)))) (string= "https://example.invalid/relative" (occurrence-value o)) (eq (local-topic map "t" doc) (occurrence-type o)) (references-current-p map))) (check "wildcards:no-extra-generated-subject-identifiers" (every (lambda (topic) (if (member (default-name-type-identifier) (topic-subject-identifiers topic) :test #'string=) t (and (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic))))) topics)))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string "?X. ?x. ?Ω. ?Ω. ?. ?. X." :document-iri doc) (check "wildcards:case-unicode-anonymous-uniqueness-and-local-namespace" (and (= 5 counter) (= 6 (length (topic-map-topics map))) (equal (mapcar (lambda (x) (list (concatenate 'string doc x))) '("#$__1.X" "#$__2.x" "#$__3.Ω" "#$__4" "#$__5" "#X")) (mapcar #'topic-item-identifiers (topic-map-topics map)))))) (let ((initial (expt 10 100))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string "?." :document-iri doc :wildcard-counter initial) (check "wildcards:arbitrary-precision-counter" (and (= (1+ initial) counter) (equal (list (format nil "~A#$__~D" doc (1+ initial))) (topic-item-identifiers (first (topic-map-topics map)))))))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string "" :document-iri doc :wildcard-counter 42) (check "wildcards:empty-document-retains-counter" (and (= 42 counter) (null (topic-map-topics map))))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-file (asdf:system-relative-pathname "topic-maps" "vendor/cxtm/ctm/in/wildcard-named2.ctm") :document-iri doc :wildcard-iri wild :wildcard-counter 100) (check "wildcards:file-api-forwards-environment-and-final-counter" (and (= 103 counter) (equal (list (concatenate 'string wild "#$__101.foo")) (topic-item-identifiers (first (topic-map-topics map)))))))) (let* ((text "?foo . alias . a - ?foo:\"N\".") (map (read-text text)) (alias (local-topic map "alias")) (name (first (topic-names (local-topic map "a"))))) (check "wildcards:named-locator-resolves-after-topic-replacement" (eq alias (topic-name-type name))) (check "wildcards:identity-properties-preserved-after-merge" (and (= 2 (length (topic-item-identifiers alias))) (equal '("urn:shared") (topic-subject-identifiers alias)) (references-current-p map)))) (let ((first (read-text "?foo.")) (second (read-text "?foo."))) (check "wildcards:environment-is-per-deserialization-not-global" (and (not (eq (first (topic-map-topics first)) (first (topic-map-topics second)))) (equal (topic-item-identifiers (first (topic-map-topics first))) (topic-item-identifiers (first (topic-map-topics second))))))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string "?owner - ?type:\"N\"@?scope(\"V\"@?extra); ?occ: \"O\"@?scope. ?assoc(?role: ?owner)@?scope" :document-iri "https://example.invalid/wild-positions.ctm") (labels ((w (number name) (find-if (lambda (topic) (member (format nil "https://example.invalid/wild-positions.ctm#$__~D.~A" number name) (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))) (let* ((owner (w 1 "owner")) (n (first (topic-names owner))) (o (first (topic-occurrences owner))) (a (first (topic-map-associations map))) (r (first (association-roles a))) (theme (w 3 "scope"))) (check "wildcards:all-type-player-scope-positions-share-one-environment" (and (= 7 counter) (= 7 (length (topic-map-topics map))) (every #'identity (list owner (w 2 "type") theme (w 4 "extra") (w 5 "occ") (w 6 "assoc") (w 7 "role"))) (eq owner (topic-name-parent n)) (eq (w 2 "type") (topic-name-type n)) (eq (w 5 "occ") (occurrence-type o)) (eq (w 6 "assoc") (association-type a)) (eq (w 7 "role") (association-role-type r)) (eq owner (association-role-player r)) (every (lambda (scope) (equal (list theme) scope)) (list (topic-name-scope n) (occurrence-scope o) (association-scope a))) (member theme (variant-scope (first (topic-name-variants n)))) (member (w 4 "extra") (variant-scope (first (topic-name-variants n)))) (references-current-p map)))))) (dolist (fixture '((:wildcard-iri nil :invalid-wildcard-iri) (:wildcard-iri "relative" :invalid-wildcard-iri) (:wildcard-iri "https://example.invalid/doc#fragment" :invalid-wildcard-iri) (:wildcard-counter -1 :invalid-wildcard-counter) (:wildcard-counter 1.0 :invalid-wildcard-counter) (:wildcard-counter nil :invalid-wildcard-counter))) (dolist (file-p '(nil t)) (let ((e (condition-from (lambda () (apply (if file-p #'topic-maps.ctm:deserialize-file #'topic-maps.ctm:deserialize-string) (if file-p "this-file-must-not-be-opened.invalid" "?.") :document-iri "https://example.invalid/doc.ctm" (subseq fixture 0 2)))))) (check (format nil "wildcards:environment:~S:file=~A:before-reading" fixture file-p) (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq (third fixture) (topic-maps.ctm:error-code e)) (= 0 (topic-maps.ctm:error-position e))))))) (check "wildcards:valid-environment-does-not-hide-file-errors" (typep (condition-from (lambda () (topic-maps.ctm:deserialize-file "this-file-must-not-be-opened.invalid" :document-iri "https://example.invalid/doc.ctm"))) 'file-error))) (defun embedded-tests () (let* ((map (read-text "[; - \"AT\"]([; - \"RT\"]: [; - \"P\"]) @[; - \"S\"] owner - [; - \"NT\"]:\"N\"@[; - \"S\"] (\"V\"@[; - \"V\"]); [; - \"OT\"]: 1@[; - \"S\"].")) (a (first (topic-map-associations map))) (r (first (association-roles a))) (owner (local-topic map "owner")) (n (first (topic-names owner))) (v (first (topic-name-variants n))) (o (first (topic-occurrences owner))) (theme (by-si map "urn:s"))) (check "embedded:all-reference-positions-use-actual-topics" (and (eq (by-si map "urn:at") (association-type a)) (eq (by-si map "urn:rt") (association-role-type r)) (eq (by-si map "urn:p") (association-role-player r)) (eq (by-si map "urn:nt") (topic-name-type n)) (eq (by-si map "urn:ot") (occurrence-type o)) (every (lambda (scope) (equal (list theme) scope)) (list (association-scope a) (topic-name-scope n) (occurrence-scope o))) (member theme (variant-scope v) :test #'eq) (member (by-si map "urn:v") (variant-scope v) :test #'eq) (eq n (variant-parent v)) (eq owner (occurrence-parent o)) (references-current-p map))) (check "embedded:identities-union-after-repeated-subject-identifier" (and (= 3 (length (topic-item-identifiers theme))) (= 9 (length (topic-map-topics map))))) (let ((before (map-state map))) (check "embedded:canonicalization-leaves-recursive-model-unchanged" (and (string= (cxtm map) (cxtm map)) (equal before (map-state map)))))) (dolist (literal '("1" "1.0" "2000-01-01" "2000-01-01T00:00:00Z")) (let* ((map (read-text (format nil "a - [t: ~A]:\"N\"." literal))) (n (first (topic-names (local-topic map "a")))) (o (first (topic-occurrences (topic-name-type n))))) (check (concatenate 'string "embedded:numeric-token-stops-before-close:" literal) (and (string= literal (occurrence-value o)) (references-current-p map))))) (dolist (depth '(1 2 5 20 40)) (let ((text "[-\"leaf\"]")) (dotimes (i (1- depth)) (setf text (format nil "[-~A:\"level\"]" text))) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string (format nil "a - ~A:\"Root\"." text) :document-iri "https://example.invalid/deep.ctm") (check (format nil "embedded:recursive-depth:~D:preorder-counter-and-identities" depth) (and (= depth counter) (= (+ depth 2) (length (topic-map-topics map))) (references-current-p map))) (let ((topic (local-topic map "a" "https://example.invalid/deep.ctm"))) (dotimes (i depth) (setf topic (topic-name-type (first (topic-names topic)))) (check (format nil "embedded:depth:~D:type-chain:~D" depth i) (equal (list (format nil "https://example.invalid/deep.ctm#$__~D" (1+ i))) (topic-item-identifiers topic))))))))) (defun merge-tests () (let* ((map (read-text "one - []:\"N\"@?a(\"V\"@?b). two - []:\"N\"@?a(\"V\"@?b). rel(role: one)")) (one (local-topic map "one")) (two (local-topic map "two")) (n1 (first (topic-names one))) (n2 (first (topic-names two))) (v1 (first (topic-name-variants n1))) (v2 (first (topic-name-variants n2))) (role (first (association-roles (first (topic-map-associations map)))))) (merge-topics one two) (merge-topics (by-si map "urn:type1") (by-si map "urn:type2")) (check "references:merge-updates-parent-type-player-and-retained-name-variant-handles" (and (eq (canonical-topic-name n1) (canonical-topic-name n2)) (eq (canonical-variant v1) (canonical-variant v2)) (eq (canonical-topic one) (topic-name-parent n1)) (eq (canonical-topic one) (association-role-player role)) (eq (canonical-topic-name n1) (variant-parent v1)) (references-current-p map))) (let ((before (map-state map)) (old (handle-state (list v1 v2))) (forward (list (topic-maps.tmdm::%topic-forward one) (topic-maps.tmdm::%topic-name-forward n1)))) (check "references:cxTM-does-not-change-retained-forward-links" (and (string= (cxtm map) (cxtm map)) (equal before (map-state map)) (equal old (handle-state (list v1 v2))) (equal forward (list (topic-maps.tmdm::%topic-forward one) (topic-maps.tmdm::%topic-name-forward n1))))))) (let* ((map (read-text "owner - \"N\"@[-\"A\"],[-\"B\"] (\"V\"@[-\"X\"]).")) (n (first (topic-names (local-topic map "owner")))) (v (first (topic-name-variants n))) (a (by-label map "A")) (b (by-label map "B")) (x (by-label map "X"))) (merge-topics a b) (check "references:embedded-theme-merge-reduces-scope-cardinality" (and (= 1 (length (topic-name-scope n))) (= 2 (length (variant-scope v))) (references-current-p map))) (let ((before (map-state map)) (e (condition-from (lambda () (merge-topics a x))))) (check "references:embedded-theme-collapse-remains-atomic" (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e)) (equal before (map-state map))))))) (defun diagnostic-tests () (dolist (fixture '(("a - []:\"N\"." :expected-topic-tail) ("a - [-\"T\".:\"N\"." :missing-embedded-close) ("a - [-\"T\"" :missing-embedded-close) ("[-\"T\"]." :embedded-topic-at-top-level) ("a t:[-\"bad\"]." :expected-occurrence-value) ("a t:\"V\"^^[-\"bad\"]." :expected-datatype-iri) ("a - [?]:\"N\"." :expected-occurrence-type-separator) ("a - [;]:\"N\"." :unexpected-token) ("a([-\"r\"]:p)" :role-separator-whitespace) ("a - \"N\"@[](\"V\"@[])." :invalid-variant-scope) ("a - \"N\"@[](\"V\"@[]). t - \"T\"@[;]." :variant-scope-collapse))) (let ((e (ctm-condition (first fixture)))) (check (format nil "reference-diagnostic:~A" (first fixture)) (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq (second fixture) (topic-maps.ctm:error-code e)) (<= 0 (topic-maps.ctm:error-position e) (length (first fixture))) (= 1 (topic-maps.ctm:error-line e)) (= (1+ (topic-maps.ctm:error-position e)) (topic-maps.ctm:error-column e)))))) (let ((e (ctm-condition (format nil "a - [~%]:\"N\".")))) (check "reference-diagnostic:empty-embedded-exact-line-column" (and (eq :expected-topic-tail (topic-maps.ctm:error-code e)) (= 2 (topic-maps.ctm:error-line e)) (= 1 (topic-maps.ctm:error-column e)) (= 6 (topic-maps.ctm:error-position e))))) (dolist (text '("a([-\"r\"] :p)" "a([-\"r\"]: p)" "a([-\"r\"]#(gap)#:[-\"p\"])")) (check (concatenate 'string "embedded:role-layout-preserved:" text) (references-current-p (read-text text)))) (dolist (text '("a - [-\"N\" ~r]:\"X\"." "a - \"N\"@[-\"T\"] ~r." "def tpl() a. end" "%include " "%mergemap " "a isa b." "a ako b." "a(r: b, s: c, t: d)")) (check (concatenate 'string "references:excluded-remains-unsupported:" text) (typep (ctm-condition text) 'topic-maps.ctm:ctm-unsupported))) (check "references:template-dependent-negative-not-promoted" (eq :not-run (topic-maps.test-foundation:test-case-status (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id "ctm/invalid/embedded-topic-template.ctm")))))) (defun run-tests () (topic-maps.variant.tests:run-tests) (let ((topic-maps.ctm.tests::*checks* nil)) (original-cases) (wildcard-tests) (embedded-tests) (merge-tests) (diagnostic-tests) (check "references:only-22-original-contract-transitions" (equal '(:pass 93 :fail 2 :unsupported 0 :not-run 178) (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses))) (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*)) (format t "~&REFERENCES-PASS: ~D checks; 21 original byte-exact comparisons and 1 specific rejection; 93 PASS, 2 known FAIL, 178 NOT-RUN.~%" (length *last-checks*)) t)) (defun write-reference-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" "topic-reference-evidence" "passed" (length *last-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-checks*) "cases" (topic-maps.test-foundation::json-array *observations*)) out) (terpri out)))