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