]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-dates.lisp
Implement XTM role reification with native conformance evidence
[topic-maps.git] / tests / ctm-dates.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defparameter *date-ids* '("occurrence-date" "occurrence-date2" "occurrence-date3" "occurrence-date-negative" "occurrence-date-another-century"))
4 (defvar *last-date-checks* nil)
5 (defvar *date-observations* nil)
6 (defvar *date-rejections* nil)
7 (defvar *date-probes* nil)
8 (defvar *date-calendar-decisions* nil)
9 (defun date-checks () *last-date-checks*)
10
11 (defun exercise-date-cases ()
12 (setf *date-observations* nil *date-rejections* nil)
13 (loop for name in *date-ids*
14 for values in '(("1976-09-19") ("1976-10-09") ("0001-12-02" "0011-12-02" "0111-12-02" "1111-12-02")
15 ("-1976-09-19") ("191976-09-19")) do
16 (let* ((id (format nil "ctm/in/~A.ctm" name))
17 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
18 (doc (topic-maps.test-foundation:test-case-document-iri case))
19 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
20 (owner (local-topic map "topic" doc)) (items (all-occurrences map)))
21 (setf (topic-maps.test-foundation:test-case-clauses case)
22 '("CTM 3.2" "CTM 3.4.1" "CTM 3.10" "TMDM 4.3-4.4" "TMDM 5.6" "TMDM 6.5"
23 "CXTM 3.16" "CXTM 4.9" "XSD 3.2.7.1" "XSD 3.2.9-3.2.9.2" "XSD D" "XSD E.1"))
24 (check (concatenate 'string name ":actual-values") (equal values (mapcar #'topic-maps.tmdm:occurrence-value items)))
25 (check (concatenate 'string name ":no-extra-items")
26 (and (= (1+ (length values)) (length (topic-map-topics map))) (= (length values) (length items))
27 (null (topic-maps.tmdm:topic-map-associations map)) (notany #'topic-names (topic-map-topics map))))
28 (loop for item in items for i from 1 do
29 (check (format nil "~A:object-references:~D" name i)
30 (and (eq owner (topic-maps.tmdm::%occurrence-parent item))
31 (eq (local-topic map (if (= (length values) 1) "occtype" (format nil "o~D" i)) doc)
32 (topic-maps.tmdm::%occurrence-type item))
33 (equal (topic-maps.tmdm:date-datatype) (topic-maps.tmdm:occurrence-datatype item))
34 (null (topic-maps.tmdm:occurrence-item-identifiers item)))))
35 (check (concatenate 'string name ":datatype-is-not-a-topic")
36 (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic)))
37 (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) (topic-map-topics map)))
38 (let* ((state (map-state map)) (retained (occurrence-handle-state items))
39 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
40 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
41 (different (mismatch actual expected)))
42 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
43 :diagnostics (when different (list (format nil "First different byte ~D; actual ~D, expected ~D bytes." different (length actual) (length expected)))))
44 (check (concatenate 'string name ":byte-exact-cxtm") (null different))
45 (check (concatenate 'string name ":serialization-pure") (and (equal state (map-state map)) (equal retained (occurrence-handle-state items))))
46 (setf *cxtm-results* (append *cxtm-results* (list (list :id id :base doc :actual (cxtm map doc)
47 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
48 :match (null different) :bytes (length actual)))))
49 (format t "~&DATE-CXTM ~A: PASS (~D bytes)~%" id (length actual)))
50 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass"
51 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
52 "actualObjects" (occurrence-observation map)) *date-observations*)))
53 (dolist (entry '(("date-invalid-day" "2000-12-32" :invalid-date-day)
54 ("date-invalid-month" "2000-13-02" :invalid-date-month)
55 ("date-invalid-year" "0000-01-02" :invalid-date-year)
56 ("date-invalid-year2" "00002000-01-02" :invalid-date-year)
57 ("date-invalid-year3" "+2000-01-02" :invalid-date-year)))
58 (destructuring-bind (name token code) entry
59 (let* ((id (format nil "ctm/invalid/~A.ctm" name))
60 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
61 (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8))
62 (condition (handler-case (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
63 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id) nil) (ctm-error (c) c))))
64 (check (concatenate 'string name ":specific-date-rejection")
65 (and (eq (type-of condition) 'ctm-rejection) (eq code (error-code condition))))
66 (check (concatenate 'string name ":position-and-source")
67 (and (= (search token source) (error-position condition)) (> (error-line condition) 1)
68 (= 14 (error-column condition)) (equal id (topic-maps.ctm:error-source condition))
69 (eq :character (topic-maps.ctm:error-position-unit condition))))
70 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.4.1" "XSD 3.2.7.1" "XSD 3.2.9.1" "XSD D"))
71 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition)))
72 (push (topic-maps.test-foundation::fields "id" id "status" "pass" "ctmSource" source
73 "code" (symbol-name (error-code condition)) "conditionClass" (symbol-name (type-of condition))
74 "position" (error-position condition) "line" (error-line condition) "column" (error-column condition)) *date-rejections*)
75 (format t "~&DATE-REJECTION ~A: PASS (~A at ~D:~D)~%" id code (error-line condition) (error-column condition)))))
76 (setf *date-observations* (nreverse *date-observations*) *date-rejections* (nreverse *date-rejections*))
77 (check "slice9:59-imported-passes-214-not-run"
78 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 59 :fail 0 :unsupported 0 :not-run 214))))
79
80 (defun date-lexical-tests ()
81 (setf *date-probes* nil)
82 (let ((values (append '("0001-01-01" "0011-12-02" "0111-12-02" "1111-12-02" "10000-01-01" "-10000-01-01"
83 "2000-02-29" "2400-02-29" "1900-02-28" "2100-02-28" "-2000-02-29" "-1900-02-28" "-0004-02-29"
84 "1976-10-09" "2000-04-30" "2000-12-31")
85 (list (concatenate 'string (make-string 10000 :initial-element #\9) "-12-31")
86 (concatenate 'string "-" (make-string 10000 :initial-element #\8) "-02-29"))
87 (loop for year in '(4 100 400 1600 1700 1800 1900 1996 2000 2004 2100 2400 -4 -100 -400)
88 append (loop for month from 1 to 12 collect (format nil "~A~4,'0D-~2,'0D-01" (if (minusp year) "-" "") (abs year) month))))))
89 (loop for value in values for i from 0 do
90 (dolist (mode '(:implicit :explicit))
91 (let* ((input (if (eq mode :explicit) (format nil " ~C~A~C " #\Tab value #\Return) value))
92 (map (read-text (if (eq mode :implicit) (format nil "a t: ~A." input)
93 (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#date>." input))))
94 (o (first (all-occurrences map))) (state (map-state map)) (handles (occurrence-handle-state (list o))) (out (cxtm map)))
95 (check (format nil "date-helper-valid:~D:~A" i mode) (topic-maps.tmdm:date-lexical-p input))
96 (check (format nil "date-canonical-form:~D:~A" i mode) (string= value (topic-maps.tmdm:canonical-date-lexical-form input)))
97 (check (format nil "date-stored-lexical-value:~D:~A" i mode) (string= input (topic-maps.tmdm:occurrence-value o)))
98 (check (format nil "date-output-and-purity:~D:~A" i mode)
99 (and (equal (list value) (serialized-values out)) (equal state (map-state map)) (equal handles (occurrence-handle-state (list o)))))
100 (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode))
101 "storedValue" (topic-maps.tmdm:occurrence-value o) "datatype" (topic-maps.tmdm:occurrence-datatype o) "actualCXTM" out) *date-probes*)))))
102 (setf *date-probes* (nreverse *date-probes*))
103 (loop for (literal kind) in '(("2000-01-01" :date) ("-2000-01-01" :date) ("1" :integer) ("1.0" :decimal) (".3" :decimal)) do
104 (let* ((lexer (topic-maps.ctm::make-lexer (concatenate 'string literal ".")))
105 (value (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer)))
106 (check (concatenate 'string "date-integer-decimal-terminator:" literal)
107 (and (eq kind (topic-maps.ctm::token-kind value)) (string= literal (topic-maps.ctm::token-value value))
108 (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot))
109 (= (length literal) (topic-maps.ctm::token-position dot))))))
110 (dolist (source '("a t: 2000-01-01.b." "a t: 2000-01-01;." "a t: 2000-01-01#(comment)#."))
111 (check (concatenate 'string "date-token-layout:" source) (equal '("2000-01-01") (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences (read-text source))))))
112 (let ((map (read-text "%prefix ex <urn:date-name:> ex:2000-01-01.")))
113 (check "date-looking-qname-remains-topic-identity"
114 (and (null (all-occurrences map)) (equal '("urn:date-name:2000-01-01") (topic-subject-identifiers (first-topic map))))))
115 (check "date-integer-decimal-and-string-distinct"
116 (equal (list (topic-maps.tmdm:date-datatype) (topic-maps.tmdm:integer-datatype) (topic-maps.tmdm:decimal-datatype) (topic-maps.tmdm:string-datatype))
117 (mapcar #'topic-maps.tmdm:occurrence-datatype (all-occurrences (read-text "a t: 2000-01-01;t: 2000;t: 2000.0;t:\"2000-01-01\"."))))))
118
119 (defun date-error-tests ()
120 (setf *date-calendar-decisions* nil)
121 (loop for (year leap) in '((4 t) (100 nil) (400 t) (1600 t) (1700 nil) (1800 nil) (1900 nil) (1996 t) (2000 t) (2004 t) (2100 nil) (2400 t) (-4 t) (-100 nil) (-400 t)) do
122 (let* ((value (format nil "~A~4,'0D-02-29" (if (minusp year) "-" "") (abs year)))
123 (condition (ctm-condition (format nil "a t: ~A." value))))
124 (check (concatenate 'string "date-century-helper:" value) (eq (not (null leap)) (topic-maps.tmdm:date-lexical-p value)))
125 (check (concatenate 'string "date-century-deserialization:" value)
126 (if leap (null condition) (and (typep condition 'ctm-rejection) (eq :invalid-date-day (error-code condition)))))
127 (push (topic-maps.test-foundation::fields "input" value "expected" (if leap "valid" "invalid")) *date-calendar-decisions*)))
128 (setf *date-calendar-decisions* (nreverse *date-calendar-decisions*))
129 (loop for value in '("" "2000" "+2000-01-01" "0000-01-01" "-0000-01-01" "02000-01-01" "-02000-01-01" "123-01-01"
130 "2000-00-01" "2000-13-01" "2000-01-00" "2000-01-32" "2000-04-31" "1900-02-29" "2100-02-29"
131 "2001-02-29" "-1900-02-29" "2000-2-01" "2000-02-1" "2000-01-01junk" "2000-01-01.0"
132 "2000-01-01+14:01" "2000-01-01-15:00" "2000-01-01+00:60" "2000-01-01z" "2000 -01-01" "2000-01-01")
133 for i from 0 do
134 (check (format nil "date-invalid-helper:~D" i) (and (not (topic-maps.tmdm:date-lexical-p value)) (null (topic-maps.tmdm:canonical-date-lexical-form value))))
135 (let ((condition (ctm-condition (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#date>." value))))
136 (check (format nil "date-invalid-explicit:~D" i)
137 (and (eq (type-of condition) 'ctm-rejection) (eq :invalid-date-value (error-code condition)) (= 4 (error-position condition))
138 (= 1 (error-line condition)) (= 5 (error-column condition))))))
139 (dolist (value '("0000-01-01" "+2000-01-01" "02000-01-01" "2000-04-31" "2001-02-29" "2000-13-01" "2000-01-01Z" "2000-01-01+00:00"))
140 (let ((condition (ctm-condition (format nil "a.~%b t: ~A." value))))
141 (check (concatenate 'string "date-bare-specific-rejection:" value)
142 (and (eq (type-of condition) 'ctm-rejection) (= 2 (error-line condition)) (= 6 (error-column condition))
143 (member (error-code condition) '(:invalid-date-year :invalid-date-month :invalid-date-day :invalid-date-syntax))
144 (equal "independent-fixture" (topic-maps.ctm:error-source condition))))))
145 (let* ((map (read-text "a. t.")) (owner (local-topic map "a")) (typ (local-topic map "t")) (state (map-state map)))
146 (let ((condition (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence owner typ "1900-02-29" :datatype (topic-maps.tmdm:date-datatype))))))
147 (check "invalid-model-date-atomic" (and (eq :invalid-date-value (topic-maps.tmdm:occurrence-error-code condition)) (equal state (map-state map)))))
148 (setf (topic-maps.tmdm::%topic-occurrences owner) (list (topic-maps.tmdm::%make-occurrence "0000-01-01" (topic-maps.tmdm:date-datatype) typ owner)))
149 (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map)))))
150 (check "invalid-private-model-date-cxtm-error"
151 (and (eq (type-of condition) 'topic-maps.cxtm:cxtm-error) (eq :invalid-date-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map))))))
152 (dolist (zone '("Z" "+00:00" "-00:00" "+14:00" "-14:00" "+12:00" "-11:59"))
153 (let* ((value (concatenate 'string "2000-01-01" zone))
154 (map (read-text (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#date>." value)))
155 (state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map)))))
156 (check (concatenate 'string "date-timezone-valid-but-explicitly-unsupported:" zone)
157 (and (topic-maps.tmdm:date-lexical-p value) (typep condition 'topic-maps.cxtm:cxtm-unsupported)
158 (eq :date-timezone-canonicalization (topic-maps.cxtm:error-code condition)) (equal state (map-state map))))))
159 (check "dateTime-now-supported" (equal '("2000-01-01T12:00:00Z") (serialized-values (cxtm (read-text "a t: 2000-01-01T12:00:00Z."))))))
160
161 (defun date-merge-and-order-tests ()
162 (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"))
163 (dt (topic-maps.tmdm:date-datatype))
164 (o1 (topic-maps.tmdm:add-occurrence a r "2000-01-01" :datatype dt :item-identifiers '("urn:date:1")))
165 (o2 (topic-maps.tmdm:add-occurrence b s "2000-01-01" :datatype dt :item-identifiers '("urn:date:2")))
166 (o3 (topic-maps.tmdm:add-occurrence a r " 2000-01-01 " :datatype dt)))
167 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
168 (check "date-merge-by-stored-properties-not-identical-output"
169 (and (= 2 (length (all-occurrences map))) (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o2))
170 (not (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o3)))))
171 (check "date-retained-handles-and-identifiers"
172 (and (equal '("urn:date:1" "urn:date:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<))
173 (not (eq o1 (topic-maps.tmdm:canonical-occurrence o1)))))
174 (let* ((live (topic-maps.tmdm:canonical-occurrence o1)) (state (map-state map)) (handles (occurrence-handle-state (list o1 o2 o3))))
175 (check "date-stored-parent-and-type-canonical"
176 (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a))
177 (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r))))
178 (check "date-identical-canonical-values-do-not-merge-items" (equal '("2000-01-01" "2000-01-01") (serialized-values (cxtm map))))
179 (check "date-canonicalization-leaves-merge-handles-untouched" (and (equal state (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 o3)))))))
180 (dolist (fixture '((("-2000-01-01" "-0001-01-01" "10000-01-01" "9999-01-01") ("-0001-01-01" "-2000-01-01" "10000-01-01" "9999-01-01"))
181 (("2000-01-01" " 2000-01-02 " "1999-01-01") ("2000-01-02" "1999-01-01" "2000-01-01"))))
182 (destructuring-bind (values expected) fixture
183 (let ((reference nil))
184 (dolist (order (permutations values))
185 (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t")))
186 (dolist (value order) (topic-maps.tmdm:add-occurrence a typ value :datatype (topic-maps.tmdm:date-datatype)))
187 (let ((state (map-state map)) (out (cxtm map)))
188 (unless reference (setf reference out))
189 (check (format nil "date-stored-lexical-order:~S" order) (equal expected (serialized-values out)))
190 (check (format nil "date-construction-order-independent:~S" order) (and (string= reference out) (equal state (map-state map))))))))))
191 (let* ((map (read-text "a t: 2000-01-01;t: 2000-01-01;t:\"2000-01-01\"^^<http://www.w3.org/2001/XMLSchema#date>;t:\"2000-01-01\".")))
192 (check "date-implicit-explicit-duplicates-datatype-sensitive" (= 2 (length (all-occurrences map))))))
193
194 (defun run-date-tests ()
195 (run-decimal-tests)
196 (let ((*checks* nil))
197 (exercise-date-cases) (date-lexical-tests) (date-error-tests) (date-merge-and-order-tests)
198 (setf *last-date-checks* (nreverse *checks*))
199 (format t "~&DATES-PASS: ~D checks; 5 new exact baselines and 5 specific rejections; 59 imported PASS, 214 NOT-RUN.~%" (length *last-date-checks*)) t))
200 (defun write-date-report (path)
201 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
202 (topic-maps.test-foundation::emit-json-value
203 (topic-maps.test-foundation::fields "kind" "date-semantic-and-cxtm-evidence" "passed" (length *last-date-checks*) "failed" 0
204 "checks" (topic-maps.test-foundation::json-array *last-date-checks*) "cases" (topic-maps.test-foundation::json-array *date-observations*)
205 "negativeCases" (topic-maps.test-foundation::json-array *date-rejections*)
206 "calendarDecisions" (topic-maps.test-foundation::json-array *date-calendar-decisions*) "lexicalProbes" (topic-maps.test-foundation::json-array *date-probes*)) out) (terpri out)))