(in-package #:topic-maps.ctm.tests) (defparameter *occurrence-ids* '("occurrence-string" "occurrence-string2" "occurrence-uri-relative" "occurrence-two")) (defvar *last-occurrence-checks* nil) (defvar *occurrence-observations* nil) (defun occurrence-checks () *last-occurrence-checks*) (defun all-occurrences (map) (mapcan #'topic-maps.tmdm:topic-occurrences (topic-map-topics map))) (defun occurrence-handle-state (items) (loop for o in items collect (list o (topic-maps.tmdm::%occurrence-forward o) (topic-maps.tmdm::%occurrence-parent o) (topic-maps.tmdm::%occurrence-type o) (topic-maps.tmdm::%occurrence-value o) (topic-maps.tmdm::%occurrence-datatype o) (copy-list (topic-maps.tmdm::%occurrence-item-identifiers o))))) (defun local-topic (map id &optional (doc "https://example.invalid/a/doc.ctm")) (find-if (lambda (topic) (member (concatenate 'string doc "#" id) (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map))) (defun occurrence-condition (thunk) (handler-case (progn (funcall thunk) nil) (topic-maps.tmdm:occurrence-error (c) c))) (defun ctm-condition (text) (handler-case (progn (read-text text) nil) (ctm-error (c) c))) (defun occurrence-observation (map) (let ((topics (topic-map-topics map))) (topic-maps.test-foundation::fields "topicCount" (length topics) "occurrenceCount" (length (all-occurrences map)) "nameCount" (reduce #'+ topics :key (lambda (x) (length (topic-names x))) :initial-value 0) "associationCount" (length (topic-maps.tmdm:topic-map-associations map)) "topics" (topic-maps.test-foundation::json-array (loop for topic in topics for ordinal from 1 collect (topic-maps.test-foundation::fields "ordinal" ordinal "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers topic)) "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers topic)) "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators topic)) "occurrences" (topic-maps.test-foundation::json-array (loop for o in (topic-maps.tmdm:topic-occurrences topic) collect (topic-maps.test-foundation::fields "value" (topic-maps.tmdm:occurrence-value o) "datatype" (topic-maps.tmdm:occurrence-datatype o) "parentTopicOrdinal" (1+ (position (topic-maps.tmdm:occurrence-parent o) topics :test #'eq)) "typeTopicOrdinal" (1+ (position (topic-maps.tmdm:occurrence-type o) topics :test #'eq)) "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-maps.tmdm:occurrence-item-identifiers o))))))))))) (defun exercise-occurrence-cases () (setf *occurrence-observations* nil) (dolist (name *occurrence-ids*) (let* ((id (format nil "ctm/in/~A.ctm" name)) (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id))) (doc (topic-maps.test-foundation:test-case-document-iri case)) (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id)) (two (string= name "occurrence-two")) (owner (local-topic map (if two "tmcl-schema" "topic") doc)) (items (topic-maps.tmdm:topic-occurrences owner))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.2" "CTM 3.3.3" "CTM 3.3.5-3.3.7" "CTM 3.3.11" "CTM 3.4.1-3.4.2" "CTM 3.8-3.10" "TMDM 4.3" "TMDM 5.1" "TMDM 5.3" "TMDM 5.6" "TMDM 6.2" "TMDM 6.5" "CXTM 3.5" "CXTM 3.8" "CXTM 3.13-3.23" "CXTM 4.2-4.6" "CXTM 4.9")) (check (concatenate 'string name ":topic-count") (= (if two 3 2) (length (topic-map-topics map)))) (check (concatenate 'string name ":occurrence-count") (= (if two 2 1) (length items))) (check (concatenate 'string name ":no-extra-items") (and (= (length items) (length (all-occurrences map))) (notany #'topic-names (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string name ":topic-identifiers") (every (lambda (tpc) (and (= 1 (length (topic-item-identifiers tpc))) (null (topic-subject-identifiers tpc)) (null (topic-subject-locators tpc)))) (topic-map-topics map))) (check (concatenate 'string name ":parents-and-empty-identifiers") (every (lambda (o) (and (eq owner (topic-maps.tmdm::%occurrence-parent o)) (eq o (topic-maps.tmdm:canonical-occurrence o)) (null (topic-maps.tmdm:occurrence-item-identifiers o)))) items)) (loop for o in items for index from 1 do (let* ((iri (or (string= name "occurrence-uri-relative") (and two (= index 2)))) (type (local-topic map (if two (if iri "see-also" "description") "occtype") doc)) (value (cond ((string= name "occurrence-uri-relative") (concatenate 'string (subseq doc 0 (1+ (position #\/ doc :from-end t))) "file.html")) ((and two iri) "http://www.isotopicmaps.org/tmcl/tmcl.html") (two (format nil "A meta-schema for TMCL, describing the allowed structure~% of TMCL schemas.")) (t "Occurrence")))) (check (format nil "~A:type-reference:~D" name index) (eq type (topic-maps.tmdm::%occurrence-type o))) (check (format nil "~A:value-and-datatype:~D" name index) (and (string= value (topic-maps.tmdm:occurrence-value o)) (string= (if iri (topic-maps.tmdm:iri-datatype) (topic-maps.tmdm:string-datatype)) (topic-maps.tmdm:occurrence-datatype o)))))) (let* ((state (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))) (different (mismatch actual expected))) (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match) :diagnostics (when different (list (format nil "First mismatch byte ~D: actual ~D bytes, expected ~D bytes." different (length actual) (length expected))))) (check (concatenate 'string name ":byte-equal-cxtm") (null different)) (check (concatenate 'string name ":canonicalization-pure") (equal state (map-state map))) (setf *cxtm-results* (append *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 (null different) :bytes (length actual)))))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass" "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "actualObjects" (occurrence-observation map)) *occurrence-observations*) (format t "~&OCCURRENCE-CXTM ~A: PASS~%" id))) (setf *occurrence-observations* (nreverse *occurrence-observations*)) (check "29-imported-passes-244-unexecuted" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 29 :fail 0 :unsupported 0 :not-run 244)))) (defun occurrence-semantic-tests () (let* ((map (read-text "a r:\"http://example.org/resource\"; s: .")) (items (all-occurrences map)) (a (local-topic map "a"))) (check "uri-looking-string-remains-string" (string= (topic-maps.tmdm:occurrence-datatype (first items)) (topic-maps.tmdm:string-datatype))) (check "iri-literal-is-locator" (string= (topic-maps.tmdm:occurrence-datatype (second items)) (topic-maps.tmdm:iri-datatype))) (check "datatype-locator-is-not-a-topic" (every (lambda (o) (stringp (topic-maps.tmdm:occurrence-datatype o))) items)) (check "uri-value-is-not-subject-identity" (and (= 3 (length (topic-map-topics map))) (every #'null (mapcar #'topic-subject-identifiers (topic-map-topics map))))) (check "public-parent-and-type-identity" (and (every (lambda (o) (eq a (topic-maps.tmdm:occurrence-parent o))) items) (eq (local-topic map "r") (topic-maps.tmdm:occurrence-type (first items)))))) (let ((map (read-text "a r:\"file.html\"; s: ."))) (check "quoted-relative-content-not-resolved" (string= "file.html" (topic-maps.tmdm:occurrence-value (first (all-occurrences map))))) (check "iri-content-resolved-once" (string= "https://example.invalid/a/file.html" (topic-maps.tmdm:occurrence-value (second (all-occurrences map)))))) (let ((map (read-text "a : \"\\n\\t\\u00C4\"; - \"Name\";. relation(role: a)"))) (check "iri-type-is-subject-identified-topic" (equal '("urn:type") (topic-subject-identifiers (topic-maps.tmdm:occurrence-type (first (all-occurrences map)))))) (check "string-escapes-preserved" (string= (format nil "~%~CÄ" #\Tab) (topic-maps.tmdm:occurrence-value (first (all-occurrences map))))) (check "name-occurrence-roleplayed-child-order" (< (search ".")) (out (cxtm map))) (check "string-url-not-locator-normalized" (search "https://example.invalid/a/doc.ctm#z" out)) (check "uri-url-locator-normalized" (search "#z" out))) (dolist (text '("a ^: \"x\"." "a =: \"x\".")) (check (concatenate 'string "occurrence-type-topic-reference:" text) (= 2 (length (topic-map-topics (read-text text)))))) (dolist (entry '(("a t: ." ctm-rejection :expected-occurrence-value 5) ("a t:" ctm-rejection :expected-occurrence-value 4) ("a t: ;." ctm-rejection :expected-occurrence-value 5) ("a t \"x\"." ctm-rejection :expected-occurrence-type-separator 4) ("a t:\"x\" r:\"y\"." ctm-rejection :expected-separator 8) ("a t: 2000-12-31T12:00:60+01:00." ctm-unsupported :datetime-leap-second-context 5) ("a t:*." ctm-unsupported :occurrence-literal-datatype 4) ("a t: 42x." ctm-rejection :invalid-occurrence-literal 5) ("a t:\"x\"^^datatype." ctm-rejection :expected-datatype-iri 9) ("a t:\"x\"^." ctm-rejection :invalid-datatype-marker 7) ("a ex:t: \"x\"." undefined-prefix :undefined-prefix 2) ("a t: ex:v." undefined-prefix :undefined-prefix 5) ("a t:\"x\"^^xsd:string." undefined-prefix :undefined-prefix 9))) (destructuring-bind (text class code position) entry (let ((condition (ctm-condition text))) (check (concatenate 'string "occurrence-diagnostic:" text) (and (typep condition class) (eq code (error-code condition)) (= position (error-position condition)) (= 1 (error-line condition)) (= (1+ position) (error-column condition))))))) (check "explicit-string-datatype-now-supported" (string= (topic-maps.tmdm:string-datatype) (topic-maps.tmdm:occurrence-datatype (first (all-occurrences (read-text "a t:\"x\"^^.")))))) (check "scoped-occurrence-now-deserialized" (= 1 (length (topic-maps.tmdm:occurrence-scope (first (all-occurrences (read-text "a t:\"x\" @theme."))))))) (dolist (text '("a t:\"x\" ~reifier." "a t: 2000-12-31T12:00:60+01:00." "a t: 2000-12-31T12:00:60-01:00." "a t:* .")) (check (concatenate 'string "occurrence-feature-boundary:" text) (if (find #\~ text) (null (ctm-condition text)) (typep (ctm-condition text) 'ctm-unsupported))))) (defun occurrence-api-tests () (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (type (local-topic map "t")) (first (topic-maps.tmdm:add-occurrence a type "Same" :item-identifiers '("urn:o:1"))) (second (topic-maps.tmdm:add-occurrence a type "Same" :item-identifiers '("urn:o:2" "urn:o:1"))) (third (topic-maps.tmdm:add-occurrence a type "Same" :item-identifiers '("urn:o:3")))) (check "duplicate-occurrence-replaced" (and (not (eq first second)) (not (eq second third)) (= 1 (length (all-occurrences map))))) (check "occurrence-canonical-handle-chain" (and (eq third (topic-maps.tmdm:canonical-occurrence first)) (eq third (topic-maps.tmdm:canonical-occurrence second)))) (check "occurrence-identifiers-union" (equal '("urn:o:1" "urn:o:2" "urn:o:3") (sort (topic-maps.tmdm:occurrence-item-identifiers first) #'string<))) (let ((items (list first second third)) (state (map-state map))) (let ((handles (occurrence-handle-state items))) (check "occurrence-identifier-cxtm-serialization" (search "urn:o:3" (cxtm map))) (check "retained-occurrence-handles-not-mutated" (equal handles (occurrence-handle-state items))) (check "occurrence-map-not-mutated" (equal state (map-state map))))) (let ((set (topic-maps.tmdm:topic-occurrences a)) (ids (topic-maps.tmdm:occurrence-item-identifiers first))) (setf (first set) nil (first ids) "corrupted") (check "occurrence-set-readers-return-copies" (and (eq third (first (topic-maps.tmdm:topic-occurrences a))) (not (member "corrupted" (topic-maps.tmdm:occurrence-item-identifiers first) :test #'string=))))) (let ((state (map-state map))) (loop for fixture in (list (list :foreign-type (lambda () (topic-maps.tmdm:add-occurrence a (first-topic (read-text "other.")) "x"))) (list :invalid-datatype (lambda () (topic-maps.tmdm:add-occurrence a type "42" :datatype "relative-datatype"))) (list :relative-uri-value (lambda () (topic-maps.tmdm:add-occurrence a type "file.html" :datatype (topic-maps.tmdm:iri-datatype)))) (list :duplicate-item-identifier (lambda () (topic-maps.tmdm:add-occurrence a type "Different" :item-identifiers '("urn:o:1")))) (list :duplicate-item-identifier (lambda () (topic-maps.tmdm:intern-topic map :item-identifier "urn:o:1"))) (list :duplicate-item-identifier (lambda () (topic-maps.tmdm:add-topic-identifier a :item-identifier "urn:o:1")))) for fixture-index from 0 do (check (format nil "invalid-occurrence-api:~A:~D" (first fixture) fixture-index) (eq (first fixture) (topic-maps.tmdm:occurrence-error-code (occurrence-condition (second fixture))))) (check (format nil "invalid-occurrence-api-atomic:~A:~D" (first fixture) (length *checks*)) (equal state (map-state map)))))) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (type (local-topic map "t")) (o (topic-maps.tmdm:add-occurrence a type "x" :item-identifiers '("urn:overlap"))) (subject (topic-maps.tmdm:intern-topic map :subject-identifier "urn:overlap"))) (check "occurrence-SI-II-overlap-does-not-reify" (and (null (topic-maps.tmdm:item-reifier o)) (null (topic-maps.tmdm:topic-reified subject)))) (check "occurrence-SI-II-overlap-preserves-distinct-properties" (and (equal '("urn:overlap") (topic-maps.tmdm:occurrence-item-identifiers o)) (equal '("urn:overlap") (topic-subject-identifiers subject)) (null (topic-item-identifiers subject))))) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (type (local-topic map "t"))) (topic-maps.tmdm:add-occurrence a type "urn:same") (topic-maps.tmdm:add-occurrence a type "urn:same" :datatype (topic-maps.tmdm:iri-datatype)) (check "same-lexical-value-distinct-datatype" (= 2 (length (all-occurrences map)))))) (defun occurrence-order-tests () (let ((reference nil)) (dolist (order (permutations '("p" "r" "s"))) (dolist (occ-order (permutations '(0 1 2 3))) (let ((map (topic-maps.tmdm:make-topic-map)) (topics (make-hash-table :test #'equal))) (dolist (id order) (setf (gethash id topics) (api-topic map (concatenate 'string "urn:" id)))) (dolist (i occ-order) (topic-maps.tmdm:add-occurrence (gethash "p" topics) (gethash (if (member i '(1 3)) "s" "r") topics) (case i ((0 1) "Same") (2 "Zulu") (3 "urn:a")) :datatype (if (= i 3) (topic-maps.tmdm:iri-datatype) (topic-maps.tmdm:string-datatype)))) (let ((state (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "occurrence-construction:~{~A~}:~{~D~}" order occ-order) (string= reference out)) (check (format nil "occurrence-construction-purity:~{~A~}:~{~D~}" order occ-order) (equal state (map-state map))))))) (check "occurrences-numbered-by-value-then-type" (search (format nil "~%Same~%http://www.w3.org/2001/XMLSchema#string~%") reference)) (check "string-values-sort-before-locator-values" (< (search "Zulu" reference) (search "urn:a" reference)))) (let* ((map (read-text "a t:<#z>; t:.")) (state (map-state map)) (out (cxtm map))) (check "anyuri-value-normalized-for-order-and-output" (< (search "#z" out) (search "http://outside.invalid/a" out))) (check "uri-value-remains-absolute-in-model" (and (equal state (map-state map)) (find "https://example.invalid/a/doc.ctm#z" (all-occurrences map) :key #'topic-maps.tmdm:occurrence-value :test #'string=)))) (let* ((map (read-text "a t:\"x\".")) (out (cxtm map "http://www.w3.org/2001/XMLSchema"))) (check "datatype-output-not-base-normalized" (search "http://www.w3.org/2001/XMLSchema#string" out)))) (defun occurrence-graph-tests () (let ((possible '((0 1) (0 2) (0 3) (1 2) (1 3) (2 3)))) (dotimes (mask 64) (let* ((edges (loop for e in possible for bit from 0 when (logbitp bit mask) collect e)) (components (graph-components '(0 1 2 3) edges)) (reference nil)) (dolist (reverse '(nil t)) (let* ((map (topic-maps.tmdm:make-topic-map)) (topics (loop for i below 4 collect (api-topic map (format nil "urn:occ-node:~D" i)))) (handles (list (topic-maps.tmdm:add-occurrence (first topics) (third topics) "Same") (topic-maps.tmdm:add-occurrence (second topics) (fourth topics) "Same") (topic-maps.tmdm:add-occurrence (first topics) (fourth topics) "urn:v" :datatype (topic-maps.tmdm:iri-datatype)) (topic-maps.tmdm:add-occurrence (second topics) (third topics) "urn:v"))) (label (format nil "occurrence-graph:~D:~A:" mask reverse))) (dolist (e (if reverse (reverse edges) edges)) (dolist (node e) (topic-maps.tmdm:add-topic-identifier (nth node topics) :subject-identifier (format nil "urn:occ-edge:~D:~D" (first e) (second e))))) (labels ((root (node) (topic-maps.tmdm:canonical-topic (nth (apply #'min (find-if (lambda (c) (member node c)) components)) topics)))) (let ((expected-parents (list (root 0) (root 1) (root 0) (root 1))) (expected-types (list (root 2) (root 3) (root 3) (root 2)))) (check (concatenate 'string label "stored-parent-references") (every (lambda (o p) (eq p (topic-maps.tmdm::%occurrence-parent (topic-maps.tmdm:canonical-occurrence o)))) handles expected-parents)) (check (concatenate 'string label "stored-type-references") (every (lambda (o typ) (eq typ (topic-maps.tmdm::%occurrence-type (topic-maps.tmdm:canonical-occurrence o)))) handles expected-types)) (check (concatenate 'string label "duplicate-count") (= (if (and (eq (root 0) (root 1)) (eq (root 2) (root 3))) 3 4) (length (all-occurrences map)))) (check (concatenate 'string label "canonical-handles-live") (every (lambda (o) (member (topic-maps.tmdm:canonical-occurrence o) (all-occurrences map) :test #'eq)) handles)))) (let ((state (map-state map)) (retained (occurrence-handle-state handles)) (out (cxtm map))) (if reference (check (concatenate 'string label "merge-order-independent") (string= reference out)) (setf reference out)) (check (concatenate 'string label "canonicalization-pure") (and (equal state (map-state map)) (equal retained (occurrence-handle-state handles))))))))))) (defun occurrence-late-merge-and-error-tests () (let* ((map (read-text "a. b. r. s.")) (a (local-topic map "a")) (b (local-topic map "b")) (r (local-topic map "r")) (s (local-topic map "s")) (o1 (topic-maps.tmdm:add-occurrence a r "Same" :item-identifiers '("urn:late:1"))) (o2 (topic-maps.tmdm:add-occurrence b s "Same" :item-identifiers '("urn:late:2")))) (topic-maps.tmdm:merge-topics a b) (check "parent-merge-alone-preserves-distinct-types" (= 2 (length (all-occurrences map)))) (topic-maps.tmdm:merge-topics r s) (check "late-type-merge-removes-duplicate" (= 1 (length (all-occurrences map)))) (check "late-merge-canonical-occurrence-aliases" (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o2))) (check "late-merge-identifiers-union" (equal '("urn:late:1" "urn:late:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<))) (check "late-merge-parent-type-raw-canonical" (let ((current (topic-maps.tmdm:canonical-occurrence o1))) (and (eq (topic-maps.tmdm::%occurrence-parent current) (topic-maps.tmdm:canonical-topic a)) (eq (topic-maps.tmdm::%occurrence-type current) (topic-maps.tmdm:canonical-topic r))))) (let ((state (map-state map)) (retained (occurrence-handle-state (list o1 o2)))) (cxtm map) (check "late-merge-canonicalization-purity" (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o1 o2))))))) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (type (local-topic map "t")) (value (concatenate 'string "<&>" (map 'string #'code-char '(101 769 #x1f600)) (string #\Return))) (o (topic-maps.tmdm:add-occurrence a type value)) (state (map-state map)) (out (cxtm map))) (check "occurrence-nfc-escaping-and-utf8" (search (concatenate 'string "<&>" (string (code-char 233)) (string (code-char #x1f600)) " ") out)) (check "occurrence-lexical-string-not-rewritten-by-nfc" (and (string= value (topic-maps.tmdm:occurrence-value o)) (equal state (map-state map))))) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (type (local-topic map "t")) (bad (topic-maps.tmdm::%make-occurrence "42" "http://www.w3.org/2001/XMLSchema#dateTime" type a))) ;; Private fixture verifies the canonicalizer independently of the reader. (setf (topic-maps.tmdm::%topic-occurrences a) (list bad)) (let ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map))))) (check "invalid-datetime-not-silently-serialized" (and (typep condition 'topic-maps.cxtm:cxtm-error) (not (typep condition 'topic-maps.cxtm:cxtm-unsupported)) (eq :invalid-datetime-value (topic-maps.cxtm:error-code condition)))) (check "unsupported-occurrence-canonicalization-is-pure" (equal state (map-state map)))))) (defun run-occurrence-tests () ;; Run the retained CXTM suite once, creating fresh case records for this run. (run-cxtm-tests) (let ((*checks* nil)) (exercise-occurrence-cases) (occurrence-semantic-tests) (occurrence-api-tests) (occurrence-order-tests) (occurrence-graph-tests) (occurrence-late-merge-and-error-tests) (setf *last-occurrence-checks* (nreverse *checks*)) (format t "~&OCCURRENCES-PASS: ~D checks; 4 new byte-equal baselines; 29 imported PASS, 244 NOT-RUN.~%" (length *last-occurrence-checks*)) t)) (defun write-occurrence-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" "occurrence-semantic-and-cxtm-evidence" "passed" (length *last-occurrence-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-occurrence-checks*) "cases" (topic-maps.test-foundation::json-array *occurrence-observations*)) out) (terpri out)))