Debian Codegraph @

test/test_parsers/test_rst/test_transitions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_transitions.py 10308 2026-03-28 18:54:25Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for transition markers.
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 maxDiff = None
27
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.legacy_ids = False
32 settings.warning_stream = ''
33 settings.halt_level = 5
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.copy())
38 parser.parse(case_input, document)
39 output = document.pformat()
40 self.assertEqual(case_expected, output)
41
42
43 totest = {}
44
45 # Each dictionary key (test type name) maps to a list of tests. Each
46 # test is a list: input, expected output.
47 totest['transitions'] = [
48 ["""\
49 Test transition markers.
50
51 --------
52
53 Paragraph
54 """,
55 """\
56 <document source="test data">
57 <paragraph>
58 Test transition markers.
59 <transition>
60 <paragraph>
61 Paragraph
62 """],
63 ["""\
64 Section 1
65 =========
66 First text division of section 1.
67
68 --------
69
70 Second text division of section 1.
71
72 Section 2
73 ---------
74 Paragraph 2 in section 2.
75 """,
76 """\
77 <document source="test data">
78 <section names="section\\ 1">
79 <title>
80 Section 1
81 <paragraph>
82 First text division of section 1.
83 <transition>
84 <paragraph>
85 Second text division of section 1.
86 <section names="section\\ 2">
87 <title>
88 Section 2
89 <paragraph>
90 Paragraph 2 in section 2.
91 """],
92 # TODO: disable this test case, as it endorses non-standard results?
93 ["""\
94 --------
95
96 According to the DTD, a section or document may not begin with a transition.
97
98 Note: There is currently no warning, but in future these
99 DTD violations should be prevented or at least trigger a warning.
100 Alternatively, the DTD may be relaxed to accomodate for more use cases.
101
102 The DTD specifies that two transitions may not
103 be adjacent:
104
105 --------
106
107 --------
108
109 --------
110
111 The DTD also specifies that a section or document
112 may not end with a transition.
113
114 --------
115 """,
116 """\
117 <document source="test data">
118 <transition>
119 <paragraph>
120 According to the DTD, a section or document may not begin with a transition.
121 <paragraph>
122 Note: There is currently no warning, but in future these
123 DTD violations should be prevented or at least trigger a warning.
124 Alternatively, the DTD may be relaxed to accomodate for more use cases.
125 <paragraph>
126 The DTD specifies that two transitions may not
127 be adjacent:
128 <transition>
129 <transition>
130 <transition>
131 <paragraph>
132 The DTD also specifies that a section or document
133 may not end with a transition.
134 <transition>
135 """],
136 ["""\
137 Test unexpected transition markers.
138
139 Block quote.
140
141 --------
142
143 Paragraph.
144 """,
145 """\
146 <document source="test data">
147 <paragraph>
148 Test unexpected transition markers.
149 <block_quote>
150 <paragraph>
151 Block quote.
152 <system_message level="3" line="5" source="test data" type="ERROR">
153 <paragraph>
154 Unexpected section title or transition.
155 <literal_block xml:space="preserve">
156 --------
157 <paragraph>
158 Paragraph.
159 """],
160 ["""\
161 Short transition marker.
162
163 ---
164
165 Paragraph
166 """,
167 """\
168 <document source="test data">
169 <paragraph>
170 Short transition marker.
171 <paragraph>
172 ---
173 <paragraph>
174 Paragraph
175 """],
176 ["""\
177 Sections with transitions at beginning and end.
178
179 Section 1
180 =========
181
182 ----------
183
184 The next transition is legal:
185
186 ----------
187
188 Section 2
189 =========
190
191 ----------
192 """,
193 """\
194 <document source="test data">
195 <paragraph>
196 Sections with transitions at beginning and end.
197 <section names="section\\ 1">
198 <title>
199 Section 1
200 <transition>
201 <paragraph>
202 The next transition is legal:
203 <transition>
204 <section names="section\\ 2">
205 <title>
206 Section 2
207 <transition>
208 """],
209 ["""\
210 A paragraph, two transitions, and a blank line.
211
212 ----------
213
214 ----------
215
216 """,
217 """\
218 <document source="test data">
219 <paragraph>
220 A paragraph, two transitions, and a blank line.
221 <transition>
222 <transition>
223 """],
224 ["""\
225 A paragraph and two transitions.
226
227 ----------
228
229 ----------
230 """,
231 """\
232 <document source="test data">
233 <paragraph>
234 A paragraph and two transitions.
235 <transition>
236 <transition>
237 """],
238 ["""\
239 ----------
240
241 Document beginning with a transition.
242 """,
243 """\
244 <document source="test data">
245 <transition>
246 <paragraph>
247 Document beginning with a transition.
248 """],
249 ["""\
250 Section 1
251 =========
252
253 Subsection 1
254 ------------
255
256 Some text.
257
258 ----------
259
260 Section 2
261 =========
262
263 Some text.
264 """,
265 """\
266 <document source="test data">
267 <section names="section\\ 1">
268 <title>
269 Section 1
270 <section names="subsection\\ 1">
271 <title>
272 Subsection 1
273 <paragraph>
274 Some text.
275 <transition>
276 <section names="section\\ 2">
277 <title>
278 Section 2
279 <paragraph>
280 Some text.
281 """],
282 ["""\
283 Section 1
284 =========
285
286 ----------
287
288 ----------
289
290 ----------
291
292 Section 2
293 =========
294
295 Some text.
296 """,
297 """\
298 <document source="test data">
299 <section names="section\\ 1">
300 <title>
301 Section 1
302 <transition>
303 <transition>
304 <transition>
305 <section names="section\\ 2">
306 <title>
307 Section 2
308 <paragraph>
309 Some text.
310 """],
311 ["""\
312 ----------
313
314 ----------
315
316 ----------
317 """,
318 """\
319 <document source="test data">
320 <transition>
321 <transition>
322 <transition>
323 """],
324 ["""\
325 A paragraph.
326
327 ----------
328
329 """,
330 """\
331 <document source="test data">
332 <paragraph>
333 A paragraph.
334 <transition>
335 """],
336 ]
337
338
339 if __name__ == '__main__':
340 unittest.main()