(in-package #:topic-maps.ctm.tests) (defvar *last-typed-name-checks* nil) (defvar *typed-name-observations* nil) (defun typed-name-checks () *last-typed-name-checks*) (defun name-handle-state (names) (mapcar (lambda (name) (list name (topic-maps.tmdm::%topic-name-forward name) (topic-maps.tmdm::%topic-name-parent name) (topic-maps.tmdm::%topic-name-type name) (topic-maps.tmdm::%topic-name-value name))) names)) (defun typed-name-original-cases () (setf *typed-name-observations* nil) (dolist (file '("name" "name2" "name-type" "name-duplicate")) (let* ((id (format nil "ctm/in/~A.ctm" file)) (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)) (owner (local-topic map "topic" doc)) (names (topic-names owner)) (name (first names)) (typed (string= file "name-type")) (type (if typed (local-topic map "nametype" doc) (find (default-name-type-identifier) (topic-map-topics map) :key (lambda (topic) (first (topic-subject-identifiers topic))) :test #'equal)))) (check (concatenate 'string id ":name-count") (= 1 (length names))) (check (concatenate 'string id ":value") (string= "Name" (topic-name-value name))) (check (concatenate 'string id ":parent-identity") (eq owner (topic-name-parent name))) (check (concatenate 'string id ":type-identity") (and type (eq type (topic-name-type name)))) (check (concatenate 'string id ":raw-references") (raw-reference-invariants map)) (check (concatenate 'string id ":only-required-items") (and (= 2 (length (topic-map-topics map))) (= 1 (count-if #'topic-names (topic-map-topics map))) (null (all-occurrences map)) (null (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string id ":type-identifiers") (if typed (and (equal (list (concatenate 'string doc "#nametype")) (topic-item-identifiers type)) (null (topic-subject-identifiers type)) (null (topic-subject-locators type))) (and (equal (list (default-name-type-identifier)) (topic-subject-identifiers type)) (null (topic-item-identifiers type)) (null (topic-subject-locators type))))) (let* ((before (map-state map)) (handles (name-handle-state names)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))) (difference (mismatch actual expected))) (topic-maps.test-foundation:record-result case (if difference :cxtm-mismatch :cxtm-match) :diagnostics (when difference (list (format nil "First different byte: ~D." difference)))) (setf (topic-maps.test-foundation:test-case-clauses case) (union '("CTM 3.3.6" "CTM 3.3.7" "CTM 3.11" "TMDM 5.4" "TMDM 6.2" "TMDM 6.3" "CXTM 3.6" "CXTM 4.7") (topic-maps.test-foundation:test-case-clauses case) :test #'equal)) (check (concatenate 'string id ":byte-exact-cxtm") (null difference)) (check (concatenate 'string id ":serialization-is-pure") (and (equal before (map-state map)) (equal handles (name-handle-state names)))) ;; Existing comparisons retain their result objects/order. Add only the ;; two contracts not previously executed by the retained Slice 11 suite. (unless (find id *cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal) (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 difference) :bytes (length actual))))))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "actualObjects" (model-observation map)) *typed-name-observations*) (format t "~&TYPED-NAME-CXTM ~A: PASS~%" id))) (setf *typed-name-observations* (nreverse *typed-name-observations*))) (defun typed-name-api-tests () (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (a (api-topic map "type-a")) (b (api-topic map "type-b")) (na (topic-maps.tmdm:add-name owner "Name" :type a)) (nb (topic-maps.tmdm:add-name owner "Name" :type b))) (check "typed-api:explicit-type-no-default" (= 3 (length (topic-map-topics map)))) (check "typed-api:same-value-different-type-distinct" (and (not (eq na nb)) (= 2 (length (topic-names owner))))) (check "typed-api:equal-properties-reuse-name" (eq na (topic-maps.tmdm:add-name owner "Name" :type a))) (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared-type") (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared-type") (let ((current (topic-maps.tmdm:canonical-topic-name na)) (type (topic-maps.tmdm:canonical-topic a))) (check "typed-api:type-identity-causes-name-merging" (= 1 (length (topic-names owner)))) (check "typed-api:retained-name-handles-resolve" (and (not (eq na current)) (not (eq nb current)) (eq current (topic-maps.tmdm:canonical-topic-name nb)))) (check "typed-api:stored-name-type-is-canonical" (eq type (topic-maps.tmdm::%topic-name-type current))) (check "typed-api:stored-name-parent-is-canonical" (eq owner (topic-maps.tmdm::%topic-name-parent current))) (check "typed-api:old-type-handle-reused" (eq current (topic-maps.tmdm:add-name owner "Name" :type b))) (let ((before (map-state map)) (handles (name-handle-state (list na nb current)))) (cxtm map) (check "typed-api:serialization-preserves-retained-handles" (and (equal before (map-state map)) (equal handles (name-handle-state (list na nb current)))))))) (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (name (topic-maps.tmdm:add-name owner "Default")) (type (topic-name-type name))) (check "typed-api:default-wrapper-compatible" (eq name (topic-maps.tmdm:add-default-name owner "Default"))) (check "typed-api:explicit-default-reuses-name" (eq name (topic-maps.tmdm:add-name owner "Default" :type type))) (check "typed-api:default-only-two-topics" (= 2 (length (topic-map-topics map))))) (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (topic-maps.tmdm:intern-topic map :item-identifier (default-name-type-identifier))) (name (topic-maps.tmdm:add-name owner "self"))) (check "typed-api:default-identity-merges-owner-correctly" (and (= 1 (length (topic-map-topics map))) (eq (topic-name-parent name) (topic-name-type name)) (raw-reference-invariants map)))) (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (other (topic-maps.tmdm:make-topic-map)) (foreign (api-topic other "foreign"))) (dolist (bad (list foreign nil 42)) (let ((before (map-state map)) (other-before (map-state other))) (check (format nil "typed-api:invalid-type-refused:~A" (type-of bad)) (handler-case (progn (topic-maps.tmdm:add-name owner "Name" :type bad) nil) (error () t))) (check (format nil "typed-api:invalid-type-no-mutation:~A" (type-of bad)) (and (equal before (map-state map)) (equal other-before (map-state other)))))))) (defun typed-name-merge-order-tests () (let ((reference nil)) (dolist (order '(:parents-first :types-first)) (let* ((map (topic-maps.tmdm:make-topic-map)) (a (api-topic map "a")) (b (api-topic map "b")) (r (api-topic map "r")) (s (api-topic map "s")) (handles (loop for owner in (list a b) append (loop for type in (list r s) collect (topic-maps.tmdm:add-name owner "Name" :type type))))) (if (eq order :parents-first) (progn (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)) (progn (topic-maps.tmdm:merge-topics r s) (topic-maps.tmdm:merge-topics a b))) (let* ((owner (topic-maps.tmdm:canonical-topic a)) (type (topic-maps.tmdm:canonical-topic r)) (name (first (topic-names owner))) (before (map-state map)) (state (name-handle-state handles)) (out (cxtm map))) (check (format nil "typed-merge:all-handles:~A" order) (every (lambda (h) (eq name (topic-maps.tmdm:canonical-topic-name h))) handles)) (check (format nil "typed-merge:single-live-name:~A" order) (= 1 (length (topic-names owner)))) (check (format nil "typed-merge:raw-references:~A" order) (and (eq owner (topic-maps.tmdm::%topic-name-parent name)) (eq type (topic-maps.tmdm::%topic-name-type name)) (raw-reference-invariants map))) (check (format nil "typed-merge:serialization-pure:~A" order) (and (equal before (map-state map)) (equal state (name-handle-state handles)))) (when reference (check "typed-merge:order-independent-cxtm" (string= reference out))) (setf reference out)))))) (defun typed-name-reader-tests () (dolist (text (list "a - type:\"Name\"." "a - type: \"Name\"." (format nil "a - type:~%#( layout )# \"Name\".") "a - type :\"Name\".")) (let* ((map (read-text text)) (owner (local-topic map "a")) (name (first (topic-names owner)))) (check (concatenate 'string "typed-reader:local-type:" text) (and (eq (local-topic map "type") (topic-name-type name)) (eq owner (topic-name-parent name)) (= 2 (length (topic-map-topics map))))))) (dolist (pair '(("" :subject-identifier "urn:type") ("<../type>" :subject-identifier "https://example.invalid/type") ("ex:type" :subject-identifier "urn:ex:type") ("^" :item-identifier "urn:type") ("=" :subject-locator "urn:type"))) (destructuring-bind (reference kind identifier) pair (let* ((map (read-text (format nil "%prefix ex a - ~A: \"Name\"." reference))) (name (first (topic-names (local-topic map "a")))) (type (topic-name-type name))) (check (format nil "typed-reader:identity-category:~A" reference) (equal (list identifier) (ecase kind (:subject-identifier (topic-subject-identifiers type)) (:item-identifier (topic-item-identifiers type)) (:subject-locator (topic-subject-locators type))))) (check (format nil "typed-reader:no-default:~A" reference) (and (= 2 (length (topic-map-topics map))) (notany (lambda (topic) (member (default-name-type-identifier) (topic-subject-identifiers topic) :test #'equal)) (topic-map-topics map))))))) (let* ((map (read-text "a - a:\"Name\"; - a:\"Name\"; - a:\"name\".")) (owner (local-topic map "a"))) (check "typed-reader:self-type-case-preserved-duplicate-removed" (and (= 1 (length (topic-map-topics map))) (= 2 (length (topic-names owner))) (every (lambda (n) (eq owner (topic-name-type n))) (topic-names owner))))) (dolist (text '("a - ex:type:\"Name\"." "a - ." "a -" "a - type:." "a - type \"Name\".")) (let* ((condition (ctm-condition text)) (undefined (search "ex:type" text)) (separator (string= text "a - type \"Name\".")) (position (if undefined 4 (if separator 9 (if (string= text "a - type:.") 9 (if (string= text "a -") 3 4)))))) (check (concatenate 'string "typed-reader:diagnostic:" text) (and (eq (type-of condition) (if undefined 'undefined-prefix 'ctm-rejection)) (eq (error-code condition) (if undefined :undefined-prefix (if separator :expected-name-type-separator :expected-name-value))) (= position (error-position condition)) (= 1 (error-line condition)) (= (1+ position) (error-column condition)))))) (let* ((map (read-text "a - type:\"Name\" @theme.")) (name (first (topic-names (local-topic map "a"))))) (check "typed-reader:scope-now-supported" (and (eq (local-topic map "type") (topic-name-type name)) (equal (list (local-topic map "theme")) (topic-maps.tmdm:topic-name-scope name))))) (dolist (text '("a - [-\"fullname\" ~r]:\"Name\"." "a - type:\"Name\" ~reifier." "a - type:\"Name\" (\"variant\" @theme ~r).")) (check (concatenate 'string "typed-reader:feature-boundary:" text) (if (find #\~ text) (null (ctm-condition text)) (typep (ctm-condition text) 'ctm-unsupported))))) (defun typed-name-order-tests () (let ((reference nil)) (dolist (order (permutations '(0 1 2))) (let* ((map (topic-maps.tmdm:make-topic-map)) (owner (api-topic map "owner")) (a (api-topic map "a")) (z (api-topic map "z"))) (dolist (i order) (topic-maps.tmdm:add-name owner (if (= i 2) "Z" "Name") :type (if (= i 1) z a))) (let* ((before (map-state map)) (out (cxtm map))) (check (format nil "typed-order:value-then-type:~S" order) (< (search "" out) (search "" out) (search "Z" out))) (when reference (check (format nil "typed-order:construction-independent:~S" order) (string= reference out))) (check (format nil "typed-order:pure:~S" order) (equal before (map-state map))) (setf reference out)))))) (defun run-typed-name-tests () (run-timezone-tests) (let ((*checks* nil)) (typed-name-original-cases) (typed-name-api-tests) (typed-name-merge-order-tests) (typed-name-reader-tests) (typed-name-order-tests) (check "typed-names:only-two-new-imported-passes" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 64 :fail 2 :unsupported 0 :not-run 207))) (setf *last-typed-name-checks* (nreverse *checks*)) (format t "~&TYPED-NAMES-PASS: ~D checks; four original comparisons; 64 PASS, 2 known FAIL, 207 NOT-RUN.~%" (length *last-typed-name-checks*)) t)) (defun write-typed-name-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" "typed-name-evidence" "passed" (length *last-typed-name-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-typed-name-checks*) "cases" (topic-maps.test-foundation::json-array *typed-name-observations*)) out) (terpri out)))