dtd-tests korrigiert
[cxml-rng.git] / test.lisp
blob53ff1545784d4d48f7e9bcbe90bed37c56e520b7
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-validation-test
65 (m n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
66 (let ((d (merge-pathnames (format nil "~3,'0D/" m) p))
67 (*break-on-signals* 'error)
68 (*debug* t)
69 (*print-level* 3))
70 (run-validation-tests m
71 (nth-value 1 (test1 d))
72 (list (let ((v (merge-pathnames
73 (format nil "~A.v.xml" n)
74 d)))
75 (if (probe-file v)
77 (merge-pathnames
78 (format nil "~A.i.xml" n)
79 d)))))))
81 (defun run-validation-tests (name grammar tests)
82 (let ((pass 0))
83 (dolist (x tests)
84 (format t "~A-~D: " name (pathname-name x))
85 (flet ((doit ()
86 (cxml:parse-file x (make-validator grammar))))
87 (if (find #\v (pathname-name x))
88 (handler-case
89 (progn
90 (doit)
91 (incf pass)
92 (format t "PASS~%"))
93 (error (c)
94 (format t "FAIL: ~A~%" c)))
95 (handler-case
96 (progn
97 (doit)
98 (format t "FAIL: didn't detect invalid document~%"))
99 (dtd-compatibility-error (c)
100 (cond
101 (*compatibility-test-p*
102 (incf pass)
103 (format t "PASS: ~A~%" (type-of c)))
105 (format t "FAIL: incorrect condition type: ~A~%" c))))
106 (rng-error (c)
107 (cond
108 (*compatibility-test-p*
109 (format t "FAIL: incorrect condition type: ~A~%" c))
111 (incf pass)
112 (format t "PASS: ~A~%" (type-of c)))))
113 (error (c)
114 (format t "FAIL: incorrect condition type: ~A~%" c))))))
115 pass))
117 (defun run-test (n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
118 (test1 (merge-pathnames (format nil "~3,'0D/" n) p)))
120 (defun parse-test (n &optional (p "/home/david/src/lisp/cxml-rng/spec-split/"))
121 (let* ((*debug* t)
122 (d (merge-pathnames (format nil "~3,'0D/" n) p))
123 (i (merge-pathnames "i.rng" d))
124 (c (merge-pathnames "c.rng" d))
125 (rng (if (probe-file c) c i)))
126 (format t "~A: " (car (last (pathname-directory d))))
127 (print rng)
128 (parse-schema rng)))
130 (defun test1 (d)
131 (let* ((i (merge-pathnames "i.rng" d))
132 (c (merge-pathnames "c.rng" d)))
133 (format t "~A: " (car (last (pathname-directory d))))
134 (if (probe-file c)
135 (handler-case
136 (let ((grammar (parse-schema c)))
137 (format t " PASS~%")
138 (values t grammar))
139 (error (c)
140 (format t " FAIL: ~A~%" c)
141 nil))
142 (handler-case
143 (progn
144 (parse-schema i)
145 (format t " FAIL: didn't detect invalid schema~%")
146 nil)
147 (dtd-compatibility-error (c)
148 (cond
149 (*compatibility-test-p*
150 (format t " PASS: ~A~%" (type-of c))
153 (format t " FAIL: incorrect condition type: ~A~%" c)
154 nil)))
155 (rng-error (c)
156 (cond
157 (*compatibility-test-p*
158 (format t " FAIL: incorrect condition type: ~A~%" c)
159 nil)
161 (format t " PASS: ~A~%" (type-of c))
162 t)))
163 (error (c)
164 (format t " FAIL: incorrect condition type: ~A~%" c)
165 nil)))))
167 (defvar *test-xmllint*)
169 (defun run-nist-tests
170 (*test-xmllint*
171 &optional (p #p"/home/david/NISTSchemaTests/NISTXMLSchemaTestSuite.xml"))
172 (dribble (if *test-xmllint*
173 "/home/david/src/lisp/cxml-rng/NIST-xmllint"
174 "/home/david/src/lisp/cxml-rng/NIST")
175 :if-exists :rename-and-delete)
176 (klacks:with-open-source (s (cxml:make-source p))
177 (let ((total 0)
178 (pass 0))
179 (loop
180 while (klacks:find-element s "Link")
182 (multiple-value-bind (n i)
183 (run-nist-tests/link (klacks:get-attribute s "href") p)
184 (incf total n)
185 (incf pass i))
186 (klacks:consume s))
187 (format t "Passed ~D/~D tests.~%" pass total)))
188 (dribble))
190 (defun run-nist-tests/link (href base)
191 (klacks:with-open-source (r (cxml:make-source (merge-pathnames href base)))
192 (let ((total 0)
193 (pass 0))
194 (let (schema)
195 (loop
196 (multiple-value-bind (key uri lname)
197 (klacks:peek-next r)
199 (unless key
200 (return))
201 (when (eq key :start-element)
202 (cond
203 ((equal lname "Schema")
204 (incf total)
205 (let ((href (klacks:get-attribute r "href")))
206 (setf schema
207 (if (or (search "-enumeration-" href)
208 (search "-whiteSpace-" href))
209 :ignore
210 (read-nist-grammar href base))))
211 (when schema
212 (incf total)))
213 ((equal lname "Instance")
214 (incf total)
215 (when (run-nist-test/Instance schema
216 (klacks:get-attribute r "href")
217 base)
218 (incf pass))))))))
219 (values total pass))))
221 (defun run-nist-test/Instance (schema href base)
222 (cond
223 ((eq schema :ignore)
224 (format t "PASS INSTANCE ~A: (ignored)~%" href)
226 ((stringp schema)
227 (assert *test-xmllint*)
228 (let ((asdf::*VERBOSE-OUT* (make-string-output-stream)))
229 (cond
230 ((zerop (asdf:run-shell-command
231 "xmllint -relaxng ~A ~A"
232 schema
233 (namestring (merge-pathnames href base))))
234 (format t "PASS INSTANCE ~A~%" href)
237 (format t "FAIL INSTANCE ~A: failed to validate:~_ ~A~%"
238 href
239 (get-output-stream-string asdf::*VERBOSE-OUT*))
240 nil))))
241 (schema
242 (handler-case
243 (progn
244 (cxml:parse-file (merge-pathnames href base)
245 (make-validator schema))
246 (format t "PASS INSTANCE ~A~%" href)
248 (rng-error (c)
249 (format t "FAIL INSTANCE ~A: failed to validate:~_ ~A~%" href c)
250 nil)
251 (error (c)
252 (format t "FAIL INSTANCE ~A: (BOGUS CONDITON) failed to validate:~_ ~A~%" href c)
253 nil)))
255 (format t "FAIL ~A: no schema~%" href)
256 nil)))
258 (defun read-nist-grammar (href base)
259 (let ((p (make-pathname :type "rng" :defaults href)))
260 (handler-case
261 (prog1
262 (if *test-xmllint*
263 (namestring (merge-pathnames p base))
264 (parse-schema (merge-pathnames p base)))
265 (format t "PASS ~A~%" href)
267 (rng-error (c)
268 (cond
269 ((search ":NAME whiteSpace" (princ-to-string c))
270 (format t "PASS ~A: whiteSpace forbidden~%" href)
271 :ignore)
272 ((search ":NAME enumeration" (princ-to-string c))
273 (format t "PASS ~A: enumeration forbidden~%" href)
274 :ignore)
276 (format t "FAIL ~A: failed to parse:~_ ~A~%" href c)
277 nil)))
278 (error (c)
279 (format t "FAIL ~A: (BOGUS CONDITION) failed to parse:~_ ~A~%" href c)
280 nil))))