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
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
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.
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
)
60 ;;; Translate catalog.xml into an actually usable katalog.xml
61 ;;; by running the test cases through xsltproc to see what it thinks
64 (defun simplify-tests (&optional
(d *tests-directory
*))
65 (with-open-file (stream (merge-pathnames "katalog.xml" d
)
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
)))
82 while
(klacks:find-event source
:start-element
)
83 for lname
= (klacks:current-lname source
)
86 ((equal lname
"major-path")
87 (klacks:skip source
:start-element
)
90 (merge-pathnames (klacks:consume-characters source
)))))
91 ((equal lname
"test-case")
94 (klacks:serialize-element source
(stp:make-builder
))))
95 (test-case (parse-original-test major-path
<test-case
>)))
96 (when (funcall test test-case
)
98 (when (funcall run-test test-case
)
101 (klacks:skip source
:start-element
))))
102 (format t
"~&Passed ~D/~D tests.~%" pass total
)))
104 (defun parse-original-test (major-path <test-case
>)
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
))
111 (stp:find-recursively-if
(stp:of-name
"scenario") <test-case
>))
114 supplemental-stylesheet
118 (dolist (<input
> (stp:filter-recursively
(stp:of-name
"input-file")
120 (let ((role (stp:attribute-value
<input
> "role"))
121 (path (concatenate 'string base
"/" (stp:string-value
<input
>))))
123 ((equal role
"principal-data")
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")
135 (let ((role (stp:attribute-value
<output
> "role"))
136 (path (concatenate 'string out-base
138 (stp:string-value
<output
>))))
140 ((equal role
"principal")
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")
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
))
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
)
173 :defaults
(test-data-pathname test
)))
175 (defun sanitize-stylesheet (in out
)
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")))
184 (with-open-file (s out
186 :if-exists
:rename-and-delete
187 :element-type
'(unsigned-byte 8))
188 (stp:serialize d
(cxml:make-octet-stream-sink s
))))
190 (warn "ignoring bogus stylesheet ~A: ~A" in c
)
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]~?~%"
203 (test-category test-case
)
206 (let* ((data (test-data-pathname test-case
))
207 (stylesheet (test-stylesheet-pathname test-case
))
208 (noindent-stylesheet (noindent-stylesheet-pathname test-case
))
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")
216 #+xuriella
::xsltproc
(xsltproc noindent-stylesheet data out
)
217 (saxon noindent-stylesheet data saxon-out
)
219 (write-simplified-test test-case
"standard")
222 (report "FAIL" ": ~A" c
)
223 (write-simplified-test test-case
"execution-error")
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
234 (report "PASS" ": expected error ~A" c
)
235 (write-simplified-test test-case
"execution-error")
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"
245 (full-namestring stylesheet
)
246 (full-namestring input
)
247 (full-namestring output
))))
249 (error "running xsltproc failed with code ~A [~%~A~%]"
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"
260 (full-namestring input
)
261 (full-namestring stylesheet
)
262 (full-namestring output
))))
264 (error "running saxon failed with code ~A [~%~A~%]"
266 (get-output-stream-string asdf
::*verbose-out
*))))))
269 ;;;; RUN-TESTS and DRIBBLE-TESTS
271 ;;; Process katalog.xml
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
))
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
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:
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
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.
322 ;; another similar test where the output is unparsable, except that
323 ;; here an entity declaration wouldn't have helped either:
324 "Copying_ResultTreeFragmentWithEscapedText"
328 ;; Input document isn't ns-wf.
330 ;; FIXME: Tweak the test suite driver to consider a test a success
331 ;; if Saxon fails and the input isn't well-formed, since that's what
332 ;; the tests are probably meant to assert. Or signal an XSLT-ERROR
333 ;; in this situation after all?
336 "Miscellaneous__84001"
337 "Namespace_XPath_Conflict_XPath_XSLT"
338 "Namespace_XPath_DefaultNamespace"
339 "Namespace_XPath_NavigatorMethods"
340 "Namespace_XPath_PredefinedPrefix_XMLNS"
341 "Namespace_XPath_SameQuery_DiffNamespace"
342 "Namespace_XPath_ScopingRules"
345 ;; Someone commented out most of this test...
348 ;; FIXME: should re-enable these at some point:
350 ;; the following tests take a lot of time due to the problems of current matching algorithm:
352 ;; probably the same problem (but I haven't checked):
355 ;; stack exhaustion -- matching problem i think
358 ;; test stylesheet doesn't exist?!
359 "ConflictResolution__77833"
362 ;; these test the value of generate-id(), which isn't specified
366 ;; Tests where the output isn't a match because of extraneous whitespace.
367 ;; For these tests, we force space normalization before comparing.
369 ;; Possible reasons for this problem are:
370 ;; a. The output method is declared in an imported stylesheet.
371 ;; SANITIZE-STYLESHEET is supposed to get rid of indent="yes", but it
372 ;; misses imported stylesheets.
373 ;; b. Saxon output isn't a match, but the official output is.
374 ;; But the official output is unaffected by SANITIZE-STYLESHEET.
376 (defparameter *whitespace-issues
*
377 (cl-ppcre:create-scanner
380 |Namespace-alias__91782$
381 |AttributeSets__91038$
389 |Output_EmptyElement1$
393 (defparameter *known-failures
*
395 ;; uses EBCDIC-CP-IT (whatever that is), but Babel's only got EBCDIC-US.
396 ;; Doesn't actually test any differences between the two, so it's
397 ;; probably just there to annoy us.
400 ;; uses KOI, which Babel doesn't support
415 ;; non-english sorting, which we don't support (yet?)
431 "Sorting_TurkishISortingTest"
435 ;; This is an HTML output method issue. The spec says the HTML
436 ;; output method should output elements with a null namespace URI as
437 ;; HTML, and if their name isn't recognized, as an inline element.
438 ;; <xml> here is such an element. It has an attribute with a
439 ;; namespace though, and the spec doesn't say what we should do with that
440 ;; attribute. We currently output it using Closure HTML, and
441 ;; lose its namespace. This test wants the attribute and its
442 ;; namespace to survive.
445 (defun run-tests (&key filter
(directory *tests-directory
*))
446 (when (typep filter
'(or string cons
))
447 (setf filter
(cl-ppcre:create-scanner filter
)))
448 (klacks:with-open-source
449 (source (klacks:make-tapping-source
450 (cxml:make-source
(merge-pathnames "katalog.xml" directory
))))
451 (let ((*default-pathname-defaults
* (merge-pathnames directory
)))
452 (map-tests #'run-test
455 (and (or (null filter
)
456 (cl-ppcre:all-matches
461 (not (find (test-id test
)
463 :test
#'equal
))))))))
465 (defun run-named-test (name &optional
(d *tests-directory
*))
466 (let ((*break-on-signals
*
467 '(and error
(not babel-encodings
:character-encoding-error
))))
468 (run-tests :filter
(format nil
"/~A$" name
) :directory d
)))
470 (defun copy-file (p q
)
471 (with-open-file (in p
:element-type
'(unsigned-byte 8))
472 (with-open-file (out q
473 :element-type
'(unsigned-byte 8)
475 :if-exists
:rename-and-delete
)
476 (let ((buf (make-array 8192 :element-type
'(unsigned-byte 8))))
477 (loop for pos
= (read-sequence buf in
)
479 do
(write-sequence buf out
:end pos
))))))
481 (defun find-named-test (name &optional
(d *tests-directory
*))
482 (klacks:with-open-source
483 (source (klacks:make-tapping-source
484 (cxml:make-source
(merge-pathnames "katalog.xml" d
))))
486 (map-tests (lambda (test)
489 :test
(lambda (test) (equal (test-id test
) name
))))))
491 (defun copy-test-files (name &optional
(d *tests-directory
*))
492 (let* ((test (find-named-test name d
))
493 (*default-pathname-defaults
* (merge-pathnames d
))
495 '(and error
(not babel-encodings
:character-encoding-error
)))
496 (target-dir (merge-pathnames "copied-test/"
497 (asdf:component-pathname
498 (asdf:find-system
:xuriella
))))
499 (xsl (merge-pathnames "test.xsl" target-dir
))
500 (xml (merge-pathnames "test.xml" target-dir
))
501 (txt (merge-pathnames "official-output.txt" target-dir
))
502 (expected (merge-pathnames "expected.xml" target-dir
))
503 (actual (merge-pathnames "actual.xml" target-dir
)))
504 (ensure-directories-exist target-dir
)
505 (copy-file (test-stylesheet-pathname test
) xsl
)
506 (copy-file (test-data-pathname test
) xml
)
507 (when (test-official-output-pathname test
)
508 (copy-file (test-official-output-pathname test
) txt
))
509 (format t
"Test stylesheet copied to:~% ~A~%~%" xsl
)
510 (format t
"Test data copied to:~% ~A~%~%" xml
)
511 (when (test-official-output-pathname test
)
512 (format t
"Official output file:~% ~A~%~%" txt
))
513 (format t
"Run xsltproc like this:~% cd ~A~% xsltproc ~A ~A >~A~%~%"
514 (namestring target-dir
)
515 (enough-namestring xsl target-dir
)
516 (enough-namestring xml target-dir
)
517 (enough-namestring expected target-dir
))
518 (format t
"Run saxon like this:~% cd ~A~% java -jar /usr/share/java/saxon.jar ~A ~A >~A~%~%"
519 (namestring target-dir
)
520 (enough-namestring xml target-dir
)
521 (enough-namestring xsl target-dir
)
522 (enough-namestring expected target-dir
))
523 (format t
"Run MSXSL like this:~% cd ~A~% wine msxsl.exe ~A ~A >~A~%~%"
524 (namestring target-dir
)
525 (enough-namestring xml target-dir
)
526 (enough-namestring xsl target-dir
)
527 (enough-namestring expected target-dir
))
528 (format t
"Run xuriella like this:~%")
529 `(apply-stylesheet ,xsl
,xml
:output
,actual
)))
531 (defun map-tests (run-test source
&key
(test (constantly t
)))
536 while
(klacks:find-event source
:start-element
)
537 for lname
= (klacks:current-lname source
)
540 ((equal lname
"test-case")
542 (stp:document-element
543 (klacks:serialize-element source
(stp:make-builder
))))
544 (test-case (parse-test <test-case
>)))
545 (when (funcall test test-case
)
547 (ecase (funcall run-test test-case
)
554 (klacks:skip source
:start-element
))))
555 (format t
"~&Passed ~D/~D tests (~D expected failures, ~D unexpected failures).~%"
556 pass total known
(- total pass known
))))
558 (defun parse-test (<test-case
>)
559 (stp:with-attributes
(id category operation
560 data stylesheet data-2 stylesheet-2
563 (make-instance 'test-case
568 :stylesheet-pathname stylesheet
569 :data-pathname-2 data-2
570 :stylesheet-pathname-2 stylesheet-2
571 :output-pathname output
572 :output-compare compare
)))
574 ;; read from file P, skipping the XMLDecl or TextDecl and Doctype at the
575 ;; beginning, if any.
576 (defun slurp-for-comparison (p)
577 (with-open-file (s p
:element-type
'(unsigned-byte 8))
578 (unless (and (eql (read-byte s nil
) #xef
)
579 (eql (read-byte s nil
) #xbb
)
580 (eql (read-byte s nil
) #xbf
))
582 (if (plusp (file-length s
))
583 (slurp-for-comparison-1 p s t
)
586 (defun slurp-for-comparison-1 (p s junk-info
)
587 (let ((pos (file-position s
)) ;for UTF-8 "BOM"
588 (xstream (runes:make-xstream s
:speed
1))
590 (setf (runes:xstream-name xstream
)
591 (cxml::make-stream-name
592 :entity-name
"main document"
594 :uri
(cxml::pathname-to-uri
(merge-pathnames p
))))
598 (flexi-streams:make-in-memory-input-stream
600 (cxml:make-source xstream
602 :entity-resolver
#'er
))))
603 (unless (eq junk-info
:nada
)
606 (setf prev-pos
(runes:xstream-position xstream
))
607 (klacks:peek-next source
))
608 until
(eq key
:start-document
))
609 (cxml::with-source
(source cxml
::context
)
610 (when (eq (cxml::zstream-token-category
611 (cxml::main-zstream cxml
::context
))
613 ;; oops, doesn't look like XML at all
614 (file-position s pos
)
615 (return-from slurp-for-comparison-1
616 (slurp-for-comparison-1 p s
:nada
)))))
619 (dotimes (x junk-info
)
620 (setf prev-pos
(runes:xstream-position xstream
))
621 (klacks:peek-next source
)))
626 (case (klacks:peek-next source
)
627 (:start-element
(return))
629 (if (whitespacep (klacks:current-characters source
))
634 ((or file-error cxml
:xml-parse-error
) ()
638 (with-open-file (u p
:element-type
'(unsigned-byte 8))
639 (file-position u pos
)
640 (return-from slurp-for-comparison-1
641 (slurp-for-comparison-1 p u nskip
)))))
642 ((member nil
:nada
)))
643 (with-output-to-string (r)
645 (cxml::with-source
(source cxml
::context
)
646 (ecase (cxml::zstream-token-category
647 (cxml::main-zstream cxml
::context
))
651 (setf prev-pos
(runes:xstream-position xstream
))
653 (off-by-one-p (or seen-char
(eq junk-info
:nada
)))
654 (new-pos (- prev-pos
(if off-by-one-p
1 0))))
656 (with-open-file (u p
:element-type
'(unsigned-byte 8))
657 (file-position u pos
)
658 (let ((y (runes:make-xstream u
:speed
1)))
660 while
(< (runes:xstream-position y
) new-pos
)
661 do
(write-char (runes:read-rune y
) r
))))
662 (write-line "<wrapper>" r
)
664 (write-char seen-char r
)))
666 for char
= (runes:read-rune xstream
)
668 do
(write-char char r
))
669 (write-line "</wrapper>" r
)))))
671 (defun parse-for-comparison (p)
672 (let* ((d (flet ((er (pub sys
)
674 (flexi-streams:make-in-memory-input-stream
676 (cxml:parse
(slurp-for-comparison p
)
677 (make-text-normalizer (stp:make-builder
))
678 :entity-resolver
#'er
)))
679 (de (stp:document-element d
)))
680 (let ((first (stp:first-child de
)))
681 (when (typep first
'stp
:text
)
683 ((whitespacep (stp:data first
))
684 (stp:delete-child first de
))
686 (setf (stp:data first
)
687 (cl-ppcre:regex-replace
#.
(format nil
"^[~A]+" *whitespace
*)
690 (let ((last (stp:last-child de
)))
691 (when (typep last
'stp
:text
)
693 ((whitespacep (stp:data last
))
694 (stp:delete-child last de
))
696 (setf (stp:data last
)
697 (cl-ppcre:regex-replace
#.
(format nil
"[~A]+$" *whitespace
*)
702 (defun output-equal-p (compare p q
&key normalize
)
705 (:html
(html-output-equal-p p q
))
706 (:text
(text-output-equal-p p q
))
707 (t (xml-output-equal-p p q normalize
)))
708 ((or error parse-number
::invalid-number
) (c)
709 (warn "comparison failed: ~A" c
)
710 ;; try again using a plain-text comparision, sometimes it helps:
711 (and (not (eq compare
:text
))
712 (output-equal-p :text p q
:normalize normalize
)))))
714 ;; Workaround for namespace_namespace23 and other tests:
715 ;; - For these tests, saxon and msxsl output a declaration for the XSL
716 ;; namespace without using that declaration.
717 ;; - I think saxon and msxsl are both wrong.
718 ;; - The official test output agrees with my assessment.
719 ;; (So does libxslt, but that's not to be trusted. :-))
720 ;; - Here's the catch: The official test output is broken in its whitespace
722 ;; So let's normalize spaces in test output that looks like an XSLT
723 ;; stylesheet, allowing us to pass these tests using the official test output.
724 (defun maybe-normalize-test-spaces (wrapper force
)
726 (loop while
(< i
(length (cxml-stp-impl::%children wrapper
))) do
727 (let ((wrapper-child (stp:nth-child i wrapper
)))
729 ((not (typep wrapper-child
'stp
:element
))
731 (stp:delete-nth-child i wrapper
)
733 ((or (equal (stp:namespace-uri wrapper-child
) *xsl
*)
735 (strip-stylesheet wrapper-child
)
736 (labels ((recurse (e &optional preserve
)
737 (stp:do-children
(child e
)
740 (setf (stp:data child
)
741 (normalize-whitespace (stp:data child
))))
743 (stp:with-attributes
((space "space" *xml
*))
747 ((namep child
"text") t
)
748 ((not space
) preserve
)
749 ((equal space
"preserve") t
)
751 (recurse child new-preserve
))))))))
752 (recurse wrapper-child
))
757 (defun xml-output-equal-p (p q normalize
)
758 (let ((r (parse-for-comparison p
))
759 (s (parse-for-comparison q
)))
760 (maybe-normalize-test-spaces (stp:document-element r
) normalize
)
761 (maybe-normalize-test-spaces (stp:document-element s
) normalize
)
762 (and (let ((u (stp:document-type r
))
763 (v (stp:document-type s
)))
767 (node= (stp:document-element r
) (stp:document-element s
)))))
769 ;; FIXME: don't do this in <pre> etc.
770 (defun normalize-html-whitespace (node)
771 (when (typep node
'stp
:parent-node
)
772 ;; ignore newlines after start tags completely
773 (let ((first (stp:first-child node
)))
774 (when (and (typep first
'stp
:text
)
775 (alexandria:starts-with
#\newline
(stp:data first
)))
776 (setf (stp:data first
) (subseq (stp:data first
) 1))))
777 ;; ignore newlines before end tags completely
778 (let ((last (stp:last-child node
)))
779 (when (and (typep last
'stp
:text
)
780 (alexandria:ends-with
#\newline
(stp:data last
)))
781 (setf (stp:data last
)
782 (subseq (stp:data last
) 0 (length (stp:data last
))))))
783 ;; normalize sequences of whitespace
784 (stp:do-children
(child node
)
785 (if (typep child
'stp
:text
)
786 (setf (stp:data child
)
787 (let ((str (normalize-whitespace (stp:data child
))))
789 ;; FIXME! Here we remove whitespace entirely.
790 ;; Totally incorrect, but I don't see how we could
791 ;; watch Saxon's output otherwise.
795 (normalize-html-whitespace child
)))
796 ;; just to be sure, join adjacent nodes
797 (cxml-stp-impl::normalize-text-nodes
! node
)))
799 ;; FIXME: this check is too lenient, because chtml is an error-correcting
801 (defun html-output-equal-p (p q
)
802 (let ((r (chtml:parse
(pathname p
) (stp:make-builder
)))
803 (s (chtml:parse
(pathname q
) (stp:make-builder
))))
804 (normalize-html-whitespace r
)
805 (normalize-html-whitespace s
)
806 (flet ((fix-case (node)
807 (xpath:with-namespaces
(("xhtml" "http://www.w3.org/1999/xhtml"))
809 (content (xpath:evaluate
"//xhtml:meta/@content" node
))
810 (setf (stp:value content
)
811 (string-downcase (stp:value content
)))))))
814 (node= (stp:document-element r
) (stp:document-element s
))))
816 (defun text-output-equal-p (p q
)
817 (with-open-file (a p
:element-type
'(unsigned-byte 8))
818 (with-open-file (b q
:element-type
'(unsigned-byte 8))
819 (let ((len (file-length a
)))
820 (and (eql len
(file-length b
))
821 (let ((d (make-array len
:element-type
'(unsigned-byte 8)))
822 (e (make-array len
:element-type
'(unsigned-byte 8))))
827 (defun strip-addresses (str)
828 (cl-ppcre:regex-replace-all
"{[0-9a-fA-F]+}\\>" str
"{xxxxxxxx}>"))
830 (defun slurp-output-method (p)
831 (xpath:with-namespaces
((nil #.
*xsl
*))
832 (let* ((d (handler-bind
833 ((warning #'muffle-warning
))
834 (cxml:parse
(pathname p
) (stp:make-builder
))))
835 (output (xpath:first-node
(xpath:evaluate
"//output" d
))))
837 (let ((method (stp:attribute-value output
"method")))
839 (intern (string-upcase method
) :keyword
)
843 (defun replace-junk (str)
844 (cl-ppcre:regex-replace-all
845 `(:group
,(namestring *tests-directory
*))
848 (if (or (eql c
#\newline
) (<= 32 (char-code c
) 126))
854 (defun run-test (test)
855 (let ((expected-saxon (test-output-pathname test
"saxon"))
857 (expected-xsltproc (test-output-pathname test
"xsltproc"))
858 (actual (test-output-pathname test
"xuriella"))
859 (official (test-official-output-pathname test
))
861 (cl-ppcre:all-matches
*whitespace-issues
* (test-id test
)))
863 (handler-bind ((|hey test suite
, this is an HTML document|
866 (setf output-method
:html
))))
867 (labels ((uri-resolver (uri)
868 (let ((str (puri:render-uri uri nil
)))
870 ((search "%5c%5c%5c%5cwebxtest%5c%5cmanagedshadow%5c%5cmanaged_b2%5c%5ctestdata%5c%5cxslt%5c%5celement%5c%5cxslt_element_NSShared.xml"
872 (cxml::pathname-to-uri
874 "MSFT_Conformance_Tests/Elements/xslt_element_NSShared.xml"
876 ((search "webxtest/testcases/91156a.xsl" str
)
877 (cxml::pathname-to-uri
879 "MSFT_Conformance_Tests/Import/91156a.xsl"
884 (with-open-file (s actual
885 :if-exists
:rename-and-delete
887 :element-type
'(unsigned-byte 8))
888 (handler-bind ((xslt-error
891 (when (find-restart 'recover
)
892 (invoke-restart 'recover
)))))
893 (apply-stylesheet (pathname (test-stylesheet-pathname test
))
894 (let ((p (test-data-pathname test
)))
896 ((search "Elements/Plants.xml" p
)
898 "MSFT_Conformance_Tests/Elements/plants.xml"
900 ((search "/OutputText.xml" p
)
902 "MSFT_Conformance_Tests/Output/Outputtext.xml"
907 :uri-resolver
#'uri-resolver
))))
910 (format t
" ~A: ~A~%"
912 (enough-namestring pathname
*tests-directory
*))))
913 (report (ok &optional
(fmt "") &rest args
)
917 (format nil
"~&~A ~A [~A]~?~%"
920 ((find (test-id test
)
923 (setf ok
:known-failure
)
931 (pp "Stylesheet" (test-stylesheet-pathname test
))
932 (pp "Data" (test-data-pathname test
))
933 (pp "Supplemental stylesheet"
934 (test-stylesheet-pathname-2 test
))
935 (pp "Supplemental data" (test-data-pathname-2 test
))
936 (pp "Expected output (1)" expected-saxon
)
938 (pp "Expected output (2)" expected-xsltproc
)
939 (pp "Actual output" actual
)
943 ((equal (test-operation test
) "standard")
946 (when (find (test-id test
)
947 nil
;;'("axes_axes47" "attribset_attribset20")
949 (error "skipping problematic test"))
951 (let* ((output-method
954 (test-stylesheet-pathname test
))))
956 (output-equal-p output-method
959 :normalize force-normalization
))
962 (output-equal-p output-method
966 (output-equal-p output-method
969 :normalize force-normalization
)))
972 #+xuriella
::xsltproc xsltproc-matches-p
976 (report t
": saxon ~A, xsltproc ~A~:[~; (MISMATCH)~]"
980 (not xsltproc-matches-p
)
981 xsltproc-matches-p
)))
983 (report nil
": output doesn't match")))))
984 ((or error parse-number
::invalid-number
) (c)
985 (report nil
": ~A" c
))))
990 (report t
": raised an xslt-error as expected" c
))
991 ((or error parse-number
::invalid-number
) (c)
992 (report nil
": condition of incorrect type: ~%~A" c
))
995 ((not (and official
(probe-file official
)))
996 (report nil
": expected error not signalled: " result
))
999 (slurp-output-method (test-stylesheet-pathname test
)))
1002 :normalize force-normalization
)
1005 (report nil
": saxon error not signalled and official output not a match")))))))))))
1007 (defun run-xpath-tests ()
1008 (run-tests :filter
"XPath-Expression/|XSLT-Data-Model/"))
1011 ;;;; from cxml-stp-test
1013 (defun assert-node= (a b
)
1015 (error "assertion failed: ~S and ~S are not NODE=" a b
)))
1017 (defun child-count (node)
1018 (stp:count-children-if
(constantly t
) node
))
1020 (defun named-node-= (a b
)
1021 (and (equal (stp:namespace-uri a
) (stp:namespace-uri b
))
1022 ;; (equal (stp:namespace-prefix a) (stp:namespace-prefix b))
1023 (equal (stp:local-name a
) (stp:local-name b
))))
1025 (defun parent-node-= (e f
)
1026 (and (eql (child-count e
)
1028 (every #'node
= (stp:list-children e
) (stp:list-children f
))))
1030 (defmethod node= ((e stp
:element
) (f stp
:element
))
1031 (and (named-node-= e f
)
1034 (set-exclusive-or (stp:list-attributes e
) (stp:list-attributes f
)
1037 (flet ((check-namespaces (a b
)
1039 (stp:map-extra-namespaces
1041 (unless (equal v
(stp:find-namespace k b
))
1045 (check-namespaces e f
)
1046 (check-namespaces f e
))
1049 (defmethod node= ((a stp
:node
) (b stp
:node
))
1052 (defmethod node= ((e stp
:document
) (f stp
:document
))
1053 (parent-node-= e f
))
1055 (defmethod node= ((a stp
:attribute
) (b stp
:attribute
))
1056 (and (named-node-= a b
)
1057 (equal (stp:value a
) (stp:value b
))))
1059 (defmethod node= ((a stp
:comment
) (b stp
:comment
))
1060 (equal (stp:data a
) (stp:data b
)))
1062 (defmethod node= ((a stp
:text
) (b stp
:text
))
1063 (equal (stp:data a
) (stp:data b
)))
1065 (defmethod node= ((a stp
:processing-instruction
)
1066 (b stp
:processing-instruction
))
1067 (and (equal (stp:data a
) (stp:data b
))
1068 (equal (stp:target a
) (stp:target b
))))
1070 (defmethod node= ((a stp
:document-type
) (b stp
:document-type
))
1071 (and (equal (stp:root-element-name a
) (stp:root-element-name b
))
1072 (equal (stp:public-id a
) (stp:public-id b
))
1073 (equal (stp:system-id a
) (stp:system-id b
))
1074 (equal (stp:internal-subset a
) (stp:internal-subset b
))))
1076 (xpath-sys:define-xpath-function
/eager
1079 (if (xpath:node-set-p thing
)
1081 initially
(format t
";;; node set:~%")
1083 for node in
(xpath:all-nodes thing
)
1085 (format t
";;; ~D: ~A~%" i
(type-of node
)))
1086 (format t
";;; ~A~%" thing
))