]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-integer-literal.lisp
feat(tmdm): readers for the TMDM 7.2/7.3 relationships; tests for both default name...
[topic-maps.git] / tests / ctm-integer-literal.lisp
1 (defpackage #:topic-maps.ctm-integer.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.registered-original.tests
4 #:check #:fields #:binary #:case-state #:identity-fields #:captured-read #:condition-data)
5 (:export #:run-tests #:write-reports))
6 (in-package #:topic-maps.ctm-integer.tests)
7 (defparameter *doc* "https://example.invalid/ctm-integer/main.ctm")
8 (defparameter *datatype* "http://psi.topicmaps.org/iso13250/ctm-integer")
9 (defparameter *qualified* "\"*\"^^<http://psi.topicmaps.org/iso13250/ctm-integer>")
10 (defvar *checks* nil)
11 (defvar *positives* nil)
12 (defvar *negatives* nil)
13 (defvar *original* nil)
14 (defvar *entry-statuses* nil)
15 (defun canonical (map) (topic-maps.cxtm:canonicalize-octets map :base-locator *doc*))
16 (defun occurrences (map)
17 (loop for topic in (topic-map-topics map) append (topic-occurrences topic)))
18 (defun item-value-p (item value datatype)
19 (and item (equal value (occurrence-value item)) (equal datatype (occurrence-datatype item))))
20 (defun accepted (label text &optional (initial-counter 0))
21 (multiple-value-bind (map counter)
22 (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label
23 :wildcard-counter initial-counter)
24 (let* ((held (topic-maps.import.tests::handles map))
25 (before (topic-maps.import.tests::state map held))
26 (actual (canonical map)))
27 (check (concatenate 'string label ":current-model-and-pure-canonicalization")
28 (and (topic-maps.reference.tests::references-current-p map)
29 (equal before (topic-maps.import.tests::state map held))
30 (equalp actual (canonical map))))
31 (push (fields "label" label "sourceText" text "initialCounter" initial-counter "finalCounter" counter
32 "model" (topic-maps.template.tests::observe map)
33 "actualCXTM" (topic-maps.ctm::decode-utf8 actual)) *positives*))
34 (values map counter)))
35 (defun assert-occurrence (label map value datatype)
36 (let* ((items (occurrences map)) (item (first items))
37 (owner (topic-maps.registered-original.tests::local map *doc* "topic"))
38 (type (topic-maps.registered-original.tests::local map *doc* "occtype")))
39 (check (concatenate 'string label ":occurrence-value-datatype-and-references")
40 (and (= 1 (length items)) (item-value-p item value datatype)
41 (eq owner (occurrence-parent item)) (eq type (occurrence-type item))
42 (= 2 (length (topic-map-topics map))) (null (topic-map-associations map))
43 (null (occurrence-scope item)) (null (item-identifiers item))))))
44 (defun positive-controls ()
45 (let* ((bare (accepted "equivalence:bare" (format nil "topic~%occtype: *;.")))
46 (qualified (accepted "equivalence:qualified" (format nil "topic~%occtype: ~A;." *qualified*)))
47 (string (accepted "distinction:string" (format nil "topic~%occtype: \"*\";."))))
48 (assert-occurrence "equivalence:bare" bare "*" *datatype*)
49 (assert-occurrence "equivalence:qualified" qualified "*" *datatype*)
50 (assert-occurrence "distinction:string" string "*" (string-datatype))
51 (check "equivalence:qualified-and-bare-CXTM-equal-string-distinct"
52 (and (equalp (canonical bare) (canonical qualified))
53 (not (equalp (canonical bare) (canonical string))))))
54 (dolist (spec '(("compact" "topic occtype:*.") ("semicolon" "topic occtype:*;.")
55 ("scope" "topic occtype:*@s.") ("reifier" "topic occtype:*~r.")
56 ("comment" "topic occtype:*# comment
57 .")
58 ("block-comment" "topic occtype:*#( ignored )#.")))
59 (destructuring-bind (label text) spec
60 (multiple-value-bind (map counter) (accepted (concatenate 'string "boundary:" label) text 31)
61 (check (concatenate 'string "boundary:" label ":exact-value-and-counter")
62 (and (= 31 counter) (= 1 (length (occurrences map)))
63 (item-value-p (first (occurrences map)) "*" *datatype*)))))))
64 (defun numeric-controls ()
65 (dolist (spec '(("+0007" "7" "integer") ("-001" "-1" "integer") ("-00" "0" "integer")
66 (".50" "0.5" "decimal") ("+01.2300" "1.23" "decimal")
67 ("2001-01-01" "2001-01-01" "date")
68 ("2000-01-01T01:00:00+01:00" "2000-01-01T00:00:00Z" "dateTime")
69 ("\"001\"^^<http://www.w3.org/2001/XMLSchema#integer>" "001" "integer")))
70 (destructuring-bind (literal value type) spec
71 (let ((label (concatenate 'string "numeric:" literal)))
72 (multiple-value-bind (map counter) (accepted label (format nil "topic occtype: ~A." literal) 31)
73 (assert-occurrence label map value (concatenate 'string "http://www.w3.org/2001/XMLSchema#" type))
74 (check (concatenate 'string label ":counter") (= 31 counter)))))))
75 (defun variant-controls ()
76 (let ((bare (accepted "variant:bare" "topic - \"N\" (* @s)." 19))
77 (qualified (accepted "variant:qualified" (format nil "topic - \"N\" (~A @s)." *qualified*) 19))
78 (string (accepted "variant:string" "topic - \"N\" (\"*\" @s)." 19)))
79 (dolist (entry (list (list "bare" bare *datatype*) (list "qualified" qualified *datatype*)
80 (list "string" string (string-datatype))))
81 (destructuring-bind (label map datatype) entry
82 (let* ((topic (topic-maps.registered-original.tests::local map *doc* "topic"))
83 (name (first (topic-names topic))) (variants (topic-name-variants name)) (v (first variants)))
84 (check (concatenate 'string "variant:" label ":actual-Variant-value-datatype-parent-and-scope")
85 (and (= 1 (length variants)) (equal "*" (variant-value v))
86 (equal datatype (variant-datatype v)) (eq name (variant-parent v))
87 (equal (list (topic-maps.registered-original.tests::local map *doc* "s")) (variant-scope v)))))))
88 (check "variant:bare-qualified-CXTM-equal-string-distinct"
89 (and (equalp (canonical bare) (canonical qualified)) (not (equalp (canonical bare) (canonical string)))))))
90 (defun wildcard-controls ()
91 (multiple-value-bind (map counter) (accepted "counter:star-only" "topic occtype:*; other:*." 41)
92 (check "counter:star-only-no-generated-identifiers"
93 (and (= 41 counter) (= 2 (length (occurrences map)))
94 (every (lambda (topic)
95 (notany (lambda (iri) (search "#$__" iri)) (topic-item-identifiers topic)))
96 (topic-map-topics map)))))
97 (let ((source "? t:*. ?named t:*. ?named u:*. ? t:*."))
98 (multiple-value-bind (bare bare-counter) (accepted "counter:interleaved-bare" source 41)
99 (multiple-value-bind (qualified qualified-counter)
100 (accepted "counter:interleaved-qualified"
101 (format nil "? t:~A. ?named t:~A. ?named u:~A. ? t:~A."
102 *qualified* *qualified* *qualified* *qualified*) 41)
103 (let* ((iris (loop for topic in (topic-map-topics bare) append (topic-item-identifiers topic)))
104 (generated (remove-if-not (lambda (iri) (search "#$__" iri)) iris))
105 (expected (mapcar (lambda (suffix) (concatenate 'string *doc* suffix))
106 '("#$__42" "#$__43.named" "#$__44"))))
107 (check "counter:only-question-wildcards-increment-and-named-identity-reused"
108 (and (= 44 bare-counter qualified-counter)
109 (topic-maps.registered-original.tests::same-set generated expected)
110 (= 4 (length (occurrences bare)))
111 (every (lambda (item) (item-value-p item "*" *datatype*)) (occurrences bare))
112 (equalp (canonical bare) (canonical qualified)))))))))
113 (defun template-controls ()
114 (dolist (spec '(("argument" "def f($v) topic occtype:$v. end f(*)" :occurrence)
115 ("body" "def f() topic occtype:*. end f()" :occurrence)
116 ("nested-argument" "def outer($v) inner($v) end def inner($v) topic occtype:$v. end outer(*)" :occurrence)
117 ("nested-body-argument" "def outer() inner(*) end def inner($v) topic occtype:$v. end outer()" :occurrence)
118 ("variant-argument" "def f($v) topic - \"N\" ($v @s). end f(*)" :variant)
119 ("variant-body" "def f() topic - \"N\" (* @s). end f()" :variant)
120 ("unused-body" "def f() topic occtype:*. end" :empty)))
121 (destructuring-bind (label text kind) spec
122 (multiple-value-bind (map counter) (accepted (concatenate 'string "template:" label) text 53)
123 (check (concatenate 'string "template:" label ":literal-and-counter")
124 (and (= 53 counter)
125 (ecase kind
126 (:occurrence (and (= 1 (length (occurrences map)))
127 (item-value-p (first (occurrences map)) "*" *datatype*)))
128 (:variant
129 (let* ((owner (topic-maps.registered-original.tests::local map *doc* "topic"))
130 (name (first (topic-names owner))) (v (first (topic-name-variants name))))
131 (and v (equal "*" (variant-value v)) (equal *datatype* (variant-datatype v))
132 (eq name (variant-parent v)))))
133 (:empty (and (null (topic-map-topics map)) (null (topic-map-associations map)))))))))))
134 (defun token-controls ()
135 (dolist (entry '(("*" ((:ctm-integer "*" 0) (:eof nil 1)))
136 ("*;." ((:ctm-integer "*" 0) (:punctuation #\; 1) (:punctuation #\. 2) (:eof nil 3)))
137 ("? ?named * $v" ((:wildcard nil 0) (:wildcard "named" 2)
138 (:ctm-integer "*" 9) (:variable "v" 11) (:eof nil 13)))))
139 (destructuring-bind (text expected) entry
140 (multiple-value-bind (tokens failure) (topic-maps.registered-original.tests::lexical-evidence text "tokens")
141 (check (concatenate 'string "tokens:" text) (and (null failure) (equal expected tokens)))))))
142 (defparameter *negative-specs*
143 '(
144 ("double-star" "topic
145 occtype: **;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
146 ("suffix-name" "topic
147 occtype: *foo;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
148 ("suffix-digit" "topic
149 occtype: *1;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
150 ("suffix-question" "topic
151 occtype: *?;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
152 ("suffix-colon" "topic
153 occtype: *:;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
154 ("signed-plus" "topic
155 occtype: +*;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
156 ("signed-minus" "topic
157 occtype: -*;." topic-maps.ctm:ctm-rejection :expected-occurrence-value 15 2 10)
158 ("date-looking" "topic
159 occtype: *-1;." topic-maps.ctm:ctm-rejection :invalid-date-year 15 2 10)
160 ("datetime-looking" "topic
161 occtype: *T;." topic-maps.ctm:ctm-rejection :invalid-date-year 15 2 10)
162 ("decimal-looking" "topic
163 occtype: *.5;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10)
164 ("qualified-bare" "topic
165 occtype: *^^<urn:d>;." topic-maps.ctm:ctm-rejection :expected-separator 16 2 11)
166 ("missing-separator" "topic
167 occtype: * extra;." topic-maps.ctm:ctm-rejection :expected-separator 17 2 12)
168 ("topic-position" "*." topic-maps.ctm:ctm-rejection :expected-topic-identity 0 1 1)
169 ("name-position" "topic - *." topic-maps.ctm:ctm-rejection :expected-name-value 8 1 9)
170 ("wildcard-value" "topic t: ?." topic-maps.ctm:ctm-rejection :expected-occurrence-value 9 1 10)
171 ("named-wildcard-value" "topic t: ?named." topic-maps.ctm:ctm-rejection :expected-occurrence-value 9 1 10)
172 ("variable-outside" "topic t: $value." topic-maps.ctm:unbound-template-variable :variable-outside-template 9 1 10)
173 ("invalid-integer" "topic t: 42x." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 9 1 10)
174 ("invalid-exponent" "topic t: 1e3." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 9 1 10)
175 ("variant-suffix" "topic - \"Name\" (*x @s)." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 16 1 17)
176 ("unterminated-string" "topic - \"Name." topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9)
177 ("unicode-escape" "topic - \"\\u0x00\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 12 1 13)
178 ("triple-unclosed" "topic t: \"\"\"x." topic-maps.ctm:ctm-rejection :unterminated-string 9 1 10)
179 ("iso88591-still-unsupported" "%encoding \"iso-8859-1\"
180 topic." topic-maps.ctm:ctm-unsupported :unsupported-encoding 11 1 12)
181 ("template-topic-type" "def f($t) $t. end f(*)" topic-maps.ctm:invalid-template-argument :invalid-template-argument 10 1 11)
182 ("template-name-type" "def f($n) topic - $n. end f(*)" topic-maps.ctm:invalid-template-argument :invalid-template-argument 18 1 19)
183 ))
184 (defun negative-controls ()
185 (dolist (spec *negative-specs*)
186 (destructuring-bind (label text class code position line column) spec
187 (multiple-value-bind (map condition)
188 (captured-read (lambda () (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label)))
189 (check (concatenate 'string "negative:" label ":class-code-exact-position")
190 (and (null map) condition (eq class (type-of condition))
191 (eq code (topic-maps.ctm:error-code condition))
192 (= position (topic-maps.ctm:error-position condition))
193 (= line (topic-maps.ctm:error-line condition)) (= column (topic-maps.ctm:error-column condition))
194 (eq :character (topic-maps.ctm:error-position-unit condition))
195 (equal label (topic-maps.ctm:error-source condition))
196 (if (eq class 'topic-maps.ctm:invalid-template-argument)
197 (consp (topic-maps.ctm:error-context condition))
198 (null (topic-maps.ctm:error-context condition)))
199 (null (topic-maps.ctm:error-cause condition))))
200 (push (fields "label" label "sourceText" text "condition" (condition-data condition)) *negatives*)))))
201 (defun original-contract ()
202 (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*) (root (topic-maps.test-foundation:corpus-root corpus))
203 (id "ctm/in/occurrence-ctm-integer.ctm")
204 (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected))
205 (cases (topic-maps.test-foundation:corpus-cases corpus))
206 (previous (mapcar (lambda (c) (cons c (case-state c))) cases))
207 (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*))
208 (*doc* (topic-maps.test-foundation:test-case-document-iri case)))
209 (setf *entry-statuses* (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
210 (check "original:existing-positive-NOT-RUN-record-and-pinned-identities"
211 (and (= 1 (length selected)) (member case cases :test #'eq)
212 (eq :valid (topic-maps.test-foundation:test-case-category case))
213 (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
214 (eq :not-run (topic-maps.test-foundation:test-case-status case))
215 (null (topic-maps.test-foundation:test-case-actual-outcome case))
216 (equal *doc* (format nil "https://ctm-tests.example.invalid/~A/~A"
217 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit) id))
218 (equal (truename (merge-pathnames id root)) (truename (topic-maps.test-foundation:test-case-source-pathname case)))))
219 (check "original:unchanged-eighteen-source-bytes"
220 (equalp (binary (topic-maps.test-foundation:test-case-source-pathname case))
221 (map '(vector (unsigned-byte 8)) #'char-code (format nil "topic~%occtype: *;."))))
222 (multiple-value-bind (row result) (topic-maps.registered-original.tests::exercise-case case :occurrences)
223 (setf *original* row)
224 (check "original:actual-356-byte-original-comparison-PASS"
225 (and result (getf result :match) (= 356 (getf result :bytes))
226 (eq :pass (topic-maps.test-foundation:test-case-status case))))
227 (multiple-value-bind (control counter) (accepted "original:qualified-control" (format nil "topic~%occtype: ~A;." *qualified*))
228 (assert-occurrence "original:qualified-control" control "*" *datatype*)
229 (check "original:qualified-control-356-byte-baseline-match-and-zero-counter"
230 (and (= 0 counter) (= 356 (length (canonical control)))
231 (equalp (canonical control) (binary (topic-maps.test-foundation:test-case-expected-baseline case)))
232 (equal (getf result :actual) (topic-maps.ctm::decode-utf8 (canonical control))))))
233 (check "original:all-prior-case-records-and-observations-preserved"
234 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*)
235 (equal cases (topic-maps.test-foundation:corpus-cases corpus))
236 (equal old-results topic-maps.ctm.tests::*cxtm-results*)
237 (every (lambda (entry) (if (eq case (car entry))
238 (equal (first (cdr entry)) (identity-fields case))
239 (equal (cdr entry) (case-state (car entry))))) previous)))
240 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results* (list result))))
241 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
242 (check "original:exactly-one-NOT-RUN-to-PASS-no-other-status-changes"
243 (and (= (1+ (getf *entry-statuses* :pass)) (getf after :pass))
244 (= (1- (getf *entry-statuses* :not-run)) (getf after :not-run))
245 (= (getf *entry-statuses* :fail) (getf after :fail))
246 (= (getf *entry-statuses* :unsupported) (getf after :unsupported)))))
247 (format t "~&CTM-INTEGER-ORIGINAL ~A: PASS; 356 byte-exact CXTM bytes~%" id)))
248 (defun run-tests ()
249 (unless topic-maps.ctm.tests::*cxtm-corpus*
250 (topic-maps.registered-original.tests:run-tests)
251 (topic-maps.registered-original.tests:run-negative-tests)
252 (topic-maps.prolog.tests:run-tests)
253 (topic-maps.registered-original.tests:run-empty2-tests))
254 (setf *positives* nil *negatives* nil *original* nil)
255 (let ((topic-maps.registered-original.tests::*checks* nil))
256 (positive-controls) (numeric-controls) (variant-controls) (wildcard-controls)
257 (template-controls) (token-controls) (negative-controls) (original-contract)
258 (check "ctm-integer:unique-check-names"
259 (= (length topic-maps.registered-original.tests::*checks*)
260 (length (remove-duplicates topic-maps.registered-original.tests::*checks* :test #'equal))))
261 (setf *checks* (nreverse topic-maps.registered-original.tests::*checks*)))
262 (setf *positives* (nreverse *positives*) *negatives* (nreverse *negatives*))
263 (format t "~&CTM-INTEGER-PASS: ~D checks; one original plus ~D positive and ~D negative controls~%"
264 (length *checks*) (length *positives*) (length *negatives*)) t)
265 (defun write-reports (prefix)
266 (with-open-file (out (concatenate 'string prefix ".literal.json") :direction :output :if-exists :supersede)
267 (topic-maps.test-foundation::emit-json-value
268 (fields "kind" "ctm-integer-literal-execution" "passed" (length *checks*) "failed" 0
269 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*)
270 "checks" (coerce *checks* 'vector) "positives" (coerce *positives* 'vector)
271 "negativeControls" (coerce *negatives* 'vector) "original" *original*) out) (terpri out))
272 (topic-maps.ctm.tests::write-cxtm-report
273 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
274 :additional-checks *checks*
275 :scope "Slice 20F: original CTM '*' literal, lexical value '*' and CTM-integer datatype. Exact original/qualified CXTM, Occurrence/Variant and existing Template literal paths, token diagnostics and unchanged Wildcard counters; previous contracts and four known FAILs preserved."))