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