]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-prolog.lisp
feat(xtm): support resource references on name variants
[topic-maps.git] / tests / ctm-prolog.lisp
1 (defpackage #:topic-maps.prolog.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.registered-original.tests
4 #:check #:binary #:fields #:case-state #:identity-fields #:captured-read #:condition-data)
5 (:export #:run-tests #:write-reports))
6 (in-package #:topic-maps.prolog.tests)
7 (defparameter *doc* "https://example.invalid/prolog/main.ctm")
8 (defparameter *child* "https://example.invalid/prolog/child.ctm")
9 (defvar *checks* nil)
10 (defvar *positives* nil)
11 (defvar *negatives* nil)
12 (defvar *originals* nil)
13 (defvar *entry-statuses* nil)
14 (defun bytes (text &optional bom)
15 (concatenate '(vector (unsigned-byte 8)) (if bom #(239 187 191) #())
16 (sb-ext:string-to-octets text :external-format :utf-8)))
17 (defun read-text (text &optional (source "memory:prolog"))
18 (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name source))
19 (defun canonical (map)
20 (topic-maps.cxtm:canonicalize-octets map :base-locator *doc*))
21 (defun file-result (label input &optional encoding)
22 (uiop:with-temporary-file (:pathname path :type "ctm")
23 (with-open-file (out path :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
24 (write-sequence input out))
25 (multiple-value-bind (map condition)
26 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri *doc* :source-name label :encoding encoding)))
27 (check (concatenate 'string label ":input-bytes-preserved") (equalp input (binary path)))
28 (values map condition))))
29 (defun positive (label map condition body &optional input)
30 (check (concatenate 'string label ":actual-model-and-CXTM")
31 (and (null condition) map (topic-maps.reference.tests::references-current-p map)
32 (equalp (canonical map) (canonical (read-text body)))))
33 (push (fields "label" label "accepted" :true "inputText" input
34 "actualCXTM" (topic-maps.ctm::decode-utf8 (canonical map))
35 "model" (topic-maps.template.tests::observe map)) *positives*))
36 (defun specific (label condition class code position unit &optional (source label) purpose)
37 (check (concatenate 'string label ":specific-condition-code-position-source")
38 (and condition (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition))
39 (= position (topic-maps.ctm:error-position condition))
40 (eq unit (topic-maps.ctm:error-position-unit condition))
41 (equal source (topic-maps.ctm:error-source condition)) (null (topic-maps.ctm:error-cause condition))
42 (if purpose
43 (eq purpose (getf (first (topic-maps.ctm:error-context condition)) :purpose))
44 (null (topic-maps.ctm:error-context condition)))))
45 (check (concatenate 'string label ":position-units")
46 (if (eq unit :byte)
47 (and (null (topic-maps.ctm:error-line condition)) (null (topic-maps.ctm:error-column condition)))
48 (and (integerp (topic-maps.ctm:error-line condition)) (integerp (topic-maps.ctm:error-column condition)))))
49 (push (fields "label" label "condition" (condition-data condition)) *negatives*) condition)
50 (defun negative-text (label text code position &optional (class 'topic-maps.ctm:ctm-rejection) purpose)
51 (multiple-value-bind (map condition) (captured-read (lambda () (read-text text label)))
52 (declare (ignore map)) (specific label condition class code position :character label purpose)))
53 (defun negative-file (label input code position &optional encoding (class 'topic-maps.ctm:ctm-rejection))
54 (multiple-value-bind (map condition) (file-result label input encoding)
55 (declare (ignore map)) (specific label condition class code position :byte)))
56
57 (defun positive-prologs ()
58 (dolist (spec (list (list "version-empty" "%version 1.0" "")
59 (list "version-compact" "%version1.0 topic." "topic.")
60 (list "version-body-adjacent" "%version1.0topic." "topic.")
61 (list "version-comments" (format nil "# preface~%%version # version~%1.0~%topic.") "topic.")
62 (list "encoding-version" (format nil "%encoding~C\"utf-8\"~%%version 1.0~%topic." #\Tab) "topic.")
63 (list "version-prefix" "%version1.0 %prefixex<urn:ex:> ex:topic." "<urn:ex:topic>.")
64 (list "version-map-reifier" "%version1.0 ~r topic." "~r topic.")))
65 (destructuring-bind (label text body) spec
66 (multiple-value-bind (map condition) (captured-read (lambda () (read-text text label)))
67 (positive label map condition body text))))
68 (let ((body "topic - \"Grüße🙂\"."))
69 (dolist (encoding '(nil "UTF-8" "utf-8"))
70 (dolist (bom '(nil t))
71 (dolist (declaration '(nil "UTF-8" "utf-8"))
72 (let* ((label (format nil "utf8:provided=~A:bom=~A:decl=~A" encoding bom declaration))
73 (text (concatenate 'string (if declaration (format nil "%encoding \"~A\"~%" declaration) "")
74 (format nil "%version 1.0~%~A" body))))
75 (multiple-value-bind (map condition) (file-result label (bytes text bom) encoding)
76 (positive label map condition body text))))))))
77
78 (defun prolog-rejections ()
79 (dolist (version '("10.0" "2.0" "1" "1.00" "01.0" "+1.0" "1.0.0" "\"1.0\"" ""))
80 (negative-text (concatenate 'string "version-spelling:" version) (concatenate 'string "%version " version)
81 :invalid-version 9))
82 (dolist (spec '(("version-after-body" "topic. %version 1.0" :misplaced-version 7)
83 ("version-after-prefix" "%prefix ex <urn:ex:> %version 1.0" :misplaced-version 21)
84 ("version-after-reifier" "~r %version 1.0" :misplaced-version 3)
85 ("version-repeated" "%version 1.0 %version 1.0" :misplaced-version 13)
86 ("encoding-after-version" "%version 1.0 %encoding \"UTF-8\"" :misplaced-encoding 13)
87 ("encoding-leading-space" " %encoding \"UTF-8\"" :misplaced-encoding 1)
88 ("encoding-after-body" "topic. %encoding \"UTF-8\"" :misplaced-encoding 7)
89 ("encoding-repeated" "%encoding \"UTF-8\" %encoding \"UTF-8\"" :misplaced-encoding 18)
90 ("unknown-original-type" "%ctm 1.0" :unknown-directive 0)
91 ("unknown-name" "%bogus" :unknown-directive 0)
92 ("unknown-case" "%VERSION 1.0" :unknown-directive 0)
93 ("known-prefix-malformed" "%prefix" :expected-prefix-name 7)
94 ("encoding-missing-quotes" "%encoding UTF-8" :invalid-encoding-declaration 10)
95 ("encoding-empty-name" "%encoding \"\"" :invalid-encoding-declaration 11)
96 ("encoding-unclosed-name" "%encoding \"UTF-8" :invalid-encoding-declaration 16)))
97 (destructuring-bind (label text code position) spec (negative-text label text code position)))
98 (negative-text "known-resource-policy" "%include <child.ctm>" :resource-resolver-required 0 'topic-maps.ctm:ctm-unsupported :include)
99 (negative-text "known-string-unclosed" "topic occ: \"\"\"x" :unterminated-string 11)
100 (negative-text "known-charset-unsupported-text" "%encoding \"iso-8859-1\"" :unsupported-encoding 11 'topic-maps.ctm:ctm-unsupported)
101 (negative-text "encoding-newline-before-name" (format nil "%encoding~%\"UTF-8\"") :invalid-encoding-declaration 9))
102
103 (defun byte-order-tests ()
104 (negative-file "supplied-vs-BOM" (bytes "topic." t) :encoding-conflict 0 "iso-8859-1")
105 (negative-file "supplied-vs-declaration" (bytes "%encoding \"iso-8859-1\" topic.") :encoding-conflict 11 "UTF-8")
106 (negative-file "supplied-unsupported-vs-UTF8-declaration" (bytes "%encoding \"UTF-8\" topic.") :encoding-conflict 11 "iso-8859-1")
107 (negative-file "matching-unsupported-charset" (bytes "%encoding \"iso-8859-1\"") :unsupported-encoding 11 "ISO-8859-1" 'topic-maps.ctm:ctm-unsupported)
108 (negative-file "supplied-unsupported-charset" (bytes "topic.") :unsupported-encoding 0 "iso-8859-1" 'topic-maps.ctm:ctm-unsupported)
109 (negative-file "invalid-UTF8-retained" #(255) :invalid-utf8 0)
110 (negative-file "invalid-UTF8-after-BOM-retained" #(239 187 191 255) :invalid-utf8 3)
111 (negative-file "truncated-BOM-retained" #(239 187) :invalid-utf8 0)
112 (negative-file "malformed-byte-encoding" (bytes "%encoding UTF-8") :invalid-encoding-declaration 10)
113 (let ((saved (symbol-function 'topic-maps.ctm::decode-utf8)) (calls 0))
114 (unwind-protect
115 (progn
116 (setf (symbol-function 'topic-maps.ctm::decode-utf8)
117 (lambda (&rest args) (incf calls) (apply saved args)))
118 (negative-file "conflict-before-invalid-body-decoding"
119 (concatenate '(vector (unsigned-byte 8)) (bytes "%encoding \"iso-8859-1\"" t) #(255)) :encoding-conflict 14)
120 (negative-file "unsupported-before-invalid-body-decoding"
121 (concatenate '(vector (unsigned-byte 8)) (bytes "%encoding \"iso-8859-1\"") #(255))
122 :unsupported-encoding 11 nil 'topic-maps.ctm:ctm-unsupported)
123 (check "encoding-selection-precedes-decoder:zero-calls" (zerop calls)))
124 (setf (symbol-function 'topic-maps.ctm::decode-utf8) saved))))
125
126 (defun resource-prolog-tests ()
127 (dolist (purpose '(:include :mergemap))
128 (let* ((parent (if (eq purpose :include) "%include <child.ctm>"
129 "%mergemap <child.ctm> <http://psi.topicmaps.org/iso13250/ctm>"))
130 (good (bytes (format nil "%encoding \"UTF-8\"~%%version 1.0~%topic - \"Grüße🙂\".") t))
131 (input (copy-seq good)) (calls 0)
132 (resolver (lambda (iri &key purpose from-document-iri notation)
133 (assert (equal iri *child*)) (assert (equal from-document-iri *doc*))
134 (assert (if (eq purpose :include) (null notation)
135 (equal notation "http://psi.topicmaps.org/iso13250/ctm")))
136 (incf calls) (values good "source:child"))))
137 (let* ((map (topic-maps.ctm:deserialize-string parent :document-iri *doc* :resource-resolver resolver))
138 (owner (topic-maps.registered-original.tests::local map *child* "topic")))
139 (check (format nil "resource:~A:UTF8-BOM-prolog-model-and-immutable-bytes" purpose)
140 (and (= calls 1) (equalp input good) owner (= 1 (length (topic-names owner)))
141 (equal "Grüße🙂" (topic-name-value (first (topic-names owner))))
142 (topic-maps.reference.tests::references-current-p map)))
143 (push (fields "label" (format nil "resource:~A:UTF8-BOM-prolog" purpose) "accepted" :true
144 "model" (topic-maps.template.tests::observe map)) *positives*))
145 (dolist (spec (list (list "conflict" (bytes "%encoding \"iso-8859-1\"" t) :encoding-conflict 14 :byte 'topic-maps.ctm:ctm-rejection)
146 (list "version" (bytes "%version 10.0") :invalid-version 9 :character 'topic-maps.ctm:ctm-rejection)
147 (list "unknown" (bytes "%ctm 1.0") :unknown-directive 0 :character 'topic-maps.ctm:ctm-rejection)
148 (list "unsupported" (bytes "%encoding \"iso-8859-1\"") :unsupported-encoding 11 :byte 'topic-maps.ctm:ctm-unsupported)))
149 (destructuring-bind (name input code position unit class) spec
150 (let* ((before (copy-seq input)) (label (format nil "resource:~A:~A" purpose name))
151 (condition (nth-value 1 (captured-read
152 (lambda () (topic-maps.ctm:deserialize-string parent :document-iri *doc*
153 :resource-resolver (lambda (&rest args) (declare (ignore args)) (values input "source:child"))))))))
154 (specific label condition class code position unit "source:child" purpose)
155 (check (concatenate 'string label ":unchanged-resource-bytes") (equalp before input))))))))
156
157 (defun original-contracts ()
158 (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*) (cases (topic-maps.test-foundation:corpus-cases corpus))
159 (previous (mapcar (lambda (case) (cons case (case-state case))) cases))
160 (old-cxtm (copy-tree topic-maps.ctm.tests::*cxtm-results*))
161 (ids '("ctm/invalid/BOM-contradicts-encoding.ctm" "ctm/invalid/unknown-directive.ctm" "ctm/invalid/wrong-version.ctm")))
162 (setf *entry-statuses* (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
163 (dolist (spec '(("BOM-contradicts-encoding" :encoding-conflict 14 :byte ("CTM 3.2" "CTM 3.5" "CTM 3.6"))
164 ("unknown-directive" :unknown-directive 0 :character ("CTM 3.5" "CTM 3.16"))
165 ("wrong-version" :invalid-version 9 :character ("CTM 3.5" "CTM 3.7"))))
166 (destructuring-bind (name code position unit clauses) spec
167 (let* ((id (format nil "ctm/invalid/~A.ctm" name))
168 (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected))
169 (path (topic-maps.test-foundation:test-case-source-pathname case))
170 (doc (topic-maps.test-foundation:test-case-document-iri case)) (input (binary path))
171 (identity (copy-tree (identity-fields case)))
172 (text (topic-maps.ctm::decode-utf8 input :strip-bom t))
173 (control (cond ((equal name "BOM-contradicts-encoding")
174 (bytes (topic-maps.registered-original.tests::replace-fragment text "iso-8859-1" "UTF-8") t))
175 ((equal name "unknown-directive") (bytes "%version 1.0"))
176 (t (bytes "%version 1.0")))))
177 (check (concatenate 'string id ":existing-negative-contract-identity")
178 (and (= 1 (length selected)) (eq :invalid (topic-maps.test-foundation:test-case-category case))
179 (eq :ctm-rejection (topic-maps.test-foundation:test-case-expected-outcome case))
180 (null (topic-maps.test-foundation:test-case-expected-baseline case))
181 (eq :not-run (topic-maps.test-foundation:test-case-status case))
182 (null (topic-maps.test-foundation:test-case-actual-outcome case))
183 (equal doc (format nil "https://ctm-tests.example.invalid/~A/~A"
184 (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit) id))))
185 (multiple-value-bind (map condition)
186 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
187 (multiple-value-bind (repeat-map repeated)
188 (captured-read (lambda () (topic-maps.ctm:deserialize-file path :document-iri doc :source-name id)))
189 (multiple-value-bind (control-map control-condition) (file-result (concatenate 'string id ":control") control)
190 (let ((qualified (and (null map) (null repeat-map)
191 (eq 'topic-maps.ctm:ctm-rejection (type-of condition))
192 (eq code (topic-maps.ctm:error-code condition))
193 (= position (topic-maps.ctm:error-position condition))
194 (eq unit (topic-maps.ctm:error-position-unit condition))
195 (equal id (topic-maps.ctm:error-source condition))
196 (null (topic-maps.ctm:error-cause condition)) (null (topic-maps.ctm:error-context condition))
197 (equal (topic-maps.registered-original.tests::condition-signature condition)
198 (topic-maps.registered-original.tests::condition-signature repeated))
199 (null control-condition) control-map
200 (if (equal name "BOM-contradicts-encoding")
201 (topic-maps.registered-original.tests::plain-locals-p control-map *doc* '("topic"))
202 (null (topic-map-topics control-map))))))
203 (check (concatenate 'string id ":immutable-source-and-identity")
204 (and (equalp input (binary path)) (equal identity (identity-fields case))))
205 (when qualified
206 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition))))
207 (push (fields "id" id "documentIRI" doc "sourceTextAfterBOM" text
208 "sourceHex" (format nil "~{~2,'0X~^ ~}" (coerce input 'list))
209 "clauses" (coerce clauses 'vector) "condition" (condition-data condition)
210 "repeatCondition" (condition-data repeated) "qualified" (if qualified :true :false)
211 "status" (topic-maps.test-foundation::word (topic-maps.test-foundation:test-case-status case))
212 "control" (fields "accepted" (if control-map :true :false) "condition" (condition-data control-condition)
213 "model" (when control-map (topic-maps.template.tests::observe control-map)))) *originals*)
214 (format t "~&PROLOG-ORIGINAL ~A: ~A; ~A~%" id
215 (topic-maps.test-foundation:test-case-status case) condition))))))))
216 (check "prolog:only-three-intended-original-records-change"
217 (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*) (equal old-cxtm topic-maps.ctm.tests::*cxtm-results*)
218 (every (lambda (entry)
219 (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal)
220 (equal (first (cdr entry)) (identity-fields (car entry)))
221 (equal (cdr entry) (case-state (car entry))))) previous)))
222 (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)))
223 (check "prolog:three-new-PASS-four-known-FAILs-retained"
224 (and (= (+ 3 (getf *entry-statuses* :pass)) (getf after :pass))
225 (= (- (getf *entry-statuses* :not-run) 3) (getf after :not-run))
226 (= (getf *entry-statuses* :fail) (getf after :fail))
227 (= (getf *entry-statuses* :unsupported) (getf after :unsupported)))))))
228
229 (defun run-tests ()
230 (unless topic-maps.ctm.tests::*cxtm-corpus*
231 (topic-maps.registered-original.tests:run-tests)
232 (topic-maps.registered-original.tests:run-negative-tests))
233 (setf *positives* nil *negatives* nil *originals* nil)
234 (let ((topic-maps.registered-original.tests::*checks* nil))
235 (positive-prologs) (prolog-rejections) (byte-order-tests) (resource-prolog-tests) (original-contracts)
236 (check "prolog:unique-check-names"
237 (= (length topic-maps.registered-original.tests::*checks*)
238 (length (remove-duplicates topic-maps.registered-original.tests::*checks* :test #'equal))))
239 (setf *checks* (nreverse topic-maps.registered-original.tests::*checks*)))
240 (setf *positives* (nreverse *positives*) *negatives* (nreverse *negatives*) *originals* (nreverse *originals*))
241 (format t "~&PROLOG-PASS: ~D checks; three intended original rejections~%" (length *checks*)) t)
242 (defun write-reports (prefix)
243 (with-open-file (out (concatenate 'string prefix ".prolog.json") :direction :output :if-exists :supersede)
244 (topic-maps.test-foundation::emit-json-value
245 (fields "kind" "ctm-prolog-execution" "passed" (length *checks*) "failed" 0
246 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*)
247 "checks" (coerce *checks* 'vector) "positives" (coerce *positives* 'vector)
248 "negativeControls" (coerce *negatives* 'vector) "originals" (coerce *originals* 'vector)) out) (terpri out))
249 (topic-maps.ctm.tests::write-cxtm-report
250 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
251 :additional-checks *checks* :scope "Slice 20C: three intended negative originals; minimal UTF-8 encoding conflict preflight and CTM 1.0 prolog. All earlier contract identities and four known FAILs retained."))