More test tweaks
[xuriella.git] / test.lisp
blob4c4d0b9382776b10e9f042ca507a2f2664ac10dc
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 '(;; Inconsistent tests:
299 ;; Some tests wants us to recover from this error, yet this one doesn't:
300 "copy_copy61"
301 "copy_copy62"
303 ;; Should we fix this?
305 ;; We signal a run-time error when and if it's actually used. The test
306 ;; wants a compilation-time error...
307 "AttributeSets_RefToUndefinedAttributeSet"
309 ;; We would pass this:
311 ;; We perform recovery, but saxon doesn't. Recovery results in non-XML
312 ;; output, which we can't parse for comparison against the official
313 ;; test case.
314 "output_output75"
316 ;; we'd pass these tests, but the test authors forgot to declare the
317 ;; entity they're writing, so we can't parse it for comparison.
318 "output_output06"
319 "output_output10"
320 "output_output61"
322 ;; another similar test where the output is unparsable, except that
323 ;; here an entity declaration wouldn't have helped either:
324 "Copying_ResultTreeFragmentWithEscapedText"
326 ;; Broken test:
328 ;; Input document isn't ns-wf.
329 "Attributes__78387"
331 ;; Someone commented out most of this test...
332 "BVTs_bvt045"
334 ;; FIXME: should re-enable these at some point:
336 ;; the following tests take a lot of time due to the problems of current matching algorithm:
337 "impincl_impincl16"
338 ;; probably the same problem (but I haven't checked):
339 "Import__91164"
341 ;; stack exhaustion -- matching problem i think
342 "Keys_PerfRepro3"))
344 ;; Tests where the output isn't a match because of extraneous whitespace.
345 ;; For these tests, we force space normalization before comparing.
347 ;; (SANITIZE-STYLESHEET is supposed to get rid of indent="yes", but it
348 ;; misses imported stylesheets.)
350 (defparameter *whitespace-issues*
351 '("BVTs_bvt044"
352 "Namespace-alias__91782"
353 "AttributeSets__91038"
354 "BVTs_bvt041"
355 "BVTs_bvt042"
356 "BVTs_bvt054"))
358 (defparameter *known-failures*
360 ;; uses EBCDIC-CP-IT (whatever that is), but Babel's only got EBCDIC-US.
361 ;; Doesn't actually test any differences between the two, so it's
362 ;; probably just there to annoy us.
363 "output_output22"
365 ;; uses KOI, which Babel doesn't support
366 "BVTs_bvt019"
368 ;; FIXME?
370 ;; This is an HTML output method issue. The spec says the HTML
371 ;; output method should output non-HTML elements using the XML output
372 ;; method. But it doesn't specify the behaviour for HTML elements with
373 ;; non-HTML attributes on them. We currently output them as HTML, and
374 ;; lose their namespaces. This test wants the attributes and their
375 ;; namespaces to survive.
376 "BVTs_bvt054"))
378 (defun run-tests (&key filter (directory *tests-directory*))
379 (when (typep filter '(or string cons))
380 (setf filter (cl-ppcre:create-scanner filter)))
381 (klacks:with-open-source
382 (source (klacks:make-tapping-source
383 (cxml:make-source (merge-pathnames "katalog.xml" directory))))
384 (let ((*default-pathname-defaults* (merge-pathnames directory)))
385 (map-tests #'run-test
386 source
387 :test (lambda (test)
388 (and (or (null filter)
389 (cl-ppcre:all-matches
390 filter
391 (format nil "~A/~A"
392 (test-category test)
393 (test-id test))))
394 (not (find (test-id test)
395 *bad-tests*
396 :test #'equal))))))))
398 (defun run-named-test (name &optional (d *tests-directory*))
399 (let ((*break-on-signals*
400 '(and error (not babel-encodings:character-encoding-error))))
401 (run-tests :filter (format nil "/~A$" name) :directory d)))
403 (defun copy-file (p q)
404 (with-open-file (in p :element-type '(unsigned-byte 8))
405 (with-open-file (out q
406 :element-type '(unsigned-byte 8)
407 :direction :output
408 :if-exists :rename-and-delete)
409 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
410 (loop for pos = (read-sequence buf in)
411 until (zerop pos)
412 do (write-sequence buf out :end pos))))))
414 (defun find-named-test (name &optional (d *tests-directory*))
415 (klacks:with-open-source
416 (source (klacks:make-tapping-source
417 (cxml:make-source (merge-pathnames "katalog.xml" d))))
418 (block nil
419 (map-tests (lambda (test)
420 (return test))
421 source
422 :test (lambda (test) (equal (test-id test) name))))))
424 (defun copy-test-files (name &optional (d *tests-directory*))
425 (let* ((test (find-named-test name d))
426 (*default-pathname-defaults* (merge-pathnames d))
427 (*break-on-signals*
428 '(and error (not babel-encodings:character-encoding-error)))
429 (target-dir (merge-pathnames "copied-test/"
430 (asdf:component-pathname
431 (asdf:find-system :xuriella))))
432 (xsl (merge-pathnames "test.xsl" target-dir))
433 (xml (merge-pathnames "test.xml" target-dir))
434 (txt (merge-pathnames "official-output.txt" target-dir))
435 (expected (merge-pathnames "expected.xml" target-dir))
436 (actual (merge-pathnames "actual.xml" target-dir)))
437 (ensure-directories-exist target-dir)
438 (copy-file (test-stylesheet-pathname test) xsl)
439 (copy-file (test-data-pathname test) xml)
440 (when (test-official-output-pathname test)
441 (copy-file (test-official-output-pathname test) txt))
442 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
443 (format t "Test data copied to:~% ~A~%~%" xml)
444 (when (test-official-output-pathname test)
445 (format t "Official output file:~% ~A~%~%" txt))
446 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
447 (namestring target-dir)
448 (enough-namestring xsl target-dir)
449 (enough-namestring xml target-dir)
450 (enough-namestring expected target-dir))
451 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
452 (namestring target-dir)
453 (enough-namestring xml target-dir)
454 (enough-namestring xsl target-dir)
455 (enough-namestring expected target-dir))
456 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
457 (namestring target-dir)
458 (enough-namestring xml target-dir)
459 (enough-namestring xsl target-dir)
460 (enough-namestring expected target-dir))
461 (format t "Run xuriella like this:~%")
462 `(apply-stylesheet ,xsl ,xml :output ,actual)))
464 (defun map-tests (run-test source &key (test (constantly t)))
465 (let ((total 0)
466 (pass 0)
467 (known 0))
468 (loop
469 while (klacks:find-event source :start-element)
470 for lname = (klacks:current-lname source)
472 (cond
473 ((equal lname "test-case")
474 (let* ((<test-case>
475 (stp:document-element
476 (klacks:serialize-element source (stp:make-builder))))
477 (test-case (parse-test <test-case>)))
478 (when (funcall test test-case)
479 (incf total)
480 (ecase (funcall run-test test-case)
481 ((nil))
482 ((t)
483 (incf pass))
484 (:known-failure
485 (incf known))))))
487 (klacks:skip source :start-element))))
488 (format t "~&Passed ~D/~D tests (~D expected failures).~%" pass total known)
489 (format t "~&Unexpected failures: ~D.~%" (- total pass known))))
491 (defun parse-test (<test-case>)
492 (stp:with-attributes (id category operation
493 data stylesheet data-2 stylesheet-2
494 output compare)
495 <test-case>
496 (make-instance 'test-case
497 :id id
498 :category category
499 :operation operation
500 :data-pathname data
501 :stylesheet-pathname stylesheet
502 :data-pathname-2 data-2
503 :stylesheet-pathname-2 stylesheet-2
504 :output-pathname output
505 :output-compare compare)))
507 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
508 ;; beginning, if any.
509 (defun slurp-for-comparison (p)
510 (with-open-file (s p :element-type '(unsigned-byte 8))
511 (unless (and (eql (read-byte s nil) #xef)
512 (eql (read-byte s nil) #xbb)
513 (eql (read-byte s nil) #xbf))
514 (file-position s 0))
515 (if (plusp (file-length s))
516 (slurp-for-comparison-1 p s t)
517 "<wrapper/>")))
519 (defun slurp-for-comparison-1 (p s junk-info)
520 (let ((pos (file-position s)) ;for UTF-8 "BOM"
521 (xstream (runes:make-xstream s :speed 1))
522 (prev-pos 0))
523 (setf (runes:xstream-name xstream)
524 (cxml::make-stream-name
525 :entity-name "main document"
526 :entity-kind :main
527 :uri (cxml::pathname-to-uri (merge-pathnames p))))
528 (let ((source
529 (flet ((er (pub sys)
530 pub sys
531 (flexi-streams:make-in-memory-input-stream
532 #())))
533 (cxml:make-source xstream
534 :pathname p
535 :entity-resolver #'er))))
536 (unless (eq junk-info :nada)
537 (loop
538 for key = (progn
539 (setf prev-pos (runes:xstream-position xstream))
540 (klacks:peek-next source))
541 until (eq key :start-document))
542 (cxml::with-source (source cxml::context)
543 (when (eq (cxml::zstream-token-category
544 (cxml::main-zstream cxml::context))
545 :NMTOKEN)
546 ;; oops, doesn't look like XML at all
547 (file-position s pos)
548 (return-from slurp-for-comparison-1
549 (slurp-for-comparison-1 p s :nada)))))
550 (etypecase junk-info
551 (integer
552 (dotimes (x junk-info)
553 (setf prev-pos (runes:xstream-position xstream))
554 (klacks:peek-next source)))
555 ((eql t)
556 (let ((nskip 0))
557 (handler-case
558 (loop
559 (case (klacks:peek-next source)
560 (:start-element (return))
561 (:characters
562 (if (whitespacep (klacks:current-characters source))
563 (incf nskip)
564 (return)))
566 (incf nskip))))
567 ((or file-error cxml:xml-parse-error) ()
568 (when (zerop nskip)
569 (setf nskip nil))))
570 ;; retry
571 (with-open-file (u p :element-type '(unsigned-byte 8))
572 (file-position u pos)
573 (return-from slurp-for-comparison-1
574 (slurp-for-comparison-1 p u nskip)))))
575 ((member nil :nada)))
576 (with-output-to-string (r)
577 (let* ((seen-char
578 (cxml::with-source (source cxml::context)
579 (ecase (cxml::zstream-token-category
580 (cxml::main-zstream cxml::context))
581 (:seen-< #\<)
582 (:? #\?)
583 ((nil :s)
584 (setf prev-pos (runes:xstream-position xstream))
585 nil))))
586 (off-by-one-p (or seen-char (eq junk-info :nada)))
587 (new-pos (- prev-pos (if off-by-one-p 1 0))))
588 ;; copy doctype over
589 (with-open-file (u p :element-type '(unsigned-byte 8))
590 (file-position u pos)
591 (let ((y (runes:make-xstream u :speed 1)))
592 (loop
593 while (< (runes:xstream-position y) new-pos)
594 do (write-char (runes:read-rune y) r))))
595 (write-line "<wrapper>" r)
596 (when seen-char
597 (write-char seen-char r)))
598 (loop
599 for char = (runes:read-rune xstream)
600 until (eq char :eof)
601 do (write-char char r))
602 (write-line "</wrapper>" r)))))
604 (defun parse-for-comparison (p)
605 (let* ((d (flet ((er (pub sys)
606 pub sys
607 (flexi-streams:make-in-memory-input-stream
608 #())))
609 (cxml:parse (slurp-for-comparison p)
610 (make-text-normalizer (stp:make-builder))
611 :entity-resolver #'er)))
612 (de (stp:document-element d)))
613 (let ((first (stp:first-child de)))
614 (when (typep first 'stp:text)
615 (cond
616 ((whitespacep (stp:data first))
617 (stp:delete-child first de))
619 (setf (stp:data first)
620 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
621 (stp:data first)
622 ""))))))
623 (let ((last (stp:last-child de)))
624 (when (typep last 'stp:text)
625 (cond
626 ((whitespacep (stp:data last))
627 (stp:delete-child last de))
629 (setf (stp:data last)
630 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
631 (stp:data last)
632 ""))))))
635 (defun output-equal-p (compare p q &key normalize)
636 (handler-case
637 (ecase compare
638 (:xml (xml-output-equal-p p q normalize))
639 (:html (html-output-equal-p p q))
640 (:text (text-output-equal-p p q)))
641 ((or error parse-number::invalid-number) (c)
642 (warn "comparison failed: ~A" c)
643 nil)))
645 ;; Workaround for namespace_namespace23 and other tests:
646 ;; - For these tests, saxon and msxsl output a declaration for the XSL
647 ;; namespace without using that declaration.
648 ;; - I think saxon and msxsl are both wrong.
649 ;; - The official test output agrees with my assessment.
650 ;; (So does libxslt, but that's not to be trusted. :-))
651 ;; - Here's the catch: The official test output is broken in its whitespace
652 ;; handling.
653 ;; So let's normalize spaces in test output that looks like an XSLT
654 ;; stylesheet, allowing us to pass these tests using the official test output.
655 (defun maybe-normalize-test-spaces (wrapper force)
656 (let ((i 0))
657 (loop while (< i (length (cxml-stp-impl::%children wrapper))) do
658 (let ((wrapper-child (stp:nth-child i wrapper)))
659 (cond
660 ((not (typep wrapper-child 'stp:element))
661 (if force
662 (stp:delete-nth-child i wrapper)
663 (incf i)))
664 ((or (equal (stp:namespace-uri wrapper-child) *xsl*)
665 force)
666 (strip-stylesheet wrapper-child)
667 (labels ((recurse (e &optional preserve)
668 (stp:do-children (child e)
669 (typecase child
670 (stp:text
671 (setf (stp:data child)
672 (normalize-whitespace (stp:data child))))
673 (stp:element
674 (stp:with-attributes ((space "space" *xml*))
675 child
676 (let ((new-preserve
677 (cond
678 ((namep child "text") t)
679 ((not space) preserve)
680 ((equal space "preserve") t)
681 (t nil))))
682 (recurse child new-preserve))))))))
683 (recurse wrapper-child))
684 (incf i))
686 (incf i)))))))
688 (defun xml-output-equal-p (p q normalize)
689 (let ((r (parse-for-comparison p))
690 (s (parse-for-comparison q)))
691 (maybe-normalize-test-spaces (stp:document-element r) normalize)
692 (maybe-normalize-test-spaces (stp:document-element s) normalize)
693 (and (let ((u (stp:document-type r))
694 (v (stp:document-type s)))
695 (if u
696 (and v (node= u v))
697 (null v)))
698 (node= (stp:document-element r) (stp:document-element s)))))
700 ;; FIXME: don't do this in <pre> etc.
701 (defun normalize-html-whitespace (node)
702 (when (typep node 'stp:parent-node)
703 ;; ignore newlines after start tags completely
704 (let ((first (stp:first-child node)))
705 (when (and (typep first 'stp:text)
706 (alexandria:starts-with #\newline (stp:data first)))
707 (setf (stp:data first) (subseq (stp:data first) 1))))
708 ;; ignore newlines before end tags completely
709 (let ((last (stp:last-child node)))
710 (when (and (typep last 'stp:text)
711 (alexandria:ends-with #\newline (stp:data last)))
712 (setf (stp:data last)
713 (subseq (stp:data last) 0 (length (stp:data last))))))
714 ;; normalize sequences of whitespace
715 (stp:do-children (child node)
716 (if (typep child 'stp:text)
717 (setf (stp:data child)
718 (let ((str (normalize-whitespace (stp:data child))))
719 (when
720 ;; FIXME! Here we remove whitespace entirely.
721 ;; Totally incorrect, but I don't see how we could
722 ;; watch Saxon's output otherwise.
723 (equal str " ")
724 (setf str ""))
725 str))
726 (normalize-html-whitespace child)))
727 ;; just to be sure, join adjacent nodes
728 (cxml-stp-impl::normalize-text-nodes! node)))
730 ;; FIXME: this check is too lenient, because chtml is an error-correcting
731 ;; parser.
732 (defun html-output-equal-p (p q)
733 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
734 (s (chtml:parse (pathname q) (stp:make-builder))))
735 (normalize-html-whitespace r)
736 (normalize-html-whitespace s)
737 (flet ((fix-case (node)
738 (xpath:with-namespaces (("xhtml" "http://www.w3.org/1999/xhtml"))
739 (xpath:do-node-set
740 (content (xpath:evaluate "//xhtml:meta/@content" node))
741 (setf (stp:value content)
742 (string-downcase (stp:value content)))))))
743 (fix-case r)
744 (fix-case s))
745 (node= (stp:document-element r) (stp:document-element s))))
747 (defun text-output-equal-p (p q)
748 (with-open-file (a p :element-type '(unsigned-byte 8))
749 (with-open-file (b q :element-type '(unsigned-byte 8))
750 (let ((len (file-length a)))
751 (and (eql len (file-length b))
752 (let ((d (make-array len :element-type '(unsigned-byte 8)))
753 (e (make-array len :element-type '(unsigned-byte 8))))
754 (read-sequence d a)
755 (read-sequence e b)
756 (equalp d e)))))))
758 (defun strip-addresses (str)
759 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
761 (defun slurp-output-method (p)
762 (xpath:with-namespaces ((nil #.*xsl*))
763 (let* ((d (handler-bind
764 ((warning #'muffle-warning))
765 (cxml:parse (pathname p) (stp:make-builder))))
766 (output (xpath:first-node (xpath:evaluate "//output" d))))
767 (if output
768 (let ((method (stp:attribute-value output "method")))
769 (if method
770 (intern (string-upcase method) :keyword)
771 :xml))
772 :xml))))
774 (defun replace-junk (str)
775 (cl-ppcre:regex-replace-all
776 `(:group ,(namestring *tests-directory*))
777 (map 'string
778 (lambda (c)
779 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
781 #\?))
782 str)
783 "..."))
785 (defun run-test (test)
786 (let ((expected-saxon (test-output-pathname test "saxon"))
787 #+xuriella::xsltproc
788 (expected-xsltproc (test-output-pathname test "xsltproc"))
789 (actual (test-output-pathname test "xuriella"))
790 (official (test-official-output-pathname test))
791 (force-normalization
792 (find (test-id test) *whitespace-issues* :test #'equal))
793 (output-method nil))
794 (handler-bind ((|hey test suite, this is an HTML document|
795 (lambda (c)
796 (declare (ignore c))
797 (setf output-method :html))))
798 (labels ((uri-resolver (uri)
799 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
800 uri)
801 (cxml::pathname-to-uri
802 (merge-pathnames
803 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
804 *tests-directory*))
805 uri))
806 (doit ()
807 (with-open-file (s actual
808 :if-exists :rename-and-delete
809 :direction :output
810 :element-type '(unsigned-byte 8))
811 (handler-bind ((xslt-error
812 (lambda (c)
813 (declare (ignore c))
814 (when (find-restart 'recover)
815 (invoke-restart 'recover)))))
816 (apply-stylesheet (pathname (test-stylesheet-pathname test))
817 (pathname (test-data-pathname test))
818 :output s
819 :uri-resolver #'uri-resolver))))
820 (pp (label pathname)
821 (when pathname
822 (format t " ~A: ~A~%"
823 label
824 (enough-namestring pathname *tests-directory*))))
825 (report (ok &optional (fmt "") &rest args)
826 (write-string
827 (replace-junk
828 (strip-addresses
829 (format nil "~&~A ~A [~A]~?~%"
830 (cond
831 (ok "PASS")
832 ((find (test-id test)
833 *known-failures*
834 :test #'equal)
835 (setf ok :known-failure)
836 "KNOWNFAIL")
838 "FAIL"))
839 (test-id test)
840 (test-category test)
842 args))))
843 (pp "Stylesheet" (test-stylesheet-pathname test))
844 (pp "Data" (test-data-pathname test))
845 (pp "Supplemental stylesheet"
846 (test-stylesheet-pathname-2 test))
847 (pp "Supplemental data" (test-data-pathname-2 test))
848 (pp "Expected output (1)" expected-saxon)
849 #+xuriella::xsltproc
850 (pp "Expected output (2)" expected-xsltproc)
851 (pp "Actual output" actual)
852 (terpri)
853 ok))
854 (cond
855 ((equal (test-operation test) "standard")
856 (handler-case
857 (progn
858 (when (find (test-id test)
859 nil ;;'("axes_axes47" "attribset_attribset20")
860 :test #'equal)
861 (error "skipping problematic test"))
862 (doit)
863 (let* ((output-method
864 (or output-method
865 (slurp-output-method
866 (test-stylesheet-pathname test))))
867 (saxon-matches-p
868 (output-equal-p output-method
869 expected-saxon
870 actual
871 :normalize force-normalization))
872 #+xuriella::xsltproc
873 (xsltproc-matches-p
874 (output-equal-p output-method
875 expected-xsltproc
876 actual))
877 (official-matches-p
878 (output-equal-p output-method
879 official
880 actual
881 :normalize force-normalization)))
882 (cond
883 ((or saxon-matches-p
884 #+xuriella::xsltproc xsltproc-matches-p
885 official-matches-p)
886 (report t)
887 #+xuriella::xsltproc
888 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
889 saxon-matches-p
890 xsltproc-matches-p
891 (if saxon-matches-p
892 (not xsltproc-matches-p)
893 xsltproc-matches-p)))
895 (report nil ": output doesn't match")))))
896 ((or error parse-number::invalid-number) (c)
897 (report nil ": ~A" c))))
899 (handler-case
900 (doit)
901 (xslt-error (c)
902 (report t ": raised an xslt-error as expected" c))
903 ((or error parse-number::invalid-number) (c)
904 (report nil ": condition of incorrect type: ~%~A" c))
905 (:no-error (result)
906 (cond
907 ((not (and official (probe-file official)))
908 (report nil ": expected error not signalled: " result))
909 ((output-equal-p
910 (or output-method
911 (slurp-output-method (test-stylesheet-pathname test)))
912 official
913 actual
914 :normalize force-normalization)
915 (report t))
917 (report nil ": saxon error not signalled and official output not a match")))))))))))
919 (defun run-xpath-tests ()
920 (run-tests :filter "XPath-Expression/|XSLT-Data-Model/"))
923 ;;;; from cxml-stp-test
925 (defun assert-node= (a b)
926 (unless (node= a b)
927 (error "assertion failed: ~S and ~S are not NODE=" a b)))
929 (defun child-count (node)
930 (stp:count-children-if (constantly t) node))
932 (defun named-node-= (a b)
933 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
934 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
935 (equal (stp:local-name a) (stp:local-name b))))
937 (defun parent-node-= (e f)
938 (and (eql (child-count e)
939 (child-count f))
940 (every #'node= (stp:list-children e) (stp:list-children f))))
942 (defmethod node= ((e stp:element) (f stp:element))
943 (and (named-node-= e f)
944 (parent-node-= e f)
945 (null
946 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
947 :test #'node=))
948 (block nil
949 (flet ((check-namespaces (a b)
950 (let ((result ()))
951 (stp:map-extra-namespaces
952 (lambda (k v)
953 (unless (equal v (stp:find-namespace k b))
954 (return nil)))
956 result)))
957 (check-namespaces e f)
958 (check-namespaces f e))
959 t)))
961 (defmethod node= ((a stp:node) (b stp:node))
962 nil)
964 (defmethod node= ((e stp:document) (f stp:document))
965 (parent-node-= e f))
967 (defmethod node= ((a stp:attribute) (b stp:attribute))
968 (and (named-node-= a b)
969 (equal (stp:value a) (stp:value b))))
971 (defmethod node= ((a stp:comment) (b stp:comment))
972 (equal (stp:data a) (stp:data b)))
974 (defmethod node= ((a stp:text) (b stp:text))
975 (equal (stp:data a) (stp:data b)))
977 (defmethod node= ((a stp:processing-instruction)
978 (b stp:processing-instruction))
979 (and (equal (stp:data a) (stp:data b))
980 (equal (stp:target a) (stp:target b))))
982 (defmethod node= ((a stp:document-type) (b stp:document-type))
983 (and (equal (stp:root-element-name a) (stp:root-element-name b))
984 (equal (stp:public-id a) (stp:public-id b))
985 (equal (stp:system-id a) (stp:system-id b))
986 (equal (stp:internal-subset a) (stp:internal-subset b))))
988 (xpath-sys:define-xpath-function/eager
989 xslt :print
990 (thing)
991 (if (xpath:node-set-p thing)
992 (loop
993 initially (format t ";;; node set:~%")
994 for i from 0
995 for node in (xpath:all-nodes thing)
997 (format t ";;; ~D: ~A~%" i (type-of node)))
998 (format t ";;; ~A~%" thing))
999 thing)