From d40f58171b89f85a73d0de88d6a45763037bad9d Mon Sep 17 00:00:00 2001 From: David Lichteblau Date: Sun, 8 Jul 2007 15:46:48 +0200 Subject: [PATCH] tests fuer PROCESSING-INSTRUCTION --- processing-instruction.lisp | 8 +-- test.lisp | 129 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 126 insertions(+), 11 deletions(-) diff --git a/processing-instruction.lisp b/processing-instruction.lisp index 1c4c869..61f1715 100644 --- a/processing-instruction.lisp +++ b/processing-instruction.lisp @@ -74,7 +74,7 @@ (when (string-equal newval "xml") (stp-error "attempt to pretend that a PI is an XMLDecl"))) -(defmethod (setf data) :before (newval (node processing-instruction)) +(defmethod (setf data) :around (newval (node processing-instruction)) (unless newval (setf newval "")) (unless (xml-characters-p newval) (stp-error "Processing instruction data includes characters that ~ @@ -82,10 +82,12 @@ newval)) (when (search "?>" newval) (stp-error "forbidden -- in processing-instruction")) - (when (or (alexandria:starts-with 10 newval :key #'char-code) + (when (or (alexandria:starts-with 9 newval :key #'char-code) + (alexandria:starts-with 10 newval :key #'char-code) (alexandria:starts-with 13 newval :key #'char-code) (alexandria:starts-with 32 newval :key #'char-code)) - (stp-error "space at beginning of processing instruction data"))) + (stp-error "space at beginning of processing instruction data")) + (call-next-method newval node)) (defmethod serialize ((node processing-instruction) handler) (sax:processing-instruction handler (target node) (data node))) diff --git a/test.lisp b/test.lisp index 95dbec2..8221101 100644 --- a/test.lisp +++ b/test.lisp @@ -33,8 +33,7 @@ (in-package :cxml-stp-test) (defmethod xmlconf::serialize-document ((document node)) - (let ((cxml-stp::*serialize-canonical-notations-only-p* t)) - (serialize document (cxml:make-octet-vector-sink :canonical 2)))) + (serialize document (cxml:make-octet-vector-sink :canonical 2))) (defun stp-test (filename handler &rest args) (declare (ignore handler)) @@ -55,7 +54,7 @@ (unless (equal a b) (error "assertion failed: ~S and ~S are not EQUAL" a b))) -(defmacro expect-exception (form type) +(defmacro expect-condition (form type) `(handler-case (progn ,form @@ -70,7 +69,7 @@ (defmacro define-exception-test (name form type) `(deftest ,name (progn - (expect-exception ,form ,type) + (expect-condition ,form ,type) (values)))) (rem-all-tests) @@ -164,7 +163,7 @@ (deftest text.leaf-node (let ((c1 (make-text "data"))) (assert-equal 0 (count-children-if #'identity c1)) - (expect-exception (nth-child 0 c1) error) + (expect-condition (nth-child 0 c1) error) (assert-equal nil (parent c1)) (let ((e (make-element "test"))) (append-child e c1) @@ -214,8 +213,8 @@ (setf (data c) "legal") (assert-equal (data c) "legal") (assert-equal (string-value c) "legal") - (expect-exception (setf (data c) "test -- test") stp-error) - (expect-exception (setf (data c) "test-") stp-error) + (expect-condition (setf (data c) "test -- test") stp-error) + (expect-condition (setf (data c) "test-") stp-error) (setf (data c) nil) (assert-equal (data c) "") (values))) @@ -227,7 +226,7 @@ (deftest comment.leaf-node (let ((c1 (make-comment "data"))) (assert-equal 0 (count-children-if #'identity c1)) - (expect-exception (nth-child 0 c1) error) + (expect-condition (nth-child 0 c1) error) (assert-equal nil (parent c1)) (let ((e (make-element "test"))) (append-child e c1) @@ -257,4 +256,118 @@ stp-error) +;;;; PROCESSING-INSTRUCTION + +(deftest pi.constructor.1 + (let ((p-i (make-processing-instruction "abc" "def"))) + (assert-equal (target p-i) "abc") + (assert-equal (data p-i) "def") + (values))) + +(deftest pi.constructor.2 + (data (make-processing-instruction "abc" "")) + "") + +(deftest pi.constructor.3 + (data (make-processing-instruction "abc" nil)) + "") + +(deftest pi.constructor.4 + (target (make-processing-instruction "abc123" nil)) + "abc123") + +(deftest pi.constructor.illegal + (progn + (expect-condition (make-processing-instruction "test:test" "test") + stp-error) + (expect-condition (make-processing-instruction "" "test") + stp-error) + (expect-condition (make-processing-instruction nil "test") + stp-error) + (expect-condition (make-processing-instruction "12345" "test") + stp-error) + (values))) + +(deftest pi.serialize + (serialize-to-string (make-processing-instruction "abc" "def")) + "") + +(deftest pi.serialize.2 + (serialize-to-string (make-processing-instruction "abc" "")) + "") + +(deftest pi.serialize.3 + (serialize-to-string + (make-processing-instruction "target" "&&greater;")) + "&&greater;?>") + +(deftest pi.copy + (let* ((c1 (make-processing-instruction "target" "data")) + (c2 (copy c1))) + (assert (not (eq c1 c2))) + (assert-equal (data c1) (data c2)) + (assert-equal (target c1) (target c2)) + (assert-equal nil (parent c2)) + (assert-equal (type-of c2) 'processing-instruction) + (values))) + +(deftest pi.setf + (let* ((p-i (make-processing-instruction "target" "data"))) + (expect-condition (setf (data p-i) "?>") stp-error) + (expect-condition (setf (data p-i) "uhesta ?>") stp-error) + (expect-condition (setf (data p-i) "uhesta ?> hst") stp-error) + (setf (data p-i) nil) + (assert-equal (data p-i) "") + (dolist (str '("" + "name=value" + "name='value'" + "name=\"value\"" + "salkdhsalkjhdkjsadhkj sadhsajkdh" + "" + "--")) + (setf (data p-i) str) + (assert-equal (data p-i) str)) + (values))) + +;;; zzz testCorrectSurrogates +;;; zzz testSurrogates + +(deftest pi.leaf-node + (let ((c1 (make-processing-instruction "target" "data"))) + (assert-equal 0 (count-children-if #'identity c1)) + (expect-condition (nth-child 0 c1) error) + (assert-equal nil (parent c1)) + (let ((e (make-element "test"))) + (append-child e c1) + (assert-equal e (parent c1)) + (assert-equal c1 (nth-child 0 e)) + (delete-child c1 e) + (assert-equal 0 (count-children-if #'identity e))) + (values))) + +;;; zzz das pruefen wir nicht +;; (define-exception-test pi.cr +;; (make-processing-instruction "target" (format nil "foo ~C bar" (code-char 13))) +;; stp-error) + +(deftest pi.invalid + (dolist (str (list " initial spaces" + (format nil "~Cinitial tab" (code-char 9)) + (format nil "~Cinitial newline" (code-char 10)) + (format nil "~Cinitial cr" (code-char 13))) + (values)) + (expect-condition (make-processing-instruction "target" str) stp-error))) + +(deftest pi.invalid.xml + (dolist (str (list "xml" "XML" "Xml") + (values)) + (expect-condition (make-processing-instruction str "data") stp-error))) + +(deftest pi.invalid.colon + (dolist (str (list "pre:target" "pre:" ":target") + (values)) + (expect-condition (make-processing-instruction str "data") stp-error))) + + (do-tests) -- 2.11.4.GIT