Re-enabled some tests
[xuriella.git] / test.lisp
blob01456dca82ed6aac8d8b759dd0cee3617ad8c8e3
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 ;; FIXME: should re-enable these at some point:
333 ;; the following tests take a lot of time due to the problems of current matching algorithm:
334 "impincl_impincl16"
335 ;; probably the same problem (but I haven't checked):
336 "Import__91164"
338 ;; stack exhaustion -- matching problem i think
339 "Keys_PerfRepro3"))
341 ;; Tests where the output isn't a match because of extraneous whitespace.
342 ;; For these tests, we force space normalization before comparing.
344 ;; (SANITIZE-STYLESHEET is supposed to get rid of indent="yes", but it
345 ;; misses imported stylesheets.)
347 ;; FIXME: Perhaps some *bad-tests* could instead be *whitespace-issues*,
348 ;; at least those where the official output wuold be a match.
350 (defparameter *whitespace-issues*
351 '("BVTs_bvt044"
352 "Namespace-alias__91782"
353 "AttributeSets__91038"))
355 (defun run-tests (&key filter (directory *tests-directory*))
356 (when (typep filter '(or string cons))
357 (setf filter (cl-ppcre:create-scanner filter)))
358 (klacks:with-open-source
359 (source (klacks:make-tapping-source
360 (cxml:make-source (merge-pathnames "katalog.xml" directory))))
361 (let ((*default-pathname-defaults* (merge-pathnames directory)))
362 (map-tests #'run-test
363 source
364 :test (lambda (test)
365 (and (or (null filter)
366 (cl-ppcre:all-matches
367 filter
368 (format nil "~A/~A"
369 (test-category test)
370 (test-id test))))
371 (not (find (test-id test)
372 *bad-tests*
373 :test #'equal))))))))
375 (defun run-named-test (name &optional (d *tests-directory*))
376 (let ((*break-on-signals* 'error))
377 (run-tests :filter (format nil "/~A$" name) :directory d)))
379 (defun copy-file (p q)
380 (with-open-file (in p :element-type '(unsigned-byte 8))
381 (with-open-file (out q
382 :element-type '(unsigned-byte 8)
383 :direction :output
384 :if-exists :rename-and-delete)
385 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
386 (loop for pos = (read-sequence buf in)
387 until (zerop pos)
388 do (write-sequence buf out :end pos))))))
390 (defun find-named-test (name &optional (d *tests-directory*))
391 (klacks:with-open-source
392 (source (klacks:make-tapping-source
393 (cxml:make-source (merge-pathnames "katalog.xml" d))))
394 (block nil
395 (map-tests (lambda (test)
396 (return test))
397 source
398 :test (lambda (test) (equal (test-id test) name))))))
400 (defun copy-test-files (name &optional (d *tests-directory*))
401 (let* ((test (find-named-test name d))
402 (*default-pathname-defaults* (merge-pathnames d))
403 (*break-on-signals* 'error)
404 (target-dir (merge-pathnames "copied-test/"
405 (asdf:component-pathname
406 (asdf:find-system :xuriella))))
407 (xsl (merge-pathnames "test.xsl" target-dir))
408 (xml (merge-pathnames "test.xml" target-dir))
409 (txt (merge-pathnames "official-output.txt" target-dir))
410 (expected (merge-pathnames "expected.xml" target-dir))
411 (actual (merge-pathnames "actual.xml" target-dir)))
412 (ensure-directories-exist target-dir)
413 (copy-file (test-stylesheet-pathname test) xsl)
414 (copy-file (test-data-pathname test) xml)
415 (when (test-official-output-pathname test)
416 (copy-file (test-official-output-pathname test) txt))
417 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
418 (format t "Test data copied to:~% ~A~%~%" xml)
419 (when (test-official-output-pathname test)
420 (format t "Official output file:~% ~A~%~%" txt))
421 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
422 (namestring target-dir)
423 (enough-namestring xsl target-dir)
424 (enough-namestring xml target-dir)
425 (enough-namestring expected target-dir))
426 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
427 (namestring target-dir)
428 (enough-namestring xml target-dir)
429 (enough-namestring xsl target-dir)
430 (enough-namestring expected target-dir))
431 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
432 (namestring target-dir)
433 (enough-namestring xml target-dir)
434 (enough-namestring xsl target-dir)
435 (enough-namestring expected target-dir))
436 (format t "Run xuriella like this:~%")
437 `(apply-stylesheet ,xsl ,xml :output ,actual)))
439 (defun map-tests (run-test source &key (test (constantly t)))
440 (let ((total 0)
441 (pass 0))
442 (loop
443 while (klacks:find-event source :start-element)
444 for lname = (klacks:current-lname source)
446 (cond
447 ((equal lname "test-case")
448 (let* ((<test-case>
449 (stp:document-element
450 (klacks:serialize-element source (stp:make-builder))))
451 (test-case (parse-test <test-case>)))
452 (when (funcall test test-case)
453 (incf total)
454 (when (funcall run-test test-case)
455 (incf pass)))))
457 (klacks:skip source :start-element))))
458 (format t "~&Passed ~D/~D tests.~%" pass total)))
460 (defun parse-test (<test-case>)
461 (stp:with-attributes (id category operation
462 data stylesheet data-2 stylesheet-2
463 output compare)
464 <test-case>
465 (make-instance 'test-case
466 :id id
467 :category category
468 :operation operation
469 :data-pathname data
470 :stylesheet-pathname stylesheet
471 :data-pathname-2 data-2
472 :stylesheet-pathname-2 stylesheet-2
473 :output-pathname output
474 :output-compare compare)))
476 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
477 ;; beginning, if any.
478 (defun slurp-for-comparison (p)
479 (with-open-file (s p :element-type '(unsigned-byte 8))
480 (unless (and (eql (read-byte s nil) #xef)
481 (eql (read-byte s nil) #xbb)
482 (eql (read-byte s nil) #xbf))
483 (file-position s 0))
484 (if (plusp (file-length s))
485 (slurp-for-comparison-1 p s t)
486 "<wrapper/>")))
488 (defun slurp-for-comparison-1 (p s junk-info)
489 (let ((pos (file-position s)) ;for UTF-8 "BOM"
490 (xstream (runes:make-xstream s :speed 1))
491 (prev-pos 0))
492 (setf (runes:xstream-name xstream)
493 (cxml::make-stream-name
494 :entity-name "main document"
495 :entity-kind :main
496 :uri (cxml::pathname-to-uri (merge-pathnames p))))
497 (let ((source
498 (flet ((er (pub sys)
499 pub sys
500 (flexi-streams:make-in-memory-input-stream
501 #())))
502 (cxml:make-source xstream
503 :pathname p
504 :entity-resolver #'er))))
505 (unless (eq junk-info :nada)
506 (loop
507 for key = (progn
508 (setf prev-pos (runes:xstream-position xstream))
509 (klacks:peek-next source))
510 until (eq key :start-document))
511 (cxml::with-source (source cxml::context)
512 (when (eq (cxml::zstream-token-category
513 (cxml::main-zstream cxml::context))
514 :NMTOKEN)
515 ;; oops, doesn't look like XML at all
516 (file-position s pos)
517 (return-from slurp-for-comparison-1
518 (slurp-for-comparison-1 p s :nada)))))
519 (etypecase junk-info
520 (integer
521 (dotimes (x junk-info)
522 (setf prev-pos (runes:xstream-position xstream))
523 (klacks:peek-next source)))
524 ((eql t)
525 (let ((nskip 0))
526 (handler-case
527 (loop
528 (case (klacks:peek-next source)
529 (:start-element (return))
530 (:characters
531 (if (whitespacep (klacks:current-characters source))
532 (incf nskip)
533 (return)))
535 (incf nskip))))
536 ((or file-error cxml:xml-parse-error) ()
537 (when (zerop nskip)
538 (setf nskip nil))))
539 ;; retry
540 (with-open-file (u p :element-type '(unsigned-byte 8))
541 (file-position u pos)
542 (return-from slurp-for-comparison-1
543 (slurp-for-comparison-1 p u nskip)))))
544 ((member nil :nada)))
545 (with-output-to-string (r)
546 (let* ((seen-char
547 (cxml::with-source (source cxml::context)
548 (ecase (cxml::zstream-token-category
549 (cxml::main-zstream cxml::context))
550 (:seen-< #\<)
551 (:? #\?)
552 ((nil :s)
553 (setf prev-pos (runes:xstream-position xstream))
554 nil))))
555 (off-by-one-p (or seen-char (eq junk-info :nada)))
556 (new-pos (- prev-pos (if off-by-one-p 1 0))))
557 ;; copy doctype over
558 (with-open-file (u p :element-type '(unsigned-byte 8))
559 (file-position u pos)
560 (let ((y (runes:make-xstream u :speed 1)))
561 (loop
562 while (< (runes:xstream-position y) new-pos)
563 do (write-char (runes:read-rune y) r))))
564 (write-line "<wrapper>" r)
565 (when seen-char
566 (write-char seen-char r)))
567 (loop
568 for char = (runes:read-rune xstream)
569 until (eq char :eof)
570 do (write-char char r))
571 (write-line "</wrapper>" r)))))
573 (defun parse-for-comparison (p)
574 (let* ((d (flet ((er (pub sys)
575 pub sys
576 (flexi-streams:make-in-memory-input-stream
577 #())))
578 (cxml:parse (slurp-for-comparison p)
579 (make-text-normalizer (stp:make-builder))
580 :entity-resolver #'er)))
581 (de (stp:document-element d)))
582 (let ((first (stp:first-child de)))
583 (when (typep first 'stp:text)
584 (cond
585 ((whitespacep (stp:data first))
586 (stp:delete-child first de))
588 (setf (stp:data first)
589 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
590 (stp:data first)
591 ""))))))
592 (let ((last (stp:last-child de)))
593 (when (typep last 'stp:text)
594 (cond
595 ((whitespacep (stp:data last))
596 (stp:delete-child last de))
598 (setf (stp:data last)
599 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
600 (stp:data last)
601 ""))))))
604 (defun output-equal-p (compare p q &key normalize)
605 (handler-case
606 (ecase compare
607 (:xml (xml-output-equal-p p q normalize))
608 (:html (html-output-equal-p p q))
609 (:text (text-output-equal-p p q)))
610 ((or error parse-number::invalid-number) (c)
611 (warn "comparison failed: ~A" c)
612 nil)))
614 ;; Workaround for namespace_namespace23 and other tests:
615 ;; - For these tests, saxon and msxsl output a declaration for the XSL
616 ;; namespace without using that declaration.
617 ;; - I think saxon and msxsl are both wrong.
618 ;; - The official test output agrees with my assessment.
619 ;; (So does libxslt, but that's not to be trusted. :-))
620 ;; - Here's the catch: The official test output is broken in its whitespace
621 ;; handling.
622 ;; So let's normalize spaces in test output that looks like an XSLT
623 ;; stylesheet, allowing us to pass these tests using the official test output.
624 (defun maybe-normalize-test-spaces (wrapper force)
625 (let ((i 0))
626 (loop while (< i (length (cxml-stp-impl::%children wrapper))) do
627 (let ((wrapper-child (stp:nth-child i wrapper)))
628 (cond
629 ((not (typep wrapper-child 'stp:element))
630 (if force
631 (stp:delete-nth-child i wrapper)
632 (incf i)))
633 ((or (equal (stp:namespace-uri wrapper-child) *xsl*)
634 force)
635 (strip-stylesheet wrapper-child)
636 (labels ((recurse (e &optional preserve)
637 (stp:do-children (child e)
638 (typecase child
639 (stp:text
640 (setf (stp:data child)
641 (normalize-whitespace (stp:data child))))
642 (stp:element
643 (stp:with-attributes ((space "space" *xml*))
644 child
645 (let ((new-preserve
646 (cond
647 ((namep child "text") t)
648 ((not space) preserve)
649 ((equal space "preserve") t)
650 (t nil))))
651 (recurse child new-preserve))))))))
652 (recurse wrapper-child))
653 (incf i))
655 (incf i)))))))
657 (defun xml-output-equal-p (p q normalize)
658 (let ((r (parse-for-comparison p))
659 (s (parse-for-comparison q)))
660 (maybe-normalize-test-spaces (stp:document-element r) normalize)
661 (maybe-normalize-test-spaces (stp:document-element s) normalize)
662 (and (let ((u (stp:document-type r))
663 (v (stp:document-type s)))
664 (if u
665 (and v (node= u v))
666 (null v)))
667 (node= (stp:document-element r) (stp:document-element s)))))
669 ;; FIXME: don't do this in <pre> etc.
670 (defun normalize-html-whitespace (node)
671 (when (typep node 'stp:parent-node)
672 ;; ignore newlines after start tags completely
673 (let ((first (stp:first-child node)))
674 (when (and (typep first 'stp:text)
675 (alexandria:starts-with #\newline (stp:data first)))
676 (setf (stp:data first) (subseq (stp:data first) 1))))
677 ;; ignore newlines before end tags completely
678 (let ((last (stp:last-child node)))
679 (when (and (typep last 'stp:text)
680 (alexandria:ends-with #\newline (stp:data last)))
681 (setf (stp:data last)
682 (subseq (stp:data last) 0 (length (stp:data last))))))
683 ;; normalize sequences of whitespace
684 (stp:do-children (child node)
685 (if (typep child 'stp:text)
686 (setf (stp:data child)
687 (let ((str (normalize-whitespace (stp:data child))))
688 (when
689 ;; FIXME! Here we remove whitespace entirely.
690 ;; Totally incorrect, but I don't see how we could
691 ;; watch Saxon's output otherwise.
692 (equal str " ")
693 (setf str ""))
694 str))
695 (normalize-html-whitespace child)))
696 ;; just to be sure, join adjacent nodes
697 (cxml-stp-impl::normalize-text-nodes! node)))
699 ;; FIXME: this check is too lenient, because chtml is an error-correcting
700 ;; parser.
701 (defun html-output-equal-p (p q)
702 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
703 (s (chtml:parse (pathname q) (stp:make-builder))))
704 (normalize-html-whitespace r)
705 (normalize-html-whitespace s)
706 (node= (stp:document-element r) (stp:document-element s))))
708 (defun text-output-equal-p (p q)
709 (with-open-file (a p :element-type '(unsigned-byte 8))
710 (with-open-file (b q :element-type '(unsigned-byte 8))
711 (let ((len (file-length a)))
712 (and (eql len (file-length b))
713 (let ((d (make-array len :element-type '(unsigned-byte 8)))
714 (e (make-array len :element-type '(unsigned-byte 8))))
715 (read-sequence d a)
716 (read-sequence e b)
717 (equalp d e)))))))
719 (defun strip-addresses (str)
720 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
722 (defun slurp-output-method (p)
723 (xpath:with-namespaces ((nil #.*xsl*))
724 (let* ((d (handler-bind
725 ((warning #'muffle-warning))
726 (cxml:parse (pathname p) (stp:make-builder))))
727 (output (xpath:first-node (xpath:evaluate "//output" d))))
728 (if output
729 (let ((method (stp:attribute-value output "method")))
730 (if method
731 (intern (string-upcase method) :keyword)
732 :xml))
733 :xml))))
735 (defun replace-junk (str)
736 (cl-ppcre:regex-replace-all
737 `(:group ,(namestring *tests-directory*))
738 (map 'string
739 (lambda (c)
740 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
742 #\?))
743 str)
744 "..."))
746 (defun run-test (test)
747 (let ((expected-saxon (test-output-pathname test "saxon"))
748 #+xuriella::xsltproc
749 (expected-xsltproc (test-output-pathname test "xsltproc"))
750 (actual (test-output-pathname test "xuriella"))
751 (official (test-official-output-pathname test))
752 (force-normalization
753 (find (test-id test) *whitespace-issues* :test #'equal))
754 (output-method nil))
755 (handler-bind ((|hey test suite, this is an HTML document|
756 (lambda (c)
757 (declare (ignore c))
758 (setf output-method :html))))
759 (labels ((uri-resolver (uri)
760 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
761 uri)
762 (cxml::pathname-to-uri
763 (merge-pathnames
764 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
765 *tests-directory*))
766 uri))
767 (doit ()
768 (with-open-file (s actual
769 :if-exists :rename-and-delete
770 :direction :output
771 :element-type '(unsigned-byte 8))
772 (handler-bind ((xslt-error
773 (lambda (c)
774 (declare (ignore c))
775 (when (find-restart 'recover)
776 (invoke-restart 'recover)))))
777 (apply-stylesheet (pathname (test-stylesheet-pathname test))
778 (pathname (test-data-pathname test))
779 :output s
780 :uri-resolver #'uri-resolver))))
781 (pp (label pathname)
782 (when pathname
783 (format t " ~A: ~A~%"
784 label
785 (enough-namestring pathname *tests-directory*))))
786 (report (ok &optional (fmt "") &rest args)
787 (write-string
788 (replace-junk
789 (strip-addresses
790 (format nil "~&~:[FAIL~;PASS~] ~A [~A]~?~%"
792 (test-id test)
793 (test-category test)
795 args))))
796 (pp "Stylesheet" (test-stylesheet-pathname test))
797 (pp "Data" (test-data-pathname test))
798 (pp "Supplemental stylesheet"
799 (test-stylesheet-pathname-2 test))
800 (pp "Supplemental data" (test-data-pathname-2 test))
801 (pp "Expected output (1)" expected-saxon)
802 #+xuriella::xsltproc
803 (pp "Expected output (2)" expected-xsltproc)
804 (pp "Actual output" actual)
805 (terpri)
806 ok))
807 (cond
808 ((equal (test-operation test) "standard")
809 (handler-case
810 (progn
811 (when (find (test-id test)
812 nil ;;'("axes_axes47" "attribset_attribset20")
813 :test #'equal)
814 (error "skipping problematic test"))
815 (doit)
816 (let* ((output-method
817 (or output-method
818 (slurp-output-method
819 (test-stylesheet-pathname test))))
820 (saxon-matches-p
821 (output-equal-p output-method
822 expected-saxon
823 actual
824 :normalize force-normalization))
825 #+xuriella::xsltproc
826 (xsltproc-matches-p
827 (output-equal-p output-method
828 expected-xsltproc
829 actual))
830 (official-matches-p
831 (output-equal-p output-method
832 official
833 actual
834 :normalize force-normalization)))
835 (cond
836 ((or saxon-matches-p
837 #+xuriella::xsltproc xsltproc-matches-p
838 official-matches-p)
839 (report t)
840 #+xuriella::xsltproc
841 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
842 saxon-matches-p
843 xsltproc-matches-p
844 (if saxon-matches-p
845 (not xsltproc-matches-p)
846 xsltproc-matches-p)))
848 (report nil ": output doesn't match")))))
849 ((or error parse-number::invalid-number) (c)
850 (report nil ": ~A" c))))
852 (handler-case
853 (doit)
854 (xslt-error (c)
855 (report t ": raised an xslt-error as expected" c))
856 ((or error parse-number::invalid-number) (c)
857 (report nil ": condition of incorrect type: ~%~A" c))
858 (:no-error (result)
859 (cond
860 ((not (and official (probe-file official)))
861 (report nil ": expected error not signalled: " result))
862 ((output-equal-p
863 (or output-method
864 (slurp-output-method (test-stylesheet-pathname test)))
865 official
866 actual
867 :normalize force-normalization)
868 (report t))
870 (report nil ": saxon error not signalled and official output not a match")))))))))))
872 (defun run-xpath-tests ()
873 (run-tests :filter "XPath-Expression/|XSLT-Data-Model/"))
876 ;;;; from cxml-stp-test
878 (defun assert-node= (a b)
879 (unless (node= a b)
880 (error "assertion failed: ~S and ~S are not NODE=" a b)))
882 (defun child-count (node)
883 (stp:count-children-if (constantly t) node))
885 (defun named-node-= (a b)
886 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
887 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
888 (equal (stp:local-name a) (stp:local-name b))))
890 (defun parent-node-= (e f)
891 (and (eql (child-count e)
892 (child-count f))
893 (every #'node= (stp:list-children e) (stp:list-children f))))
895 (defmethod node= ((e stp:element) (f stp:element))
896 (and (named-node-= e f)
897 (parent-node-= e f)
898 (null
899 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
900 :test #'node=))
901 (block nil
902 (flet ((check-namespaces (a b)
903 (let ((result ()))
904 (stp:map-extra-namespaces
905 (lambda (k v)
906 (unless (equal v (stp:find-namespace k b))
907 (return nil)))
909 result)))
910 (check-namespaces e f)
911 (check-namespaces f e))
912 t)))
914 (defmethod node= ((a stp:node) (b stp:node))
915 nil)
917 (defmethod node= ((e stp:document) (f stp:document))
918 (parent-node-= e f))
920 (defmethod node= ((a stp:attribute) (b stp:attribute))
921 (and (named-node-= a b)
922 (equal (stp:value a) (stp:value b))))
924 (defmethod node= ((a stp:comment) (b stp:comment))
925 (equal (stp:data a) (stp:data b)))
927 (defmethod node= ((a stp:text) (b stp:text))
928 (equal (stp:data a) (stp:data b)))
930 (defmethod node= ((a stp:processing-instruction)
931 (b stp:processing-instruction))
932 (and (equal (stp:data a) (stp:data b))
933 (equal (stp:target a) (stp:target b))))
935 (defmethod node= ((a stp:document-type) (b stp:document-type))
936 (and (equal (stp:root-element-name a) (stp:root-element-name b))
937 (equal (stp:public-id a) (stp:public-id b))
938 (equal (stp:system-id a) (stp:system-id b))
939 (equal (stp:internal-subset a) (stp:internal-subset b))))
941 (xpath-sys:define-xpath-function/eager
942 xslt :print
943 (thing)
944 (if (xpath:node-set-p thing)
945 (loop
946 initially (format t ";;; node set:~%")
947 for i from 0
948 for node in (xpath:all-nodes thing)
950 (format t ";;; ~D: ~A~%" i (type-of node)))
951 (format t ";;; ~A~%" thing))
952 thing)