Clean up syntax-table usage in xml.el
[emacs.git] / test / automated / xml-parse-tests.el
blobe65530603450f73bdf245dd2da928e420271f084
1 ;;; xml-parse-tests.el --- Test suite for XML parsing.
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Chong Yidong <cyd@stupidchicken.com>
6 ;; Keywords: internal
7 ;; Human-Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Type M-x test-xml-parse RET to generate the test buffer.
28 ;;; Code:
30 (require 'xml)
32 (defvar xml-parse-tests--data
33 `(;; General entity substitution
34 ("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY ent \"AbC\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
35 ((foo ((a . "b")) (bar nil "AbC;"))))
36 ("<?xml version=\"1.0\"?><foo>&amp;amp;&#x26;apos;&apos;&lt;&gt;&quot;</foo>" .
37 ((foo () "&amp;&apos;'<>\"")))
38 ;; Parameter entity substitution
39 ("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY % pent \"AbC\"><!ENTITY ent \"%pent;\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
40 ((foo ((a . "b")) (bar nil "AbC;"))))
41 ;; Tricky parameter entity substitution (like XML spec Appendix D)
42 ("<?xml version='1.0'?><!DOCTYPE foo [ <!ENTITY % xx '&#37;zz;'><!ENTITY % zz '&#60;!ENTITY ent \"b\" >' > %xx; ]><foo>A&ent;C</foo>" .
43 ((foo () "AbC")))
44 ;; Bug#7172
45 ("<?xml version=\"1.0\"?><!DOCTYPE foo [ <!ELEMENT EXAM_PLE EMPTY> ]><foo></foo>" .
46 ((foo ())))
47 ;; Entities referencing entities, in character data
48 ("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo>&abc;</foo>" .
49 ((foo () "aBc")))
50 ;; Entities referencing entities, in attribute values
51 ("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo a=\"-&abc;-\">1</foo>" .
52 ((foo ((a . "-aBc-")) "1")))
53 ;; Character references must be treated as character data
54 ("<foo>AT&amp;T;</foo>" . ((foo () "AT&T;")))
55 ("<foo>&#38;amp;</foo>" . ((foo () "&amp;")))
56 ("<foo>&#x26;amp;</foo>" . ((foo () "&amp;")))
57 ;; Unusual but valid XML names [5]
58 ("<ÀÖØö.3·-‿⁀󯿿>abc</ÀÖØö.3·-‿⁀󯿿>" . ((,(intern "ÀÖØö.3·-‿⁀󯿿") () "abc")))
59 ("<:>abc</:>" . ((,(intern ":") () "abc"))))
60 "Alist of XML strings and their expected parse trees.")
62 (defvar xml-parse-tests--bad-data
63 '(;; XML bomb in content
64 "<!DOCTYPE foo [<!ENTITY lol \"lol\"><!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\"><!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">]><foo>&lol2;</foo>"
65 ;; XML bomb in attribute value
66 "<!DOCTYPE foo [<!ENTITY lol \"lol\"><!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\"><!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">]><foo a=\"&lol2;\">!</foo>"
67 ;; Non-terminating DTD
68 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">"
69 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf"
70 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf&abc;"
71 ;; Invalid XML names
72 "<0foo>abc</0foo>"
73 "<‿foo>abc</‿foo>"
74 "<f¿>abc</f¿>")
75 "List of XML strings that should signal an error in the parser")
77 (ert-deftest xml-parse-tests ()
78 "Test XML parsing."
79 (with-temp-buffer
80 (dolist (test xml-parse-tests--data)
81 (erase-buffer)
82 (insert (car test))
83 (should (equal (cdr test) (xml-parse-region))))
84 (let ((xml-entity-expansion-limit 50))
85 (dolist (test xml-parse-tests--bad-data)
86 (erase-buffer)
87 (insert test)
88 (should-error (xml-parse-region))))))
90 ;; Local Variables:
91 ;; no-byte-compile: t
92 ;; End:
94 ;;; xml-parse-tests.el ends here.