Fixed Errors_err036
[xuriella.git] / test.lisp
blobfa836afe52954f6480a93c82f1e788884a1691fd
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella)
32 (defparameter *tests-directory*
33 "/home/david/src/XSLT-testsuite-04/testsuite/TESTS/")
35 (defclass test-case ()
36 ((id :initarg :id :accessor test-id)
37 (category :initarg :category :accessor test-category)
38 (operation :initarg :operation :accessor test-operation)
39 (data-pathname :initarg :data-pathname :accessor test-data-pathname)
40 (stylesheet-pathname :initarg :stylesheet-pathname
41 :accessor test-stylesheet-pathname)
42 (data-pathname-2 :initarg :data-pathname-2 :accessor test-data-pathname-2)
43 (stylesheet-pathname-2 :initarg :stylesheet-pathname-2
44 :accessor test-stylesheet-pathname-2)
45 (output-pathname :initarg :output-pathname
46 :accessor test-official-output-pathname)
47 (output-compare :initarg :output-compare
48 :accessor test-output-compare)))
50 (defmethod print-object ((object test-case) stream)
51 (print-unreadable-object (object stream :identity nil :type t)
52 (format stream "~A ~A/~A"
53 (test-operation object)
54 (test-category object)
55 (test-id object))))
58 ;;;; SIMPLIFY-TESTS
60 ;;; Translate catalog.xml into an actually usable katalog.xml
61 ;;; by running the test cases through xsltproc to see what it thinks
62 ;;; about them.
64 (defun simplify-tests (&optional (d *tests-directory*))
65 (with-open-file (stream (merge-pathnames "katalog.xml" d)
66 :direction :output
67 :if-exists :supersede
68 :element-type '(unsigned-byte 8))
69 (cxml:with-xml-output (cxml:make-octet-stream-sink stream)
70 (cxml:with-element "simplified-test-suite"
71 (klacks:with-open-source
72 (source (klacks:make-tapping-source
73 (cxml:make-source (merge-pathnames "catalog.xml" d))))
74 (let ((*default-pathname-defaults* (merge-pathnames d)))
75 (map-original-tests #'simplify-test source)))))))
77 (defun map-original-tests (run-test source &key (test (constantly t)))
78 (let ((total 0)
79 (pass 0)
80 major-path)
81 (loop
82 while (klacks:find-event source :start-element)
83 for lname = (klacks:current-lname source)
85 (cond
86 ((equal lname "major-path")
87 (klacks:skip source :start-element)
88 (setf major-path
89 (namestring
90 (merge-pathnames (klacks:consume-characters source)))))
91 ((equal lname "test-case")
92 (let* ((<test-case>
93 (stp:document-element
94 (klacks:serialize-element source (stp:make-builder))))
95 (test-case (parse-original-test major-path <test-case>)))
96 (when (funcall test test-case)
97 (incf total)
98 (when (funcall run-test test-case)
99 (incf pass)))))
101 (klacks:skip source :start-element))))
102 (format t "~&Passed ~D/~D tests.~%" pass total)))
104 (defun parse-original-test (major-path <test-case>)
105 (let* ((file-path
106 (stp:string-value
107 (stp:find-recursively-if (stp:of-name "file-path") <test-case>)))
108 (base (concatenate 'string major-path "/" file-path))
109 (out-base (concatenate 'string major-path "/REF_OUT/" file-path))
110 (scenario
111 (stp:find-recursively-if (stp:of-name "scenario") <test-case>))
112 data
113 stylesheet
114 supplemental-stylesheet
115 supplemental-data
116 output
117 compare)
118 (dolist (<input> (stp:filter-recursively (stp:of-name "input-file")
119 <test-case>))
120 (let ((role (stp:attribute-value <input> "role"))
121 (path (concatenate 'string base "/" (stp:string-value <input>))))
122 (cond
123 ((equal role "principal-data")
124 (setf data path))
125 ((equal role "principal-stylesheet")
126 (setf stylesheet path))
127 ((equal role "supplemental-stylesheet")
128 (setf supplemental-stylesheet path))
129 ((equal role "supplemental-data")
130 (setf supplemental-data path))
132 (error "unrecognized role: ~A" role)))))
133 (dolist (<output> (stp:filter-recursively (stp:of-name "output-file")
134 <test-case>))
135 (let ((role (stp:attribute-value <output> "role"))
136 (path (concatenate 'string out-base
138 (stp:string-value <output>))))
139 (cond
140 ((equal role "principal")
141 (setf output path)
142 (setf compare (stp:attribute-value <output> "compare")))
144 (error "unrecognized role: ~A" role)))))
145 (make-instance 'test-case
146 :id (stp:attribute-value <test-case> "id")
147 :category (stp:attribute-value <test-case> "category")
148 :operation (stp:attribute-value scenario "operation")
149 :data-pathname data
150 :stylesheet-pathname stylesheet
151 :stylesheet-pathname-2 supplemental-stylesheet
152 :data-pathname-2 supplemental-data
153 :output-pathname output
154 :output-compare compare)))
156 (defun write-simplified-test (test-case operation)
157 (cxml:with-element "test-case"
158 (cxml:attribute "id" (test-id test-case))
159 (cxml:attribute "category" (test-category test-case))
160 (flet ((p (l p)
161 (cxml:attribute l (and p (namestring p)))))
162 (p "data" (test-data-pathname test-case))
163 (p "stylesheet" (noindent-stylesheet-pathname test-case))
164 (p "data-2" (test-data-pathname-2 test-case))
165 (p "stylesheet-2" (test-stylesheet-pathname-2 test-case))
166 (p "output" (test-official-output-pathname test-case))
167 (p "compare" (test-output-compare test-case)))
168 (cxml:attribute "operation" operation)))
170 (defun test-output-pathname (test type)
171 (make-pathname :name (test-id test)
172 :type type
173 :defaults (test-data-pathname test)))
175 (defun sanitize-stylesheet (in out)
176 (if (probe-file in)
177 (handler-case
178 (let ((d (cxml:parse (pathname in) (stp:make-builder))))
179 (xpath:with-namespaces ((nil #.*xsl*))
180 (xpath:do-node-set (output (xpath:evaluate "//output" d))
181 (let ((a (stp:find-attribute-named output "indent")))
182 (when a
183 (stp:detach a)))))
184 (with-open-file (s out
185 :direction :output
186 :if-exists :rename-and-delete
187 :element-type '(unsigned-byte 8))
188 (stp:serialize d (cxml:make-octet-stream-sink s))))
189 (error (c)
190 (warn "ignoring bogus stylesheet ~A: ~A" in c)
191 (copy-file in out)))
192 (warn "oops, ignoring missing stylesheet: ~A" in)))
194 (defun noindent-stylesheet-pathname (test-case)
195 (make-pathname :type "noindent-xsl"
196 :defaults (test-stylesheet-pathname test-case)))
198 (defun simplify-test (test-case)
199 (flet ((report (status &optional (fmt "") &rest args)
200 (format t "~&~A ~A [~A]~?~%"
201 status
202 (test-id test-case)
203 (test-category test-case)
205 args)))
206 (let* ((data (test-data-pathname test-case))
207 (stylesheet (test-stylesheet-pathname test-case))
208 (noindent-stylesheet (noindent-stylesheet-pathname test-case))
209 #+xuriella::xsltproc
210 (out (test-output-pathname test-case "xsltproc"))
211 (saxon-out (test-output-pathname test-case "saxon")))
212 (sanitize-stylesheet stylesheet noindent-stylesheet)
213 (if (equal (test-operation test-case) "standard")
214 (handler-case
215 (progn
216 #+xuriella::xsltproc (xsltproc noindent-stylesheet data out)
217 (saxon noindent-stylesheet data saxon-out)
218 (report "PASS")
219 (write-simplified-test test-case "standard")
221 (error (c)
222 (report "FAIL" ": ~A" c)
223 (write-simplified-test test-case "execution-error")
224 nil))
225 (handler-case
226 (progn
227 #+xuriella::xsltproc
228 (xsltproc noindent-stylesheet data "/dev/null")
229 (saxon noindent-stylesheet data "/dev/null")
230 (report "FAIL" ": expected error not signalled")
231 ;; let's ignore unexpected successes for now
232 nil)
233 (error (c)
234 (report "PASS" ": expected error ~A" c)
235 (write-simplified-test test-case "execution-error")
236 t))))))
238 (defun xsltproc (stylesheet input output)
239 (flet ((full-namestring (x)
240 (namestring (merge-pathnames x))))
241 (let* ((asdf::*verbose-out* (make-string-output-stream))
242 (code (asdf:run-shell-command
243 "cd ~S && xsltproc ~S ~S >~S"
244 (full-namestring "")
245 (full-namestring stylesheet)
246 (full-namestring input)
247 (full-namestring output))))
248 (unless (zerop code)
249 (error "running xsltproc failed with code ~A [~%~A~%]"
250 code
251 (get-output-stream-string asdf::*verbose-out*))))))
253 (defun saxon (stylesheet input output)
254 (flet ((full-namestring (x)
255 (namestring (merge-pathnames x))))
256 (let* ((asdf::*verbose-out* (make-string-output-stream))
257 (code (asdf:run-shell-command
258 "cd ~S && java -jar /usr/share/java/saxon.jar ~S ~S >~S"
259 (full-namestring "")
260 (full-namestring input)
261 (full-namestring stylesheet)
262 (full-namestring output))))
263 (unless (zerop code)
264 (error "running saxon failed with code ~A [~%~A~%]"
265 code
266 (get-output-stream-string asdf::*verbose-out*))))))
269 ;;;; RUN-TESTS and DRIBBLE-TESTS
271 ;;; Process katalog.xml
273 (defun dribble-tests
274 (&key filter (directory *tests-directory*) (file "TEST"))
275 (let ((*package* (find-package 'cl-user))
276 (*print-circle* nil))
277 (with-open-file (dribble
278 (merge-pathnames file
279 (slot-value (asdf:find-system :xuriella)
280 'asdf::relative-pathname))
281 :direction :output
282 :if-exists :supersede
283 :external-format :utf-8)
284 (let* ((dribble (make-broadcast-stream dribble *standard-output*))
285 (*standard-output* dribble)
286 (*trace-output* dribble)
287 (*error-output* dribble)
288 (*terminal-io* (make-two-way-stream *standard-input* dribble)))
289 (handler-bind ((warning
290 (lambda (c)
291 (warn "~A" (replace-junk (princ-to-string c)))
292 (muffle-warning c))))
293 (run-tests :filter filter
294 :directory directory))))))
296 (defparameter *bad-tests*
297 '(;; Inconsistent tests:
299 ;; Some tests wants us to recover from this error, yet this one doesn't:
300 "copy_copy61"
301 "copy_copy62"
303 ;; Should we fix this?
305 ;; We signal a run-time error when and if it's actually used. The test
306 ;; wants a compilation-time error...
307 "AttributeSets_RefToUndefinedAttributeSet"
309 ;; We would pass this:
311 ;; We perform recovery, but saxon doesn't. Recovery results in non-XML
312 ;; output, which we can't parse for comparison against the official
313 ;; test case.
314 "output_output75"
316 ;; we'd pass these tests, but the test authors forgot to declare the
317 ;; entity they're writing, so we can't parse it for comparison.
318 "output_output06"
319 "output_output10"
320 "output_output61"
322 ;; another similar test where the output is unparsable, except that
323 ;; here an entity declaration wouldn't have helped either:
324 "Copying_ResultTreeFragmentWithEscapedText"
326 ;; Broken test:
328 ;; Input document isn't ns-wf.
329 "Attributes__78387"
331 ;; Someone commented out most of this test...
332 "BVTs_bvt045"
334 ;; FIXME: should re-enable these at some point:
336 ;; the following tests take a lot of time due to the problems of current matching algorithm:
337 "impincl_impincl16"
338 ;; probably the same problem (but I haven't checked):
339 "Import__91164"
341 ;; stack exhaustion -- matching problem i think
342 "Keys_PerfRepro3"
344 ;; test stylesheet doesn't exist?!
345 "ConflictResolution__77833"))
347 ;; Tests where the output isn't a match because of extraneous whitespace.
348 ;; For these tests, we force space normalization before comparing.
350 ;; Possible reasons for this problem are:
351 ;; a. The output method is declared in an imported stylesheet.
352 ;; SANITIZE-STYLESHEET is supposed to get rid of indent="yes", but it
353 ;; misses imported stylesheets.
354 ;; b. Saxon output isn't a match, but the official output is.
355 ;; But the official output is unaffected by SANITIZE-STYLESHEET.
357 (defparameter *whitespace-issues*
358 '("BVTs_bvt044"
359 "Namespace-alias__91782"
360 "AttributeSets__91038"
361 "BVTs_bvt041"
362 "BVTs_bvt042"
363 "BVTs_bvt054"
364 "BVTs_bvt058"))
366 (defparameter *known-failures*
368 ;; uses EBCDIC-CP-IT (whatever that is), but Babel's only got EBCDIC-US.
369 ;; Doesn't actually test any differences between the two, so it's
370 ;; probably just there to annoy us.
371 "output_output22"
373 ;; uses KOI, which Babel doesn't support
374 "BVTs_bvt019"
376 ;; FIXME?
378 ;; This is an HTML output method issue. The spec says the HTML
379 ;; output method should output elements with a null namespace URI as
380 ;; HTML, and if their name isn't recognized, as an inline element.
381 ;; <xml> here is such an element. It has an attributes with a
382 ;; namespace though, and the spec doesn't say what we should do with that
383 ;; attribute. We currently output it using Closure HTML, and
384 ;; lose its namespace. This test wants the attribute and its
385 ;; namespace to survive.
386 "BVTs_bvt054"))
388 (defun run-tests (&key filter (directory *tests-directory*))
389 (when (typep filter '(or string cons))
390 (setf filter (cl-ppcre:create-scanner filter)))
391 (klacks:with-open-source
392 (source (klacks:make-tapping-source
393 (cxml:make-source (merge-pathnames "katalog.xml" directory))))
394 (let ((*default-pathname-defaults* (merge-pathnames directory)))
395 (map-tests #'run-test
396 source
397 :test (lambda (test)
398 (and (or (null filter)
399 (cl-ppcre:all-matches
400 filter
401 (format nil "~A/~A"
402 (test-category test)
403 (test-id test))))
404 (not (find (test-id test)
405 *bad-tests*
406 :test #'equal))))))))
408 (defun run-named-test (name &optional (d *tests-directory*))
409 (let ((*break-on-signals*
410 '(and error (not babel-encodings:character-encoding-error))))
411 (run-tests :filter (format nil "/~A$" name) :directory d)))
413 (defun copy-file (p q)
414 (with-open-file (in p :element-type '(unsigned-byte 8))
415 (with-open-file (out q
416 :element-type '(unsigned-byte 8)
417 :direction :output
418 :if-exists :rename-and-delete)
419 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
420 (loop for pos = (read-sequence buf in)
421 until (zerop pos)
422 do (write-sequence buf out :end pos))))))
424 (defun find-named-test (name &optional (d *tests-directory*))
425 (klacks:with-open-source
426 (source (klacks:make-tapping-source
427 (cxml:make-source (merge-pathnames "katalog.xml" d))))
428 (block nil
429 (map-tests (lambda (test)
430 (return test))
431 source
432 :test (lambda (test) (equal (test-id test) name))))))
434 (defun copy-test-files (name &optional (d *tests-directory*))
435 (let* ((test (find-named-test name d))
436 (*default-pathname-defaults* (merge-pathnames d))
437 (*break-on-signals*
438 '(and error (not babel-encodings:character-encoding-error)))
439 (target-dir (merge-pathnames "copied-test/"
440 (asdf:component-pathname
441 (asdf:find-system :xuriella))))
442 (xsl (merge-pathnames "test.xsl" target-dir))
443 (xml (merge-pathnames "test.xml" target-dir))
444 (txt (merge-pathnames "official-output.txt" target-dir))
445 (expected (merge-pathnames "expected.xml" target-dir))
446 (actual (merge-pathnames "actual.xml" target-dir)))
447 (ensure-directories-exist target-dir)
448 (copy-file (test-stylesheet-pathname test) xsl)
449 (copy-file (test-data-pathname test) xml)
450 (when (test-official-output-pathname test)
451 (copy-file (test-official-output-pathname test) txt))
452 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
453 (format t "Test data copied to:~% ~A~%~%" xml)
454 (when (test-official-output-pathname test)
455 (format t "Official output file:~% ~A~%~%" txt))
456 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
457 (namestring target-dir)
458 (enough-namestring xsl target-dir)
459 (enough-namestring xml target-dir)
460 (enough-namestring expected target-dir))
461 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
462 (namestring target-dir)
463 (enough-namestring xml target-dir)
464 (enough-namestring xsl target-dir)
465 (enough-namestring expected target-dir))
466 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
467 (namestring target-dir)
468 (enough-namestring xml target-dir)
469 (enough-namestring xsl target-dir)
470 (enough-namestring expected target-dir))
471 (format t "Run xuriella like this:~%")
472 `(apply-stylesheet ,xsl ,xml :output ,actual)))
474 (defun map-tests (run-test source &key (test (constantly t)))
475 (let ((total 0)
476 (pass 0)
477 (known 0))
478 (loop
479 while (klacks:find-event source :start-element)
480 for lname = (klacks:current-lname source)
482 (cond
483 ((equal lname "test-case")
484 (let* ((<test-case>
485 (stp:document-element
486 (klacks:serialize-element source (stp:make-builder))))
487 (test-case (parse-test <test-case>)))
488 (when (funcall test test-case)
489 (incf total)
490 (ecase (funcall run-test test-case)
491 ((nil))
492 ((t)
493 (incf pass))
494 (:known-failure
495 (incf known))))))
497 (klacks:skip source :start-element))))
498 (format t "~&Passed ~D/~D tests (~D expected failures, ~D unexpected failures).~%"
499 pass total known (- total pass known))))
501 (defun parse-test (<test-case>)
502 (stp:with-attributes (id category operation
503 data stylesheet data-2 stylesheet-2
504 output compare)
505 <test-case>
506 (make-instance 'test-case
507 :id id
508 :category category
509 :operation operation
510 :data-pathname data
511 :stylesheet-pathname stylesheet
512 :data-pathname-2 data-2
513 :stylesheet-pathname-2 stylesheet-2
514 :output-pathname output
515 :output-compare compare)))
517 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
518 ;; beginning, if any.
519 (defun slurp-for-comparison (p)
520 (with-open-file (s p :element-type '(unsigned-byte 8))
521 (unless (and (eql (read-byte s nil) #xef)
522 (eql (read-byte s nil) #xbb)
523 (eql (read-byte s nil) #xbf))
524 (file-position s 0))
525 (if (plusp (file-length s))
526 (slurp-for-comparison-1 p s t)
527 "<wrapper/>")))
529 (defun slurp-for-comparison-1 (p s junk-info)
530 (let ((pos (file-position s)) ;for UTF-8 "BOM"
531 (xstream (runes:make-xstream s :speed 1))
532 (prev-pos 0))
533 (setf (runes:xstream-name xstream)
534 (cxml::make-stream-name
535 :entity-name "main document"
536 :entity-kind :main
537 :uri (cxml::pathname-to-uri (merge-pathnames p))))
538 (let ((source
539 (flet ((er (pub sys)
540 pub sys
541 (flexi-streams:make-in-memory-input-stream
542 #())))
543 (cxml:make-source xstream
544 :pathname p
545 :entity-resolver #'er))))
546 (unless (eq junk-info :nada)
547 (loop
548 for key = (progn
549 (setf prev-pos (runes:xstream-position xstream))
550 (klacks:peek-next source))
551 until (eq key :start-document))
552 (cxml::with-source (source cxml::context)
553 (when (eq (cxml::zstream-token-category
554 (cxml::main-zstream cxml::context))
555 :NMTOKEN)
556 ;; oops, doesn't look like XML at all
557 (file-position s pos)
558 (return-from slurp-for-comparison-1
559 (slurp-for-comparison-1 p s :nada)))))
560 (etypecase junk-info
561 (integer
562 (dotimes (x junk-info)
563 (setf prev-pos (runes:xstream-position xstream))
564 (klacks:peek-next source)))
565 ((eql t)
566 (let ((nskip 0))
567 (handler-case
568 (loop
569 (case (klacks:peek-next source)
570 (:start-element (return))
571 (:characters
572 (if (whitespacep (klacks:current-characters source))
573 (incf nskip)
574 (return)))
576 (incf nskip))))
577 ((or file-error cxml:xml-parse-error) ()
578 (when (zerop nskip)
579 (setf nskip nil))))
580 ;; retry
581 (with-open-file (u p :element-type '(unsigned-byte 8))
582 (file-position u pos)
583 (return-from slurp-for-comparison-1
584 (slurp-for-comparison-1 p u nskip)))))
585 ((member nil :nada)))
586 (with-output-to-string (r)
587 (let* ((seen-char
588 (cxml::with-source (source cxml::context)
589 (ecase (cxml::zstream-token-category
590 (cxml::main-zstream cxml::context))
591 (:seen-< #\<)
592 (:? #\?)
593 ((nil :s)
594 (setf prev-pos (runes:xstream-position xstream))
595 nil))))
596 (off-by-one-p (or seen-char (eq junk-info :nada)))
597 (new-pos (- prev-pos (if off-by-one-p 1 0))))
598 ;; copy doctype over
599 (with-open-file (u p :element-type '(unsigned-byte 8))
600 (file-position u pos)
601 (let ((y (runes:make-xstream u :speed 1)))
602 (loop
603 while (< (runes:xstream-position y) new-pos)
604 do (write-char (runes:read-rune y) r))))
605 (write-line "<wrapper>" r)
606 (when seen-char
607 (write-char seen-char r)))
608 (loop
609 for char = (runes:read-rune xstream)
610 until (eq char :eof)
611 do (write-char char r))
612 (write-line "</wrapper>" r)))))
614 (defun parse-for-comparison (p)
615 (let* ((d (flet ((er (pub sys)
616 pub sys
617 (flexi-streams:make-in-memory-input-stream
618 #())))
619 (cxml:parse (slurp-for-comparison p)
620 (make-text-normalizer (stp:make-builder))
621 :entity-resolver #'er)))
622 (de (stp:document-element d)))
623 (let ((first (stp:first-child de)))
624 (when (typep first 'stp:text)
625 (cond
626 ((whitespacep (stp:data first))
627 (stp:delete-child first de))
629 (setf (stp:data first)
630 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
631 (stp:data first)
632 ""))))))
633 (let ((last (stp:last-child de)))
634 (when (typep last 'stp:text)
635 (cond
636 ((whitespacep (stp:data last))
637 (stp:delete-child last de))
639 (setf (stp:data last)
640 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
641 (stp:data last)
642 ""))))))
645 (defun output-equal-p (compare p q &key normalize)
646 (handler-case
647 (ecase compare
648 (:xml (xml-output-equal-p p q normalize))
649 (:html (html-output-equal-p p q))
650 (:text (text-output-equal-p p q)))
651 ((or error parse-number::invalid-number) (c)
652 (warn "comparison failed: ~A" c)
653 nil)))
655 ;; Workaround for namespace_namespace23 and other tests:
656 ;; - For these tests, saxon and msxsl output a declaration for the XSL
657 ;; namespace without using that declaration.
658 ;; - I think saxon and msxsl are both wrong.
659 ;; - The official test output agrees with my assessment.
660 ;; (So does libxslt, but that's not to be trusted. :-))
661 ;; - Here's the catch: The official test output is broken in its whitespace
662 ;; handling.
663 ;; So let's normalize spaces in test output that looks like an XSLT
664 ;; stylesheet, allowing us to pass these tests using the official test output.
665 (defun maybe-normalize-test-spaces (wrapper force)
666 (let ((i 0))
667 (loop while (< i (length (cxml-stp-impl::%children wrapper))) do
668 (let ((wrapper-child (stp:nth-child i wrapper)))
669 (cond
670 ((not (typep wrapper-child 'stp:element))
671 (if force
672 (stp:delete-nth-child i wrapper)
673 (incf i)))
674 ((or (equal (stp:namespace-uri wrapper-child) *xsl*)
675 force)
676 (strip-stylesheet wrapper-child)
677 (labels ((recurse (e &optional preserve)
678 (stp:do-children (child e)
679 (typecase child
680 (stp:text
681 (setf (stp:data child)
682 (normalize-whitespace (stp:data child))))
683 (stp:element
684 (stp:with-attributes ((space "space" *xml*))
685 child
686 (let ((new-preserve
687 (cond
688 ((namep child "text") t)
689 ((not space) preserve)
690 ((equal space "preserve") t)
691 (t nil))))
692 (recurse child new-preserve))))))))
693 (recurse wrapper-child))
694 (incf i))
696 (incf i)))))))
698 (defun xml-output-equal-p (p q normalize)
699 (let ((r (parse-for-comparison p))
700 (s (parse-for-comparison q)))
701 (maybe-normalize-test-spaces (stp:document-element r) normalize)
702 (maybe-normalize-test-spaces (stp:document-element s) normalize)
703 (and (let ((u (stp:document-type r))
704 (v (stp:document-type s)))
705 (if u
706 (and v (node= u v))
707 (null v)))
708 (node= (stp:document-element r) (stp:document-element s)))))
710 ;; FIXME: don't do this in <pre> etc.
711 (defun normalize-html-whitespace (node)
712 (when (typep node 'stp:parent-node)
713 ;; ignore newlines after start tags completely
714 (let ((first (stp:first-child node)))
715 (when (and (typep first 'stp:text)
716 (alexandria:starts-with #\newline (stp:data first)))
717 (setf (stp:data first) (subseq (stp:data first) 1))))
718 ;; ignore newlines before end tags completely
719 (let ((last (stp:last-child node)))
720 (when (and (typep last 'stp:text)
721 (alexandria:ends-with #\newline (stp:data last)))
722 (setf (stp:data last)
723 (subseq (stp:data last) 0 (length (stp:data last))))))
724 ;; normalize sequences of whitespace
725 (stp:do-children (child node)
726 (if (typep child 'stp:text)
727 (setf (stp:data child)
728 (let ((str (normalize-whitespace (stp:data child))))
729 (when
730 ;; FIXME! Here we remove whitespace entirely.
731 ;; Totally incorrect, but I don't see how we could
732 ;; watch Saxon's output otherwise.
733 (equal str " ")
734 (setf str ""))
735 str))
736 (normalize-html-whitespace child)))
737 ;; just to be sure, join adjacent nodes
738 (cxml-stp-impl::normalize-text-nodes! node)))
740 ;; FIXME: this check is too lenient, because chtml is an error-correcting
741 ;; parser.
742 (defun html-output-equal-p (p q)
743 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
744 (s (chtml:parse (pathname q) (stp:make-builder))))
745 (normalize-html-whitespace r)
746 (normalize-html-whitespace s)
747 (flet ((fix-case (node)
748 (xpath:with-namespaces (("xhtml" "http://www.w3.org/1999/xhtml"))
749 (xpath:do-node-set
750 (content (xpath:evaluate "//xhtml:meta/@content" node))
751 (setf (stp:value content)
752 (string-downcase (stp:value content)))))))
753 (fix-case r)
754 (fix-case s))
755 (node= (stp:document-element r) (stp:document-element s))))
757 (defun text-output-equal-p (p q)
758 (with-open-file (a p :element-type '(unsigned-byte 8))
759 (with-open-file (b q :element-type '(unsigned-byte 8))
760 (let ((len (file-length a)))
761 (and (eql len (file-length b))
762 (let ((d (make-array len :element-type '(unsigned-byte 8)))
763 (e (make-array len :element-type '(unsigned-byte 8))))
764 (read-sequence d a)
765 (read-sequence e b)
766 (equalp d e)))))))
768 (defun strip-addresses (str)
769 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
771 (defun slurp-output-method (p)
772 (xpath:with-namespaces ((nil #.*xsl*))
773 (let* ((d (handler-bind
774 ((warning #'muffle-warning))
775 (cxml:parse (pathname p) (stp:make-builder))))
776 (output (xpath:first-node (xpath:evaluate "//output" d))))
777 (if output
778 (let ((method (stp:attribute-value output "method")))
779 (if method
780 (intern (string-upcase method) :keyword)
781 :xml))
782 :xml))))
784 (defun replace-junk (str)
785 (cl-ppcre:regex-replace-all
786 `(:group ,(namestring *tests-directory*))
787 (map 'string
788 (lambda (c)
789 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
791 #\?))
792 str)
793 "..."))
795 (defun run-test (test)
796 (let ((expected-saxon (test-output-pathname test "saxon"))
797 #+xuriella::xsltproc
798 (expected-xsltproc (test-output-pathname test "xsltproc"))
799 (actual (test-output-pathname test "xuriella"))
800 (official (test-official-output-pathname test))
801 (force-normalization
802 (find (test-id test) *whitespace-issues* :test #'equal))
803 (output-method nil))
804 (handler-bind ((|hey test suite, this is an HTML document|
805 (lambda (c)
806 (declare (ignore c))
807 (setf output-method :html))))
808 (labels ((uri-resolver (uri)
809 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
810 uri)
811 (cxml::pathname-to-uri
812 (merge-pathnames
813 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
814 *tests-directory*))
815 uri))
816 (doit ()
817 (with-open-file (s actual
818 :if-exists :rename-and-delete
819 :direction :output
820 :element-type '(unsigned-byte 8))
821 (handler-bind ((xslt-error
822 (lambda (c)
823 (declare (ignore c))
824 (when (find-restart 'recover)
825 (invoke-restart 'recover)))))
826 (apply-stylesheet (pathname (test-stylesheet-pathname test))
827 (let ((p (test-data-pathname test)))
828 (if (search "Elements/Plants.xml" p)
829 (merge-pathnames
830 "MSFT_Conformance_Tests/Elements/plants.xml"
831 *tests-directory*)
832 (pathname p)))
833 :output s
834 :uri-resolver #'uri-resolver))))
835 (pp (label pathname)
836 (when pathname
837 (format t " ~A: ~A~%"
838 label
839 (enough-namestring pathname *tests-directory*))))
840 (report (ok &optional (fmt "") &rest args)
841 (write-string
842 (replace-junk
843 (strip-addresses
844 (format nil "~&~A ~A [~A]~?~%"
845 (cond
846 (ok "PASS")
847 ((find (test-id test)
848 *known-failures*
849 :test #'equal)
850 (setf ok :known-failure)
851 "KNOWNFAIL")
853 "FAIL"))
854 (test-id test)
855 (test-category test)
857 args))))
858 (pp "Stylesheet" (test-stylesheet-pathname test))
859 (pp "Data" (test-data-pathname test))
860 (pp "Supplemental stylesheet"
861 (test-stylesheet-pathname-2 test))
862 (pp "Supplemental data" (test-data-pathname-2 test))
863 (pp "Expected output (1)" expected-saxon)
864 #+xuriella::xsltproc
865 (pp "Expected output (2)" expected-xsltproc)
866 (pp "Actual output" actual)
867 (terpri)
868 ok))
869 (cond
870 ((equal (test-operation test) "standard")
871 (handler-case
872 (progn
873 (when (find (test-id test)
874 nil ;;'("axes_axes47" "attribset_attribset20")
875 :test #'equal)
876 (error "skipping problematic test"))
877 (doit)
878 (let* ((output-method
879 (or output-method
880 (slurp-output-method
881 (test-stylesheet-pathname test))))
882 (saxon-matches-p
883 (output-equal-p output-method
884 expected-saxon
885 actual
886 :normalize force-normalization))
887 #+xuriella::xsltproc
888 (xsltproc-matches-p
889 (output-equal-p output-method
890 expected-xsltproc
891 actual))
892 (official-matches-p
893 (output-equal-p output-method
894 official
895 actual
896 :normalize force-normalization)))
897 (cond
898 ((or saxon-matches-p
899 #+xuriella::xsltproc xsltproc-matches-p
900 official-matches-p)
901 (report t)
902 #+xuriella::xsltproc
903 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
904 saxon-matches-p
905 xsltproc-matches-p
906 (if saxon-matches-p
907 (not xsltproc-matches-p)
908 xsltproc-matches-p)))
910 (report nil ": output doesn't match")))))
911 ((or error parse-number::invalid-number) (c)
912 (report nil ": ~A" c))))
914 (handler-case
915 (doit)
916 (xslt-error (c)
917 (report t ": raised an xslt-error as expected" c))
918 ((or error parse-number::invalid-number) (c)
919 (report nil ": condition of incorrect type: ~%~A" c))
920 (:no-error (result)
921 (cond
922 ((not (and official (probe-file official)))
923 (report nil ": expected error not signalled: " result))
924 ((output-equal-p
925 (or output-method
926 (slurp-output-method (test-stylesheet-pathname test)))
927 official
928 actual
929 :normalize force-normalization)
930 (report t))
932 (report nil ": saxon error not signalled and official output not a match")))))))))))
934 (defun run-xpath-tests ()
935 (run-tests :filter "XPath-Expression/|XSLT-Data-Model/"))
938 ;;;; from cxml-stp-test
940 (defun assert-node= (a b)
941 (unless (node= a b)
942 (error "assertion failed: ~S and ~S are not NODE=" a b)))
944 (defun child-count (node)
945 (stp:count-children-if (constantly t) node))
947 (defun named-node-= (a b)
948 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
949 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
950 (equal (stp:local-name a) (stp:local-name b))))
952 (defun parent-node-= (e f)
953 (and (eql (child-count e)
954 (child-count f))
955 (every #'node= (stp:list-children e) (stp:list-children f))))
957 (defmethod node= ((e stp:element) (f stp:element))
958 (and (named-node-= e f)
959 (parent-node-= e f)
960 (null
961 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
962 :test #'node=))
963 (block nil
964 (flet ((check-namespaces (a b)
965 (let ((result ()))
966 (stp:map-extra-namespaces
967 (lambda (k v)
968 (unless (equal v (stp:find-namespace k b))
969 (return nil)))
971 result)))
972 (check-namespaces e f)
973 (check-namespaces f e))
974 t)))
976 (defmethod node= ((a stp:node) (b stp:node))
977 nil)
979 (defmethod node= ((e stp:document) (f stp:document))
980 (parent-node-= e f))
982 (defmethod node= ((a stp:attribute) (b stp:attribute))
983 (and (named-node-= a b)
984 (equal (stp:value a) (stp:value b))))
986 (defmethod node= ((a stp:comment) (b stp:comment))
987 (equal (stp:data a) (stp:data b)))
989 (defmethod node= ((a stp:text) (b stp:text))
990 (equal (stp:data a) (stp:data b)))
992 (defmethod node= ((a stp:processing-instruction)
993 (b stp:processing-instruction))
994 (and (equal (stp:data a) (stp:data b))
995 (equal (stp:target a) (stp:target b))))
997 (defmethod node= ((a stp:document-type) (b stp:document-type))
998 (and (equal (stp:root-element-name a) (stp:root-element-name b))
999 (equal (stp:public-id a) (stp:public-id b))
1000 (equal (stp:system-id a) (stp:system-id b))
1001 (equal (stp:internal-subset a) (stp:internal-subset b))))
1003 (xpath-sys:define-xpath-function/eager
1004 xslt :print
1005 (thing)
1006 (if (xpath:node-set-p thing)
1007 (loop
1008 initially (format t ";;; node set:~%")
1009 for i from 0
1010 for node in (xpath:all-nodes thing)
1012 (format t ";;; ~D: ~A~%" i (type-of node)))
1013 (format t ";;; ~A~%" thing))
1014 thing)