documentation update
[cxml-rng.git] / test.lisp
blob868b87a30f55ac4627e4191500bb350901430d1e
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
2 ;;;
3 ;;; Copyright (c) 2007 David Lichteblau. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :cxml-rng)
32 (defun run-tests (&optional (p "/home/david/src/lisp/cxml-rng/spec-split/*")
33 (output-file "/home/david/src/lisp/cxml-rng/TEST"))
34 (dribble output-file :if-exists :rename-and-delete)
35 (let ((pass 0)
36 (total 0)
37 (*package* (find-package :cxml-rng))
38 (*print-level* 3))
39 (dolist (d (directory p))
40 (let ((name (car (last (pathname-directory d)))))
41 (when (parse-integer name :junk-allowed t)
42 (let ((xml (directory (merge-pathnames "*.xml" d))))
43 (incf total (1+ (length xml)))
44 (multiple-value-bind (ok grammar) (test1 d)
45 (cond
46 (ok
47 (incf pass (1+ (run-validation-tests name grammar xml))))
49 (dolist (x xml)
50 (format t "~A-~D: FAIL: cannot run test~%"
51 name
52 (pathname-name x))))))))))
53 (format t "Passed ~D/~D tests.~%" pass total))
54 (dribble))
56 (defvar *compatibility-test-p* nil)
58 (defun run-dtd-tests
59 (&optional (p "/home/david/src/lisp/cxml-rng/dtd-split/*")
60 (q "/home/david/src/lisp/cxml-rng/DTDTEST"))
61 (let ((*compatibility-test-p* t))
62 (run-tests p q)))
64 (defun run-dtd-test
65 (n &optional (p "/home/david/src/lisp/cxml-rng/dtd-split/*"))
66 (let ((*break-on-signals* 'error))
67 (run-test n p)))
69 (defun run-validation-test
70 (m n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
71 (let ((d (merge-pathnames (format nil "~3,'0D/" m) p))
72 (*break-on-signals* 'error)
73 (*debug* t)
74 (*print-level* 3))
75 (run-validation-tests m
76 (nth-value 1 (test1 d))
77 (list (let ((v (merge-pathnames
78 (format nil "~A.v.xml" n)
79 d)))
80 (if (probe-file v)
82 (merge-pathnames
83 (format nil "~A.i.xml" n)
84 d)))))))
86 (defun run-validation-tests (name grammar tests)
87 (let ((pass 0))
88 (dolist (x tests)
89 (format t "~A-~D: " name (pathname-name x))
90 (flet ((doit ()
91 (cxml:parse-file x (make-dtd-compatibility-handler
92 grammar
93 (make-validator grammar)))))
94 (if (find #\v (pathname-name x))
95 (handler-case
96 (progn
97 (doit)
98 (incf pass)
99 (format t "PASS~%"))
100 (error (c)
101 (format t "FAIL: ~A~%" c)))
102 (handler-case
103 (progn
104 (doit)
105 (format t "FAIL: didn't detect invalid document~%"))
106 (dtd-compatibility-error (c)
107 (cond
108 (*compatibility-test-p*
109 (incf pass)
110 (format t "PASS: ~A~%" (type-of c)))
112 (format t "FAIL: incorrect condition type (a): ~A~%" c))))
113 (rng-error (c)
114 (cond
115 (*compatibility-test-p*
116 (format t "FAIL: incorrect condition type (b): ~A~%" c))
118 (incf pass)
119 (format t "PASS: ~A~%" (type-of c)))))
120 (error (c)
121 (format t "FAIL: incorrect condition type (c): ~A~%" c))))))
122 pass))
124 (defun run-test (n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
125 (test1 (merge-pathnames (format nil "~3,'0D/" n) p)))
127 (defun parse-test (n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
128 (let* ((*debug* t)
129 (d (merge-pathnames (format nil "~3,'0D/" n) p))
130 (i (merge-pathnames "i.rng" d))
131 (c (merge-pathnames "c.rng" d))
132 (rng (if (probe-file c) c i)))
133 (format t "~A: " (car (last (pathname-directory d))))
134 (print rng)
135 (parse-schema rng)))
137 (defun test1 (d)
138 (let* ((i (merge-pathnames "i.rng" d))
139 (c (merge-pathnames "c.rng" d)))
140 (format t "~A: " (car (last (pathname-directory d))))
141 (if (probe-file c)
142 (handler-case
143 (let ((grammar (parse-schema c)))
144 (format t " PASS~%")
145 (values t grammar))
146 (error (c)
147 (format t " FAIL: ~A~%" c)
148 nil))
149 (handler-case
150 (progn
151 (parse-schema i)
152 (format t " FAIL: didn't detect invalid schema~%")
153 nil)
154 (dtd-compatibility-error (c)
155 (cond
156 (*compatibility-test-p*
157 (format t " PASS: ~A~%" (type-of c))
160 (format t " FAIL: incorrect condition type (A): ~A~%" c)
161 nil)))
162 (rng-error (c)
163 (cond
164 (*compatibility-test-p*
165 (format t " FAIL: incorrect condition type (B): ~A~%" c)
166 nil)
168 (format t " PASS: ~A~%" (type-of c))
169 t)))
170 (error (c)
171 (format t " FAIL: incorrect condition type (C): ~A~%" c)
172 nil)))))
174 (defvar *test-xmllint*)
176 (defun run-nist-tests
177 (*test-xmllint*
178 &optional (p #p"/home/david/NISTSchemaTests/NISTXMLSchemaTestSuite.xml"))
179 (dribble (if *test-xmllint*
180 "/home/david/src/lisp/cxml-rng/NIST-xmllint"
181 "/home/david/src/lisp/cxml-rng/NIST")
182 :if-exists :rename-and-delete)
183 (klacks:with-open-source (s (cxml:make-source p))
184 (let ((total 0)
185 (pass 0))
186 (loop
187 while (klacks:find-element s "Link")
189 (multiple-value-bind (n i)
190 (run-nist-tests/link (klacks:get-attribute s "href") p)
191 (incf total n)
192 (incf pass i))
193 (klacks:consume s))
194 (format t "Passed ~D/~D tests.~%" pass total)))
195 (dribble))
197 (defun run-nist-tests/link (href base)
198 (klacks:with-open-source (r (cxml:make-source (merge-pathnames href base)))
199 (let ((total 0)
200 (pass 0))
201 (let (schema)
202 (loop
203 (multiple-value-bind (key uri lname)
204 (klacks:peek-next r)
206 (unless key
207 (return))
208 (when (eq key :start-element)
209 (cond
210 ((equal lname "Schema")
211 (incf total)
212 (let ((href (klacks:get-attribute r "href")))
213 (setf schema
214 (if (or (search "-enumeration-" href)
215 (search "-whiteSpace-" href))
216 :ignore
217 (read-nist-grammar href base))))
218 (when schema
219 (incf total)))
220 ((equal lname "Instance")
221 (incf total)
222 (when (run-nist-test/Instance schema
223 (klacks:get-attribute r "href")
224 base)
225 (incf pass))))))))
226 (values total pass))))
228 (defun run-nist-test/Instance (schema href base)
229 (cond
230 ((eq schema :ignore)
231 (format t "PASS INSTANCE ~A: (ignored)~%" href)
233 ((stringp schema)
234 (assert *test-xmllint*)
235 (let ((asdf::*VERBOSE-OUT* (make-string-output-stream)))
236 (cond
237 ((zerop (asdf:run-shell-command
238 "xmllint -relaxng ~A ~A"
239 schema
240 (namestring (merge-pathnames href base))))
241 (format t "PASS INSTANCE ~A~%" href)
244 (format t "FAIL INSTANCE ~A: failed to validate:~_ ~A~%"
245 href
246 (get-output-stream-string asdf::*VERBOSE-OUT*))
247 nil))))
248 (schema
249 (handler-case
250 (progn
251 (cxml:parse-file (merge-pathnames href base)
252 (make-validator schema))
253 (format t "PASS INSTANCE ~A~%" href)
255 (rng-error (c)
256 (format t "FAIL INSTANCE ~A: failed to validate:~_ ~A~%" href c)
257 nil)
258 (error (c)
259 (format t "FAIL INSTANCE ~A: (BOGUS CONDITON) failed to validate:~_ ~A~%" href c)
260 nil)))
262 (format t "FAIL ~A: no schema~%" href)
263 nil)))
265 (defun read-nist-grammar (href base)
266 (let ((p (make-pathname :type "rng" :defaults href)))
267 (handler-case
268 (prog1
269 (if *test-xmllint*
270 (namestring (merge-pathnames p base))
271 (parse-schema (merge-pathnames p base)))
272 (format t "PASS ~A~%" href)
274 (rng-error (c)
275 (cond
276 ((search ":NAME whiteSpace" (princ-to-string c))
277 (format t "PASS ~A: whiteSpace forbidden~%" href)
278 :ignore)
279 ((search ":NAME enumeration" (princ-to-string c))
280 (format t "PASS ~A: enumeration forbidden~%" href)
281 :ignore)
283 (format t "FAIL ~A: failed to parse:~_ ~A~%" href c)
284 nil)))
285 (error (c)
286 (format t "FAIL ~A: (BOGUS CONDITION) failed to parse:~_ ~A~%" href c)
287 nil))))