sxml.simple.test 4.4 KB

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