always sort according to document order, because xsl:sort might be a stable no-op...
[xuriella.git] / test.lisp
blob1922a13739fa428027aedb5155a21deec9857cb0
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: 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 (run-tests category d)))))
296 (defparameter *bad-tests*
297 '( ;; some tests wants us to recover from this error, yet this one doesn't:
298 "copy_copy61"
299 "copy_copy62"
300 ;; the following test is actually okay, but running it takes longer
301 ;; than I'm willing to wait before every checkin:
302 "Import__91164"))
304 (defun run-tests (&optional (categories *default-categories*)
305 (d *tests-directory*))
306 (unless (listp categories)
307 (setf categories (list categories)))
308 (klacks:with-open-source
309 (source (klacks:make-tapping-source
310 (cxml:make-source (merge-pathnames "katalog.xml" d))))
311 (let ((*default-pathname-defaults* (merge-pathnames d)))
312 (map-tests #'run-test
313 source
314 :test (lambda (test)
315 (and (or (null categories)
316 (find (test-category test)
317 categories
318 :test #'equal))
319 (not (find (test-id test)
320 *bad-tests*
321 :test #'equal))))))))
323 (defun run-named-test (name &optional (d *tests-directory*))
324 (klacks:with-open-source
325 (source (klacks:make-tapping-source
326 (cxml:make-source (merge-pathnames "katalog.xml" d))))
327 (let ((*default-pathname-defaults* (merge-pathnames d))
328 (*break-on-signals* 'error))
329 (map-tests #'run-test
330 source
331 :test (lambda (test) (equal (test-id test) name))))))
333 (defun copy-file (p q)
334 (with-open-file (in p :element-type '(unsigned-byte 8))
335 (with-open-file (out q
336 :element-type '(unsigned-byte 8)
337 :direction :output
338 :if-exists :rename-and-delete)
339 (let ((buf (make-array 8192 :element-type '(unsigned-byte 8))))
340 (loop for pos = (read-sequence buf in)
341 until (zerop pos)
342 do (write-sequence buf out :end pos))))))
344 (defun find-named-test (name &optional (d *tests-directory*))
345 (klacks:with-open-source
346 (source (klacks:make-tapping-source
347 (cxml:make-source (merge-pathnames "katalog.xml" d))))
348 (block nil
349 (map-tests (lambda (test)
350 (return test))
351 source
352 :test (lambda (test) (equal (test-id test) name))))))
354 (defun copy-test-files (name &optional (d *tests-directory*))
355 (let* ((test (find-named-test name d))
356 (*default-pathname-defaults* (merge-pathnames d))
357 (*break-on-signals* 'error)
358 (target-dir (merge-pathnames "copied-test/"
359 (asdf:component-pathname
360 (asdf:find-system :xuriella))))
361 (xsl (merge-pathnames "test.xsl" target-dir))
362 (xml (merge-pathnames "test.xml" target-dir))
363 (txt (merge-pathnames "official-output.txt" target-dir))
364 (expected (merge-pathnames "expected.xml" target-dir))
365 (actual (merge-pathnames "actual.xml" target-dir)))
366 (ensure-directories-exist target-dir)
367 (copy-file (test-stylesheet-pathname test) xsl)
368 (copy-file (test-data-pathname test) xml)
369 (when (test-official-output-pathname test)
370 (copy-file (test-official-output-pathname test) txt))
371 (format t "Test stylesheet copied to:~% ~A~%~%" xsl)
372 (format t "Test data copied to:~% ~A~%~%" xml)
373 (when (test-official-output-pathname test)
374 (format t "Official output file:~% ~A~%~%" txt))
375 (format t "Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
376 (namestring target-dir)
377 (enough-namestring xsl target-dir)
378 (enough-namestring xml target-dir)
379 (enough-namestring expected target-dir))
380 (format t "Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
381 (namestring target-dir)
382 (enough-namestring xml target-dir)
383 (enough-namestring xsl target-dir)
384 (enough-namestring expected target-dir))
385 (format t "Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
386 (namestring target-dir)
387 (enough-namestring xml target-dir)
388 (enough-namestring xsl target-dir)
389 (enough-namestring expected target-dir))
390 (format t "Run xuriella like this:~%")
391 `(apply-stylesheet ,xsl ,xml :output ,actual)))
393 (defun map-tests (run-test source &key (test (constantly t)))
394 (let ((total 0)
395 (pass 0))
396 (loop
397 while (klacks:find-event source :start-element)
398 for lname = (klacks:current-lname source)
400 (cond
401 ((equal lname "test-case")
402 (let* ((<test-case>
403 (stp:document-element
404 (klacks:serialize-element source (stp:make-builder))))
405 (test-case (parse-test <test-case>)))
406 (when (funcall test test-case)
407 (incf total)
408 (when (funcall run-test test-case)
409 (incf pass)))))
411 (klacks:skip source :start-element))))
412 (format t "~&Passed ~D/~D tests.~%" pass total)))
414 (defun parse-test (<test-case>)
415 (stp:with-attributes (id category operation
416 data stylesheet data-2 stylesheet-2
417 output compare)
418 <test-case>
419 (make-instance 'test-case
420 :id id
421 :category category
422 :operation operation
423 :data-pathname data
424 :stylesheet-pathname stylesheet
425 :data-pathname-2 data-2
426 :stylesheet-pathname-2 stylesheet-2
427 :output-pathname output
428 :output-compare compare)))
430 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
431 ;; beginning, if any.
432 (defun slurp-for-comparison (p)
433 (with-open-file (s p :element-type '(unsigned-byte 8))
434 (unless (and (eql (read-byte s nil) #xef)
435 (eql (read-byte s nil) #xbb)
436 (eql (read-byte s nil) #xbf))
437 (file-position s 0))
438 (if (plusp (file-length s))
439 (let ((xstream (runes:make-xstream s :speed 1)))
440 (setf (runes:xstream-name xstream)
441 (cxml::make-stream-name
442 :entity-name "main document"
443 :entity-kind :main
444 :uri (cxml::pathname-to-uri (merge-pathnames p))))
445 (let ((source (cxml:make-source xstream :pathname p)))
446 (loop
447 for key = (klacks:peek-next source)
448 until (eq key :start-document))
449 (with-output-to-string (r)
450 (write-line "<wrapper>" r)
451 (cxml::with-source (source cxml::context)
452 (when (eq (cxml::zstream-token-category
453 (cxml::main-zstream cxml::context))
454 :seen-<)
455 (write-char #\< r)))
456 (loop
457 for char = (runes:read-rune xstream)
458 until (eq char :eof)
459 do (write-char char r))
460 (write-line "</wrapper>" r))))
461 "<wrapper/>")))
463 (defun parse-for-comparison (p)
464 (let* ((d (cxml:parse (slurp-for-comparison p)
465 (make-text-normalizer (stp:make-builder))))
466 (de (stp:document-element d)))
467 (let ((first (stp:first-child de)))
468 (when (typep first 'stp:text)
469 (cond
470 ((whitespacep (stp:data first))
471 (stp:delete-child first de))
473 (setf (stp:data first)
474 (cl-ppcre:regex-replace #.(format nil "^[~A]+" *whitespace*)
475 (stp:data first)
476 ""))))))
477 (let ((last (stp:last-child de)))
478 (when (typep last 'stp:text)
479 (cond
480 ((whitespacep (stp:data last))
481 (stp:delete-child last de))
483 (setf (stp:data last)
484 (cl-ppcre:regex-replace #.(format nil "[~A]+$" *whitespace*)
485 (stp:data last)
486 ""))))))
489 (defun output-equal-p (compare p q)
490 (handler-case
491 (ecase compare
492 (:xml (xml-output-equal-p p q))
493 (:html (html-output-equal-p p q)))
494 ((or error parse-number::invalid-number) (c)
495 (warn "comparison failed: ~A" c)
496 nil)))
498 (defun xml-output-equal-p (p q)
499 (let ((r (parse-for-comparison p))
500 (s (parse-for-comparison q)))
501 (node= (stp:document-element r) (stp:document-element s))))
503 ;; FIXME: don't do this in <pre> etc.
504 (defun normalize-html-whitespace (node)
505 (when (typep node 'stp:parent-node)
506 ;; ignore newlines after start tags completely
507 (let ((first (stp:first-child node)))
508 (when (and (typep first 'stp:text)
509 (alexandria:starts-with #\newline (stp:data first)))
510 (setf (stp:data first) (subseq (stp:data first) 1))))
511 ;; ignore newlines before end tags completely
512 (let ((last (stp:last-child node)))
513 (when (and (typep last 'stp:text)
514 (alexandria:ends-with #\newline (stp:data last)))
515 (setf (stp:data last)
516 (subseq (stp:data last) 0 (length (stp:data last))))))
517 ;; normalize sequences of whitespace
518 (stp:do-children (child node)
519 (if (typep child 'stp:text)
520 (setf (stp:data child)
521 (let ((str (normalize-whitespace (stp:data child))))
522 (when
523 ;; FIXME! Here we remove whitespace entirely.
524 ;; Totally incorrect, but I don't see how we could
525 ;; watch Saxon's output otherwise.
526 (equal str " ")
527 (setf str ""))
528 str))
529 (normalize-html-whitespace child)))
530 ;; just to be sure, join adjacent nodes
531 (cxml-stp-impl::normalize-text-nodes! node)))
533 ;; FIXME: this check is too lenient, because chtml is an error-correcting
534 ;; parser.
535 (defun html-output-equal-p (p q)
536 (let ((r (chtml:parse (pathname p) (stp:make-builder)))
537 (s (chtml:parse (pathname q) (stp:make-builder))))
538 (normalize-html-whitespace r)
539 (normalize-html-whitespace s)
540 (node= (stp:document-element r) (stp:document-element s))))
542 (defun strip-addresses (str)
543 (cl-ppcre:regex-replace-all "{[0-9a-fA-F]+}\\>" str "{xxxxxxxx}>"))
545 (xpath:with-namespaces ((nil #.*xsl*))
546 (defun slurp-output-method (p)
547 (let* ((d (handler-bind
548 ((warning #'muffle-warning))
549 (cxml:parse (pathname p) (stp:make-builder))))
550 (output (xpath:first-node (xpath:evaluate "//output" d))))
551 (if output
552 (let ((method (stp:attribute-value output "method")))
553 (if method
554 (intern (string-upcase method) :keyword)
555 :xml))
556 :xml))))
558 (defun replace-junk (str)
559 (map 'string
560 (lambda (c)
561 (if (or (eql c #\newline) (<= 32 (char-code c) 126))
563 #\?))
564 str))
566 (defun run-test (test)
567 (let ((expected-saxon (test-output-pathname test "saxon"))
568 #+xuriella::xsltproc
569 (expected-xsltproc (test-output-pathname test "xsltproc"))
570 (actual (test-output-pathname test "xuriella"))
571 (official (test-official-output-pathname test)))
572 (labels ((uri-resolver (uri)
573 (describe uri)
574 (if (search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
575 uri)
576 (cxml::pathname-to-uri
577 (merge-pathnames
578 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
579 *tests-directory*))
580 uri))
581 (doit ()
582 (with-open-file (s actual
583 :if-exists :rename-and-delete
584 :direction :output
585 :element-type '(unsigned-byte 8))
586 (handler-bind ((xslt-error
587 (lambda (c)
588 (declare (ignore c))
589 (when (find-restart 'recover)
590 (invoke-restart 'recover)))))
591 (apply-stylesheet (pathname (test-stylesheet-pathname test))
592 (pathname (test-data-pathname test))
593 :output s
594 :uri-resolver #'uri-resolver))))
595 (pp (label pathname)
596 (when pathname
597 (format t " ~A: ~A~%"
598 label
599 (enough-namestring pathname *tests-directory*))))
600 (report (ok &optional (fmt "") &rest args)
601 (write-string
602 (replace-junk
603 (strip-addresses
604 (format nil "~&~:[FAIL~;PASS~] ~A [~A]~?~%"
606 (test-id test)
607 (test-category test)
609 args))))
610 (pp "Stylesheet" (test-stylesheet-pathname test))
611 (pp "Data" (test-data-pathname test))
612 (pp "Supplemental stylesheet"
613 (test-stylesheet-pathname-2 test))
614 (pp "Supplemental data" (test-data-pathname-2 test))
615 (pp "Expected output (1)" expected-saxon)
616 #+xuriella::xsltproc
617 (pp "Expected output (2)" expected-xsltproc)
618 (pp "Actual output" actual)
619 (terpri)
620 ok))
621 (cond
622 ((equal (test-operation test) "standard")
623 (handler-case
624 (let ((output-method
625 (slurp-output-method (test-stylesheet-pathname test))))
626 (when (find (test-id test)
627 nil ;;'("axes_axes47" "attribset_attribset20")
628 :test #'equal)
629 (error "skipping problematic test"))
630 (doit)
631 (let ((saxon-matches-p
632 (output-equal-p output-method
633 expected-saxon
634 actual))
635 #+xuriella::xsltproc
636 (xsltproc-matches-p
637 (output-equal-p output-method
638 expected-xsltproc
639 actual))
640 (official-matches-p
641 (output-equal-p output-method
642 official
643 actual)))
644 (cond
645 ((or saxon-matches-p
646 #+xuriella::xsltproc xsltproc-matches-p
647 official-matches-p)
648 (report t)
649 #+xuriella::xsltproc
650 (report t ": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
651 saxon-matches-p
652 xsltproc-matches-p
653 (if saxon-matches-p
654 (not xsltproc-matches-p)
655 xsltproc-matches-p)))
657 (report nil ": output doesn't match")))))
658 ((or error parse-number::invalid-number) (c)
659 (report nil ": ~A" c))))
661 (handler-case
662 (doit)
663 (xslt-error (c)
664 (report t ": raised an xslt-error as expected" c))
665 ((or error parse-number::invalid-number) (c)
666 (report nil ": condition of incorrect type: ~%~A" c))
667 (:no-error (result)
668 (cond
669 ((not (and official (probe-file official)))
670 (report nil ": expected error not signalled: " result))
671 ((output-equal-p
672 (slurp-output-method (test-stylesheet-pathname test))
673 official
674 actual)
675 (report t))
677 (report nil ": saxon error not signalled and official output not a match"))))))))))
679 (defun run-xpath-tests ()
680 (run-tests '("XPath-Expression" "XSLT-Data-Model")))
683 ;;;; from cxml-stp-test
685 (defun assert-node= (a b)
686 (unless (node= a b)
687 (error "assertion failed: ~S and ~S are not NODE=" a b)))
689 (defun child-count (node)
690 (stp:count-children-if (constantly t) node))
692 (defun named-node-= (a b)
693 (and (equal (stp:namespace-uri a) (stp:namespace-uri b))
694 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
695 (equal (stp:local-name a) (stp:local-name b))))
697 (defun parent-node-= (e f)
698 (and (eql (child-count e)
699 (child-count f))
700 (every #'node= (stp:list-children e) (stp:list-children f))))
702 (defmethod node= ((e stp:element) (f stp:element))
703 (and (named-node-= e f)
704 (parent-node-= e f)
705 (null
706 (set-exclusive-or (stp:list-attributes e) (stp:list-attributes f)
707 :test #'node=))
708 (flet ((collect-namespaces (elt)
709 (let ((result ()))
710 (stp:map-extra-namespaces
711 (lambda (k v) (push (cons k v) result))
712 elt)
713 result)))
714 (null
715 (set-exclusive-or (collect-namespaces e) (collect-namespaces f)
716 :test #'equal)))))
718 (defmethod node= ((a stp:node) (b stp:node))
719 nil)
721 (defmethod node= ((e stp:document) (f stp:document))
722 (parent-node-= e f))
724 (defmethod node= ((a stp:attribute) (b stp:attribute))
725 (and (named-node-= a b)
726 (equal (stp:value a) (stp:value b))))
728 (defmethod node= ((a stp:comment) (b stp:comment))
729 (equal (stp:data a) (stp:data b)))
731 (defmethod node= ((a stp:text) (b stp:text))
732 (equal (stp:data a) (stp:data b)))
734 (defmethod node= ((a stp:processing-instruction)
735 (b stp:processing-instruction))
736 (and (equal (stp:data a) (stp:data b))
737 (equal (stp:target a) (stp:target b))))
739 (defmethod node= ((a stp:document-type) (b stp:document-type))
740 (and (equal (stp:root-element-name a) (stp:root-element-name b))
741 (equal (stp:public-id a) (stp:public-id b))
742 (equal (stp:system-id a) (stp:system-id b))
743 (equal (stp:internal-subset a) (stp:internal-subset b))))
745 (xpath::define-xpath-function/eager
746 :print
747 (thing)
748 (if (xpath:node-set-p thing)
749 (loop
750 initially (format t ";;; node set:~%")
751 for i from 0
752 for node in (xpath:all-nodes thing)
754 (format t ";;; ~D: ~A~%" i (type-of node)))
755 (format t ";;; ~A~%" thing))
756 thing)