Recover from invalid PI names
[xuriella.git] / test.lisp
blob09ce05cd7b042f9ea3007f9c9c554ebe7f0b9b98
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.
330 ;; FIXME: Tweak the test suite driver to consider a test a success
331 ;; if Saxon fails and the input isn't well-formed, since that's what
332 ;; the tests are probably meant to assert. Or signal an XSLT-ERROR
333 ;; in this situation after all?
335 "Attributes__78387"
336 "Miscellaneous__84001"
337 "Namespace_XPath_Conflict_XPath_XSLT"
338 "Namespace_XPath_DefaultNamespace"
339 "Namespace_XPath_NavigatorMethods"
340 "Namespace_XPath_PredefinedPrefix_XMLNS"
341 "Namespace_XPath_SameQuery_DiffNamespace"
342 "Namespace_XPath_ScopingRules"
345 ;; Someone commented out most of this test...
346 "BVTs_bvt045"
348 ;; FIXME: should re-enable these at some point:
350 ;; the following tests take a lot of time due to the problems of current matching algorithm:
351 "impincl_impincl16"
352 ;; probably the same problem (but I haven't checked):
353 "Import__91164"
355 ;; stack exhaustion -- matching problem i think
356 "Keys_PerfRepro3"
358 ;; test stylesheet doesn't exist?!
359 "ConflictResolution__77833"
360 "Include__77736"
362 ;; these test the value of generate-id(), which isn't specified
363 "Keys__91832"
364 "Keys__91833"))
366 ;; Tests where the output isn't a match because of extraneous whitespace.
367 ;; For these tests, we force space normalization before comparing.
369 ;; Possible reasons for this problem are:
370 ;; a. The output method is declared in an imported stylesheet.
371 ;; SANITIZE-STYLESHEET is supposed to get rid of indent="yes", but it
372 ;; misses imported stylesheets.
373 ;; b. Saxon output isn't a match, but the official output is.
374 ;; But the official output is unaffected by SANITIZE-STYLESHEET.
376 (defparameter *whitespace-issues*
377 (cl-ppcre:create-scanner
378 "(?smx)
379 ^(BVTs_bvt044$
380 |Namespace-alias__91782$
381 |AttributeSets__91038$
382 |BVTs_bvt041$
383 |BVTs_bvt042$
384 |BVTs_bvt054$
385 |BVTs_bvt058$
386 |Import__
387 |Include__
388 |Output__77931$
389 |Output_EmptyElement1$
390 )"))
392 (defparameter *known-failures*
394 ;; uses EBCDIC-CP-IT (whatever that is), but Babel's only got EBCDIC-US.
395 ;; Doesn't actually test any differences between the two, so it's
396 ;; probably just there to annoy us.
397 "output_output22"
399 ;; uses KOI, which Babel doesn't support
400 "BVTs_bvt019"
402 ;; ... shift_jis
403 "Include__77515"
404 "Output__78222"
406 ;; ... iso-2022-jp
407 "Output__78223"
408 "Output__78224"
409 "Output__78225"
410 "Output__78226"
411 "Output__78227"
412 "Output__78229"
414 ;; FIXME?
416 ;; This is an HTML output method issue. The spec says the HTML
417 ;; output method should output elements with a null namespace URI as
418 ;; HTML, and if their name isn't recognized, as an inline element.
419 ;; <xml> here is such an element. It has an attribute with a
420 ;; namespace though, and the spec doesn't say what we should do with that
421 ;; attribute. We currently output it using Closure HTML, and
422 ;; lose its namespace. This test wants the attribute and its
423 ;; namespace to survive.
424 "BVTs_bvt054"))
426 (defun run-tests (&key filter (directory *tests-directory*))
427 (when (typep filter '(or string cons))
428 (setf filter (cl-ppcre:create-scanner filter)))
429 (klacks:with-open-source
430 (source (klacks:make-tapping-source
431 (cxml:make-source (merge-pathnames "katalog.xml" directory))))
432 (let ((*default-pathname-defaults* (merge-pathnames directory)))
433 (map-tests #'run-test
434 source
435 :test (lambda (test)
436 (and (or (null filter)
437 (cl-ppcre:all-matches
438 filter
439 (format nil "~A/~A"
440 (test-category test)
441 (test-id test))))
442 (not (find (test-id test)
443 *bad-tests*
444 :test #'equal))))))))
446 (defun run-named-test (name &optional (d *tests-directory*))
447 (let ((*break-on-signals*
448 '(and error (not babel-encodings:character-encoding-error))))
449 (run-tests :filter (format nil "/~A$" name) :directory d)))
451 (defun copy-file (p q)
452 (with-open-file (in p :element-type '(unsigned-byte 8))
453 (with-open-file (out q
454 :element-type '(unsigned-byte 8)
455 :direction :output
456 :if-exists :rename-and-delete)
457 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
458 (loop for pos = (read-sequence buf in)
459 until (zerop pos)
460 do (write-sequence buf out :end pos))))))
462 (defun find-named-test (name &optional (d *tests-directory*))
463 (klacks:with-open-source
464 (source (klacks:make-tapping-source
465 (cxml:make-source (merge-pathnames "katalog.xml" d))))
466 (block nil
467 (map-tests (lambda (test)
468 (return test))
469 source
470 :test (lambda (test) (equal (test-id test) name))))))
472 (defun copy-test-files (name &optional (d *tests-directory*))
473 (let* ((test (find-named-test name d))
474 (*default-pathname-defaults* (merge-pathnames d))
475 (*break-on-signals*
476 '(and error (not babel-encodings:character-encoding-error)))
477 (target-dir (merge-pathnames "copied-test/"
478 (asdf:component-pathname
479 (asdf:find-system :xuriella))))
480 (xsl (merge-pathnames "test.xsl" target-dir))
481 (xml (merge-pathnames "test.xml" target-dir))
482 (txt (merge-pathnames "official-output.txt" target-dir))
483 (expected (merge-pathnames "expected.xml" target-dir))
484 (actual (merge-pathnames "actual.xml" target-dir)))
485 (ensure-directories-exist target-dir)
486 (copy-file (test-stylesheet-pathname test) xsl)
487 (copy-file (test-data-pathname test) xml)
488 (when (test-official-output-pathname test)
489 (copy-file (test-official-output-pathname test) txt))
490 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
491 (format t "Test data copied to:~% ~A~%~%" xml)
492 (when (test-official-output-pathname test)
493 (format t "Official output file:~% ~A~%~%" txt))
494 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
495 (namestring target-dir)
496 (enough-namestring xsl target-dir)
497 (enough-namestring xml target-dir)
498 (enough-namestring expected target-dir))
499 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
500 (namestring target-dir)
501 (enough-namestring xml target-dir)
502 (enough-namestring xsl target-dir)
503 (enough-namestring expected target-dir))
504 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
505 (namestring target-dir)
506 (enough-namestring xml target-dir)
507 (enough-namestring xsl target-dir)
508 (enough-namestring expected target-dir))
509 (format t "Run xuriella like this:~%")
510 `(apply-stylesheet ,xsl ,xml :output ,actual)))
512 (defun map-tests (run-test source &key (test (constantly t)))
513 (let ((total 0)
514 (pass 0)
515 (known 0))
516 (loop
517 while (klacks:find-event source :start-element)
518 for lname = (klacks:current-lname source)
520 (cond
521 ((equal lname "test-case")
522 (let* ((<test-case>
523 (stp:document-element
524 (klacks:serialize-element source (stp:make-builder))))
525 (test-case (parse-test <test-case>)))
526 (when (funcall test test-case)
527 (incf total)
528 (ecase (funcall run-test test-case)
529 ((nil))
530 ((t)
531 (incf pass))
532 (:known-failure
533 (incf known))))))
535 (klacks:skip source :start-element))))
536 (format t "~&Passed ~D/~D tests (~D expected failures, ~D unexpected failures).~%"
537 pass total known (- total pass known))))
539 (defun parse-test (<test-case>)
540 (stp:with-attributes (id category operation
541 data stylesheet data-2 stylesheet-2
542 output compare)
543 <test-case>
544 (make-instance 'test-case
545 :id id
546 :category category
547 :operation operation
548 :data-pathname data
549 :stylesheet-pathname stylesheet
550 :data-pathname-2 data-2
551 :stylesheet-pathname-2 stylesheet-2
552 :output-pathname output
553 :output-compare compare)))
555 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
556 ;; beginning, if any.
557 (defun slurp-for-comparison (p)
558 (with-open-file (s p :element-type '(unsigned-byte 8))
559 (unless (and (eql (read-byte s nil) #xef)
560 (eql (read-byte s nil) #xbb)
561 (eql (read-byte s nil) #xbf))
562 (file-position s 0))
563 (if (plusp (file-length s))
564 (slurp-for-comparison-1 p s t)
565 "<wrapper/>")))
567 (defun slurp-for-comparison-1 (p s junk-info)
568 (let ((pos (file-position s)) ;for UTF-8 "BOM"
569 (xstream (runes:make-xstream s :speed 1))
570 (prev-pos 0))
571 (setf (runes:xstream-name xstream)
572 (cxml::make-stream-name
573 :entity-name "main document"
574 :entity-kind :main
575 :uri (cxml::pathname-to-uri (merge-pathnames p))))
576 (let ((source
577 (flet ((er (pub sys)
578 pub sys
579 (flexi-streams:make-in-memory-input-stream
580 #())))
581 (cxml:make-source xstream
582 :pathname p
583 :entity-resolver #'er))))
584 (unless (eq junk-info :nada)
585 (loop
586 for key = (progn
587 (setf prev-pos (runes:xstream-position xstream))
588 (klacks:peek-next source))
589 until (eq key :start-document))
590 (cxml::with-source (source cxml::context)
591 (when (eq (cxml::zstream-token-category
592 (cxml::main-zstream cxml::context))
593 :NMTOKEN)
594 ;; oops, doesn't look like XML at all
595 (file-position s pos)
596 (return-from slurp-for-comparison-1
597 (slurp-for-comparison-1 p s :nada)))))
598 (etypecase junk-info
599 (integer
600 (dotimes (x junk-info)
601 (setf prev-pos (runes:xstream-position xstream))
602 (klacks:peek-next source)))
603 ((eql t)
604 (let ((nskip 0))
605 (handler-case
606 (loop
607 (case (klacks:peek-next source)
608 (:start-element (return))
609 (:characters
610 (if (whitespacep (klacks:current-characters source))
611 (incf nskip)
612 (return)))
614 (incf nskip))))
615 ((or file-error cxml:xml-parse-error) ()
616 (when (zerop nskip)
617 (setf nskip nil))))
618 ;; retry
619 (with-open-file (u p :element-type '(unsigned-byte 8))
620 (file-position u pos)
621 (return-from slurp-for-comparison-1
622 (slurp-for-comparison-1 p u nskip)))))
623 ((member nil :nada)))
624 (with-output-to-string (r)
625 (let* ((seen-char
626 (cxml::with-source (source cxml::context)
627 (ecase (cxml::zstream-token-category
628 (cxml::main-zstream cxml::context))
629 (:seen-< #\<)
630 (:? #\?)
631 ((nil :s)
632 (setf prev-pos (runes:xstream-position xstream))
633 nil))))
634 (off-by-one-p (or seen-char (eq junk-info :nada)))
635 (new-pos (- prev-pos (if off-by-one-p 1 0))))
636 ;; copy doctype over
637 (with-open-file (u p :element-type '(unsigned-byte 8))
638 (file-position u pos)
639 (let ((y (runes:make-xstream u :speed 1)))
640 (loop
641 while (< (runes:xstream-position y) new-pos)
642 do (write-char (runes:read-rune y) r))))
643 (write-line "<wrapper>" r)
644 (when seen-char
645 (write-char seen-char r)))
646 (loop
647 for char = (runes:read-rune xstream)
648 until (eq char :eof)
649 do (write-char char r))
650 (write-line "</wrapper>" r)))))
652 (defun parse-for-comparison (p)
653 (let* ((d (flet ((er (pub sys)
654 pub sys
655 (flexi-streams:make-in-memory-input-stream
656 #())))
657 (cxml:parse (slurp-for-comparison p)
658 (make-text-normalizer (stp:make-builder))
659 :entity-resolver #'er)))
660 (de (stp:document-element d)))
661 (let ((first (stp:first-child de)))
662 (when (typep first 'stp:text)
663 (cond
664 ((whitespacep (stp:data first))
665 (stp:delete-child first de))
667 (setf (stp:data first)
668 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
669 (stp:data first)
670 ""))))))
671 (let ((last (stp:last-child de)))
672 (when (typep last 'stp:text)
673 (cond
674 ((whitespacep (stp:data last))
675 (stp:delete-child last de))
677 (setf (stp:data last)
678 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
679 (stp:data last)
680 ""))))))
683 (defun output-equal-p (compare p q &key normalize)
684 (handler-case
685 (case compare
686 (:html (html-output-equal-p p q))
687 (:text (text-output-equal-p p q))
688 (t (xml-output-equal-p p q normalize)))
689 ((or error parse-number::invalid-number) (c)
690 (warn "comparison failed: ~A" c)
691 ;; try again using a plain-text comparision, sometimes it helps:
692 (and (not (eq compare :text))
693 (output-equal-p :text p q :normalize normalize)))))
695 ;; Workaround for namespace_namespace23 and other tests:
696 ;; - For these tests, saxon and msxsl output a declaration for the XSL
697 ;; namespace without using that declaration.
698 ;; - I think saxon and msxsl are both wrong.
699 ;; - The official test output agrees with my assessment.
700 ;; (So does libxslt, but that's not to be trusted. :-))
701 ;; - Here's the catch: The official test output is broken in its whitespace
702 ;; handling.
703 ;; So let's normalize spaces in test output that looks like an XSLT
704 ;; stylesheet, allowing us to pass these tests using the official test output.
705 (defun maybe-normalize-test-spaces (wrapper force)
706 (let ((i 0))
707 (loop while (< i (length (cxml-stp-impl::%children wrapper))) do
708 (let ((wrapper-child (stp:nth-child i wrapper)))
709 (cond
710 ((not (typep wrapper-child 'stp:element))
711 (if force
712 (stp:delete-nth-child i wrapper)
713 (incf i)))
714 ((or (equal (stp:namespace-uri wrapper-child) *xsl*)
715 force)
716 (strip-stylesheet wrapper-child)
717 (labels ((recurse (e &optional preserve)
718 (stp:do-children (child e)
719 (typecase child
720 (stp:text
721 (setf (stp:data child)
722 (normalize-whitespace (stp:data child))))
723 (stp:element
724 (stp:with-attributes ((space "space" *xml*))
725 child
726 (let ((new-preserve
727 (cond
728 ((namep child "text") t)
729 ((not space) preserve)
730 ((equal space "preserve") t)
731 (t nil))))
732 (recurse child new-preserve))))))))
733 (recurse wrapper-child))
734 (incf i))
736 (incf i)))))))
738 (defun xml-output-equal-p (p q normalize)
739 (let ((r (parse-for-comparison p))
740 (s (parse-for-comparison q)))
741 (maybe-normalize-test-spaces (stp:document-element r) normalize)
742 (maybe-normalize-test-spaces (stp:document-element s) normalize)
743 (and (let ((u (stp:document-type r))
744 (v (stp:document-type s)))
745 (if u
746 (and v (node= u v))
747 (null v)))
748 (node= (stp:document-element r) (stp:document-element s)))))
750 ;; FIXME: don't do this in <pre> etc.
751 (defun normalize-html-whitespace (node)
752 (when (typep node 'stp:parent-node)
753 ;; ignore newlines after start tags completely
754 (let ((first (stp:first-child node)))
755 (when (and (typep first 'stp:text)
756 (alexandria:starts-with #\newline (stp:data first)))
757 (setf (stp:data first) (subseq (stp:data first) 1))))
758 ;; ignore newlines before end tags completely
759 (let ((last (stp:last-child node)))
760 (when (and (typep last 'stp:text)
761 (alexandria:ends-with #\newline (stp:data last)))
762 (setf (stp:data last)
763 (subseq (stp:data last) 0 (length (stp:data last))))))
764 ;; normalize sequences of whitespace
765 (stp:do-children (child node)
766 (if (typep child 'stp:text)
767 (setf (stp:data child)
768 (let ((str (normalize-whitespace (stp:data child))))
769 (when
770 ;; FIXME! Here we remove whitespace entirely.
771 ;; Totally incorrect, but I don't see how we could
772 ;; watch Saxon's output otherwise.
773 (equal str " ")
774 (setf str ""))
775 str))
776 (normalize-html-whitespace child)))
777 ;; just to be sure, join adjacent nodes
778 (cxml-stp-impl::normalize-text-nodes! node)))
780 ;; FIXME: this check is too lenient, because chtml is an error-correcting
781 ;; parser.
782 (defun html-output-equal-p (p q)
783 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
784 (s (chtml:parse (pathname q) (stp:make-builder))))
785 (normalize-html-whitespace r)
786 (normalize-html-whitespace s)
787 (flet ((fix-case (node)
788 (xpath:with-namespaces (("xhtml" "http://www.w3.org/1999/xhtml"))
789 (xpath:do-node-set
790 (content (xpath:evaluate "//xhtml:meta/@content" node))
791 (setf (stp:value content)
792 (string-downcase (stp:value content)))))))
793 (fix-case r)
794 (fix-case s))
795 (node= (stp:document-element r) (stp:document-element s))))
797 (defun text-output-equal-p (p q)
798 (with-open-file (a p :element-type '(unsigned-byte 8))
799 (with-open-file (b q :element-type '(unsigned-byte 8))
800 (let ((len (file-length a)))
801 (and (eql len (file-length b))
802 (let ((d (make-array len :element-type '(unsigned-byte 8)))
803 (e (make-array len :element-type '(unsigned-byte 8))))
804 (read-sequence d a)
805 (read-sequence e b)
806 (equalp d e)))))))
808 (defun strip-addresses (str)
809 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
811 (defun slurp-output-method (p)
812 (xpath:with-namespaces ((nil #.*xsl*))
813 (let* ((d (handler-bind
814 ((warning #'muffle-warning))
815 (cxml:parse (pathname p) (stp:make-builder))))
816 (output (xpath:first-node (xpath:evaluate "//output" d))))
817 (if output
818 (let ((method (stp:attribute-value output "method")))
819 (if method
820 (intern (string-upcase method) :keyword)
821 :xml))
822 :xml))))
824 (defun replace-junk (str)
825 (cl-ppcre:regex-replace-all
826 `(:group ,(namestring *tests-directory*))
827 (map 'string
828 (lambda (c)
829 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
831 #\?))
832 str)
833 "..."))
835 (defun run-test (test)
836 (let ((expected-saxon (test-output-pathname test "saxon"))
837 #+xuriella::xsltproc
838 (expected-xsltproc (test-output-pathname test "xsltproc"))
839 (actual (test-output-pathname test "xuriella"))
840 (official (test-official-output-pathname test))
841 (force-normalization
842 (cl-ppcre:all-matches *whitespace-issues* (test-id test)))
843 (output-method nil))
844 (handler-bind ((|hey test suite, this is an HTML document|
845 (lambda (c)
846 (declare (ignore c))
847 (setf output-method :html))))
848 (labels ((uri-resolver (uri)
849 (let ((str (puri:render-uri uri nil)))
850 (cond
851 ((search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
852 str)
853 (cxml::pathname-to-uri
854 (merge-pathnames
855 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
856 *tests-directory*)))
857 ((search "webxtest/testcases/91156a.xsl" str)
858 (cxml::pathname-to-uri
859 (merge-pathnames
860 "MSFT_Conformance_Tests/Import/91156a.xsl"
861 *tests-directory*)))
863 uri))))
864 (doit ()
865 (with-open-file (s actual
866 :if-exists :rename-and-delete
867 :direction :output
868 :element-type '(unsigned-byte 8))
869 (handler-bind ((xslt-error
870 (lambda (c)
871 (declare (ignore c))
872 (when (find-restart 'recover)
873 (invoke-restart 'recover)))))
874 (apply-stylesheet (pathname (test-stylesheet-pathname test))
875 (let ((p (test-data-pathname test)))
876 (cond
877 ((search "Elements/Plants.xml" p)
878 (merge-pathnames
879 "MSFT_Conformance_Tests/Elements/plants.xml"
880 *tests-directory*))
881 ((search "/OutputText.xml" p)
882 (merge-pathnames
883 "MSFT_Conformance_Tests/Output/Outputtext.xml"
884 *tests-directory*))
886 (pathname p))))
887 :output s
888 :uri-resolver #'uri-resolver))))
889 (pp (label pathname)
890 (when pathname
891 (format t " ~A: ~A~%"
892 label
893 (enough-namestring pathname *tests-directory*))))
894 (report (ok &optional (fmt "") &rest args)
895 (write-string
896 (replace-junk
897 (strip-addresses
898 (format nil "~&~A ~A [~A]~?~%"
899 (cond
900 (ok "PASS")
901 ((find (test-id test)
902 *known-failures*
903 :test #'equal)
904 (setf ok :known-failure)
905 "KNOWNFAIL")
907 "FAIL"))
908 (test-id test)
909 (test-category test)
911 args))))
912 (pp "Stylesheet" (test-stylesheet-pathname test))
913 (pp "Data" (test-data-pathname test))
914 (pp "Supplemental stylesheet"
915 (test-stylesheet-pathname-2 test))
916 (pp "Supplemental data" (test-data-pathname-2 test))
917 (pp "Expected output (1)" expected-saxon)
918 #+xuriella::xsltproc
919 (pp "Expected output (2)" expected-xsltproc)
920 (pp "Actual output" actual)
921 (terpri)
922 ok))
923 (cond
924 ((equal (test-operation test) "standard")
925 (handler-case
926 (progn
927 (when (find (test-id test)
928 nil ;;'("axes_axes47" "attribset_attribset20")
929 :test #'equal)
930 (error "skipping problematic test"))
931 (doit)
932 (let* ((output-method
933 (or output-method
934 (slurp-output-method
935 (test-stylesheet-pathname test))))
936 (saxon-matches-p
937 (output-equal-p output-method
938 expected-saxon
939 actual
940 :normalize force-normalization))
941 #+xuriella::xsltproc
942 (xsltproc-matches-p
943 (output-equal-p output-method
944 expected-xsltproc
945 actual))
946 (official-matches-p
947 (output-equal-p output-method
948 official
949 actual
950 :normalize force-normalization)))
951 (cond
952 ((or saxon-matches-p
953 #+xuriella::xsltproc xsltproc-matches-p
954 official-matches-p)
955 (report t)
956 #+xuriella::xsltproc
957 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
958 saxon-matches-p
959 xsltproc-matches-p
960 (if saxon-matches-p
961 (not xsltproc-matches-p)
962 xsltproc-matches-p)))
964 (report nil ": output doesn't match")))))
965 ((or error parse-number::invalid-number) (c)
966 (report nil ": ~A" c))))
968 (handler-case
969 (doit)
970 (xslt-error (c)
971 (report t ": raised an xslt-error as expected" c))
972 ((or error parse-number::invalid-number) (c)
973 (report nil ": condition of incorrect type: ~%~A" c))
974 (:no-error (result)
975 (cond
976 ((not (and official (probe-file official)))
977 (report nil ": expected error not signalled: " result))
978 ((output-equal-p
979 (or output-method
980 (slurp-output-method (test-stylesheet-pathname test)))
981 official
982 actual
983 :normalize force-normalization)
984 (report t))
986 (report nil ": saxon error not signalled and official output not a match")))))))))))
988 (defun run-xpath-tests ()
989 (run-tests :filter "XPath-Expression/|XSLT-Data-Model/"))
992 ;;;; from cxml-stp-test
994 (defun assert-node= (a b)
995 (unless (node= a b)
996 (error "assertion failed: ~S and ~S are not NODE=" a b)))
998 (defun child-count (node)
999 (stp:count-children-if (constantly t) node))
1001 (defun named-node-= (a b)
1002 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
1003 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
1004 (equal (stp:local-name a) (stp:local-name b))))
1006 (defun parent-node-= (e f)
1007 (and (eql (child-count e)
1008 (child-count f))
1009 (every #'node= (stp:list-children e) (stp:list-children f))))
1011 (defmethod node= ((e stp:element) (f stp:element))
1012 (and (named-node-= e f)
1013 (parent-node-= e f)
1014 (null
1015 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
1016 :test #'node=))
1017 (block nil
1018 (flet ((check-namespaces (a b)
1019 (let ((result ()))
1020 (stp:map-extra-namespaces
1021 (lambda (k v)
1022 (unless (equal v (stp:find-namespace k b))
1023 (return nil)))
1025 result)))
1026 (check-namespaces e f)
1027 (check-namespaces f e))
1028 t)))
1030 (defmethod node= ((a stp:node) (b stp:node))
1031 nil)
1033 (defmethod node= ((e stp:document) (f stp:document))
1034 (parent-node-= e f))
1036 (defmethod node= ((a stp:attribute) (b stp:attribute))
1037 (and (named-node-= a b)
1038 (equal (stp:value a) (stp:value b))))
1040 (defmethod node= ((a stp:comment) (b stp:comment))
1041 (equal (stp:data a) (stp:data b)))
1043 (defmethod node= ((a stp:text) (b stp:text))
1044 (equal (stp:data a) (stp:data b)))
1046 (defmethod node= ((a stp:processing-instruction)
1047 (b stp:processing-instruction))
1048 (and (equal (stp:data a) (stp:data b))
1049 (equal (stp:target a) (stp:target b))))
1051 (defmethod node= ((a stp:document-type) (b stp:document-type))
1052 (and (equal (stp:root-element-name a) (stp:root-element-name b))
1053 (equal (stp:public-id a) (stp:public-id b))
1054 (equal (stp:system-id a) (stp:system-id b))
1055 (equal (stp:internal-subset a) (stp:internal-subset b))))
1057 (xpath-sys:define-xpath-function/eager
1058 xslt :print
1059 (thing)
1060 (if (xpath:node-set-p thing)
1061 (loop
1062 initially (format t ";;; node set:~%")
1063 for i from 0
1064 for node in (xpath:all-nodes thing)
1066 (format t ";;; ~D: ~A~%" i (type-of node)))
1067 (format t ";;; ~A~%" thing))
1068 thing)