First attempt at case-order support
[xuriella.git] / test.lisp
blobebacf55b5feb3ea538da6ba9fce6f1115de8f0db
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 ;; temporary configuration until we support enough XSLT that it's worth
274 ;; running all tests:
275 (defparameter *default-categories*
276 ;; '("XSLT-Data-Model" "XPath-Expression" "XPath-Data-Model")
277 nil)
279 (defun dribble-tests (&optional (category *default-categories*)
280 (d *tests-directory*))
281 (let ((*package* (find-package 'cl-user))
282 (*print-circle* nil))
283 (with-open-file (dribble
284 (merge-pathnames "TEST"
285 (slot-value (asdf:find-system :xuriella)
286 'asdf::relative-pathname))
287 :direction :output
288 :if-exists :supersede
289 :external-format :utf-8)
290 (let* ((dribble (make-broadcast-stream dribble *standard-output*))
291 (*standard-output* dribble)
292 (*trace-output* dribble)
293 (*error-output* dribble)
294 (*terminal-io* (make-two-way-stream *standard-input* dribble)))
295 (handler-bind ((warning
296 (lambda (c)
297 (warn "~A" (replace-junk (princ-to-string c)))
298 (muffle-warning c))))
299 (run-tests category d))))))
301 (defparameter *bad-tests*
302 '( ;; some tests wants us to recover from this error, yet this one doesn't:
303 "copy_copy61"
304 "copy_copy62"
306 ;; we perform recovery, but saxon doesn't. Recovery results in non-XML
307 ;; output, which we can't parse for comparison against the official test
308 ;; case.
309 "output_output75"
311 ;; we'd pass these tests, but the test authors forgot to declare the
312 ;; entity they're writing, so we can't parse it for comparison.
313 "output_output06"
314 "output_output10"
315 "output_output61"
317 ;; another similar test where the output is unparsable, except that
318 ;; here an entity declaration wouldn't have helped either:
319 "Copying_ResultTreeFragmentWithEscapedText"
321 ;; the following tests take a lot of time due to the problems of current matching algorithm:
322 "impincl_impincl16"
323 "match_match13"
324 ;; stack exhaustion -- matching problem i think
325 "Keys_PerfRepro3"
326 ;; probably the same problem (but I haven't checked):
327 "numbering_numbering03"
328 "numbering_numbering10"
329 "numbering_numbering11"
330 "numbering_numbering80"
331 "numbering_numbering81"
332 "numbering_numbering94"
333 "numbering_numbering95"
334 "Import__91164"))
336 (defun run-tests (&optional (categories *default-categories*)
337 (d *tests-directory*))
338 (unless (listp categories)
339 (setf categories (list categories)))
340 (klacks:with-open-source
341 (source (klacks:make-tapping-source
342 (cxml:make-source (merge-pathnames "katalog.xml" d))))
343 (let ((*default-pathname-defaults* (merge-pathnames d)))
344 (map-tests #'run-test
345 source
346 :test (lambda (test)
347 (and (or (null categories)
348 (find (test-category test)
349 categories
350 :test #'equal))
351 (not (find (test-id test)
352 *bad-tests*
353 :test #'equal))))))))
355 (defun run-named-test (name &optional (d *tests-directory*))
356 (klacks:with-open-source
357 (source (klacks:make-tapping-source
358 (cxml:make-source (merge-pathnames "katalog.xml" d))))
359 (let ((*default-pathname-defaults* (merge-pathnames d))
360 (*break-on-signals* 'error))
361 (map-tests #'run-test
362 source
363 :test (lambda (test) (equal (test-id test) name))))))
365 (defun copy-file (p q)
366 (with-open-file (in p :element-type '(unsigned-byte 8))
367 (with-open-file (out q
368 :element-type '(unsigned-byte 8)
369 :direction :output
370 :if-exists :rename-and-delete)
371 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
372 (loop for pos = (read-sequence buf in)
373 until (zerop pos)
374 do (write-sequence buf out :end pos))))))
376 (defun find-named-test (name &optional (d *tests-directory*))
377 (klacks:with-open-source
378 (source (klacks:make-tapping-source
379 (cxml:make-source (merge-pathnames "katalog.xml" d))))
380 (block nil
381 (map-tests (lambda (test)
382 (return test))
383 source
384 :test (lambda (test) (equal (test-id test) name))))))
386 (defun copy-test-files (name &optional (d *tests-directory*))
387 (let* ((test (find-named-test name d))
388 (*default-pathname-defaults* (merge-pathnames d))
389 (*break-on-signals* 'error)
390 (target-dir (merge-pathnames "copied-test/"
391 (asdf:component-pathname
392 (asdf:find-system :xuriella))))
393 (xsl (merge-pathnames "test.xsl" target-dir))
394 (xml (merge-pathnames "test.xml" target-dir))
395 (txt (merge-pathnames "official-output.txt" target-dir))
396 (expected (merge-pathnames "expected.xml" target-dir))
397 (actual (merge-pathnames "actual.xml" target-dir)))
398 (ensure-directories-exist target-dir)
399 (copy-file (test-stylesheet-pathname test) xsl)
400 (copy-file (test-data-pathname test) xml)
401 (when (test-official-output-pathname test)
402 (copy-file (test-official-output-pathname test) txt))
403 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
404 (format t "Test data copied to:~% ~A~%~%" xml)
405 (when (test-official-output-pathname test)
406 (format t "Official output file:~% ~A~%~%" txt))
407 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
408 (namestring target-dir)
409 (enough-namestring xsl target-dir)
410 (enough-namestring xml target-dir)
411 (enough-namestring expected target-dir))
412 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
413 (namestring target-dir)
414 (enough-namestring xml target-dir)
415 (enough-namestring xsl target-dir)
416 (enough-namestring expected target-dir))
417 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
418 (namestring target-dir)
419 (enough-namestring xml target-dir)
420 (enough-namestring xsl target-dir)
421 (enough-namestring expected target-dir))
422 (format t "Run xuriella like this:~%")
423 `(apply-stylesheet ,xsl ,xml :output ,actual)))
425 (defun map-tests (run-test source &key (test (constantly t)))
426 (let ((total 0)
427 (pass 0))
428 (loop
429 while (klacks:find-event source :start-element)
430 for lname = (klacks:current-lname source)
432 (cond
433 ((equal lname "test-case")
434 (let* ((<test-case>
435 (stp:document-element
436 (klacks:serialize-element source (stp:make-builder))))
437 (test-case (parse-test <test-case>)))
438 (when (funcall test test-case)
439 (incf total)
440 (when (funcall run-test test-case)
441 (incf pass)))))
443 (klacks:skip source :start-element))))
444 (format t "~&Passed ~D/~D tests.~%" pass total)))
446 (defun parse-test (<test-case>)
447 (stp:with-attributes (id category operation
448 data stylesheet data-2 stylesheet-2
449 output compare)
450 <test-case>
451 (make-instance 'test-case
452 :id id
453 :category category
454 :operation operation
455 :data-pathname data
456 :stylesheet-pathname stylesheet
457 :data-pathname-2 data-2
458 :stylesheet-pathname-2 stylesheet-2
459 :output-pathname output
460 :output-compare compare)))
462 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
463 ;; beginning, if any.
464 (defun slurp-for-comparison (p)
465 (with-open-file (s p :element-type '(unsigned-byte 8))
466 (unless (and (eql (read-byte s nil) #xef)
467 (eql (read-byte s nil) #xbb)
468 (eql (read-byte s nil) #xbf))
469 (file-position s 0))
470 (if (plusp (file-length s))
471 (slurp-for-comparison-1 p s t)
472 "<wrapper/>")))
474 (defun slurp-for-comparison-1 (p s junk-info)
475 (let ((pos (file-position s)) ;for UTF-8 "BOM"
476 (xstream (runes:make-xstream s :speed 1))
477 (prev-pos 0))
478 (setf (runes:xstream-name xstream)
479 (cxml::make-stream-name
480 :entity-name "main document"
481 :entity-kind :main
482 :uri (cxml::pathname-to-uri (merge-pathnames p))))
483 (let ((source
484 (flet ((er (pub sys)
485 pub sys
486 (flexi-streams:make-in-memory-input-stream
487 #())))
488 (cxml:make-source xstream
489 :pathname p
490 :entity-resolver #'er))))
491 (unless (eq junk-info :nada)
492 (loop
493 for key = (progn
494 (setf prev-pos (runes:xstream-position xstream))
495 (klacks:peek-next source))
496 until (eq key :start-document))
497 (cxml::with-source (source cxml::context)
498 (when (eq (cxml::zstream-token-category
499 (cxml::main-zstream cxml::context))
500 :NMTOKEN)
501 ;; oops, doesn't look like XML at all
502 (file-position s pos)
503 (return-from slurp-for-comparison-1
504 (slurp-for-comparison-1 p s :nada)))))
505 (etypecase junk-info
506 (integer
507 (dotimes (x junk-info)
508 (setf prev-pos (runes:xstream-position xstream))
509 (klacks:peek-next source)))
510 ((eql t)
511 (let ((nskip 0))
512 (handler-case
513 (loop
514 (case (klacks:peek-next source)
515 (:start-element (return))
516 (:characters
517 (if (whitespacep (klacks:current-characters source))
518 (incf nskip)
519 (return)))
521 (incf nskip))))
522 ((or file-error cxml:xml-parse-error) ()
523 (when (zerop nskip)
524 (setf nskip nil))))
525 ;; retry
526 (with-open-file (u p :element-type '(unsigned-byte 8))
527 (file-position u pos)
528 (return-from slurp-for-comparison-1
529 (slurp-for-comparison-1 p u nskip)))))
530 ((member nil :nada)))
531 (with-output-to-string (r)
532 (let* ((seen-char
533 (cxml::with-source (source cxml::context)
534 (ecase (cxml::zstream-token-category
535 (cxml::main-zstream cxml::context))
536 (:seen-< #\<)
537 (:? #\?)
538 ((nil :s)
539 (setf prev-pos (runes:xstream-position xstream))
540 nil))))
541 (off-by-one-p (or seen-char (eq junk-info :nada)))
542 (new-pos (- prev-pos (if off-by-one-p 1 0))))
543 ;; copy doctype over
544 (with-open-file (u p :element-type '(unsigned-byte 8))
545 (file-position u pos)
546 (let ((y (runes:make-xstream u :speed 1)))
547 (loop
548 while (< (runes:xstream-position y) new-pos)
549 do (write-char (runes:read-rune y) r))))
550 (write-line "<wrapper>" r)
551 (when seen-char
552 (write-char seen-char r)))
553 (loop
554 for char = (runes:read-rune xstream)
555 until (eq char :eof)
556 do (write-char char r))
557 (write-line "</wrapper>" r)))))
559 (defun parse-for-comparison (p)
560 (let* ((d (flet ((er (pub sys)
561 pub sys
562 (flexi-streams:make-in-memory-input-stream
563 #())))
564 (cxml:parse (slurp-for-comparison p)
565 (make-text-normalizer (stp:make-builder))
566 :entity-resolver #'er)))
567 (de (stp:document-element d)))
568 (let ((first (stp:first-child de)))
569 (when (typep first 'stp:text)
570 (cond
571 ((whitespacep (stp:data first))
572 (stp:delete-child first de))
574 (setf (stp:data first)
575 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
576 (stp:data first)
577 ""))))))
578 (let ((last (stp:last-child de)))
579 (when (typep last 'stp:text)
580 (cond
581 ((whitespacep (stp:data last))
582 (stp:delete-child last de))
584 (setf (stp:data last)
585 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
586 (stp:data last)
587 ""))))))
590 (defun output-equal-p (compare p q &key normalize)
591 (handler-case
592 (ecase compare
593 (:xml (xml-output-equal-p p q normalize))
594 (:html (html-output-equal-p p q))
595 (:text (text-output-equal-p p q)))
596 ((or error parse-number::invalid-number) (c)
597 (warn "comparison failed: ~A" c)
598 nil)))
600 ;; Workaround for namespace_namespace23 and other tests:
601 ;; - For these tests, saxon and msxsl output a declaration for the XSL
602 ;; namespace without using that declaration.
603 ;; - I think saxon and msxsl are both wrong.
604 ;; - The official test output agrees with my assessment.
605 ;; (So does libxslt, but that's not to be trusted. :-))
606 ;; - Here's the catch: The official test output is broken in its whitespace
607 ;; handling.
608 ;; So let's normalize spaces in test output that looks like an XSLT
609 ;; stylesheet, allowing us to pass these tests using the official test output.
610 (defun maybe-normalize-test-spaces (wrapper force)
611 (stp:do-children (wrapper-child wrapper)
612 (when (and (typep wrapper-child 'stp:element)
613 (or (equal (stp:namespace-uri wrapper-child) *xsl*)
614 force))
615 (strip-stylesheet wrapper-child)
616 (labels ((recurse (e &optional preserve)
617 (stp:do-children (child e)
618 (typecase child
619 (stp:text
620 (setf (stp:data child)
621 (normalize-whitespace (stp:data child))))
622 (stp:element
623 (stp:with-attributes ((space "space" *xml*))
624 child
625 (let ((new-preserve
626 (cond
627 ((namep child "text") t)
628 ((not space) preserve)
629 ((equal space "preserve") t)
630 (t nil))))
631 (recurse child new-preserve))))))))
632 (recurse wrapper-child)))))
634 (defun xml-output-equal-p (p q normalize)
635 (let ((r (parse-for-comparison p))
636 (s (parse-for-comparison q)))
637 (maybe-normalize-test-spaces (stp:document-element r) normalize)
638 (maybe-normalize-test-spaces (stp:document-element s) normalize)
639 (and (let ((u (stp:document-type r))
640 (v (stp:document-type s)))
641 (if u
642 (and v (node= u v))
643 (null v)))
644 (node= (stp:document-element r) (stp:document-element s)))))
646 ;; FIXME: don't do this in <pre> etc.
647 (defun normalize-html-whitespace (node)
648 (when (typep node 'stp:parent-node)
649 ;; ignore newlines after start tags completely
650 (let ((first (stp:first-child node)))
651 (when (and (typep first 'stp:text)
652 (alexandria:starts-with #\newline (stp:data first)))
653 (setf (stp:data first) (subseq (stp:data first) 1))))
654 ;; ignore newlines before end tags completely
655 (let ((last (stp:last-child node)))
656 (when (and (typep last 'stp:text)
657 (alexandria:ends-with #\newline (stp:data last)))
658 (setf (stp:data last)
659 (subseq (stp:data last) 0 (length (stp:data last))))))
660 ;; normalize sequences of whitespace
661 (stp:do-children (child node)
662 (if (typep child 'stp:text)
663 (setf (stp:data child)
664 (let ((str (normalize-whitespace (stp:data child))))
665 (when
666 ;; FIXME! Here we remove whitespace entirely.
667 ;; Totally incorrect, but I don't see how we could
668 ;; watch Saxon's output otherwise.
669 (equal str " ")
670 (setf str ""))
671 str))
672 (normalize-html-whitespace child)))
673 ;; just to be sure, join adjacent nodes
674 (cxml-stp-impl::normalize-text-nodes! node)))
676 ;; FIXME: this check is too lenient, because chtml is an error-correcting
677 ;; parser.
678 (defun html-output-equal-p (p q)
679 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
680 (s (chtml:parse (pathname q) (stp:make-builder))))
681 (normalize-html-whitespace r)
682 (normalize-html-whitespace s)
683 (node= (stp:document-element r) (stp:document-element s))))
685 (defun text-output-equal-p (p q)
686 (with-open-file (a p :element-type '(unsigned-byte 8))
687 (with-open-file (b q :element-type '(unsigned-byte 8))
688 (let ((len (file-length a)))
689 (and (eql len (file-length b))
690 (let ((d (make-array len :element-type '(unsigned-byte 8)))
691 (e (make-array len :element-type '(unsigned-byte 8))))
692 (read-sequence d a)
693 (read-sequence e b)
694 (equalp d e)))))))
696 (defun strip-addresses (str)
697 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
699 (defun slurp-output-method (p)
700 (xpath:with-namespaces ((nil #.*xsl*))
701 (let* ((d (handler-bind
702 ((warning #'muffle-warning))
703 (cxml:parse (pathname p) (stp:make-builder))))
704 (output (xpath:first-node (xpath:evaluate "//output" d))))
705 (if output
706 (let ((method (stp:attribute-value output "method")))
707 (if method
708 (intern (string-upcase method) :keyword)
709 :xml))
710 :xml))))
712 (defun replace-junk (str)
713 (cl-ppcre:regex-replace-all
714 `(:group ,(namestring *tests-directory*))
715 (map 'string
716 (lambda (c)
717 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
719 #\?))
720 str)
721 "..."))
723 (defun run-test (test)
724 (let ((expected-saxon (test-output-pathname test "saxon"))
725 #+xuriella::xsltproc
726 (expected-xsltproc (test-output-pathname test "xsltproc"))
727 (actual (test-output-pathname test "xuriella"))
728 (official (test-official-output-pathname test))
729 (force-normalization
730 (find (test-id test) '("Namespace-alias__91782") :test #'equal))
731 (output-method nil))
732 (handler-bind ((|hey test suite, this is an HTML document|
733 (lambda (c)
734 (declare (ignore c))
735 (setf output-method :html))))
736 (labels ((uri-resolver (uri)
737 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
738 uri)
739 (cxml::pathname-to-uri
740 (merge-pathnames
741 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
742 *tests-directory*))
743 uri))
744 (doit ()
745 (with-open-file (s actual
746 :if-exists :rename-and-delete
747 :direction :output
748 :element-type '(unsigned-byte 8))
749 (handler-bind ((xslt-error
750 (lambda (c)
751 (declare (ignore c))
752 (when (find-restart 'recover)
753 (invoke-restart 'recover)))))
754 (apply-stylesheet (pathname (test-stylesheet-pathname test))
755 (pathname (test-data-pathname test))
756 :output s
757 :uri-resolver #'uri-resolver))))
758 (pp (label pathname)
759 (when pathname
760 (format t " ~A: ~A~%"
761 label
762 (enough-namestring pathname *tests-directory*))))
763 (report (ok &optional (fmt "") &rest args)
764 (write-string
765 (replace-junk
766 (strip-addresses
767 (format nil "~&~:[FAIL~;PASS~] ~A [~A]~?~%"
769 (test-id test)
770 (test-category test)
772 args))))
773 (pp "Stylesheet" (test-stylesheet-pathname test))
774 (pp "Data" (test-data-pathname test))
775 (pp "Supplemental stylesheet"
776 (test-stylesheet-pathname-2 test))
777 (pp "Supplemental data" (test-data-pathname-2 test))
778 (pp "Expected output (1)" expected-saxon)
779 #+xuriella::xsltproc
780 (pp "Expected output (2)" expected-xsltproc)
781 (pp "Actual output" actual)
782 (terpri)
783 ok))
784 (cond
785 ((equal (test-operation test) "standard")
786 (handler-case
787 (progn
788 (when (find (test-id test)
789 nil ;;'("axes_axes47" "attribset_attribset20")
790 :test #'equal)
791 (error "skipping problematic test"))
792 (doit)
793 (let* ((output-method
794 (or output-method
795 (slurp-output-method
796 (test-stylesheet-pathname test))))
797 (saxon-matches-p
798 (output-equal-p output-method
799 expected-saxon
800 actual))
801 #+xuriella::xsltproc
802 (xsltproc-matches-p
803 (output-equal-p output-method
804 expected-xsltproc
805 actual))
806 (official-matches-p
807 (output-equal-p output-method
808 official
809 actual
810 :normalize force-normalization)))
811 (cond
812 ((or saxon-matches-p
813 #+xuriella::xsltproc xsltproc-matches-p
814 official-matches-p)
815 (report t)
816 #+xuriella::xsltproc
817 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
818 saxon-matches-p
819 xsltproc-matches-p
820 (if saxon-matches-p
821 (not xsltproc-matches-p)
822 xsltproc-matches-p)))
824 (report nil ": output doesn't match")))))
825 ((or error parse-number::invalid-number) (c)
826 (report nil ": ~A" c))))
828 (handler-case
829 (doit)
830 (xslt-error (c)
831 (report t ": raised an xslt-error as expected" c))
832 ((or error parse-number::invalid-number) (c)
833 (report nil ": condition of incorrect type: ~%~A" c))
834 (:no-error (result)
835 (cond
836 ((not (and official (probe-file official)))
837 (report nil ": expected error not signalled: " result))
838 ((output-equal-p
839 (or output-method
840 (slurp-output-method (test-stylesheet-pathname test)))
841 official
842 actual
843 :normalize force-normalization)
844 (report t))
846 (report nil ": saxon error not signalled and official output not a match")))))))))))
848 (defun run-xpath-tests ()
849 (run-tests '("XPath-Expression" "XSLT-Data-Model")))
852 ;;;; from cxml-stp-test
854 (defun assert-node= (a b)
855 (unless (node= a b)
856 (error "assertion failed: ~S and ~S are not NODE=" a b)))
858 (defun child-count (node)
859 (stp:count-children-if (constantly t) node))
861 (defun named-node-= (a b)
862 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
863 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
864 (equal (stp:local-name a) (stp:local-name b))))
866 (defun parent-node-= (e f)
867 (and (eql (child-count e)
868 (child-count f))
869 (every #'node= (stp:list-children e) (stp:list-children f))))
871 (defmethod node= ((e stp:element) (f stp:element))
872 (and (named-node-= e f)
873 (parent-node-= e f)
874 (null
875 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
876 :test #'node=))
877 (block nil
878 (flet ((check-namespaces (a b)
879 (let ((result ()))
880 (stp:map-extra-namespaces
881 (lambda (k v)
882 (unless (equal v (stp:find-namespace k b))
883 (return nil)))
885 result)))
886 (check-namespaces e f)
887 (check-namespaces f e))
888 t)))
890 (defmethod node= ((a stp:node) (b stp:node))
891 nil)
893 (defmethod node= ((e stp:document) (f stp:document))
894 (parent-node-= e f))
896 (defmethod node= ((a stp:attribute) (b stp:attribute))
897 (and (named-node-= a b)
898 (equal (stp:value a) (stp:value b))))
900 (defmethod node= ((a stp:comment) (b stp:comment))
901 (equal (stp:data a) (stp:data b)))
903 (defmethod node= ((a stp:text) (b stp:text))
904 (equal (stp:data a) (stp:data b)))
906 (defmethod node= ((a stp:processing-instruction)
907 (b stp:processing-instruction))
908 (and (equal (stp:data a) (stp:data b))
909 (equal (stp:target a) (stp:target b))))
911 (defmethod node= ((a stp:document-type) (b stp:document-type))
912 (and (equal (stp:root-element-name a) (stp:root-element-name b))
913 (equal (stp:public-id a) (stp:public-id b))
914 (equal (stp:system-id a) (stp:system-id b))
915 (equal (stp:internal-subset a) (stp:internal-subset b))))
917 (xpath-sys:define-xpath-function/eager
918 xslt :print
919 (thing)
920 (if (xpath:node-set-p thing)
921 (loop
922 initially (format t ";;; node set:~%")
923 for i from 0
924 for node in (xpath:all-nodes thing)
926 (format t ";;; ~D: ~A~%" i (type-of node)))
927 (format t ";;; ~A~%" thing))
928 thing)