Debian Codegraph @

test/test_parsers/test_rst/test_comments.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_comments.py 9425 2023-06-30 14:56:47Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for states.py.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
23
24
25 class ParserTestCase(unittest.TestCase):
26 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 settings.warning_stream = ''
30 for name, cases in totest.items():
31 for casenum, (case_input, case_expected) in enumerate(cases):
32 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
33 document = new_document('test data', settings.copy())
34 parser.parse(case_input, document)
35 output = document.pformat()
36 self.assertEqual(case_expected, output)
37
38
39 totest = {}
40
41 totest['comments'] = [
42 ["""\
43 .. A comment
44
45 Paragraph.
46 """,
47 """\
48 <document source="test data">
49 <comment xml:space="preserve">
50 A comment
51 <paragraph>
52 Paragraph.
53 """],
54 ["""\
55 .. A comment
56 block.
57
58 Paragraph.
59 """,
60 """\
61 <document source="test data">
62 <comment xml:space="preserve">
63 A comment
64 block.
65 <paragraph>
66 Paragraph.
67 """],
68 ["""\
69 ..
70 A comment consisting of multiple lines
71 starting on the line after the
72 explicit markup start.
73 """,
74 """\
75 <document source="test data">
76 <comment xml:space="preserve">
77 A comment consisting of multiple lines
78 starting on the line after the
79 explicit markup start.
80 """],
81 ["""\
82 .. A comment.
83 .. Another.
84
85 Paragraph.
86 """,
87 """\
88 <document source="test data">
89 <comment xml:space="preserve">
90 A comment.
91 <comment xml:space="preserve">
92 Another.
93 <paragraph>
94 Paragraph.
95 """],
96 ["""\
97 .. A comment
98 no blank line
99
100 Paragraph.
101 """,
102 """\
103 <document source="test data">
104 <comment xml:space="preserve">
105 A comment
106 <system_message level="2" line="2" source="test data" type="WARNING">
107 <paragraph>
108 Explicit markup ends without a blank line; unexpected unindent.
109 <paragraph>
110 no blank line
111 <paragraph>
112 Paragraph.
113 """],
114 ["""\
115 .. A comment.
116 .. Another.
117 no blank line
118
119 Paragraph.
120 """,
121 """\
122 <document source="test data">
123 <comment xml:space="preserve">
124 A comment.
125 <comment xml:space="preserve">
126 Another.
127 <system_message level="2" line="3" source="test data" type="WARNING">
128 <paragraph>
129 Explicit markup ends without a blank line; unexpected unindent.
130 <paragraph>
131 no blank line
132 <paragraph>
133 Paragraph.
134 """],
135 ["""\
136 .. A comment::
137
138 Paragraph.
139 """,
140 """\
141 <document source="test data">
142 <comment xml:space="preserve">
143 A comment::
144 <paragraph>
145 Paragraph.
146 """],
147 ["""\
148 ..
149 comment::
150
151 The extra newline before the comment text prevents
152 the parser from recognizing a directive.
153 """,
154 """\
155 <document source="test data">
156 <comment xml:space="preserve">
157 comment::
158 <paragraph>
159 The extra newline before the comment text prevents
160 the parser from recognizing a directive.
161 """],
162 ["""\
163 ..
164 _comment: http://example.org
165
166 The extra newline before the comment text prevents
167 the parser from recognizing a hyperlink target.
168 """,
169 """\
170 <document source="test data">
171 <comment xml:space="preserve">
172 _comment: http://example.org
173 <paragraph>
174 The extra newline before the comment text prevents
175 the parser from recognizing a hyperlink target.
176 """],
177 ["""\
178 ..
179 [comment] Not a citation.
180
181 The extra newline before the comment text prevents
182 the parser from recognizing a citation.
183 """,
184 """\
185 <document source="test data">
186 <comment xml:space="preserve">
187 [comment] Not a citation.
188 <paragraph>
189 The extra newline before the comment text prevents
190 the parser from recognizing a citation.
191 """],
192 ["""\
193 ..
194 |comment| image:: bogus.png
195
196 The extra newline before the comment text prevents
197 the parser from recognizing a substitution definition.
198 """,
199 """\
200 <document source="test data">
201 <comment xml:space="preserve">
202 |comment| image:: bogus.png
203 <paragraph>
204 The extra newline before the comment text prevents
205 the parser from recognizing a substitution definition.
206 """],
207 ["""\
208 .. Next is an empty comment, which serves to end this comment and
209 prevents the following block quote being swallowed up.
210
211 ..
212
213 A block quote.
214 """,
215 """\
216 <document source="test data">
217 <comment xml:space="preserve">
218 Next is an empty comment, which serves to end this comment and
219 prevents the following block quote being swallowed up.
220 <comment xml:space="preserve">
221 <block_quote>
222 <paragraph>
223 A block quote.
224 """],
225 ["""\
226 term 1
227 definition 1
228
229 .. a comment
230
231 term 2
232 definition 2
233 """,
234 """\
235 <document source="test data">
236 <definition_list>
237 <definition_list_item>
238 <term>
239 term 1
240 <definition>
241 <paragraph>
242 definition 1
243 <comment xml:space="preserve">
244 a comment
245 <definition_list_item>
246 <term>
247 term 2
248 <definition>
249 <paragraph>
250 definition 2
251 """],
252 ["""\
253 term 1
254 definition 1
255
256 .. a comment
257
258 term 2
259 definition 2
260 """,
261 """\
262 <document source="test data">
263 <definition_list>
264 <definition_list_item>
265 <term>
266 term 1
267 <definition>
268 <paragraph>
269 definition 1
270 <comment xml:space="preserve">
271 a comment
272 <definition_list>
273 <definition_list_item>
274 <term>
275 term 2
276 <definition>
277 <paragraph>
278 definition 2
279 """],
280 ["""\
281 + bullet paragraph 1
282
283 bullet paragraph 2
284
285 .. comment between bullet paragraphs 2 and 3
286
287 bullet paragraph 3
288 """,
289 """\
290 <document source="test data">
291 <bullet_list bullet="+">
292 <list_item>
293 <paragraph>
294 bullet paragraph 1
295 <paragraph>
296 bullet paragraph 2
297 <comment xml:space="preserve">
298 comment between bullet paragraphs 2 and 3
299 <paragraph>
300 bullet paragraph 3
301 """],
302 ["""\
303 + bullet paragraph 1
304
305 .. comment between bullet paragraphs 1 (leader) and 2
306
307 bullet paragraph 2
308 """,
309 """\
310 <document source="test data">
311 <bullet_list bullet="+">
312 <list_item>
313 <paragraph>
314 bullet paragraph 1
315 <comment xml:space="preserve">
316 comment between bullet paragraphs 1 (leader) and 2
317 <paragraph>
318 bullet paragraph 2
319 """],
320 ["""\
321 + bullet
322
323 .. trailing comment
324 """,
325 """\
326 <document source="test data">
327 <bullet_list bullet="+">
328 <list_item>
329 <paragraph>
330 bullet
331 <comment xml:space="preserve">
332 trailing comment
333 """],
334 ]
335
336 if __name__ == '__main__':
337 unittest.main()