]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/report.lisp
docs: design draft for a read-only SHOP3 adapter over TMDM objects
[topic-maps.git] / src / report.lisp
1 (in-package #:topic-maps.test-foundation)
2
3 ;;; A small JSON encoder for our report data, not a parser and not a general
4 ;;; serialization framework. It emits ASCII, so output encoding is portable.
5 (defstruct (json-object (:constructor json-object (members))) members)
6
7 (defun json-string (value stream)
8 (write-char #\" stream)
9 (loop for c across value for n = (char-code c) do
10 (case c
11 (#\" (write-string "\\\"" stream))
12 (#\\ (write-string "\\\\" stream))
13 (otherwise
14 (cond ((and (>= n 32) (< n 127)) (write-char c stream))
15 ((<= n #xffff) (format stream "\\u~4,'0X" n))
16 (t (let ((rest (- n #x10000)))
17 (format stream "\\u~4,'0X\\u~4,'0X"
18 (+ #xd800 (ash rest -10))
19 (+ #xdc00 (logand rest #x3ff)))))))))
20 (write-char #\" stream))
21
22 (defun json-array (items)
23 ;; NIL is JSON null; a zero-length vector represents an empty JSON array.
24 (coerce items 'vector))
25
26 (defun emit-json-value (value stream)
27 (typecase value
28 (json-object
29 (write-char #\{ stream)
30 (loop for (key . item) in (json-object-members value) for first = t then nil do
31 (unless first (write-char #\, stream))
32 (json-string key stream) (write-char #\: stream) (emit-json-value item stream))
33 (write-char #\} stream))
34 (string (json-string value stream))
35 (vector
36 (write-char #\[ stream)
37 (loop for item across value for first = t then nil do
38 (unless first (write-char #\, stream)) (emit-json-value item stream))
39 (write-char #\] stream))
40 (integer (format stream "~D" value))
41 (symbol (write-string (case value (:true "true") (:false "false")
42 ((nil) "null")
43 (otherwise (error "Unsupported JSON symbol ~S" value))) stream))
44 (t (error "Unsupported JSON value ~S" value))))
45
46 (defun word (keyword)
47 (when keyword (string-downcase (symbol-name keyword))))
48
49 (defun fields (&rest alternating)
50 (json-object (loop for (key value) on alternating by #'cddr collect (cons key value))))
51
52 (defun counts-object (plist)
53 (json-object (loop for (key value) on plist by #'cddr collect (cons (word key) value))))
54
55 (defun correspondence-object (value)
56 (fields "ok" (if (getf value :ok) :true :false)
57 "missingInputs" (json-array (getf value :missing-inputs))
58 "missingBaselines" (json-array (getf value :missing-baselines))
59 "unmatchedBaselines" (json-array (getf value :unmatched-baselines))
60 "duplicateIDs" (json-array (getf value :duplicate-ids))))
61
62 (defun report-data (corpus infrastructure-checks implementation)
63 (let ((summary (summarize-corpus corpus)) (root (corpus-root corpus)))
64 (fields
65 "schemaVersion" 1
66 "origin" (word (corpus-origin corpus))
67 "repository" (getf summary :repository) "commit" (getf summary :commit)
68 "qualification" (getf summary :qualification)
69 "implemented" (fields "ctmParsing" (if (getf implementation :ctm) :true :false)
70 "tmdmDeserialization" (if (getf implementation :tmdm) :true :false)
71 "cxtmCanonicalization" (if (getf implementation :cxtm) :true :false))
72 "implementationScope" (getf implementation :scope)
73 "counts" (counts-object (getf summary :counts))
74 "countsMatchPin" (if (getf summary :counts-match-pin) :true :false)
75 "statuses" (counts-object (getf summary :statuses))
76 "correspondence" (correspondence-object (getf summary :correspondence))
77 "infrastructureChecks" (json-array infrastructure-checks)
78 "cases"
79 (json-array
80 (loop for case in (corpus-cases corpus) collect
81 (fields "id" (test-case-id case) "category" (word (test-case-category case))
82 "sourcePathname" (relative-file-name (test-case-source-pathname case) root)
83 "documentIRI" (test-case-document-iri case)
84 "expectedOutcome" (word (test-case-expected-outcome case))
85 "expectedCXTM" (when (test-case-expected-baseline case)
86 (relative-file-name (test-case-expected-baseline case) root))
87 "actualOutcome" (word (test-case-actual-outcome case))
88 "status" (word (test-case-status case))
89 "diagnostics" (json-array (test-case-diagnostics case))
90 "specificationClauses" (json-array (test-case-clauses case)))))
91 "baselines" (json-array (mapcar (lambda (p) (relative-file-name p root))
92 (corpus-baselines corpus)))
93 "auxiliaryResources"
94 (json-array
95 (loop for auxiliary in (corpus-auxiliaries corpus) collect
96 (fields "id" (getf auxiliary :id) "role" (word (getf auxiliary :role))
97 "sourcePathname" (relative-file-name (getf auxiliary :source-pathname) root)
98 "documentIRI" (getf auxiliary :document-iri)))))))
99
100 (defun report-string (corpus &key infrastructure-checks implementation)
101 (with-output-to-string (stream)
102 (emit-json-value (report-data corpus infrastructure-checks implementation) stream)
103 (terpri stream)))
104
105 (defun write-validation-report (corpus pathname &key infrastructure-checks implementation)
106 (with-open-file (stream pathname :direction :output :if-exists :supersede
107 :if-does-not-exist :create)
108 (write-string (report-string corpus :infrastructure-checks infrastructure-checks :implementation implementation) stream))
109 pathname)