More checks for disallowed attributes
[xuriella.git] / test.lisp
blob3d2705c506cc16a28d0a1f6487e56d6219b0d9e2
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella)
32 (defparameter *tests-directory*
33 "/home/david/src/XSLT-testsuite-04/testsuite/TESTS/")
35 (defclass test-case ()
36 ((id :initarg :id :accessor test-id)
37 (category :initarg :category :accessor test-category)
38 (operation :initarg :operation :accessor test-operation)
39 (data-pathname :initarg :data-pathname :accessor test-data-pathname)
40 (stylesheet-pathname :initarg :stylesheet-pathname
41 :accessor test-stylesheet-pathname)
42 (data-pathname-2 :initarg :data-pathname-2 :accessor test-data-pathname-2)
43 (stylesheet-pathname-2 :initarg :stylesheet-pathname-2
44 :accessor test-stylesheet-pathname-2)
45 (output-pathname :initarg :output-pathname
46 :accessor test-official-output-pathname)
47 (output-compare :initarg :output-compare
48 :accessor test-output-compare)))
50 (defmethod print-object ((object test-case) stream)
51 (print-unreadable-object (object stream :identity nil :type t)
52 (format stream "~A ~A/~A"
53 (test-operation object)
54 (test-category object)
55 (test-id object))))
58 ;;;; SIMPLIFY-TESTS
60 ;;; Translate catalog.xml into an actually usable katalog.xml
61 ;;; by running the test cases through xsltproc to see what it thinks
62 ;;; about them.
64 (defun simplify-tests (&optional (d *tests-directory*))
65 (with-open-file (stream (merge-pathnames "katalog.xml" d)
66 :direction :output
67 :if-exists :supersede
68 :element-type '(unsigned-byte 8))
69 (cxml:with-xml-output (cxml:make-octet-stream-sink stream)
70 (cxml:with-element "simplified-test-suite"
71 (klacks:with-open-source
72 (source (klacks:make-tapping-source
73 (cxml:make-source (merge-pathnames "catalog.xml" d))))
74 (let ((*default-pathname-defaults* (merge-pathnames d)))
75 (map-original-tests #'simplify-test source)))))))
77 (defun map-original-tests (run-test source &key (test (constantly t)))
78 (let ((total 0)
79 (pass 0)
80 major-path)
81 (loop
82 while (klacks:find-event source :start-element)
83 for lname = (klacks:current-lname source)
85 (cond
86 ((equal lname "major-path")
87 (klacks:skip source :start-element)
88 (setf major-path
89 (namestring
90 (merge-pathnames (klacks:consume-characters source)))))
91 ((equal lname "test-case")
92 (let* ((<test-case>
93 (stp:document-element
94 (klacks:serialize-element source (stp:make-builder))))
95 (test-case (parse-original-test major-path <test-case>)))
96 (when (funcall test test-case)
97 (incf total)
98 (when (funcall run-test test-case)
99 (incf pass)))))
101 (klacks:skip source :start-element))))
102 (format t "~&Passed ~D/~D tests.~%" pass total)))
104 (defun parse-original-test (major-path <test-case>)
105 (let* ((file-path
106 (stp:string-value
107 (stp:find-recursively-if (stp:of-name "file-path") <test-case>)))
108 (base (concatenate 'string major-path "/" file-path))
109 (out-base (concatenate 'string major-path "/REF_OUT/" file-path))
110 (scenario
111 (stp:find-recursively-if (stp:of-name "scenario") <test-case>))
112 data
113 stylesheet
114 supplemental-stylesheet
115 supplemental-data
116 output
117 compare)
118 (dolist (<input> (stp:filter-recursively (stp:of-name "input-file")
119 <test-case>))
120 (let ((role (stp:attribute-value <input> "role"))
121 (path (concatenate 'string base "/" (stp:string-value <input>))))
122 (cond
123 ((equal role "principal-data")
124 (setf data path))
125 ((equal role "principal-stylesheet")
126 (setf stylesheet path))
127 ((equal role "supplemental-stylesheet")
128 (setf supplemental-stylesheet path))
129 ((equal role "supplemental-data")
130 (setf supplemental-data path))
132 (error "unrecognized role: ~A" role)))))
133 (dolist (<output> (stp:filter-recursively (stp:of-name "output-file")
134 <test-case>))
135 (let ((role (stp:attribute-value <output> "role"))
136 (path (concatenate 'string out-base
138 (stp:string-value <output>))))
139 (cond
140 ((equal role "principal")
141 (setf output path)
142 (setf compare (stp:attribute-value <output> "compare")))
144 (error "unrecognized role: ~A" role)))))
145 (make-instance 'test-case
146 :id (stp:attribute-value <test-case> "id")
147 :category (stp:attribute-value <test-case> "category")
148 :operation (stp:attribute-value scenario "operation")
149 :data-pathname data
150 :stylesheet-pathname stylesheet
151 :stylesheet-pathname-2 supplemental-stylesheet
152 :data-pathname-2 supplemental-data
153 :output-pathname output
154 :output-compare compare)))
156 (defun write-simplified-test (test-case operation)
157 (cxml:with-element "test-case"
158 (cxml:attribute "id" (test-id test-case))
159 (cxml:attribute "category" (test-category test-case))
160 (flet ((p (l p)
161 (cxml:attribute l (and p (namestring p)))))
162 (p "data" (test-data-pathname test-case))
163 (p "stylesheet" (noindent-stylesheet-pathname test-case))
164 (p "data-2" (test-data-pathname-2 test-case))
165 (p "stylesheet-2" (test-stylesheet-pathname-2 test-case))
166 (p "output" (test-official-output-pathname test-case))
167 (p "compare" (test-output-compare test-case)))
168 (cxml:attribute "operation" operation)))
170 (defun test-output-pathname (test type)
171 (make-pathname :name (test-id test)
172 :type type
173 :defaults (test-data-pathname test)))
175 (defun sanitize-stylesheet (in out)
176 (if (probe-file in)
177 (handler-case
178 (let ((d (cxml:parse (pathname in) (stp:make-builder))))
179 (xpath:with-namespaces ((nil #.*xsl*))
180 (xpath:do-node-set (output (xpath:evaluate "//output" d))
181 (let ((a (stp:find-attribute-named output "indent")))
182 (when a
183 (stp:detach a)))))
184 (with-open-file (s out
185 :direction :output
186 :if-exists :rename-and-delete
187 :element-type '(unsigned-byte 8))
188 (stp:serialize d (cxml:make-octet-stream-sink s))))
189 (error (c)
190 (warn "ignoring bogus stylesheet ~A: ~A" in c)
191 (copy-file in out)))
192 (warn "oops, ignoring missing stylesheet: ~A" in)))
194 (defun noindent-stylesheet-pathname (test-case)
195 (make-pathname :type "noindent-xsl"
196 :defaults (test-stylesheet-pathname test-case)))
198 (defun simplify-test (test-case)
199 (flet ((report (status &optional (fmt "") &rest args)
200 (format t "~&~A ~A [~A]~?~%"
201 status
202 (test-id test-case)
203 (test-category test-case)
205 args)))
206 (let* ((data (test-data-pathname test-case))
207 (stylesheet (test-stylesheet-pathname test-case))
208 (noindent-stylesheet (noindent-stylesheet-pathname test-case))
209 #+xuriella::xsltproc
210 (out (test-output-pathname test-case "xsltproc"))
211 (saxon-out (test-output-pathname test-case "saxon")))
212 (sanitize-stylesheet stylesheet noindent-stylesheet)
213 (if (equal (test-operation test-case) "standard")
214 (handler-case
215 (progn
216 #+xuriella::xsltproc (xsltproc noindent-stylesheet data out)
217 (saxon noindent-stylesheet data saxon-out)
218 (report "PASS")
219 (write-simplified-test test-case "standard")
221 (error (c)
222 (report "FAIL" ": ~A" c)
223 (write-simplified-test test-case "execution-error")
224 nil))
225 (handler-case
226 (progn
227 #+xuriella::xsltproc
228 (xsltproc noindent-stylesheet data "/dev/null")
229 (saxon noindent-stylesheet data "/dev/null")
230 (report "FAIL" ": expected error not signalled")
231 ;; let's ignore unexpected successes for now
232 nil)
233 (error (c)
234 (report "PASS" ": expected error ~A" c)
235 (write-simplified-test test-case "execution-error")
236 t))))))
238 (defun xsltproc (stylesheet input output)
239 (flet ((full-namestring (x)
240 (namestring (merge-pathnames x))))
241 (let* ((asdf::*verbose-out* (make-string-output-stream))
242 (code (asdf:run-shell-command
243 "cd ~S && xsltproc ~S ~S >~S"
244 (full-namestring "")
245 (full-namestring stylesheet)
246 (full-namestring input)
247 (full-namestring output))))
248 (unless (zerop code)
249 (error "running xsltproc failed with code ~A [~%~A~%]"
250 code
251 (get-output-stream-string asdf::*verbose-out*))))))
253 (defun saxon (stylesheet input output)
254 (flet ((full-namestring (x)
255 (namestring (merge-pathnames x))))
256 (let* ((asdf::*verbose-out* (make-string-output-stream))
257 (code (asdf:run-shell-command
258 "cd ~S && java -jar /usr/share/java/saxon.jar ~S ~S >~S"
259 (full-namestring "")
260 (full-namestring input)
261 (full-namestring stylesheet)
262 (full-namestring output))))
263 (unless (zerop code)
264 (error "running saxon failed with code ~A [~%~A~%]"
265 code
266 (get-output-stream-string asdf::*verbose-out*))))))
269 ;;;; RUN-TESTS and DRIBBLE-TESTS
271 ;;; Process katalog.xml
273 (defun dribble-tests
274 (&key filter (directory *tests-directory*) (file "TEST"))
275 (let ((*package* (find-package 'cl-user))
276 (*print-circle* nil))
277 (with-open-file (dribble
278 (merge-pathnames file
279 (slot-value (asdf:find-system :xuriella)
280 'asdf::relative-pathname))
281 :direction :output
282 :if-exists :supersede
283 :external-format :utf-8)
284 (let* ((dribble (make-broadcast-stream dribble *standard-output*))
285 (*standard-output* dribble)
286 (*trace-output* dribble)
287 (*error-output* dribble)
288 (*terminal-io* (make-two-way-stream *standard-input* dribble)))
289 (handler-bind ((warning
290 (lambda (c)
291 (warn "~A" (replace-junk (princ-to-string c)))
292 (muffle-warning c))))
293 (run-tests :filter filter
294 :directory directory))))))
296 (defparameter *bad-tests*
297 '( ;; some tests wants us to recover from this error, yet this one doesn't:
298 "copy_copy61"
299 "copy_copy62"
301 ;; we perform recovery, but saxon doesn't. Recovery results in non-XML
302 ;; output, which we can't parse for comparison against the official test
303 ;; case.
304 "output_output75"
306 ;; we'd pass these tests, but the test authors forgot to declare the
307 ;; entity they're writing, so we can't parse it for comparison.
308 "output_output06"
309 "output_output10"
310 "output_output61"
312 ;; another similar test where the output is unparsable, except that
313 ;; here an entity declaration wouldn't have helped either:
314 "Copying_ResultTreeFragmentWithEscapedText"
316 ;; input document not ns-wf
317 "Attributes__78387"
319 ;; the following tests take a lot of time due to the problems of current matching algorithm:
320 "impincl_impincl16"
321 "match_match13"
322 ;; stack exhaustion -- matching problem i think
323 "Keys_PerfRepro3"
324 ;; probably the same problem (but I haven't checked):
325 "numbering_numbering03"
326 "numbering_numbering10"
327 "numbering_numbering11"
328 "numbering_numbering80"
329 "numbering_numbering81"
330 "numbering_numbering94"
331 "numbering_numbering95"
332 "Import__91164"))
334 (defun run-tests (&key filter (directory *tests-directory*))
335 (when (typep filter '(or string cons))
336 (setf filter (cl-ppcre:create-scanner filter)))
337 (klacks:with-open-source
338 (source (klacks:make-tapping-source
339 (cxml:make-source (merge-pathnames "katalog.xml" directory))))
340 (let ((*default-pathname-defaults* (merge-pathnames directory)))
341 (map-tests #'run-test
342 source
343 :test (lambda (test)
344 (and (or (null filter)
345 (cl-ppcre:all-matches
346 filter
347 (format nil "~A/~A"
348 (test-category test)
349 (test-id test))))
350 (not (find (test-id test)
351 *bad-tests*
352 :test #'equal))))))))
354 (defun run-named-test (name &optional (d *tests-directory*))
355 (let ((*break-on-signals* 'error))
356 (run-tests :filter (format nil "/~A$" name) :directory d)))
358 (defun copy-file (p q)
359 (with-open-file (in p :element-type '(unsigned-byte 8))
360 (with-open-file (out q
361 :element-type '(unsigned-byte 8)
362 :direction :output
363 :if-exists :rename-and-delete)
364 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
365 (loop for pos = (read-sequence buf in)
366 until (zerop pos)
367 do (write-sequence buf out :end pos))))))
369 (defun find-named-test (name &optional (d *tests-directory*))
370 (klacks:with-open-source
371 (source (klacks:make-tapping-source
372 (cxml:make-source (merge-pathnames "katalog.xml" d))))
373 (block nil
374 (map-tests (lambda (test)
375 (return test))
376 source
377 :test (lambda (test) (equal (test-id test) name))))))
379 (defun copy-test-files (name &optional (d *tests-directory*))
380 (let* ((test (find-named-test name d))
381 (*default-pathname-defaults* (merge-pathnames d))
382 (*break-on-signals* 'error)
383 (target-dir (merge-pathnames "copied-test/"
384 (asdf:component-pathname
385 (asdf:find-system :xuriella))))
386 (xsl (merge-pathnames "test.xsl" target-dir))
387 (xml (merge-pathnames "test.xml" target-dir))
388 (txt (merge-pathnames "official-output.txt" target-dir))
389 (expected (merge-pathnames "expected.xml" target-dir))
390 (actual (merge-pathnames "actual.xml" target-dir)))
391 (ensure-directories-exist target-dir)
392 (copy-file (test-stylesheet-pathname test) xsl)
393 (copy-file (test-data-pathname test) xml)
394 (when (test-official-output-pathname test)
395 (copy-file (test-official-output-pathname test) txt))
396 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
397 (format t "Test data copied to:~% ~A~%~%" xml)
398 (when (test-official-output-pathname test)
399 (format t "Official output file:~% ~A~%~%" txt))
400 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
401 (namestring target-dir)
402 (enough-namestring xsl target-dir)
403 (enough-namestring xml target-dir)
404 (enough-namestring expected target-dir))
405 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
406 (namestring target-dir)
407 (enough-namestring xml target-dir)
408 (enough-namestring xsl target-dir)
409 (enough-namestring expected target-dir))
410 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
411 (namestring target-dir)
412 (enough-namestring xml target-dir)
413 (enough-namestring xsl target-dir)
414 (enough-namestring expected target-dir))
415 (format t "Run xuriella like this:~%")
416 `(apply-stylesheet ,xsl ,xml :output ,actual)))
418 (defun map-tests (run-test source &key (test (constantly t)))
419 (let ((total 0)
420 (pass 0))
421 (loop
422 while (klacks:find-event source :start-element)
423 for lname = (klacks:current-lname source)
425 (cond
426 ((equal lname "test-case")
427 (let* ((<test-case>
428 (stp:document-element
429 (klacks:serialize-element source (stp:make-builder))))
430 (test-case (parse-test <test-case>)))
431 (when (funcall test test-case)
432 (incf total)
433 (when (funcall run-test test-case)
434 (incf pass)))))
436 (klacks:skip source :start-element))))
437 (format t "~&Passed ~D/~D tests.~%" pass total)))
439 (defun parse-test (<test-case>)
440 (stp:with-attributes (id category operation
441 data stylesheet data-2 stylesheet-2
442 output compare)
443 <test-case>
444 (make-instance 'test-case
445 :id id
446 :category category
447 :operation operation
448 :data-pathname data
449 :stylesheet-pathname stylesheet
450 :data-pathname-2 data-2
451 :stylesheet-pathname-2 stylesheet-2
452 :output-pathname output
453 :output-compare compare)))
455 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
456 ;; beginning, if any.
457 (defun slurp-for-comparison (p)
458 (with-open-file (s p :element-type '(unsigned-byte 8))
459 (unless (and (eql (read-byte s nil) #xef)
460 (eql (read-byte s nil) #xbb)
461 (eql (read-byte s nil) #xbf))
462 (file-position s 0))
463 (if (plusp (file-length s))
464 (slurp-for-comparison-1 p s t)
465 "<wrapper/>")))
467 (defun slurp-for-comparison-1 (p s junk-info)
468 (let ((pos (file-position s)) ;for UTF-8 "BOM"
469 (xstream (runes:make-xstream s :speed 1))
470 (prev-pos 0))
471 (setf (runes:xstream-name xstream)
472 (cxml::make-stream-name
473 :entity-name "main document"
474 :entity-kind :main
475 :uri (cxml::pathname-to-uri (merge-pathnames p))))
476 (let ((source
477 (flet ((er (pub sys)
478 pub sys
479 (flexi-streams:make-in-memory-input-stream
480 #())))
481 (cxml:make-source xstream
482 :pathname p
483 :entity-resolver #'er))))
484 (unless (eq junk-info :nada)
485 (loop
486 for key = (progn
487 (setf prev-pos (runes:xstream-position xstream))
488 (klacks:peek-next source))
489 until (eq key :start-document))
490 (cxml::with-source (source cxml::context)
491 (when (eq (cxml::zstream-token-category
492 (cxml::main-zstream cxml::context))
493 :NMTOKEN)
494 ;; oops, doesn't look like XML at all
495 (file-position s pos)
496 (return-from slurp-for-comparison-1
497 (slurp-for-comparison-1 p s :nada)))))
498 (etypecase junk-info
499 (integer
500 (dotimes (x junk-info)
501 (setf prev-pos (runes:xstream-position xstream))
502 (klacks:peek-next source)))
503 ((eql t)
504 (let ((nskip 0))
505 (handler-case
506 (loop
507 (case (klacks:peek-next source)
508 (:start-element (return))
509 (:characters
510 (if (whitespacep (klacks:current-characters source))
511 (incf nskip)
512 (return)))
514 (incf nskip))))
515 ((or file-error cxml:xml-parse-error) ()
516 (when (zerop nskip)
517 (setf nskip nil))))
518 ;; retry
519 (with-open-file (u p :element-type '(unsigned-byte 8))
520 (file-position u pos)
521 (return-from slurp-for-comparison-1
522 (slurp-for-comparison-1 p u nskip)))))
523 ((member nil :nada)))
524 (with-output-to-string (r)
525 (let* ((seen-char
526 (cxml::with-source (source cxml::context)
527 (ecase (cxml::zstream-token-category
528 (cxml::main-zstream cxml::context))
529 (:seen-< #\<)
530 (:? #\?)
531 ((nil :s)
532 (setf prev-pos (runes:xstream-position xstream))
533 nil))))
534 (off-by-one-p (or seen-char (eq junk-info :nada)))
535 (new-pos (- prev-pos (if off-by-one-p 1 0))))
536 ;; copy doctype over
537 (with-open-file (u p :element-type '(unsigned-byte 8))
538 (file-position u pos)
539 (let ((y (runes:make-xstream u :speed 1)))
540 (loop
541 while (< (runes:xstream-position y) new-pos)
542 do (write-char (runes:read-rune y) r))))
543 (write-line "<wrapper>" r)
544 (when seen-char
545 (write-char seen-char r)))
546 (loop
547 for char = (runes:read-rune xstream)
548 until (eq char :eof)
549 do (write-char char r))
550 (write-line "</wrapper>" r)))))
552 (defun parse-for-comparison (p)
553 (let* ((d (flet ((er (pub sys)
554 pub sys
555 (flexi-streams:make-in-memory-input-stream
556 #())))
557 (cxml:parse (slurp-for-comparison p)
558 (make-text-normalizer (stp:make-builder))
559 :entity-resolver #'er)))
560 (de (stp:document-element d)))
561 (let ((first (stp:first-child de)))
562 (when (typep first 'stp:text)
563 (cond
564 ((whitespacep (stp:data first))
565 (stp:delete-child first de))
567 (setf (stp:data first)
568 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
569 (stp:data first)
570 ""))))))
571 (let ((last (stp:last-child de)))
572 (when (typep last 'stp:text)
573 (cond
574 ((whitespacep (stp:data last))
575 (stp:delete-child last de))
577 (setf (stp:data last)
578 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
579 (stp:data last)
580 ""))))))
583 (defun output-equal-p (compare p q &key normalize)
584 (handler-case
585 (ecase compare
586 (:xml (xml-output-equal-p p q normalize))
587 (:html (html-output-equal-p p q))
588 (:text (text-output-equal-p p q)))
589 ((or error parse-number::invalid-number) (c)
590 (warn "comparison failed: ~A" c)
591 nil)))
593 ;; Workaround for namespace_namespace23 and other tests:
594 ;; - For these tests, saxon and msxsl output a declaration for the XSL
595 ;; namespace without using that declaration.
596 ;; - I think saxon and msxsl are both wrong.
597 ;; - The official test output agrees with my assessment.
598 ;; (So does libxslt, but that's not to be trusted. :-))
599 ;; - Here's the catch: The official test output is broken in its whitespace
600 ;; handling.
601 ;; So let's normalize spaces in test output that looks like an XSLT
602 ;; stylesheet, allowing us to pass these tests using the official test output.
603 (defun maybe-normalize-test-spaces (wrapper force)
604 (stp:do-children (wrapper-child wrapper)
605 (when (and (typep wrapper-child 'stp:element)
606 (or (equal (stp:namespace-uri wrapper-child) *xsl*)
607 force))
608 (strip-stylesheet wrapper-child)
609 (labels ((recurse (e &optional preserve)
610 (stp:do-children (child e)
611 (typecase child
612 (stp:text
613 (setf (stp:data child)
614 (normalize-whitespace (stp:data child))))
615 (stp:element
616 (stp:with-attributes ((space "space" *xml*))
617 child
618 (let ((new-preserve
619 (cond
620 ((namep child "text") t)
621 ((not space) preserve)
622 ((equal space "preserve") t)
623 (t nil))))
624 (recurse child new-preserve))))))))
625 (recurse wrapper-child)))))
627 (defun xml-output-equal-p (p q normalize)
628 (let ((r (parse-for-comparison p))
629 (s (parse-for-comparison q)))
630 (maybe-normalize-test-spaces (stp:document-element r) normalize)
631 (maybe-normalize-test-spaces (stp:document-element s) normalize)
632 (and (let ((u (stp:document-type r))
633 (v (stp:document-type s)))
634 (if u
635 (and v (node= u v))
636 (null v)))
637 (node= (stp:document-element r) (stp:document-element s)))))
639 ;; FIXME: don't do this in <pre> etc.
640 (defun normalize-html-whitespace (node)
641 (when (typep node 'stp:parent-node)
642 ;; ignore newlines after start tags completely
643 (let ((first (stp:first-child node)))
644 (when (and (typep first 'stp:text)
645 (alexandria:starts-with #\newline (stp:data first)))
646 (setf (stp:data first) (subseq (stp:data first) 1))))
647 ;; ignore newlines before end tags completely
648 (let ((last (stp:last-child node)))
649 (when (and (typep last 'stp:text)
650 (alexandria:ends-with #\newline (stp:data last)))
651 (setf (stp:data last)
652 (subseq (stp:data last) 0 (length (stp:data last))))))
653 ;; normalize sequences of whitespace
654 (stp:do-children (child node)
655 (if (typep child 'stp:text)
656 (setf (stp:data child)
657 (let ((str (normalize-whitespace (stp:data child))))
658 (when
659 ;; FIXME! Here we remove whitespace entirely.
660 ;; Totally incorrect, but I don't see how we could
661 ;; watch Saxon's output otherwise.
662 (equal str " ")
663 (setf str ""))
664 str))
665 (normalize-html-whitespace child)))
666 ;; just to be sure, join adjacent nodes
667 (cxml-stp-impl::normalize-text-nodes! node)))
669 ;; FIXME: this check is too lenient, because chtml is an error-correcting
670 ;; parser.
671 (defun html-output-equal-p (p q)
672 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
673 (s (chtml:parse (pathname q) (stp:make-builder))))
674 (normalize-html-whitespace r)
675 (normalize-html-whitespace s)
676 (node= (stp:document-element r) (stp:document-element s))))
678 (defun text-output-equal-p (p q)
679 (with-open-file (a p :element-type '(unsigned-byte 8))
680 (with-open-file (b q :element-type '(unsigned-byte 8))
681 (let ((len (file-length a)))
682 (and (eql len (file-length b))
683 (let ((d (make-array len :element-type '(unsigned-byte 8)))
684 (e (make-array len :element-type '(unsigned-byte 8))))
685 (read-sequence d a)
686 (read-sequence e b)
687 (equalp d e)))))))
689 (defun strip-addresses (str)
690 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
692 (defun slurp-output-method (p)
693 (xpath:with-namespaces ((nil #.*xsl*))
694 (let* ((d (handler-bind
695 ((warning #'muffle-warning))
696 (cxml:parse (pathname p) (stp:make-builder))))
697 (output (xpath:first-node (xpath:evaluate "//output" d))))
698 (if output
699 (let ((method (stp:attribute-value output "method")))
700 (if method
701 (intern (string-upcase method) :keyword)
702 :xml))
703 :xml))))
705 (defun replace-junk (str)
706 (cl-ppcre:regex-replace-all
707 `(:group ,(namestring *tests-directory*))
708 (map 'string
709 (lambda (c)
710 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
712 #\?))
713 str)
714 "..."))
716 (defun run-test (test)
717 (let ((expected-saxon (test-output-pathname test "saxon"))
718 #+xuriella::xsltproc
719 (expected-xsltproc (test-output-pathname test "xsltproc"))
720 (actual (test-output-pathname test "xuriella"))
721 (official (test-official-output-pathname test))
722 (force-normalization
723 (find (test-id test) '("Namespace-alias__91782") :test #'equal))
724 (output-method nil))
725 (handler-bind ((|hey test suite, this is an HTML document|
726 (lambda (c)
727 (declare (ignore c))
728 (setf output-method :html))))
729 (labels ((uri-resolver (uri)
730 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
731 uri)
732 (cxml::pathname-to-uri
733 (merge-pathnames
734 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
735 *tests-directory*))
736 uri))
737 (doit ()
738 (with-open-file (s actual
739 :if-exists :rename-and-delete
740 :direction :output
741 :element-type '(unsigned-byte 8))
742 (handler-bind ((xslt-error
743 (lambda (c)
744 (declare (ignore c))
745 (when (find-restart 'recover)
746 (invoke-restart 'recover)))))
747 (apply-stylesheet (pathname (test-stylesheet-pathname test))
748 (pathname (test-data-pathname test))
749 :output s
750 :uri-resolver #'uri-resolver))))
751 (pp (label pathname)
752 (when pathname
753 (format t " ~A: ~A~%"
754 label
755 (enough-namestring pathname *tests-directory*))))
756 (report (ok &optional (fmt "") &rest args)
757 (write-string
758 (replace-junk
759 (strip-addresses
760 (format nil "~&~:[FAIL~;PASS~] ~A [~A]~?~%"
762 (test-id test)
763 (test-category test)
765 args))))
766 (pp "Stylesheet" (test-stylesheet-pathname test))
767 (pp "Data" (test-data-pathname test))
768 (pp "Supplemental stylesheet"
769 (test-stylesheet-pathname-2 test))
770 (pp "Supplemental data" (test-data-pathname-2 test))
771 (pp "Expected output (1)" expected-saxon)
772 #+xuriella::xsltproc
773 (pp "Expected output (2)" expected-xsltproc)
774 (pp "Actual output" actual)
775 (terpri)
776 ok))
777 (cond
778 ((equal (test-operation test) "standard")
779 (handler-case
780 (progn
781 (when (find (test-id test)
782 nil ;;'("axes_axes47" "attribset_attribset20")
783 :test #'equal)
784 (error "skipping problematic test"))
785 (doit)
786 (let* ((output-method
787 (or output-method
788 (slurp-output-method
789 (test-stylesheet-pathname test))))
790 (saxon-matches-p
791 (output-equal-p output-method
792 expected-saxon
793 actual))
794 #+xuriella::xsltproc
795 (xsltproc-matches-p
796 (output-equal-p output-method
797 expected-xsltproc
798 actual))
799 (official-matches-p
800 (output-equal-p output-method
801 official
802 actual
803 :normalize force-normalization)))
804 (cond
805 ((or saxon-matches-p
806 #+xuriella::xsltproc xsltproc-matches-p
807 official-matches-p)
808 (report t)
809 #+xuriella::xsltproc
810 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
811 saxon-matches-p
812 xsltproc-matches-p
813 (if saxon-matches-p
814 (not xsltproc-matches-p)
815 xsltproc-matches-p)))
817 (report nil ": output doesn't match")))))
818 ((or error parse-number::invalid-number) (c)
819 (report nil ": ~A" c))))
821 (handler-case
822 (doit)
823 (xslt-error (c)
824 (report t ": raised an xslt-error as expected" c))
825 ((or error parse-number::invalid-number) (c)
826 (report nil ": condition of incorrect type: ~%~A" c))
827 (:no-error (result)
828 (cond
829 ((not (and official (probe-file official)))
830 (report nil ": expected error not signalled: " result))
831 ((output-equal-p
832 (or output-method
833 (slurp-output-method (test-stylesheet-pathname test)))
834 official
835 actual
836 :normalize force-normalization)
837 (report t))
839 (report nil ": saxon error not signalled and official output not a match")))))))))))
841 (defun run-xpath-tests ()
842 (run-tests "XPath-Expression/|XSLT-Data-Model/"))
845 ;;;; from cxml-stp-test
847 (defun assert-node= (a b)
848 (unless (node= a b)
849 (error "assertion failed: ~S and ~S are not NODE=" a b)))
851 (defun child-count (node)
852 (stp:count-children-if (constantly t) node))
854 (defun named-node-= (a b)
855 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
856 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
857 (equal (stp:local-name a) (stp:local-name b))))
859 (defun parent-node-= (e f)
860 (and (eql (child-count e)
861 (child-count f))
862 (every #'node= (stp:list-children e) (stp:list-children f))))
864 (defmethod node= ((e stp:element) (f stp:element))
865 (and (named-node-= e f)
866 (parent-node-= e f)
867 (null
868 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
869 :test #'node=))
870 (block nil
871 (flet ((check-namespaces (a b)
872 (let ((result ()))
873 (stp:map-extra-namespaces
874 (lambda (k v)
875 (unless (equal v (stp:find-namespace k b))
876 (return nil)))
878 result)))
879 (check-namespaces e f)
880 (check-namespaces f e))
881 t)))
883 (defmethod node= ((a stp:node) (b stp:node))
884 nil)
886 (defmethod node= ((e stp:document) (f stp:document))
887 (parent-node-= e f))
889 (defmethod node= ((a stp:attribute) (b stp:attribute))
890 (and (named-node-= a b)
891 (equal (stp:value a) (stp:value b))))
893 (defmethod node= ((a stp:comment) (b stp:comment))
894 (equal (stp:data a) (stp:data b)))
896 (defmethod node= ((a stp:text) (b stp:text))
897 (equal (stp:data a) (stp:data b)))
899 (defmethod node= ((a stp:processing-instruction)
900 (b stp:processing-instruction))
901 (and (equal (stp:data a) (stp:data b))
902 (equal (stp:target a) (stp:target b))))
904 (defmethod node= ((a stp:document-type) (b stp:document-type))
905 (and (equal (stp:root-element-name a) (stp:root-element-name b))
906 (equal (stp:public-id a) (stp:public-id b))
907 (equal (stp:system-id a) (stp:system-id b))
908 (equal (stp:internal-subset a) (stp:internal-subset b))))
910 (xpath-sys:define-xpath-function/eager
911 xslt :print
912 (thing)
913 (if (xpath:node-set-p thing)
914 (loop
915 initially (format t ";;; node set:~%")
916 for i from 0
917 for node in (xpath:all-nodes thing)
919 (format t ";;; ~D: ~A~%" i (type-of node)))
920 (format t ";;; ~A~%" thing))
921 thing)