File size: 4,555 Bytes
3dcad1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
;;;; sxml.simple.test --- (sxml simple) -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; Copyright (C) 2010, 2013 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-sxml-simple)
#:use-module (test-suite lib)
#:use-module (sxml simple))
(define parser-error '(parser-error . ""))
(define %xml-sample
;; An XML sample without any space in between tags, to make it easier.
(string-append "<?xml version='1.0' encoding='utf-8'?>"
"<foo chbouib=\"yes\">"
"<bar/>"
"<baz>"
"<smurf one=\"1\"/>"
"</baz>"
"</foo>"))
(with-test-prefix "simple"
(pass-if "xml->sxml"
(equal? (xml->sxml (open-input-string %xml-sample))
'(*TOP*
(*PI* xml "version='1.0' encoding='utf-8'")
(foo (@ (chbouib "yes"))
(bar)
(baz (smurf (@ (one "1"))))))))
(pass-if "xml->sxml->xml->sxml"
;; Regression test for bug #29260.
(equal? (xml->sxml (open-input-string %xml-sample))
(xml->sxml
(open-input-string
(with-output-to-string
(lambda ()
(sxml->xml
(xml->sxml (open-input-string %xml-sample))))))))))
(with-test-prefix "namespaces"
(pass-if-equal
(xml->sxml "<foo xmlns=\"http://example.org/ns1\">text</foo>")
'(*TOP* (http://example.org/ns1:foo "text")))
(pass-if-equal
(xml->sxml "<foo xmlns=\"http://example.org/ns1\">text</foo>"
#:namespaces '((ns1 . "http://example.org/ns1")))
'(*TOP* (ns1:foo "text")))
(pass-if-equal
(xml->sxml "<foo xmlns:bar=\"http://example.org/ns2\"><bar:baz/></foo>"
#:namespaces '((ns2 . "http://example.org/ns2")))
'(*TOP* (foo (ns2:baz))))
(pass-if-equal
(xml->sxml "<foo><ns2:baz/></foo>"
#:namespaces '((ns2 . "http://example.org/ns2")))
'(*TOP* (foo (ns2:baz))))
(pass-if-exception "namespace undeclared" parser-error
(xml->sxml "<foo><ns2:baz/></foo>"
#:namespaces '((ns2 . "http://example.org/ns2"))
#:declare-namespaces? #f)))
(with-test-prefix "whitespace"
(pass-if-equal
(xml->sxml "<foo>\n<bar> Alfie the parrot! </bar>\n</foo>")
'(*TOP* (foo "\n" (bar " Alfie the parrot! ") "\n")))
(pass-if-equal
(xml->sxml "<foo>\n<bar> Alfie the parrot! </bar>\n</foo>"
#:trim-whitespace? #t)
'(*TOP* (foo (bar " Alfie the parrot! ")))))
(with-test-prefix "parsed entities"
(pass-if-equal
'(*TOP* (foo "&"))
(xml->sxml "<foo>&</foo>"))
(pass-if-exception "nbsp undefined" parser-error
(xml->sxml "<foo> </foo>"))
(pass-if-equal
'(*TOP* (foo "\xA0"))
(xml->sxml "<foo> </foo>"
#:entities '((nbsp . "\xA0"))))
(pass-if-equal
'(*TOP* (foo "\xA0"))
(xml->sxml "<foo> </foo>"))
(let ((ents '()))
(pass-if-equal
(xml->sxml "<foo> &foo;</foo>"
#:default-entity-handler
(lambda (port name)
(case name
((nbsp) "\xa0")
(else
(set! ents (cons name ents))
"qux"))))
'(*TOP* (foo "\xa0 qux")))
(pass-if-equal
ents
'(foo))))
(with-test-prefix "doctype handlers"
(define (handle-foo docname systemid internal-subset)
(case docname
((foo)
(values #:entities '((greets . "<i>Hello, world!</i>"))))
(else
(values))))
(pass-if-equal
(xml->sxml "<!DOCTYPE foo><p>&greets;</p>"
#:doctype-handler handle-foo)
'(*TOP* (p (i "Hello, world!")))))
|