Debian Codegraph @

test/test_parsers/test_docutils_xml/test_parse.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2024 Günter Milde.
3 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8 # This file is offered as-is, without any warranty.
9 #
10 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
11
12 """Tests for `docutils.parsers.docutils_xml.Parser.parse()`."""
13
14 from pathlib import Path
15 import sys
16 import unittest
17
18 if __name__ == '__main__':
19 # prepend the "docutils root" to the Python library path
20 # so we import the local `docutils` package.
21 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
22
23 from docutils.frontend import get_default_settings
24 from docutils.parsers import docutils_xml
25 from docutils.utils import new_document
26
27 parser = docutils_xml.Parser()
28
29
30 class XmlParserTestCase(unittest.TestCase):
31 def test_parse(self):
32 settings = get_default_settings(parser)
33 settings.warning_stream = ''
34 for name, cases in totest.items():
35 for casenum, (case_input, case_expected) in enumerate(cases):
36 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
37 document = new_document('test data', settings)
38 parser.parse(case_input, document)
39 output = document.pformat()
40 self.assertEqual(case_expected, output)
41
42
43 totest = {}
44
45 totest['simple'] = [
46 ["""\
47 <document>
48 <paragraph>A paragraph.</paragraph>
49 </document>
50 """,
51 """\
52 <document source="test data">
53 <paragraph>
54 A paragraph.
55 """],
56 ["""\
57 <document source="test sample">
58 <paragraph>A paragraph
59 with two lines.</paragraph>
60 </document>
61 """,
62 """\
63 <document source="test sample">
64 <paragraph>
65 A paragraph
66 with two lines.
67 """],
68 ["""\
69 <document>
70 <paragraph>Paragraph 1.</paragraph>
71 <paragraph>Paragraph 2.</paragraph>
72 </document>
73 """,
74 """\
75 <document source="test data">
76 <paragraph>
77 Paragraph 1.
78 <paragraph>
79 Paragraph 2.
80 """],
81 ["""\
82 <paragraph>Sub-trees are parsed, too.
83 Line breaks are preserved.</paragraph>
84 """,
85 """\
86 <document source="test data">
87 <paragraph>
88 Sub-trees are parsed, too.
89 Line breaks are preserved.
90 """],
91 ]
92
93 totest['inline'] = [
94 ["""\
95 <paragraph><emphasis>emphatically</emphasis></paragraph>
96 """,
97 """\
98 <document source="test data">
99 <paragraph>
100 <emphasis>
101 emphatically
102 """],
103 ["""\
104 <paragraph><strong>strong</strong> week</paragraph>
105 """,
106 """\
107 <document source="test data">
108 <paragraph>
109 <strong>
110 strong
111 week
112 """],
113 ["""\
114 <document source="test data">
115 <literal_block xml:space="preserve"> Inline element
116 with <strong> space at start,
117 in the middle</strong>
118 and after end.</literal_block>
119 </document>
120 """,
121 """\
122 <document source="test data">
123 <literal_block xml:space="preserve">
124 Inline element
125 with \n\
126 <strong>
127 space at start,
128 in the middle
129 \n\
130 and after end.
131 """],
132 ]
133
134 totest['attributes'] = [
135 ["""\
136 <paragraph classes="top secret">classified text</paragraph>
137 """,
138 """\
139 <document source="test data">
140 <paragraph classes="top secret">
141 classified text
142 """],
143 [r"""
144 <paragraph ids="ref-2 ref-1" names="ref\ 2 ref\ 1">target paragraph</paragraph>
145 """,
146 r"""<document source="test data">
147 <paragraph ids="ref-2 ref-1" names="ref\ 2 ref\ 1">
148 target paragraph
149 """],
150 ]
151
152 totest['invalid'] = [
153 ["""\
154 <document>
155 <tip>
156 spurious text
157 <paragraph>A paragraph.</paragraph>
158 </tip>
159 </document>
160 """,
161 """\
162 <document source="test data">
163 <tip>
164 spurious text
165 <paragraph>
166 A paragraph.
167 """],
168 ["""\
169 <document>
170 spurious text
171 <paragraph>A paragraph.</paragraph>
172 </document>
173 """,
174 """\
175 <document source="test data">
176 spurious text
177 <paragraph>
178 A paragraph.
179 """],
180 ["""\
181 <document>
182 <tip>
183 <paragraph>A paragraph.</paragraph>
184 spurious tailing text
185 </tip>
186 </document>
187 """,
188 """\
189 <document source="test data">
190 <tip>
191 <paragraph>
192 A paragraph.
193 spurious tailing text
194 """],
195 ["""\
196 <document>
197 <paragraph>A paragraph.</paragraph>
198 spurious tailing text
199 </document>
200 """,
201 """\
202 <document source="test data">
203 <paragraph>
204 A paragraph.
205 spurious tailing text
206 """],
207 ["""\
208 just spurious text
209 """,
210 """\
211 <document source="test data">
212 <paragraph>
213 <problematic>
214 No XML element found.
215 """],
216 ]
217
218
219 if __name__ == '__main__':
220 unittest.main()