]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/default-name-types.lisp
docs(tmdm): coverage matrix, relationship readers, default name types; run report
[topic-maps.git] / tests / default-name-types.lisp
1 (defpackage #:topic-maps.default-name-type.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check)
4 (:export #:run-tests #:default-name-type-checks))
5 (in-package #:topic-maps.default-name-type.tests)
6
7 ;;; Two documented default name type Subject Identifiers.
8 ;;; The TMDM text dated 2006-06-18 spells clause 7.5's identifier .../model/name-type; the text dated
9 ;;; 2008-06-03 spells it .../model/topic-name, as CTM 3.11 and XTM 2.0 do. Both are supported as different
10 ;;; identifiers. Nothing here equates them, and nothing here decides which text is authoritative.
11 ;;; These tests exercise the unchanged model, CTM reader and CXTM canonicalizer; they add no production code.
12
13 (defvar *last-checks* nil)
14 (defun default-name-type-checks () *last-checks*)
15 (defparameter +document+ "https://example.invalid/a/doc.ctm")
16 (defparameter +topic-name+ "http://psi.topicmaps.org/iso13250/model/topic-name")
17 (defparameter +name-type+ "http://psi.topicmaps.org/iso13250/model/name-type")
18 (defparameter +type+ "http://psi.topicmaps.org/iso13250/model/type")
19
20 (defun read-ctm (text)
21 (topic-maps.ctm:deserialize-string text :document-iri +document+ :source-name "default-name-types"))
22 (defun cxtm (map) (topic-maps.cxtm:canonicalize map :base-locator +document+))
23 (defun with-si (map iri)
24 (find-if (lambda (topic) (member iri (topic-subject-identifiers topic) :test #'string=)) (topic-map-topics map)))
25 (defun local (map id)
26 (find-if (lambda (topic) (member (format nil "~A#~A" +document+ id) (topic-item-identifiers topic) :test #'string=))
27 (topic-map-topics map)))
28 (defun name-types (topic) (mapcar #'topic-name-type (topic-names topic)))
29
30 (defun implicit-tests ()
31 "CTM 3.11: a name without a type gets the default name type, spelled .../topic-name."
32 (let* ((map (read-ctm "a - \"N\" .")) (a (local map "a")) (type (first (name-types a))))
33 (check "implicit:ctm-untyped-name-uses-topic-name"
34 (and (= 1 (length (topic-names a))) (equal (list +topic-name+) (topic-subject-identifiers type))))
35 (check "implicit:no-name-type-topic-is-created" (null (with-si map +name-type+)))
36 (check "implicit:identifier-reader-is-unchanged" (string= +topic-name+ (default-name-type-identifier)))))
37
38 (defun explicit-tests ()
39 "An explicitly stated name type is kept, whichever of the two identifiers it carries."
40 (let* ((map (read-ctm (format nil "a - <~A> : \"N\" ." +name-type+)))
41 (a (local map "a")) (type (first (name-types a))))
42 (check "explicit:name-type-is-preserved-on-import"
43 (and (= 1 (length (topic-names a))) (equal (list +name-type+) (topic-subject-identifiers type))))
44 (check "explicit:name-type-creates-no-topic-name-topic" (null (with-si map +topic-name+)))
45 (check "explicit:name-type-is-preserved-on-cxtm-export"
46 (and (search +name-type+ (cxtm map)) (not (search +topic-name+ (cxtm map))))))
47 (let* ((map (read-ctm (format nil "a - <~A> : \"N\" ; - \"N\" ." +topic-name+))) (a (local map "a")))
48 ;; Same Subject Identifier, hence the same Topic (TMDM 5.3.5) and equal Names (TMDM 5.4).
49 (check "explicit:topic-name-is-the-implicit-default"
50 (and (= 1 (length (topic-names a))) (eq (with-si map +topic-name+) (first (name-types a)))))))
51
52 (defun coexistence-tests ()
53 "Both identifiers in one map stay two Topics; equal-looking Names of the two types stay two Names."
54 (let* ((map (read-ctm (format nil "a - \"N\" ; - <~A> : \"N\" ." +name-type+)))
55 (a (local map "a")) (old (with-si map +name-type+)) (new (with-si map +topic-name+)))
56 (check "coexistence:two-type-topics" (and old new (not (eq old new))))
57 (check "coexistence:two-names" (= 2 (length (topic-names a))))
58 (check "coexistence:each-name-keeps-its-type"
59 (null (set-exclusive-or (list old new) (name-types a) :test #'eq)))
60 (let ((before (cxtm map)))
61 (normalize-topic-map map)
62 (check "coexistence:normalization-equates-nothing"
63 (and (= 2 (length (topic-names a))) (not (eq (canonical-topic old) (canonical-topic new)))
64 (string= before (cxtm map)))))
65 (check "coexistence:both-identifiers-in-cxtm"
66 (and (search +name-type+ (cxtm map)) (search +topic-name+ (cxtm map))))))
67
68 (defun import-tests ()
69 "IMPORT-MAP-CONTENT copies explicit name types of both identifiers unchanged."
70 (let* ((source (read-ctm (format nil "a - \"N\" ; - <~A> : \"M\" ." +name-type+)))
71 (target (make-topic-map)))
72 (import-map-content target source)
73 (let ((a (local target "a")))
74 (check "import:both-type-topics-arrive"
75 (and (with-si target +name-type+) (with-si target +topic-name+)
76 (not (eq (with-si target +name-type+) (with-si target +topic-name+)))))
77 (check "import:names-keep-their-types"
78 (and (= 2 (length (topic-names a)))
79 (every (lambda (name)
80 (equal (topic-subject-identifiers (topic-name-type name))
81 (list (if (string= "N" (topic-name-value name)) +topic-name+ +name-type+))))
82 (topic-names a))))
83 (check "import:cxtm-equal-to-source" (string= (cxtm source) (cxtm target))))))
84
85 (defun characterization-tests ()
86 "What the library does today. Not a normative equivalence and not an interoperability rule."
87 ;; An explicit merge of the two type Topics is a caller's own assertion that they have one subject.
88 (let* ((map (read-ctm (format nil "a - \"N\" ; - <~A> : \"N\" ." +name-type+)))
89 (a (local map "a")) (old (with-si map +name-type+)) (new (with-si map +topic-name+))
90 (merged (merge-topics old new)))
91 (check "characterization:explicit-merge-is-accepted"
92 (and (eq merged (canonical-topic old)) (eq merged (canonical-topic new))))
93 (check "characterization:merged-topic-carries-both-identifiers"
94 (null (set-exclusive-or (list +name-type+ +topic-name+) (topic-subject-identifiers merged) :test #'string=)))
95 (check "characterization:names-become-equal-and-merge" (= 1 (length (topic-names a)))))
96 ;; The protected Core Subject set holds .../name-type and not .../topic-name; it is left as it is.
97 (check "characterization:core-set-is-unchanged"
98 (and (member +name-type+ (core-subject-identifiers) :test #'string=)
99 (not (member +topic-name+ (core-subject-identifiers) :test #'string=))
100 (= 8 (length (core-subject-identifiers)))))
101 (let* ((map (make-topic-map)) (topic (intern-topic map :subject-identifier +name-type+)))
102 (check "characterization:name-type-with-another-core-subject-is-rejected"
103 (handler-case (progn (add-topic-identifier topic :subject-identifier +type+) nil)
104 (core-subject-error (c) (eq :core-subject-conflict (core-subject-error-code c))))))
105 (let* ((map (make-topic-map)) (topic (intern-topic map :subject-identifier +topic-name+))
106 (result (add-topic-identifier topic :subject-identifier +type+)))
107 (check "characterization:topic-name-with-another-core-subject-is-accepted-today"
108 (= 2 (length (topic-subject-identifiers result))))))
109
110 (defun run-tests ()
111 (let ((topic-maps.ctm.tests::*checks* nil))
112 (implicit-tests) (explicit-tests) (coexistence-tests) (import-tests) (characterization-tests)
113 (setf *last-checks* (reverse topic-maps.ctm.tests::*checks*))
114 (format t "~&DEFAULT-NAME-TYPE-PASS: ~D checks; both identifiers kept apart; XTM path NOT-RUN in this suite.~%"
115 (length *last-checks*))
116 *last-checks*))