(in-package #:topic-maps.ctm.tests) (defparameter *prefix-positive-ids* '("occurrence-explicit-datatype" "occurrence-datatype-unknown" "prefix-duplicate" "prefix-duplicate2" "prefix-multiline")) (defvar *last-prefix-checks* nil) (defvar *prefix-observations* nil) (defun prefix-checks () *last-prefix-checks*) (defun exercise-prefix-cases () (setf *prefix-observations* nil) (dolist (name *prefix-positive-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)) (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)) (occurrence (first (all-occurrences map)))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.2" "CTM 3.3.3-3.3.7" "CTM 3.4.1" "CTM 3.10" "CTM 3.16.1" "TMDM 4.3-4.4" "TMDM 5.3" "TMDM 5.6" "CXTM 3.8" "CXTM 3.14" "CXTM 3.16" "CXTM 3.18-3.20" "CXTM 4.3-4.6" "CXTM 4.9")) (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-exact") (null different)) (check (concatenate 'string name ":pure") (equal state (map-state map))) (check (concatenate 'string name ":no-extra-items") (and (null (topic-maps.tmdm:topic-map-associations map)) (notany #'topic-names (topic-map-topics map)) (= (if occurrence 2 (if (string= name "prefix-duplicate2") 2 1)) (length (topic-map-topics map))))) (if occurrence (progn (check (concatenate 'string name ":parent-type-references") (and (eq (local-topic map "topic" doc) (topic-maps.tmdm::%occurrence-parent occurrence)) (eq (local-topic map "occtype" doc) (topic-maps.tmdm::%occurrence-type occurrence)) (= 1 (length (all-occurrences map))))) (check (concatenate 'string name ":lexical-value-and-datatype") (and (string= (if (string= name "occurrence-explicit-datatype") "Occurrence" "BOGUS") (topic-maps.tmdm:occurrence-value occurrence)) (string= (if (string= name "occurrence-explicit-datatype") (topic-maps.tmdm:string-datatype) "http://example.com/bogus") (topic-maps.tmdm:occurrence-datatype occurrence)))) (check (concatenate 'string name ":datatype-is-no-topic") (every (lambda (topic) (and (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)) (= 1 (length (topic-item-identifiers topic))))) (topic-map-topics map)))) (check (concatenate 'string name ":subject-identifiers-only") (equal (if (string= name "prefix-duplicate2") '("http://psi.example.org/fake" "http://psi.example.org/fake2") '("http://psi.example.org/fake")) (loop for topic in (topic-map-topics map) collect (when (and (null (topic-item-identifiers topic)) (null (topic-subject-locators topic)) (= 1 (length (topic-subject-identifiers topic)))) (first (topic-subject-identifiers topic))))))) (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)) *prefix-observations*) (format t "~&PREFIX-CXTM ~A: PASS (~D bytes)~%" id (length actual)))) (dolist (entry '(("prefix-collision" topic-maps.ctm:prefix-collision :prefix-collision 43 2 9) ("prefix-undefined2" undefined-prefix :undefined-prefix 136 6 15))) (destructuring-bind (name class code position line column) entry (let* ((id (format nil "ctm/invalid/~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)) (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)) (condition (handler-case (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id) nil) (ctm-error (c) c)))) (check (concatenate 'string name ":specific-rejection") (and (typep condition class) (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported)) (eq code (error-code condition)))) (check (concatenate 'string name ":source-position") (and (= position (error-position condition)) (= line (error-line condition)) (= column (error-column condition)) (string= id (topic-maps.ctm:error-source condition)) (eq :character (topic-maps.ctm:error-position-unit condition)))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.3.4" "CTM 3.16.1")) (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass" "ctmSource" source "conditionClass" (symbol-name (type-of condition)) "code" (symbol-name (error-code condition)) "position" (error-position condition) "line" (error-line condition) "column" (error-column condition) "source" (topic-maps.ctm:error-source condition) "positionUnit" "character") *prefix-observations*) (format t "~&PREFIX-REJECTION ~A: ~A~%" id condition)))) (setf *prefix-observations* (nreverse *prefix-observations*)) (check "slice6:36-passes-237-unexecuted" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 36 :fail 0 :unsupported 0 :not-run 237)))) (defun prefix-semantic-tests () (dolist (reference '("ex:Ä" "ex:0123" "ex:12a-b" "ex:a.b" "ex:a-b")) (let* ((text (format nil "%prefix ex ~A. ." reference (subseq reference 3))) (topics (topic-map-topics (read-text text)))) (check (concatenate 'string "qname-equals-explicit-iri:" reference) (and (= 1 (length topics)) (equal (list (concatenate 'string "urn:base:" (subseq reference 3))) (topic-subject-identifiers (first topics))))))) (let ((map (read-text "%prefix ex <../vocab/> ex:A. =ex:B. ^ex:C."))) (check "prefix-relative-binding-resolved-against-document" (equal '("https://example.invalid/vocab/A") (topic-subject-identifiers (first-topic map)))) (check "qname-subject-locator-distinct" (equal '("https://example.invalid/vocab/B") (topic-subject-locators (second (topic-map-topics map))))) (check "qname-item-identifier-distinct" (equal '("https://example.invalid/vocab/C") (topic-item-identifiers (third (topic-map-topics map)))))) (let ((map (read-text "%prefix ex ex:A."))) (check "qname-binding-not-percent-decoded-twice" (equal '("urn:percent:%20A") (topic-subject-identifiers (first-topic map))))) (check "prefix-terminal-does-not-require-extra-layout" (equal '("urn:ex:a") (topic-subject-identifiers (first-topic (read-text "%prefixexex:a."))))) (check "prefix-directives-create-no-information-items" (null (topic-map-topics (read-text "%prefix ex ")))) (check "identical-resolved-binding-permitted" (= 1 (length (topic-map-topics (read-text "%prefix ex <../vocab/> %prefix ex ex:a."))))) (check "prefix-binding-case-preserved" (equal '("urn:Upper:A") (topic-subject-identifiers (first-topic (read-text "%prefix EX EX:A."))))) (dolist (text '("%prefix ex EX:a." "ex:a. %prefix ex " "ex:a." "a t:\"v\"^^xsd:string.")) (check (concatenate 'string "prefix-is-case-sensitive-ordered-and-local:" text) (typep (ctm-condition text) 'undefined-prefix))) (check "numeric-leading-qname-local-cannot-contain-dots" (typep (ctm-condition "%prefix ex ex:12.3.") 'ctm-rejection)) (read-text "%prefix private private:a.") (check "prefix-does-not-leak-between-reads" (typep (ctm-condition "private:a.") 'undefined-prefix)) (let* ((map (read-text (format nil "%prefix~%# comment~%ex~%#( comment )#~% ex:a.")))) (check "prefix-multiline-comments-layout" (equal '("urn:ex:a") (topic-subject-identifiers (first-topic map))))) (let* ((map (read-text "%prefix ex ex:rel(ex:role: ex:player)")) (a (first (topic-maps.tmdm:topic-map-associations map))) (r (first (topic-maps.tmdm:association-roles a)))) (check "qname-association-type" (equal '("urn:ex:rel") (topic-subject-identifiers (topic-maps.tmdm:association-type a)))) (check "qname-role-type" (equal '("urn:ex:role") (topic-subject-identifiers (topic-maps.tmdm:association-role-type r)))) (check "qname-role-player" (equal '("urn:ex:player") (topic-subject-identifiers (topic-maps.tmdm:association-role-player r))))) (let* ((map (read-text "%prefix ex a ex:type: ex:value.")) (o (first (all-occurrences map)))) (check "qname-iri-value-not-interned" (and (= 2 (length (topic-map-topics map))) (string= "urn:ex:value" (topic-maps.tmdm:occurrence-value o)))) (check "qname-iri-value-datatype" (string= (topic-maps.tmdm:iri-datatype) (topic-maps.tmdm:occurrence-datatype o)))) (dolist (entry (list (list "%prefix" :expected-prefix-name 7) (list "%prefix ex" :expected-prefix-iri 10) (list "%prefix ex other" :expected-prefix-iri 11) (list "%prefix ex %prefix new ex:a" :expected-prefix-iri 33) (list "%prefix ex %prefix ex " :prefix-collision 30) (list "a t:\"v\"^^." :expected-datatype-iri 9) (list "a t:\"v\"^ ^." :invalid-datatype-marker 7) (list "a t:\"file.html\"^^." :relative-uri-value 4))) (destructuring-bind (text code position) entry (let ((c (ctm-condition text))) (check (concatenate 'string "prefix-datatype-diagnostic:" text) (and (typep c 'ctm-rejection) (eq code (error-code c)) (= position (error-position c)) (= 1 (error-line c)) (= (1+ position) (error-column c))))))) (let ((c (ctm-condition (format nil "%prefix ex ~C~C%prefix ex " #\Return #\Newline)))) (check "prefix-collision-crlf-location" (and (typep c 'topic-maps.ctm:prefix-collision) (= 2 (error-line c)) (= 9 (error-column c))))) (dolist (text '("%include " "%version 1.0" "%prefix ex a - ex:type: \"Name\".")) (check (concatenate 'string "remaining-construct-unsupported:" text) (typep (ctm-condition text) 'ctm-unsupported)))) (defun explicit-datatype-tests () (dolist (datatype '("urn:datatype:custom" "http://example.org/types/custom" "http://www.w3.org/2001/XMLSchema#string")) (let* ((text (format nil "%prefix d <~A> a t:\" 00042 %41 file.html \\u00C4 \\n\"^^d:value." datatype)) (map (read-text text)) (o (first (all-occurrences map))) (value (format nil " 00042 %41 file.html Ä ~%")) (state (map-state map)) (out (cxtm map))) (check (concatenate 'string "explicit-arbitrary-datatype:" datatype) (string= (concatenate 'string datatype "value") (topic-maps.tmdm:occurrence-datatype o))) (check (concatenate 'string "explicit-lexical-content:" datatype) (string= value (topic-maps.tmdm:occurrence-value o))) (check (concatenate 'string "unknown-datatype-other-cxtm:" datatype) (search (concatenate 'string "" value "") out)) (check (concatenate 'string "datatype-not-base-normalized:" datatype) (search (concatenate 'string "" datatype "value") (cxtm map datatype))) (check (concatenate 'string "datatype-not-topic-and-map-pure:" datatype) (and (= 2 (length (topic-map-topics map))) (equal state (map-state map)))))) (check "explicit-datatype-permits-layout-around-marker" (string= "urn:d" (topic-maps.tmdm:occurrence-datatype (first (all-occurrences (read-text "a t: \"v\" ^^ .")))))) (let* ((map (read-text "a t:\"v\"^^<../datatype>.")) (o (first (all-occurrences map)))) (check "relative-datatype-iri-is-resolved" (string= "https://example.invalid/datatype" (topic-maps.tmdm:occurrence-datatype o)))) (let* ((map (read-text "a t:\"https://example.invalid/a/doc.ctm#z\"^^.")) (o (first (all-occurrences map)))) (check "explicit-absolute-anyuri-stored-unchanged" (string= "https://example.invalid/a/doc.ctm#z" (topic-maps.tmdm:occurrence-value o))) (check "explicit-anyuri-cxtm-locator-rule" (search "#z" (cxtm map)))) (dolist (name '("integer" "decimal" "date" "dateTime")) (let* ((dt (concatenate 'string "http://www.w3.org/2001/XMLSchema#" name)) (map (read-text (format nil "a t:\"00042\"^^<~A>." dt))) (o (first (all-occurrences map))) (state (map-state map)) (c (cxtm-condition (lambda () (cxtm map))))) (check (concatenate 'string "excluded-normalization-model-preserves:" name) (and (string= dt (topic-maps.tmdm:occurrence-datatype o)) (string= "00042" (topic-maps.tmdm:occurrence-value o)))) (if (string= name "integer") (check "integer-explicit-cxtm-now-supported" (and (null c) (search "42" (cxtm map)) (equal state (map-state map)))) (check (concatenate 'string "excluded-normalization-explicit-cxtm-unsupported:" name) (and (typep c 'topic-maps.cxtm:cxtm-unsupported) (eq :unsupported-occurrence-datatype (topic-maps.cxtm:error-code c)) (equal state (map-state map))))))) (let* ((map (read-text "%prefix xsd a t:\"Same\"; t:\"Same\"^^xsd:string."))) (check "implicit-explicit-string-datatypes-have-identical-equality" (= 1 (length (all-occurrences map))))) (let* ((value (concatenate 'string "<&>" (map 'string #'code-char '(101 769)))) (map (read-text (format nil "a : \"~A\"^^." value))) (o (first (all-occurrences map))) (state (map-state map)) (out (cxtm map))) (check "datatype-equals-type-iri-without-extra-topic" (and (= 2 (length (topic-map-topics map))) (equal '("urn:datatype") (topic-subject-identifiers (topic-maps.tmdm:occurrence-type o))) (string= "urn:datatype" (topic-maps.tmdm:occurrence-datatype o)))) (check "other-datatype-xml-escaping-and-nfc" (search (concatenate 'string "<&>" (string (code-char 233)) "") out)) (check "other-datatype-model-retains-decomposed-lexical-value" (and (string= value (topic-maps.tmdm:occurrence-value o)) (equal state (map-state map))))) ;; Unknown datatype equality remains lexical. No value-space interpretation. (let* ((map (read-text "a t:\"01\"^^; t:\"1\"^^; t:\"01\"^^; t:\"01\"^^."))) (check "datatype-and-lexical-value-control-duplicates" (= 3 (length (all-occurrences map)))))) (defun prefix-order-and-merge-tests () (let ((reference nil)) (dolist (order (permutations '(0 1 2 3))) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t"))) (dolist (i order) (topic-maps.tmdm:add-occurrence a typ (if (= i 3) "z" "Same") :datatype (nth i '("urn:a" "urn:b" "urn:c" "urn:a")))) (let ((state (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "explicit-datatype-construction-order:~{~D~}" order) (string= reference out)) (check (format nil "explicit-datatype-order-pure:~{~D~}" order) (equal state (map-state map)))))) (check "occurrences-sort-by-datatype-after-value" (< (search "urn:a" reference) (search "urn:b" reference) (search "urn:c" reference)))) (let* ((map (read-text "%prefix ex a ex:r:\"Same\"^^ex:d. b ex:s:\"Same\"^^ex:d.")) (handles (all-occurrences map)) (a (local-topic map "a")) (b (local-topic map "b")) (r (topic-maps.tmdm:occurrence-type (first handles))) (s (topic-maps.tmdm:occurrence-type (second handles)))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (check "qualified-occurrences-merge-by-parent-type-value-datatype" (and (= 1 (length (all-occurrences map))) (eq (topic-maps.tmdm:canonical-occurrence (first handles)) (topic-maps.tmdm:canonical-occurrence (second handles))))) (let* ((live (topic-maps.tmdm:canonical-occurrence (first handles))) (state (map-state map)) (retained (occurrence-handle-state handles))) (check "qualified-merge-raw-parent-type-canonical" (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a)) (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r)) (string= "urn:ex:d" (topic-maps.tmdm:occurrence-datatype live)))) (cxtm map) (check "qualified-canonicalization-preserves-retained-handles" (and (equal state (map-state map)) (equal retained (occurrence-handle-state handles))))))) (defun run-prefix-tests () (run-occurrence-tests) (let ((*checks* nil)) (exercise-prefix-cases) (prefix-semantic-tests) (explicit-datatype-tests) (prefix-order-and-merge-tests) (setf *last-prefix-checks* (nreverse *checks*)) (format t "~&PREFIX-DATATYPES-PASS: ~D checks; 5 new byte-exact baselines, 2 specific rejections; 36 imported PASS, 237 NOT-RUN.~%" (length *last-prefix-checks*)) t)) (defun write-prefix-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" "prefix-and-explicit-datatype-evidence" "passed" (length *last-prefix-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-prefix-checks*) "cases" (topic-maps.test-foundation::json-array *prefix-observations*)) out) (terpri out)))