(defpackage #:topic-maps.triple.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.registered-original.tests #:check #:fields #:binary #:case-state #:identity-fields #:captured-read #:condition-data) (:export #:run-tests #:write-reports)) (in-package #:topic-maps.triple.tests) (defparameter *doc* "https://example.invalid/triple/main.ctm") (defvar *checks* nil) (defvar *positives* nil) (defvar *negatives* nil) (defvar *original-rows* nil) (defvar *entry-statuses* nil) (defun canonical (map) (topic-maps.cxtm:canonicalize-octets map :base-locator *doc*)) (defun quoted (value) (with-output-to-string (out) (write-char #\" out) (loop for c across value do (case c (#\" (write-string "\\\"" out)) (#\\ (write-string "\\\\" out)) (#\Newline (write-string "\\n" out)) (#\Return (write-string "\\r" out)) (#\Tab (write-string "\\t" out)) (otherwise (write-char c out)))) (write-char #\" out))) (defun source-for (context literal) (ecase context (:occurrence (format nil "topic occtype: ~A." literal)) (:name (format nil "topic - ~A." literal)) (:variant (format nil "topic - \"Name\" (~A @s)." literal)))) (defun value-in (context map) (let ((owner (topic-maps.registered-original.tests::local map *doc* "topic"))) (ecase context (:occurrence (let ((items (topic-occurrences owner))) (and (= 1 (length items)) (eq owner (occurrence-parent (first items))) (equal (string-datatype) (occurrence-datatype (first items))) (occurrence-value (first items))))) (:name (let ((names (topic-names owner))) (and (= 1 (length names)) (eq owner (topic-name-parent (first names))) (topic-name-value (first names))))) (:variant (let* ((name (first (topic-names owner))) (items (topic-name-variants name)) (v (first items))) (and (= 1 (length items)) (eq name (variant-parent v)) (equal (string-datatype) (variant-datatype v)) (equal (list (topic-maps.registered-original.tests::local map *doc* "s")) (variant-scope v)) (variant-value v))))))) (defun accepted (label text) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label :wildcard-counter 37) (let* ((held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held)) (actual (canonical map))) (check (concatenate 'string label ":model-counter-and-pure-canonicalization") (and (= 37 counter) (topic-maps.reference.tests::references-current-p map) (equal before (topic-maps.import.tests::state map held)) (equalp actual (canonical map)))) (push (fields "label" label "sourceText" text "initialCounter" 37 "finalCounter" counter "actualCXTM" (topic-maps.ctm::decode-utf8 actual) "model" (topic-maps.template.tests::observe map)) *positives*)) map)) (defparameter *positive-specs* (list (list "empty" "" "") (list "plain" "alpha" "alpha") (list "single-quote" "one\"quote" "one\"quote") (list "double-quote" "two\"\"quotes" "two\"\"quotes") (list "leading-quote" "\"first" "\"first") (list "leading-two-quotes" "\"\"first" "\"\"first") (list "escaped-triple" "\\\"\\\"\\\"" "\"\"\"") (list "trailing-escaped-quote" "end\\\"" "end\"") (list "trailing-backslash" "end\\\\" "end\\") (list "raw-then-escaped-quote" "\"\\\"" "\"\"") (list "two-raw-then-escaped-quote" "\"\"\\\"" "\"\"\"") (list "LF" "line1 line2" "line1 line2") (list "CR" (concatenate 'string "line1" (string #\Return) "line2") (concatenate 'string "line1" (string #\Return) "line2")) (list "CRLF" (concatenate 'string "line1" (string #\Return) " line2") (concatenate 'string "line1" (string #\Return) " line2")) (list "leading-trailing-LF" (format nil "~% line ~%") (format nil "~% line ~%")) (list "whitespace" " x " " x ") (list "syntax-as-data" "# comment #(not)# %version $var *" "# comment #(not)# %version $var *") (list "utf8" "Grüße🙂" "Grüße🙂") (list "all-escapes" "\\n\\r\\t\\\\\\\"" (concatenate 'string " " (string #\Return) " \\\"")) (list "unicode-u4" "\\u005a\\u005A" "ZZ") (list "unicode-U6" "\\U00005a\\U00005A" "ZZ") (list "unicode-mixed" "\\u00e9\\U01F642" "é🙂") (list "unicode-quote-backslash" "\\u0022\\u005c" "\"\\") )) (defun positive-controls () (dolist (spec *positive-specs*) (destructuring-bind (label body value) spec ;; Exercise the common String token in all existing value contexts. (dolist (context '(:occurrence :name :variant)) (let* ((key (format nil "~A:~(~A~)" label context)) (literal (concatenate 'string "\"\"\"" body "\"\"\"")) (map (accepted key (source-for context literal))) (plain (topic-maps.ctm:deserialize-string (source-for context (quoted value)) :document-iri *doc*))) (check (concatenate 'string key ":exact-value-and-ordinary-string-equivalence") (and (equal value (value-in context map)) (equalp (canonical map) (canonical plain))))))))) (defun token-controls () (loop for n from 3 to 8 do (let* ((source (concatenate 'string "\"\"\"x" (make-string n :initial-element #\") ";tail")) (topic-maps.ctm::*source-text* source) (lexer (topic-maps.ctm::make-lexer source)) (token (topic-maps.ctm::next-token lexer))) (check (format nil "tokens:closing-run-~D-consumes-exactly-three-quotes" n) (and (eq :string (topic-maps.ctm::token-kind token)) (equal "x" (topic-maps.ctm::token-value token)) (= 0 (topic-maps.ctm::token-position token)) (= 7 (topic-maps.ctm::lexer-cursor lexer)) (equal (subseq source 7) (concatenate 'string (make-string (- n 3) :initial-element #\") ";tail")))))) (dolist (text '("\"\"\"\"\"\"" "\"\"" "\"\"\"a\"\"\";next" "\"\"\"a\"\"\"^^")) (multiple-value-bind (tokens failure) (topic-maps.registered-original.tests::lexical-evidence text "tokens") (check (concatenate 'string "tokens:first-string:" text) (and (null failure) (eq :string (caar tokens)) (= 0 (third (first tokens)))))))) (defun qualified-and-template-controls () (dolist (spec '(("\"\"\"001\"\"\"^^" "001" "http://www.w3.org/2001/XMLSchema#integer") ("\"\"\"*\"\"\"" "*" "http://www.w3.org/2001/XMLSchema#string"))) (destructuring-bind (literal value datatype) spec (let* ((map (accepted (concatenate 'string "qualified:" literal) (source-for :occurrence literal))) (o (first (topic-occurrences (topic-maps.registered-original.tests::local map *doc* "topic"))))) (check (concatenate 'string "qualified:" literal ":existing-datatype-path") (and (equal value (occurrence-value o)) (equal datatype (occurrence-datatype o))))))) (dolist (text '("def f($s) topic - $s. end f(\"\"\"Name\"\"\")" "def f() topic - \"\"\"Name\"\"\". end f()")) (let ((map (accepted (concatenate 'string "template:" text) text))) (check (concatenate 'string "template:" text ":existing-String-token-path") (equal "Name" (value-in :name map)))))) (defparameter *negative-specs* '( ("quoted-unclosed" "topic - \"x" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("quoted-invalid-escape" "topic - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 10 1 11) ("quoted-invalid-u4" "topic - \"\\u00x0\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 13 1 14) ("quoted-short-U6" "topic - \"\\U0000\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16) ("quoted-surrogate" "topic - \"\\uD800\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9) ("quoted-out-of-range" "topic - \"\\U110000\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9) ("quoted-error-LF" "topic. other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 17 2 11) ("quoted-error-CR" "topic. other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 17 2 11) ("quoted-error-CRLF" "topic. other - \"\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 18 2 11) ("quoted-unicode-prefix" "topic - \"Ä\\q\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 11 1 12) ("quoted-adjacent-token" "topic occtype: \"a\"x." topic-maps.ctm:ctm-rejection :expected-separator 18 1 19) ("quoted-variant-missing-scope" "topic - \"N\" (\"V\")." topic-maps.ctm:ctm-rejection :expected-variant-scope 16 1 17) ("triple-opening-only" "topic - \"\"\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("triple-unclosed" "topic - \"\"\"body" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("triple-final-one-quote" "topic - \"\"\"body\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("triple-final-two-quotes" "topic - \"\"\"body\"\"" topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("triple-dangling-escape" "topic - \"\"\"body\\" topic-maps.ctm:ctm-rejection :invalid-string-escape 16 1 17) ("triple-unknown-escape" "topic - \"\"\"\\q\"\"\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 12 1 13) ("triple-short-u4" "topic - \"\"\"\\u00\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16) ("triple-short-U6" "topic - \"\"\"\\U0000\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 17 1 18) ("triple-bad-hex" "topic - \"\"\"\\u00G0\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 15 1 16) ("triple-surrogate" "topic - \"\"\"\\uD800\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9) ("triple-out-of-range" "topic - \"\"\"\\U110000\"\"\"." topic-maps.ctm:ctm-rejection :invalid-unicode 8 1 9) ("triple-unclosed-after-CRLF" "topic. other - \"\"\"bad" topic-maps.ctm:ctm-rejection :unterminated-string 16 2 9) ("triple-unicode-LF-error" "topic - \"\"\"Ä \\q\"\"\"." topic-maps.ctm:ctm-rejection :invalid-string-escape 14 2 2) ("triple-surplus-closing-4" "topic occtype: \"\"\"x\"\"\"\";." topic-maps.ctm:ctm-rejection :unterminated-string 22 1 23) ("triple-surplus-closing-5" "topic occtype: \"\"\"x\"\"\"\"\";." topic-maps.ctm:ctm-rejection :unexpected-token 22 1 23) ("triple-surplus-closing-6" "topic occtype: \"\"\"x\"\"\"\"\"\";." topic-maps.ctm:ctm-rejection :unterminated-string 22 1 23) ("triple-adjacent-name" "topic occtype: \"\"\"x\"\"\"extra." topic-maps.ctm:ctm-rejection :expected-separator 22 1 23) ("triple-adjacent-string" "topic occtype: \"\"\"x\"\"\"\"extra\"." topic-maps.ctm:ctm-rejection :unexpected-token 22 1 23) ("triple-variant-missing-scope" "topic - \"N\" (\"\"\"V\"\"\")." topic-maps.ctm:ctm-rejection :expected-variant-scope 20 1 21) )) ;; Construct CR explicitly; source-file line-ending normalization must not ;; change these input streams. Build fresh rows rather than mutate constants. (setf *negative-specs* (mapcar (lambda (spec) (let ((text (cond ((equal "quoted-error-CR" (first spec)) (format nil "topic.~Cother - \"\\q\"." #\Return)) ((equal "quoted-error-CRLF" (first spec)) (format nil "topic.~C~Cother - \"\\q\"." #\Return #\Newline)) ((equal "triple-unclosed-after-CRLF" (first spec)) (format nil "topic.~C~Cother - \"\"\"bad" #\Return #\Newline))))) (if text (list* (first spec) text (cddr spec)) spec))) *negative-specs*)) (defun negative-controls () (dolist (spec *negative-specs*) (destructuring-bind (label text class code position line column) spec (multiple-value-bind (map condition) (captured-read (lambda () (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label))) (check (concatenate 'string "negative:" label ":class-code-position-source") (and (null map) condition (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition)) (= position (topic-maps.ctm:error-position condition)) (= line (topic-maps.ctm:error-line condition)) (= column (topic-maps.ctm:error-column condition)) (eq :character (topic-maps.ctm:error-position-unit condition)) (equal label (topic-maps.ctm:error-source condition)) (null (topic-maps.ctm:error-cause condition)) (null (topic-maps.ctm:error-context condition)))) (push (fields "label" label "sourceText" text "condition" (condition-data condition)) *negatives*))))) (defparameter *originals* (list (list "occurrence-string-triple" :occurrences "Occurrence \" Value" 41 367) (list "occurrence-string-triple2" :occurrences "Occurrence\"\" \" Value\"" 48 370) (list "unicode-escape-u4-triple-string" :escapes (format nil "ZZ ~%Top") 33 331) (list "unicode-escape-u6-triple-string" :escapes (format nil "ZZ ~%Top") 37 331))) (defun original-contracts () (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*) (root (topic-maps.test-foundation:corpus-root corpus)) (cases (topic-maps.test-foundation:corpus-cases corpus)) (previous (mapcar (lambda (c) (cons c (case-state c))) cases)) (ids (mapcar (lambda (spec) (format nil "ctm/in/~A.ctm" (first spec))) *originals*)) (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*)) (new-results nil)) (setf *entry-statuses* (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))) (dolist (spec *originals*) (destructuring-bind (name group value source-size expected-size) spec (let* ((id (format nil "ctm/in/~A.ctm" name)) (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected)) (*doc* (topic-maps.test-foundation:test-case-document-iri case))) (check (concatenate 'string id ":existing-positive-NOT-RUN-record-and-identity") (and (= 1 (length selected)) (member case cases :test #'eq) (eq :valid (topic-maps.test-foundation:test-case-category case)) (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case)) (eq :not-run (topic-maps.test-foundation:test-case-status case)) (null (topic-maps.test-foundation:test-case-actual-outcome case)) (equal *doc* (format nil "https://ctm-tests.example.invalid/~A/~A" (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit) id)) (equal (truename (merge-pathnames id root)) (truename (topic-maps.test-foundation:test-case-source-pathname case))) (= source-size (length (binary (topic-maps.test-foundation:test-case-source-pathname case)))))) (multiple-value-bind (row result) (topic-maps.registered-original.tests::exercise-case case group) (push row *original-rows*) (check (concatenate 'string id ":actual-original-byte-comparison-PASS") (and result (getf result :match) (= expected-size (getf result :bytes)) (eq :pass (topic-maps.test-foundation:test-case-status case)))) (let* ((context (if (eq group :occurrences) :occurrence :name)) (plain (source-for context (quoted value)))) ;; The Unicode originals use zztop as owner; retain that identity. (when (eq context :name) (setf plain (concatenate 'string "zztop" (subseq plain 5)))) (let* ((map (accepted (concatenate 'string id ":ordinary-control") plain)) (actual (canonical map))) (check (concatenate 'string id ":ordinary-control-same-original-CXTM") (and (= expected-size (length actual)) (equalp actual (binary (topic-maps.test-foundation:test-case-expected-baseline case))) (equal (getf result :actual) (topic-maps.ctm::decode-utf8 actual)))))) (push result new-results)) (format t "~&TRIPLE-ORIGINAL ~A: PASS; ~D byte-exact CXTM bytes~%" id expected-size)))) (check "originals:all-prior-case-records-and-observations-preserved" (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*) (equal cases (topic-maps.test-foundation:corpus-cases corpus)) (equal old-results topic-maps.ctm.tests::*cxtm-results*) (every (lambda (entry) (if (member (topic-maps.test-foundation:test-case-id (car entry)) ids :test #'equal) (equal (first (cdr entry)) (identity-fields (car entry))) (equal (cdr entry) (case-state (car entry))))) previous))) (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))) (check "originals:exactly-four-NOT-RUN-to-PASS-no-other-status-changes" (and (= (+ 4 (getf *entry-statuses* :pass)) (getf after :pass)) (= (- (getf *entry-statuses* :not-run) 4) (getf after :not-run)) (= (getf *entry-statuses* :fail) (getf after :fail)) (= (getf *entry-statuses* :unsupported) (getf after :unsupported))))) (setf topic-maps.ctm.tests::*cxtm-results* (append old-results (nreverse new-results))))) (defun run-tests () (unless topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.registered-original.tests:run-tests) (topic-maps.registered-original.tests:run-negative-tests) (topic-maps.prolog.tests:run-tests) (topic-maps.registered-original.tests:run-empty2-tests) (topic-maps.ctm-integer.tests:run-tests)) (setf *positives* nil *negatives* nil *original-rows* nil) (let ((topic-maps.registered-original.tests::*checks* nil)) (positive-controls) (token-controls) (qualified-and-template-controls) (negative-controls) (original-contracts) (check "triple:unique-check-names" (= (length topic-maps.registered-original.tests::*checks*) (length (remove-duplicates topic-maps.registered-original.tests::*checks* :test #'equal)))) (setf *checks* (nreverse topic-maps.registered-original.tests::*checks*))) (setf *positives* (nreverse *positives*) *negatives* (nreverse *negatives*) *original-rows* (nreverse *original-rows*)) (format t "~&TRIPLE-PASS: ~D checks; four originals plus ~D positive and ~D negative controls~%" (length *checks*) (length *positives*) (length *negatives*)) t) (defun write-reports (prefix) (with-open-file (out (concatenate 'string prefix ".triple.json") :direction :output :if-exists :supersede) (topic-maps.test-foundation::emit-json-value (fields "kind" "ctm-triple-string-execution" "passed" (length *checks*) "failed" 0 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*) "checks" (coerce *checks* 'vector) "positives" (coerce *positives* 'vector) "negativeControls" (coerce *negatives* 'vector) "originals" (coerce *original-rows* 'vector)) out) (terpri out)) (topic-maps.ctm.tests::write-cxtm-report (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json") :additional-checks *checks* :scope "Slice 20G: four original Triple String contracts using the existing original harness; shared escapes/Unicode, exact original and ordinary-string CXTM, unchanged model/handles and counters. Prior contracts and four known FAILs preserved."))