(defpackage #:topic-maps.mergemap.tests
(:use #:cl #:topic-maps.tmdm)
(:import-from #:topic-maps.ctm.tests #:check #:cxtm #:binary-file)
(:import-from #:topic-maps.variant.tests #:condition-from)
(:export #:run-tests #:mergemap-checks #:write-mergemap-report))
(in-package #:topic-maps.mergemap.tests)
(defvar *last-checks* nil)
(defvar *observations* nil)
(defvar *rejections* nil)
(defparameter *notation* "http://psi.topicmaps.org/iso13250/ctm")
(defparameter *doc* "https://example.invalid/maps/main.ctm")
(defparameter *originals* '("mergemap" "mergemap-syntax-qname" "mergemap-template" "mergemap-tm-reifier"))
(defun mergemap-checks () *last-checks*)
(defun ascii (text)
(assert (every (lambda (c) (< (char-code c) 128)) text))
(map '(vector (unsigned-byte 8)) #'char-code text))
(defun directive (resource) (format nil "%mergemap <~A> <~A>" resource *notation*))
(defun resources (entries)
"Finite table of (absolute IRI bytes source-key); no filesystem/HTTP fallback."
(let ((calls nil))
(values (lambda (iri &key purpose from-document-iri notation)
(let ((entry (assoc iri entries :test #'string=)))
(push (topic-maps.test-foundation::fields "resourceIRI" iri "purpose" (symbol-name purpose)
"fromDocumentIRI" from-document-iri "notation" notation
"sourceKey" (if entry (third entry) nil)) calls)
(when entry (values (second entry) (third entry)))))
(lambda () (reverse calls)))))
(defun original-resources ()
(resources (loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries topic-maps.ctm.tests::*cxtm-corpus*)
collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id)))))
(defun read-map (text &rest keys)
(apply #'topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name "memory:main" keys))
(defun local (map iri)
(find-if (lambda (topic) (member iri (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))
(defun si (map iri)
(find-if (lambda (topic) (member iri (topic-subject-identifiers topic) :test #'string=)) (topic-map-topics map)))
(defun valid (map) (topic-maps.reference.tests::references-current-p map))
(defun observe (map) (topic-maps.template.tests::observe map))
(defun call-with-import-observer (observer thunk)
;; Temporary instrumentation in the fresh test process only. Every invocation
;; delegates to the actual Slice 19A operation; no alternate model/import path.
(let ((saved (symbol-function 'topic-maps.tmdm:import-map-content)))
(unwind-protect
(progn (setf (symbol-function 'topic-maps.tmdm:import-map-content)
(lambda (target source) (funcall observer saved target source)))
(funcall thunk))
(setf (symbol-function 'topic-maps.tmdm:import-map-content) saved))))
(defun rejection (label text class code &rest keys)
(let ((c (condition-from (lambda () (apply #'read-map text keys)))))
(check (concatenate 'string label ":specific-condition")
(and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c))))
(check (concatenate 'string label ":source-position")
(and (stringp (topic-maps.ctm:error-source c)) (integerp (topic-maps.ctm:error-position c))
(not (minusp (topic-maps.ctm:error-position c))))) c))
(defun resolution-tests ()
(dolist (base '("https://example.invalid/a/main.ctm" "https://example.invalid/b/main.ctm"))
(let* ((resource (concatenate 'string (subseq base 0 (1+ (position #\/ base :from-end t))) "child.ctm"))
(text (format nil "~A~%topic." (directive "child.ctm"))))
(multiple-value-bind (resolver calls) (resources (list (list resource (ascii "topic.") "memory:child")))
(let* ((map (topic-maps.ctm:deserialize-string text :document-iri base :resource-resolver resolver))
(request (first (funcall calls))))
(check (concatenate 'string "resolution:Document-IRI:" base)
(and (= 2 (length (topic-map-topics map))) (local map (concatenate 'string base "#topic"))
(local map (concatenate 'string resource "#topic")) (valid map)))
(check (concatenate 'string "resolution:resolver-arguments:" base)
(and (= 1 (length (funcall calls)))
(equal (topic-maps.test-foundation::json-object-members (topic-maps.test-foundation::fields "resourceIRI" resource "purpose" "MERGEMAP"
"fromDocumentIRI" base "notation" *notation* "sourceKey" "memory:child"))
(topic-maps.test-foundation::json-object-members request))))))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "t.") "/local/path/is-only-a-source-key")))
(let ((map (read-map (directive "urn:child") :resource-resolver resolver)))
(check "resolution:absolute-resource-IRI-and-source-key-are-independent"
(and (local map "urn:child#t") (not (local map "/local/path/is-only-a-source-key#t")) (= 1 (length (funcall calls)))))))
(multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" (ascii "t.") "child")))
(let ((map (read-map (format nil "%prefix r <./>~%%prefix n ~%%mergemap r:child.ctm n:ctm")
:resource-resolver resolver)))
(check "resolution:resource-and-notation-QNames"
(and (local map "https://example.invalid/maps/child.ctm#t") (= 1 (length (funcall calls)))))))
;; Strict existing UTF-8 decoding, including a BOM and multibyte content.
(let ((bytes (concatenate '(vector (unsigned-byte 8)) #(239 187 191) (ascii "t - \"") #(195 169) (ascii "\"."))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" bytes "utf8:child")))
(declare (ignore calls))
(let* ((before (copy-seq bytes)) (map (read-map (directive "urn:child") :resource-resolver resolver))
(name (first (topic-names (local map "urn:child#t")))))
(check "resolution:strict-UTF8-BOM-Unicode-and-input-bytes-preserved"
(and (string= (string (code-char #xe9)) (topic-name-value name)) (equalp before bytes)))))))
(defun environment-tests ()
(let* ((resource "https://example.invalid/maps/child.ctm")
(child "%prefix p def f() p:t - \"Child\". end f()")
(parent (format nil "%prefix p def f() p:t - \"Parent\". end~%~A~%f()" (directive "child.ctm"))))
(multiple-value-bind (resolver calls) (resources (list (list resource (ascii child) "env:child")))
(declare (ignore calls))
(let ((map (read-map parent :resource-resolver resolver)))
(check "environment:independent-prefixes-and-template-registers"
(and (si map "urn:parent:t") (si map "urn:child:t")
(string= "Parent" (topic-name-value (first (topic-names (si map "urn:parent:t")))))
(string= "Child" (topic-name-value (first (topic-names (si map "urn:child:t"))))) (valid map))))))
(dolist (fixture (list (list "p:t." (format nil "%prefix p ~%~A" (directive "child.ctm")) 'topic-maps.ctm:undefined-prefix :undefined-prefix t)
(list "%prefix p " (format nil "~A~%p:t." (directive "child.ctm")) 'topic-maps.ctm:undefined-prefix :undefined-prefix nil)
(list "f()" (format nil "def f() t. end~%~A" (directive "child.ctm")) 'topic-maps.ctm:undefined-template :undefined-template t)
(list "def f() t. end" (format nil "~A~%f()" (directive "child.ctm")) 'topic-maps.ctm:undefined-template :undefined-template nil)))
(destructuring-bind (child parent class code child-error-p) fixture
(multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" (ascii child) "env:child")))
(let ((c (rejection (format nil "environment:isolation:~A:~A" code child-error-p) parent class code :resource-resolver resolver)))
(check (format nil "environment:isolation:~A:~A:correct-failure-source" code child-error-p)
(and (= 1 (length (funcall calls))) (string= (if child-error-p "env:child" "memory:main") (topic-maps.ctm:error-source c))))))))
;; A child is freshly processed for each occurrence. No child Wildcard state,
;; aliases or counter updates enter the parent environment.
(multiple-value-bind (resolver calls)
(resources (list (list "https://example.invalid/maps/child.ctm" (ascii "?x - \"C\". ?x. ? - \"A\".") "wild:child")))
(multiple-value-bind (map counter)
(read-map (format nil "?x. ~A~%~A~%?." (directive "child.ctm") (directive "child.ctm"))
:resource-resolver resolver :wildcard-iri "urn:parent-wild" :wildcard-counter 40)
(check "environment:own-Document-Wildcard-IRI-and-counter"
(and (= counter 42) (local map "urn:parent-wild#$__41.x") (local map "urn:parent-wild#$__42")
(local map "https://example.invalid/maps/child.ctm#$__1.x")
(local map "https://example.invalid/maps/child.ctm#$__2")
(= 5 (length (topic-map-topics map))) (= 2 (length (funcall calls))) (valid map))))))
(defun traversal-tests ()
(multiple-value-bind (resolver calls)
(resources (list (list "https://example.invalid/maps/sub/child.ctm"
(ascii (format nil "a. ~A~%b." (directive "../grand.ctm"))) "nested:child")
(list "https://example.invalid/maps/grand.ctm" (ascii "g.") "nested:grand")))
(let ((map (read-map (format nil "before. ~A~%after." (directive "sub/child.ctm")) :resource-resolver resolver)))
(check "traversal:nested-resources-relative-to-their-own-Document-IRIs"
(and (= 5 (length (topic-map-topics map))) (local map "https://example.invalid/maps/grand.ctm#g")
(equal (mapcar (lambda (c) (cdr (assoc "fromDocumentIRI" (topic-maps.test-foundation::json-object-members c) :test #'string=))) (funcall calls))
(list *doc* "https://example.invalid/maps/sub/child.ctm")) (valid map)))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "~ child-map") "source:child")))
(let ((map (read-map (format nil "~A~%~~ parent-map" (directive "urn:child")) :resource-resolver resolver)))
(check "traversal:caller-map-Reifier-remains-independent-after-leading-directive"
(and (eq (local map (concatenate 'string *doc* "#parent-map")) (item-reifier map))
(null (topic-reified (local map "urn:child#child-map"))) (= 1 (length (funcall calls)))))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii "t.") "child")))
(let ((observed nil))
(call-with-import-observer
(lambda (saved target source)
(setf observed (and (local target (concatenate 'string *doc* "#before"))
(null (local target (concatenate 'string *doc* "#after")))
(local source "urn:child#t")))
(funcall saved target source))
(lambda () (read-map (format nil "before. ~A~%after." (directive "urn:child")) :resource-resolver resolver)))
(check "traversal:directive-executed-immediately-before-caller-continues" (and observed (= 1 (length (funcall calls))))))))
(defun diagnostic-tests ()
(rejection "diagnostic:missing-resource" "%mergemap" 'topic-maps.ctm:ctm-rejection :expected-mergemap-resource)
(rejection "diagnostic:missing-notation" "%mergemap topic." 'topic-maps.ctm:ctm-rejection :expected-mergemap-notation)
(rejection "diagnostic:unknown-notation" "%mergemap " 'topic-maps.ctm:ctm-rejection :unknown-mergemap-notation)
(rejection "diagnostic:known-XTM-unimplemented" "%mergemap " 'topic-maps.ctm:ctm-unsupported :xtm-mergemap-notation)
(rejection "diagnostic:no-implicit-resource-access" (directive "file:///private/never-open-this") 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
(multiple-value-bind (resolver calls) (resources nil)
(rejection "diagnostic:resolver-declines-missing-resource" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :resource-unavailable :resource-resolver resolver)
(check "diagnostic:missing-resource-request-recorded" (= 1 (length (funcall calls)))))
(rejection "diagnostic:resolver-error" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :resource-resolver-failure
:resource-resolver (lambda (&rest args) (declare (ignore args)) (error "resource denied")))
(dolist (bad (list "not bytes" #(256) '()))
(rejection (format nil "diagnostic:invalid-resolver-result:~S" bad) (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error
(if bad :invalid-resource-result :resource-unavailable)
:resource-resolver (lambda (&rest args) (declare (ignore args)) (values bad "bad"))))
(rejection "diagnostic:invalid-source-key" (directive "child.ctm") 'topic-maps.ctm:ctm-resource-error :invalid-resource-result
:resource-resolver (lambda (&rest args) (declare (ignore args)) (values #(97 46) 42)))
(dolist (fixture (list (list #(255) :invalid-utf8 :byte) (list (ascii "a - .") :expected-name-value :character)))
(destructuring-bind (bytes code unit) fixture
(multiple-value-bind (resolver calls) (resources (list (list "https://example.invalid/maps/child.ctm" bytes "invalid:child")))
(let* ((text (format nil "before.~%~A" (directive "child.ctm")))
(c (rejection (format nil "diagnostic:imported-source:~A" code) text 'topic-maps.ctm:ctm-rejection code :resource-resolver resolver))
(frame (first (topic-maps.ctm:error-context c))))
(check (format nil "diagnostic:imported-source:~A:both-source-locations" code)
(and (string= "invalid:child" (topic-maps.ctm:error-source c)) (eq unit (topic-maps.ctm:error-position-unit c))
(eq :mergemap (getf frame :purpose)) (= 8 (getf frame :position))
(string= "memory:main" (getf frame :source)) (string= *doc* (getf frame :document-iri))
(string= "invalid:child" (getf frame :resource-source))
(search "memory:main" (princ-to-string c)) (search "invalid:child" (princ-to-string c))
(= 1 (length (funcall calls)))))))))
(multiple-value-bind (resolver calls)
(resources (list (list "urn:a" (ascii (directive "urn:b")) "cycle:a")
(list "urn:b" (ascii (directive "urn:a")) "cycle:b")))
(let ((c (rejection "diagnostic:active-resource-cycle" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :cyclic-mergemap :resource-resolver resolver)))
(check "diagnostic:cycle-has-complete-context-without-completed-resource-cache"
(and (= 3 (length (topic-maps.ctm:error-context c))) (= 2 (length (funcall calls)))))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:a" (ascii (directive "urn:b")) "depth:a")))
(let ((c (rejection "diagnostic:resource-depth-policy" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :resource-depth-limit
:resource-resolver resolver :max-resource-depth 1)))
(check "diagnostic:depth-limit-is-not-normative-rejection" (and (= 1 (length (funcall calls))) (= 2 (length (topic-maps.ctm:error-context c))))))))
(defun resolver-policy-tests ()
(rejection "policy:invalid-resolver" "a." 'topic-maps.ctm:ctm-resource-error :invalid-resource-resolver :resource-resolver 42)
(dolist (bad '(-1 1/2))
(rejection (format nil "policy:invalid-depth:~A" bad) "a." 'topic-maps.ctm:ctm-resource-error :invalid-resource-depth-limit :max-resource-depth bad))
(multiple-value-bind (resolver calls) (resources nil)
(rejection "policy:zero-depth-without-retrieval" (directive "urn:a") 'topic-maps.ctm:ctm-unsupported :resource-depth-limit
:resource-resolver resolver :max-resource-depth 0)
(check "policy:zero-depth-does-not-call-resolver" (null (funcall calls)))
(rejection "policy:XTM-recognized-without-retrieval" "%mergemap "
'topic-maps.ctm:ctm-unsupported :xtm-mergemap-notation :resource-resolver resolver)
(check "policy:XTM-does-not-call-resolver" (null (funcall calls))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:empty" #() nil)))
(let ((map (read-map (format nil "a. ~A~%b." (directive "urn:empty")) :resource-resolver resolver)))
(check "policy:empty-resource-accepted-and-caller-continues" (and (= 2 (length (topic-map-topics map))) (= 1 (length (funcall calls))) (valid map)))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:invalid" (ascii "a - .") nil)))
(declare (ignore calls))
(let ((c (rejection "policy:NIL-source-key-defaults-to-resource-IRI" (directive "urn:invalid") 'topic-maps.ctm:ctm-rejection
:expected-name-value :resource-resolver resolver)))
(check "policy:diagnostic-key-does-not-replace-Document-IRI" (string= "urn:invalid" (topic-maps.ctm:error-source c))))))
(defun identity-tests ()
(loop for pair in '(("" "") ("^" "^")
("" "^") ("=" "="))
for index from 1 do
(let* ((parent (format nil "a ~A; - : \"N\".~%~A" (first pair) (directive "urn:child")))
(child (format nil "b ~A; - : \"N\"." (second pair))))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "identity:child")))
(declare (ignore calls))
(let ((old nil))
(let* ((map (call-with-import-observer
(lambda (saved target source)
(setf old (local target (concatenate 'string *doc* "#a")))
(funcall saved target source))
(lambda () (read-map parent :resource-resolver resolver))))
(owner (local map "urn:child#b")))
(check (format nil "identity:~D:existing-rules-merge-Topics-and-dependent-Names" index)
(and (eq owner (canonical-topic old)) (not (eq owner old))
(eq owner (local map (concatenate 'string *doc* "#a")))
(= 1 (length (topic-names owner))) (= 2 (length (topic-map-topics map))) (valid map)))))))))
(defun model-tests ()
(let* ((child "~ map-r owner ; - type: \"N\" @theme ~name-r (\"V\" @extra ~variant-r); type: \"O\" @theme ~occ-r. assoc(role: owner ~role-r) @theme ~assoc-r")
(parent (format nil "local . ~A~%local - type: \"N\" @." (directive "urn:child"))))
;; Use the actual source/target objects and resolver returned by Slice 19A.
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "model:child")))
(declare (ignore calls))
(let ((seen nil))
(let ((map (call-with-import-observer
(lambda (saved target source)
(add-item-identifier source "urn:model:source-map-only")
(loop for item in (rest (topic-maps.import.tests::handles source)) for i from 1
when (topic-maps.tmdm::reifiable-p item) do
(add-item-identifier item (format nil "urn:model:item:~D" i)))
(let* ((held (topic-maps.import.tests::handles source))
(before (topic-maps.import.tests::state source held)) (xml (cxtm source)))
(multiple-value-bind (result resolve) (funcall saved target source)
(check "model:one-real-TMDM-import-with-unchanged-source"
(and (eq target result) (not (eq target source))
(equal before (topic-maps.import.tests::state source held)) (string= xml (cxtm source))))
(check "model:all-six-kind-Identifier-metadata-and-root-exclusion"
(and (null (item-identifiers target))
(every (lambda (item) (subsetp (item-identifiers item) (item-identifiers (funcall resolve item)) :test #'string=)) (rest held))))
(let* ((p (funcall resolve (local source "urn:child#owner")))
(name (funcall resolve (first (topic-names (local source "urn:child#owner"))))))
(setf seen p)
(check "model:canonical-Parent-Type-Player-Scope-Variant-and-Reifier-EQ"
(and (eq p (topic-name-parent name))
(eq (funcall resolve (local source "urn:child#type")) (topic-name-type name))
(eq name (variant-parent (first (topic-name-variants name))))
(eq (local target "urn:child#theme") (first (topic-name-scope name)))
(eq p (occurrence-parent (first (topic-occurrences p))))
(eq p (association-role-player (first (association-roles (first (topic-map-associations target))))))
(eq name (topic-reified (item-reifier name)))
(null (topic-reified (funcall resolve (item-reifier source)))) (valid target))))
(values result resolve))))
(lambda () (read-map parent :resource-resolver resolver)))))
(check "model:caller-retained-handle-and-canonical-target-after-continuation"
(and (eq seen (si map "urn:owner")) (valid map)))
(let ((snapshot (topic-maps.import.tests::state map (topic-maps.import.tests::handles map))))
(check "model:deterministic-mutation-free-CXTM"
(and (string= (cxtm map) (cxtm map)) (equal snapshot (topic-maps.import.tests::state map (topic-maps.import.tests::handles map))))))))))
(dolist (fixture
(list
(list "core" "a ; ^."
"b ; ^." :core-subject-conflict)
(list "reifier" "a - \"One\" ~." "b - \"Two\" ~." :reifier-conflict)
(list "identifiers" "a ^." "b ; : \"O\"." :duplicate-item-identifier)
(list "variant" "a ; ." "o - \"N\" @ (\"V\" @)." :variant-scope-collapse)))
(destructuring-bind (label before child code) fixture
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "conflict:child")))
(declare (ignore calls))
(let ((intercepted nil))
(let ((condition (condition-from (lambda ()
(call-with-import-observer
(lambda (saved target source)
(when (string= label "identifiers")
(add-item-identifier (first (topic-occurrences (si source "urn:child"))) "urn:collision"))
(let* ((th (topic-maps.import.tests::handles target)) (sh (topic-maps.import.tests::handles source))
(tb (topic-maps.import.tests::state target th)) (sb (topic-maps.import.tests::state source sh))
(tx (cxtm target)) (sx (cxtm source)) (c (condition-from (lambda () (funcall saved target source)))))
(check (format nil "model:atomic-~A:both-real-graphs-and-CXTM-unchanged" label)
(and c (equal tb (topic-maps.import.tests::state target th)) (equal sb (topic-maps.import.tests::state source sh))
(string= tx (cxtm target)) (string= sx (cxtm source))))
(setf intercepted t) (error c)))
(lambda () (read-map (format nil "~A~%~A" before (directive "urn:child")) :resource-resolver resolver)))))))
(check (format nil "model:atomic-~A:caller-diagnostic" label)
(and intercepted (typep condition 'topic-maps.ctm:ctm-rejection)
(eq code (topic-maps.ctm:error-code condition))
(string= "memory:main" (topic-maps.ctm:error-source condition))
(string= "conflict:child" (getf (first (topic-maps.ctm:error-context condition)) :resource-source))))))))))
(defun repeated-role-tests ()
(let* ((child "(: ~) ~")
(text (format nil "~A~%~A" (directive "urn:child") (directive "urn:child")))
(old-association nil) (old-role nil) (imports 0))
(multiple-value-bind (resolver calls) (resources (list (list "urn:child" (ascii child) "repeat:child")))
(let ((map
(call-with-import-observer
(lambda (saved target source)
(incf imports)
(let* ((a (first (topic-map-associations source))) (r (first (association-roles a))))
;; Metadata supplied through the model API, not CTM syntax.
(add-item-identifier a (format nil "urn:association-copy:~D" imports))
(add-item-identifier r (format nil "urn:role-copy:~D" imports))
(let* ((held (topic-maps.import.tests::handles source)) (before (topic-maps.import.tests::state source held)) (xml (cxtm source)))
(multiple-value-bind (result resolve) (funcall saved target source)
(check (format nil "repeat-roles:~D:source-preserved" imports)
(and (equal before (topic-maps.import.tests::state source held)) (string= xml (cxtm source))))
(when (= imports 1) (setf old-association (funcall resolve a) old-role (funcall resolve r)))
(check (format nil "repeat-roles:~D:resolver-canonical-Association-and-Role-EQ" imports)
(and (eq (canonical-association old-association) (funcall resolve a))
(eq (canonical-association-role old-role) (funcall resolve r))))
(values result resolve)))))
(lambda () (read-map text :resource-resolver resolver)))))
(let* ((a (first (topic-map-associations map))) (r (first (association-roles a))))
(check "repeat-roles:repeated-directive-and-retained-forwarding-handles"
(and (= 2 imports) (= 2 (length (funcall calls))) (= 1 (length (topic-map-associations map)))
(= 1 (length (association-roles a)))
(not (eq a old-association)) (eq a (canonical-association old-association))
(not (eq r old-role)) (eq r (canonical-association-role old-role))))
(check "repeat-roles:Identifier-union-Reifier-and-Parent-EQ"
(and (= 2 (length (item-identifiers a))) (= 2 (length (item-identifiers r)))
(eq a (association-role-parent r)) (eq a (topic-reified (item-reifier a)))
(eq r (topic-reified (item-reifier r))) (valid map))))))))
(defun original-cases ()
(setf *observations* nil *rejections* nil)
(dolist (file *originals*)
(let* ((id (format nil "ctm/in/~A.ctm" file))
(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 (resolver calls) (original-resources)
(let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
:document-iri doc :source-name id :resource-resolver resolver))
(held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held))
(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 ":complete-model-and-reference-invariants") (valid map))
(check (concatenate 'string id ":original-byte-exact-CXTM") (equalp actual expected))
(check (concatenate 'string id ":deterministic-and-pure-CXTM")
(and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal before (topic-maps.import.tests::state map held))))
(if (string= file "mergemap-tm-reifier")
(check "original:map-Reifier-edge-excluded-content-Topic-retained"
(and (= 1 (length (topic-map-topics map))) (null (item-reifier map))
(null (topic-reified (first (topic-map-topics map))))))
(check (concatenate 'string id ":separate-document-local-subjects-without-Include-aliases")
(and (= 7 (length (topic-map-topics map))) (= 2 (length (topic-map-associations map)))
(local map (concatenate 'string doc "#lars"))
(= 1 (length (topic-item-identifiers (local map (concatenate 'string doc "#lars"))))))))
(topic-maps.test-foundation:record-result case :cxtm-match)
(setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "CTM 3.2" "CTM 3.3.3-3.3.8" "TMDM 5.1-5.8" "TMDM 6.1-6.7" "CXTM 3.18"))
(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)))))
(push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))
"requests" (topic-maps.test-foundation::json-array (funcall calls)) "model" (observe map) "byteCount" (length actual)) *observations*)
(format t "~&MERGEMAP-CXTM ~A: PASS (~D bytes)~%" id (length actual))))))
(dolist (fixture '(("mergemap-missing-syntax-iri" topic-maps.ctm:ctm-rejection :expected-mergemap-notation 0)
("mergemap-template" topic-maps.ctm:undefined-template :undefined-template 1)
("mergemap-unknown-syntax" topic-maps.ctm:ctm-rejection :unknown-mergemap-notation 0)))
(destructuring-bind (file class code requests) fixture
(let* ((id (format nil "ctm/invalid/~A.ctm" file))
(case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id))))
(multiple-value-bind (resolver calls) (original-resources)
(let* ((text (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)))
(c (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 :resource-resolver resolver)))))
(check (concatenate 'string id ":specific-rejection-from-intended-cause")
(and (eq class (type-of c)) (eq code (topic-maps.ctm:error-code c)) (= requests (length (funcall calls)))
(string= id (topic-maps.ctm:error-source c)) (<= 0 (topic-maps.ctm:error-position c) (length text))))
(topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string c)))
(setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "CTM 3.15"))
(push (topic-maps.test-foundation::fields "id" id "class" (symbol-name class) "code" (symbol-name code)
"source" (topic-maps.ctm:error-source c) "position" (topic-maps.ctm:error-position c)
"line" (topic-maps.ctm:error-line c) "column" (topic-maps.ctm:error-column c)
"requests" (topic-maps.test-foundation::json-array (funcall calls))) *rejections*)
(format t "~&MERGEMAP-REJECTION ~A: PASS (~A)~%" id code))))))
(setf *observations* (nreverse *observations*) *rejections* (nreverse *rejections*)))
(defun run-tests ()
(topic-maps.import.tests:run-tests)
(let ((topic-maps.ctm.tests::*checks* nil))
(resolution-tests) (environment-tests) (traversal-tests) (diagnostic-tests) (resolver-policy-tests) (identity-tests) (model-tests) (repeated-role-tests) (original-cases)
(check "mergemap:only-seven-new-contracts-registered"
(equal '(:pass 194 :fail 2 :unsupported 0 :not-run 77)
(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 "~&MERGEMAP-PASS: ~D checks; four byte-exact originals, three specific rejections.~%" (length *last-checks*)) t))
(defun write-mergemap-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" "ctm-mergemap-execution-evidence" "passed" (length *last-checks*) "failed" 0
"checks" (topic-maps.test-foundation::json-array *last-checks*)
"cases" (topic-maps.test-foundation::json-array *observations*)
"rejections" (topic-maps.test-foundation::json-array *rejections*)) out)
(terpri out)))