xsl:number fixes: non-element nodes
[xuriella.git] / test.lisp
blob55449ac473724b5df9b604a86d5155082572823c
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 (with-open-file (dribble
283 (merge-pathnames "TEST"
284 (slot-value (asdf:find-system :xuriella)
285 'asdf::relative-pathname))
286 :direction :output
287 :if-exists :supersede
288 :external-format :utf-8)
289 (let* ((dribble (make-broadcast-stream dribble *standard-output*))
290 (*standard-output* dribble)
291 (*trace-output* dribble)
292 (*error-output* dribble)
293 (*terminal-io* (make-two-way-stream *standard-input* dribble)))
294 (handler-bind ((warning
295 (lambda (c)
296 (warn "~A" (replace-junk (princ-to-string c)))
297 (muffle-warning c))))
298 (run-tests category d))))))
300 (defparameter *bad-tests*
301 '( ;; some tests wants us to recover from this error, yet this one doesn't:
302 "copy_copy61"
303 "copy_copy62"
304 ;; the following tests take a lot of time due to the problems of current matching algorithm:
305 "impincl_impincl16"
306 "match_match13"
307 ;; stack exhaustion -- matching problem i think
308 "Keys_PerfRepro3"
309 ;; probably the same problem (but I haven't checked):
310 "numbering_numbering03"
311 "numbering_numbering10"
312 "numbering_numbering11"
313 "numbering_numbering80"
314 "numbering_numbering81"
315 "numbering_numbering94"
316 "numbering_numbering95"
317 "Import__91164"))
319 (defun run-tests (&optional (categories *default-categories*)
320 (d *tests-directory*))
321 (unless (listp categories)
322 (setf categories (list categories)))
323 (klacks:with-open-source
324 (source (klacks:make-tapping-source
325 (cxml:make-source (merge-pathnames "katalog.xml" d))))
326 (let ((*default-pathname-defaults* (merge-pathnames d)))
327 (map-tests #'run-test
328 source
329 :test (lambda (test)
330 (and (or (null categories)
331 (find (test-category test)
332 categories
333 :test #'equal))
334 (not (find (test-id test)
335 *bad-tests*
336 :test #'equal))))))))
338 (defun run-named-test (name &optional (d *tests-directory*))
339 (klacks:with-open-source
340 (source (klacks:make-tapping-source
341 (cxml:make-source (merge-pathnames "katalog.xml" d))))
342 (let ((*default-pathname-defaults* (merge-pathnames d))
343 (*break-on-signals* 'error))
344 (map-tests #'run-test
345 source
346 :test (lambda (test) (equal (test-id test) name))))))
348 (defun copy-file (p q)
349 (with-open-file (in p :element-type '(unsigned-byte 8))
350 (with-open-file (out q
351 :element-type '(unsigned-byte 8)
352 :direction :output
353 :if-exists :rename-and-delete)
354 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
355 (loop for pos = (read-sequence buf in)
356 until (zerop pos)
357 do (write-sequence buf out :end pos))))))
359 (defun find-named-test (name &optional (d *tests-directory*))
360 (klacks:with-open-source
361 (source (klacks:make-tapping-source
362 (cxml:make-source (merge-pathnames "katalog.xml" d))))
363 (block nil
364 (map-tests (lambda (test)
365 (return test))
366 source
367 :test (lambda (test) (equal (test-id test) name))))))
369 (defun copy-test-files (name &optional (d *tests-directory*))
370 (let* ((test (find-named-test name d))
371 (*default-pathname-defaults* (merge-pathnames d))
372 (*break-on-signals* 'error)
373 (target-dir (merge-pathnames "copied-test/"
374 (asdf:component-pathname
375 (asdf:find-system :xuriella))))
376 (xsl (merge-pathnames "test.xsl" target-dir))
377 (xml (merge-pathnames "test.xml" target-dir))
378 (txt (merge-pathnames "official-output.txt" target-dir))
379 (expected (merge-pathnames "expected.xml" target-dir))
380 (actual (merge-pathnames "actual.xml" target-dir)))
381 (ensure-directories-exist target-dir)
382 (copy-file (test-stylesheet-pathname test) xsl)
383 (copy-file (test-data-pathname test) xml)
384 (when (test-official-output-pathname test)
385 (copy-file (test-official-output-pathname test) txt))
386 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
387 (format t "Test data copied to:~% ~A~%~%" xml)
388 (when (test-official-output-pathname test)
389 (format t "Official output file:~% ~A~%~%" txt))
390 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
391 (namestring target-dir)
392 (enough-namestring xsl target-dir)
393 (enough-namestring xml target-dir)
394 (enough-namestring expected target-dir))
395 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
396 (namestring target-dir)
397 (enough-namestring xml target-dir)
398 (enough-namestring xsl target-dir)
399 (enough-namestring expected target-dir))
400 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
401 (namestring target-dir)
402 (enough-namestring xml target-dir)
403 (enough-namestring xsl target-dir)
404 (enough-namestring expected target-dir))
405 (format t "Run xuriella like this:~%")
406 `(apply-stylesheet ,xsl ,xml :output ,actual)))
408 (defun map-tests (run-test source &key (test (constantly t)))
409 (let ((total 0)
410 (pass 0))
411 (loop
412 while (klacks:find-event source :start-element)
413 for lname = (klacks:current-lname source)
415 (cond
416 ((equal lname "test-case")
417 (let* ((<test-case>
418 (stp:document-element
419 (klacks:serialize-element source (stp:make-builder))))
420 (test-case (parse-test <test-case>)))
421 (when (funcall test test-case)
422 (incf total)
423 (when (funcall run-test test-case)
424 (incf pass)))))
426 (klacks:skip source :start-element))))
427 (format t "~&Passed ~D/~D tests.~%" pass total)))
429 (defun parse-test (<test-case>)
430 (stp:with-attributes (id category operation
431 data stylesheet data-2 stylesheet-2
432 output compare)
433 <test-case>
434 (make-instance 'test-case
435 :id id
436 :category category
437 :operation operation
438 :data-pathname data
439 :stylesheet-pathname stylesheet
440 :data-pathname-2 data-2
441 :stylesheet-pathname-2 stylesheet-2
442 :output-pathname output
443 :output-compare compare)))
445 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
446 ;; beginning, if any.
447 (defun slurp-for-comparison (p)
448 (with-open-file (s p :element-type '(unsigned-byte 8))
449 (unless (and (eql (read-byte s nil) #xef)
450 (eql (read-byte s nil) #xbb)
451 (eql (read-byte s nil) #xbf))
452 (file-position s 0))
453 (if (plusp (file-length s))
454 (let ((xstream (runes:make-xstream s :speed 1)))
455 (setf (runes:xstream-name xstream)
456 (cxml::make-stream-name
457 :entity-name "main document"
458 :entity-kind :main
459 :uri (cxml::pathname-to-uri (merge-pathnames p))))
460 (let ((source (cxml:make-source xstream :pathname p)))
461 (loop
462 for key = (klacks:peek-next source)
463 until (eq key :start-document))
464 (with-output-to-string (r)
465 (write-line "<wrapper>" r)
466 (cxml::with-source (source cxml::context)
467 (when (eq (cxml::zstream-token-category
468 (cxml::main-zstream cxml::context))
469 :seen-<)
470 (write-char #\< r)))
471 (loop
472 for char = (runes:read-rune xstream)
473 until (eq char :eof)
474 do (write-char char r))
475 (write-line "</wrapper>" r))))
476 "<wrapper/>")))
478 (defun parse-for-comparison (p)
479 (let* ((d (cxml:parse (slurp-for-comparison p)
480 (make-text-normalizer (stp:make-builder))))
481 (de (stp:document-element d)))
482 (let ((first (stp:first-child de)))
483 (when (typep first 'stp:text)
484 (cond
485 ((whitespacep (stp:data first))
486 (stp:delete-child first de))
488 (setf (stp:data first)
489 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
490 (stp:data first)
491 ""))))))
492 (let ((last (stp:last-child de)))
493 (when (typep last 'stp:text)
494 (cond
495 ((whitespacep (stp:data last))
496 (stp:delete-child last de))
498 (setf (stp:data last)
499 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
500 (stp:data last)
501 ""))))))
504 (defun output-equal-p (compare p q &key normalize)
505 (handler-case
506 (ecase compare
507 (:xml (xml-output-equal-p p q normalize))
508 (:html (html-output-equal-p p q))
509 (:text (text-output-equal-p p q)))
510 ((or error parse-number::invalid-number) (c)
511 (warn "comparison failed: ~A" c)
512 nil)))
514 ;; Workaround for namespace_namespace23 and other tests:
515 ;; - For these tests, saxon and msxsl output a declaration for the XSL
516 ;; namespace without using that declaration.
517 ;; - I think saxon and msxsl are both wrong.
518 ;; - The official test output agrees with my assessment.
519 ;; (So does libxslt, but that's not to be trusted. :-))
520 ;; - Here's the catch: The official test output is broken in its whitespace
521 ;; handling.
522 ;; So let's normalize spaces in test output that looks like an XSLT
523 ;; stylesheet, allowing us to pass these tests using the official test output.
524 (defun maybe-normalize-test-spaces (wrapper force)
525 (stp:do-children (wrapper-child wrapper)
526 (when (and (typep wrapper-child 'stp:element)
527 (or (equal (stp:namespace-uri wrapper-child) *xsl*)
528 force))
529 (strip-stylesheet wrapper-child)
530 (labels ((recurse (e &optional preserve)
531 (stp:do-children (child e)
532 (typecase child
533 (stp:text
534 (setf (stp:data child)
535 (normalize-whitespace (stp:data child))))
536 (stp:element
537 (stp:with-attributes ((space "space" *xml*))
538 child
539 (let ((new-preserve
540 (cond
541 ((namep child "text") t)
542 ((not space) preserve)
543 ((equal space "preserve") t)
544 (t nil))))
545 (recurse child new-preserve))))))))
546 (recurse wrapper-child)))))
548 (defun xml-output-equal-p (p q normalize)
549 (let ((r (parse-for-comparison p))
550 (s (parse-for-comparison q)))
551 (maybe-normalize-test-spaces (stp:document-element r) normalize)
552 (maybe-normalize-test-spaces (stp:document-element s) normalize)
553 (node= (stp:document-element r) (stp:document-element s))))
555 ;; FIXME: don't do this in <pre> etc.
556 (defun normalize-html-whitespace (node)
557 (when (typep node 'stp:parent-node)
558 ;; ignore newlines after start tags completely
559 (let ((first (stp:first-child node)))
560 (when (and (typep first 'stp:text)
561 (alexandria:starts-with #\newline (stp:data first)))
562 (setf (stp:data first) (subseq (stp:data first) 1))))
563 ;; ignore newlines before end tags completely
564 (let ((last (stp:last-child node)))
565 (when (and (typep last 'stp:text)
566 (alexandria:ends-with #\newline (stp:data last)))
567 (setf (stp:data last)
568 (subseq (stp:data last) 0 (length (stp:data last))))))
569 ;; normalize sequences of whitespace
570 (stp:do-children (child node)
571 (if (typep child 'stp:text)
572 (setf (stp:data child)
573 (let ((str (normalize-whitespace (stp:data child))))
574 (when
575 ;; FIXME! Here we remove whitespace entirely.
576 ;; Totally incorrect, but I don't see how we could
577 ;; watch Saxon's output otherwise.
578 (equal str " ")
579 (setf str ""))
580 str))
581 (normalize-html-whitespace child)))
582 ;; just to be sure, join adjacent nodes
583 (cxml-stp-impl::normalize-text-nodes! node)))
585 ;; FIXME: this check is too lenient, because chtml is an error-correcting
586 ;; parser.
587 (defun html-output-equal-p (p q)
588 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
589 (s (chtml:parse (pathname q) (stp:make-builder))))
590 (normalize-html-whitespace r)
591 (normalize-html-whitespace s)
592 (node= (stp:document-element r) (stp:document-element s))))
594 (defun text-output-equal-p (p q)
595 (with-open-file (a p :element-type '(unsigned-byte 8))
596 (with-open-file (b q :element-type '(unsigned-byte 8))
597 (let ((len (file-length a)))
598 (and (eql len (file-length b))
599 (let ((d (make-array len :element-type '(unsigned-byte 8)))
600 (e (make-array len :element-type '(unsigned-byte 8))))
601 (read-sequence d a)
602 (read-sequence e b)
603 (equalp d e)))))))
605 (defun strip-addresses (str)
606 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
608 (defun slurp-output-method (p)
609 (xpath:with-namespaces ((nil #.*xsl*))
610 (let* ((d (handler-bind
611 ((warning #'muffle-warning))
612 (cxml:parse (pathname p) (stp:make-builder))))
613 (output (xpath:first-node (xpath:evaluate "//output" d))))
614 (if output
615 (let ((method (stp:attribute-value output "method")))
616 (if method
617 (intern (string-upcase method) :keyword)
618 :xml))
619 :xml))))
621 (defun replace-junk (str)
622 (cl-ppcre:regex-replace-all
623 `(:group ,(namestring *tests-directory*))
624 (map 'string
625 (lambda (c)
626 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
628 #\?))
629 str)
630 "..."))
632 (defun run-test (test)
633 (let ((expected-saxon (test-output-pathname test "saxon"))
634 #+xuriella::xsltproc
635 (expected-xsltproc (test-output-pathname test "xsltproc"))
636 (actual (test-output-pathname test "xuriella"))
637 (official (test-official-output-pathname test))
638 (force-normalization
639 (find (test-id test) '("Namespace-alias__91782") :test #'equal)))
640 (labels ((uri-resolver (uri)
641 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
642 uri)
643 (cxml::pathname-to-uri
644 (merge-pathnames
645 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
646 *tests-directory*))
647 uri))
648 (doit ()
649 (with-open-file (s actual
650 :if-exists :rename-and-delete
651 :direction :output
652 :element-type '(unsigned-byte 8))
653 (handler-bind ((xslt-error
654 (lambda (c)
655 (declare (ignore c))
656 (when (find-restart 'recover)
657 (invoke-restart 'recover)))))
658 (apply-stylesheet (pathname (test-stylesheet-pathname test))
659 (pathname (test-data-pathname test))
660 :output s
661 :uri-resolver #'uri-resolver))))
662 (pp (label pathname)
663 (when pathname
664 (format t " ~A: ~A~%"
665 label
666 (enough-namestring pathname *tests-directory*))))
667 (report (ok &optional (fmt "") &rest args)
668 (write-string
669 (replace-junk
670 (strip-addresses
671 (format nil "~&~:[FAIL~;PASS~] ~A [~A]~?~%"
673 (test-id test)
674 (test-category test)
676 args))))
677 (pp "Stylesheet" (test-stylesheet-pathname test))
678 (pp "Data" (test-data-pathname test))
679 (pp "Supplemental stylesheet"
680 (test-stylesheet-pathname-2 test))
681 (pp "Supplemental data" (test-data-pathname-2 test))
682 (pp "Expected output (1)" expected-saxon)
683 #+xuriella::xsltproc
684 (pp "Expected output (2)" expected-xsltproc)
685 (pp "Actual output" actual)
686 (terpri)
687 ok))
688 (cond
689 ((equal (test-operation test) "standard")
690 (handler-case
691 (let ((output-method
692 (slurp-output-method (test-stylesheet-pathname test))))
693 (when (find (test-id test)
694 nil ;;'("axes_axes47" "attribset_attribset20")
695 :test #'equal)
696 (error "skipping problematic test"))
697 (doit)
698 (let ((saxon-matches-p
699 (output-equal-p output-method
700 expected-saxon
701 actual))
702 #+xuriella::xsltproc
703 (xsltproc-matches-p
704 (output-equal-p output-method
705 expected-xsltproc
706 actual))
707 (official-matches-p
708 (output-equal-p output-method
709 official
710 actual
711 :normalize force-normalization)))
712 (cond
713 ((or saxon-matches-p
714 #+xuriella::xsltproc xsltproc-matches-p
715 official-matches-p)
716 (report t)
717 #+xuriella::xsltproc
718 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
719 saxon-matches-p
720 xsltproc-matches-p
721 (if saxon-matches-p
722 (not xsltproc-matches-p)
723 xsltproc-matches-p)))
725 (report nil ": output doesn't match")))))
726 ((or error parse-number::invalid-number) (c)
727 (report nil ": ~A" c))))
729 (handler-case
730 (doit)
731 (xslt-error (c)
732 (report t ": raised an xslt-error as expected" c))
733 ((or error parse-number::invalid-number) (c)
734 (report nil ": condition of incorrect type: ~%~A" c))
735 (:no-error (result)
736 (cond
737 ((not (and official (probe-file official)))
738 (report nil ": expected error not signalled: " result))
739 ((output-equal-p
740 (slurp-output-method (test-stylesheet-pathname test))
741 official
742 actual
743 :normalize force-normalization)
744 (report t))
746 (report nil ": saxon error not signalled and official output not a match"))))))))))
748 (defun run-xpath-tests ()
749 (run-tests '("XPath-Expression" "XSLT-Data-Model")))
752 ;;;; from cxml-stp-test
754 (defun assert-node= (a b)
755 (unless (node= a b)
756 (error "assertion failed: ~S and ~S are not NODE=" a b)))
758 (defun child-count (node)
759 (stp:count-children-if (constantly t) node))
761 (defun named-node-= (a b)
762 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
763 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
764 (equal (stp:local-name a) (stp:local-name b))))
766 (defun parent-node-= (e f)
767 (and (eql (child-count e)
768 (child-count f))
769 (every #'node= (stp:list-children e) (stp:list-children f))))
771 (defmethod node= ((e stp:element) (f stp:element))
772 (and (named-node-= e f)
773 (parent-node-= e f)
774 (null
775 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
776 :test #'node=))
777 (block nil
778 (flet ((check-namespaces (a b)
779 (let ((result ()))
780 (stp:map-extra-namespaces
781 (lambda (k v)
782 (unless (equal v (stp:find-namespace k b))
783 (return nil)))
785 result)))
786 (check-namespaces e f)
787 (check-namespaces f e))
788 t)))
790 (defmethod node= ((a stp:node) (b stp:node))
791 nil)
793 (defmethod node= ((e stp:document) (f stp:document))
794 (parent-node-= e f))
796 (defmethod node= ((a stp:attribute) (b stp:attribute))
797 (and (named-node-= a b)
798 (equal (stp:value a) (stp:value b))))
800 (defmethod node= ((a stp:comment) (b stp:comment))
801 (equal (stp:data a) (stp:data b)))
803 (defmethod node= ((a stp:text) (b stp:text))
804 (equal (stp:data a) (stp:data b)))
806 (defmethod node= ((a stp:processing-instruction)
807 (b stp:processing-instruction))
808 (and (equal (stp:data a) (stp:data b))
809 (equal (stp:target a) (stp:target b))))
811 (defmethod node= ((a stp:document-type) (b stp:document-type))
812 (and (equal (stp:root-element-name a) (stp:root-element-name b))
813 (equal (stp:public-id a) (stp:public-id b))
814 (equal (stp:system-id a) (stp:system-id b))
815 (equal (stp:internal-subset a) (stp:internal-subset b))))
817 (xpath-sys:define-xpath-function/eager
818 xslt :print
819 (thing)
820 (if (xpath:node-set-p thing)
821 (loop
822 initially (format t ";;; node set:~%")
823 for i from 0
824 for node in (xpath:all-nodes thing)
826 (format t ";;; ~D: ~A~%" i (type-of node)))
827 (format t ";;; ~A~%" thing))
828 thing)