Debian Codegraph @

test/test_parsers/test_recommonmark/test_transitions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_transitions.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 transitions (`thematic breaks`).
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.recommonmark_wrapper import Parser
22 from docutils.utils import new_document
23
24
25 class RecommonmarkParserTestCase(unittest.TestCase):
26 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 for name, cases in totest.items():
30 for casenum, (case_input, case_expected) in enumerate(cases):
31 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
32 document = new_document('test data', settings.copy())
33 parser.parse(case_input, document)
34 output = document.pformat()
35 self.assertEqual(case_expected, output)
36
37
38 totest = {}
39
40 # Each dictionary key (test type name) maps to a list of tests. Each
41 # test is a list: input, expected output.
42 totest['transitions'] = [
43 ["""\
44 Test transition markers.
45
46 --------
47
48 Paragraph
49 """,
50 """\
51 <document source="test data">
52 <paragraph>
53 Test transition markers.
54 <transition>
55 <paragraph>
56 Paragraph
57 """],
58 ["""\
59 Section 1
60 =========
61 First text division of section 1.
62
63 --------
64
65 Second text division of section 1.
66
67 Section 2
68 ---------
69 Paragraph 2 in section 2.
70 """,
71 """\
72 <document source="test data">
73 <section ids="section-1" names="section\\ 1">
74 <title>
75 Section 1
76 <paragraph>
77 First text division of section 1.
78 <transition>
79 <paragraph>
80 Second text division of section 1.
81 <section ids="section-2" names="section\\ 2">
82 <title>
83 Section 2
84 <paragraph>
85 Paragraph 2 in section 2.
86 """],
87 # ["""\
88 # --------
89 #
90 # A section or document may not begin with a transition.
91 #
92 # The DTD specifies that two transitions may not
93 # be adjacent:
94 #
95 # --------
96 #
97 # --------
98 #
99 # --------
100 #
101 # The DTD also specifies that a section or document
102 # may not end with a transition.
103 #
104 # --------
105 # """,
106 # """\
107 # <document source="test data">
108 # <transition>
109 # <paragraph>
110 # A section or document may not begin with a transition.
111 # <paragraph>
112 # The DTD specifies that two transitions may not
113 # be adjacent:
114 # <transition>
115 # <transition>
116 # <transition>
117 # <paragraph>
118 # The DTD also specifies that a section or document
119 # may not end with a transition.
120 # <transition>
121 # """],
122 # TODO: should we allow transitions in block elements?
123 # +1 Other document formats allow this (HTML, markdown, LaTeX)
124 # and a quoted text may contain a transition.
125 # -1 Requires changing the document model.
126 # ["""\
127 # Test unexpected transition markers.
128 #
129 # > Block quote.
130 # >
131 # > --------
132 # >
133 # > Paragraph.
134 # """,
135 # """\
136 # <document source="test data">
137 # <paragraph>
138 # Test unexpected transition markers.
139 # <block_quote>
140 # <paragraph>
141 # Block quote.
142 # <transition>
143 # <paragraph>
144 # Paragraph.
145 # """],
146 ["""\
147 Short transition marker.
148
149 ---
150
151 Too short transition marker.
152
153 --
154
155 Paragraph
156 """,
157 """\
158 <document source="test data">
159 <paragraph>
160 Short transition marker.
161 <transition>
162 <paragraph>
163 Too short transition marker.
164 <paragraph>
165 --
166 <paragraph>
167 Paragraph
168 """],
169 ["""\
170 Sections with transitions at beginning and end.
171
172 Section 1
173 =========
174
175 ----------
176
177 The next transition is legal:
178
179 ----------
180
181 Section 2
182 =========
183
184 ----------
185 """,
186 """\
187 <document source="test data">
188 <paragraph>
189 Sections with transitions at beginning and end.
190 <section ids="section-1" names="section\\ 1">
191 <title>
192 Section 1
193 <transition>
194 <paragraph>
195 The next transition is legal:
196 <transition>
197 <section ids="section-2" names="section\\ 2">
198 <title>
199 Section 2
200 <transition>
201 """],
202 ["""\
203 A paragraph, two transitions, and a blank line.
204
205 ----------
206
207 ----------
208
209 """,
210 """\
211 <document source="test data">
212 <paragraph>
213 A paragraph, two transitions, and a blank line.
214 <transition>
215 <transition>
216 """],
217 ["""\
218 A paragraph and two transitions.
219
220 ----------
221
222 ----------
223 """,
224 """\
225 <document source="test data">
226 <paragraph>
227 A paragraph and two transitions.
228 <transition>
229 <transition>
230 """],
231 ["""\
232 ----------
233
234 Document beginning with a transition.
235 """,
236 """\
237 <document source="test data">
238 <transition>
239 <paragraph>
240 Document beginning with a transition.
241 """],
242 ["""\
243 Section 1
244 =========
245
246 Subsection 1
247 ------------
248
249 Some text.
250
251 ----------
252
253 Section 2
254 =========
255
256 Some text.
257 """,
258 """\
259 <document source="test data">
260 <section ids="section-1" names="section\\ 1">
261 <title>
262 Section 1
263 <section ids="subsection-1" names="subsection\\ 1">
264 <title>
265 Subsection 1
266 <paragraph>
267 Some text.
268 <transition>
269 <section ids="section-2" names="section\\ 2">
270 <title>
271 Section 2
272 <paragraph>
273 Some text.
274 """],
275 ["""\
276 Section 1
277 =========
278
279 ----------
280
281 ----------
282
283 ----------
284
285 Section 2
286 =========
287
288 Some text.
289 """,
290 """\
291 <document source="test data">
292 <section ids="section-1" names="section\\ 1">
293 <title>
294 Section 1
295 <transition>
296 <transition>
297 <transition>
298 <section ids="section-2" names="section\\ 2">
299 <title>
300 Section 2
301 <paragraph>
302 Some text.
303 """],
304 ["""\
305 ----------
306
307 ----------
308
309 ----------
310 """,
311 """\
312 <document source="test data">
313 <transition>
314 <transition>
315 <transition>
316 """],
317 ["""\
318 A paragraph.
319
320 ----------
321
322 """,
323 """\
324 <document source="test data">
325 <paragraph>
326 A paragraph.
327 <transition>
328 """],
329 ]
330
331
332 if __name__ == '__main__':
333 unittest.main()