Debian Codegraph @

test/test_parsers/test_recommonmark/test_enumerated_lists.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2020 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 Test for enumerated lists in CommonMark parsers
13 Cf. the `CommonMark Specification <https://spec.commonmark.org/>`__
14 """
15
16 from pathlib import Path
17 import sys
18 import unittest
19
20 if __name__ == '__main__':
21 # prepend the "docutils root" to the Python library path
22 # so we import the local `docutils` package.
23 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
24
25 from docutils.frontend import get_default_settings
26 from docutils.parsers.recommonmark_wrapper import Parser
27 from docutils.utils import new_document
28
29
30 class RecommonmarkParserTestCase(unittest.TestCase):
31 def test_parser(self):
32 parser = Parser()
33 settings = get_default_settings(Parser)
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 totest['enumerated_lists'] = [
46 ["""\
47 1. Item one.
48
49 2. Item two.
50
51 3. Item three.
52 """,
53 """\
54 <document source="test data">
55 <enumerated_list>
56 <list_item>
57 <paragraph>
58 Item one.
59 <list_item>
60 <paragraph>
61 Item two.
62 <list_item>
63 <paragraph>
64 Item three.
65 """],
66 ["""\
67 No blank lines betwen items:
68
69 1. Item one.
70 2. Item two.
71 3. Item three.
72 """,
73 """\
74 <document source="test data">
75 <paragraph>
76 No blank lines betwen items:
77 <enumerated_list>
78 <list_item>
79 <paragraph>
80 Item one.
81 <list_item>
82 <paragraph>
83 Item two.
84 <list_item>
85 <paragraph>
86 Item three.
87 """],
88 ["""\
89 1.
90 Content may start at the next line
91 if it's indented at least 3 spaces.
92 """,
93 """\
94 <document source="test data">
95 <enumerated_list>
96 <list_item>
97 <paragraph>
98 Content may start at the next line
99 if it's indented at least 3 spaces.
100 """],
101 ["""\
102 1.
103 empty item above, no blank line, no indent
104 """,
105 """\
106 <document source="test data">
107 <enumerated_list>
108 <list_item>
109 <paragraph>
110 empty item above, no blank line, no indent
111 """],
112 ["""\
113 Items are auto-numbered.
114 No check for consistency: Skipping item 3
115
116 1. Item 1.
117 2. Item 2.
118 4. Item 4.
119 """,
120 """\
121 <document source="test data">
122 <paragraph>
123 Items are auto-numbered.
124 No check for consistency: Skipping item 3
125 <enumerated_list>
126 <list_item>
127 <paragraph>
128 Item 1.
129 <list_item>
130 <paragraph>
131 Item 2.
132 <list_item>
133 <paragraph>
134 Item 4.
135 """],
136 ["""\
137 No warning when starting with non-ordinal-1:
138
139 0. Item zero.
140 1. Item one.
141 2. Item two.
142 3. Item three.
143
144 And again:
145
146 2. Item two.
147 3. Item three.
148 """,
149 """\
150 <document source="test data">
151 <paragraph>
152 No warning when starting with non-ordinal-1:
153 <enumerated_list>
154 <list_item>
155 <paragraph>
156 Item zero.
157 <list_item>
158 <paragraph>
159 Item one.
160 <list_item>
161 <paragraph>
162 Item two.
163 <list_item>
164 <paragraph>
165 Item three.
166 <paragraph>
167 And again:
168 <enumerated_list>
169 <list_item>
170 <paragraph>
171 Item two.
172 <list_item>
173 <paragraph>
174 Item three.
175 """],
176 ["""\
177 1. Item one: line 1,
178 line 2.
179 2. Item two: line 1,
180 line 2.
181 3. Item three: paragraph 1, line 1,
182 line 2.
183
184 Paragraph 2.
185 """,
186 """\
187 <document source="test data">
188 <enumerated_list>
189 <list_item>
190 <paragraph>
191 Item one: line 1,
192 line 2.
193 <list_item>
194 <paragraph>
195 Item two: line 1,
196 line 2.
197 <list_item>
198 <paragraph>
199 Item three: paragraph 1, line 1,
200 line 2.
201 <paragraph>
202 Paragraph 2.
203 """],
204 ["""\
205 Supported enumeration sequences:
206
207 1. Item 1.
208 2. Item 2.
209
210 1) Item 1)
211 2) Item 2)
212
213 """,
214 """\
215 <document source="test data">
216 <paragraph>
217 Supported enumeration sequences:
218 <enumerated_list>
219 <list_item>
220 <paragraph>
221 Item 1.
222 <list_item>
223 <paragraph>
224 Item 2.
225 <enumerated_list>
226 <list_item>
227 <paragraph>
228 Item 1)
229 <list_item>
230 <paragraph>
231 Item 2)
232 """],
233 ["""\
234 Nested enumerated lists:
235
236 1. Item 1
237
238 1) Item 1.1
239 2) Item 1.2
240 3) Item 1.3
241
242 2. Item 2
243
244 1. Item 2.1
245
246 1) Item 2.1.1
247 2) Item 2.1.2
248 3) Item 2.1.3
249
250 2. Item 2.2
251
252 3. Item 2.3
253
254 3. Item 3.
255 """,
256 """\
257 <document source="test data">
258 <paragraph>
259 Nested enumerated lists:
260 <enumerated_list>
261 <list_item>
262 <paragraph>
263 Item 1
264 <enumerated_list>
265 <list_item>
266 <paragraph>
267 Item 1.1
268 <list_item>
269 <paragraph>
270 Item 1.2
271 <list_item>
272 <paragraph>
273 Item 1.3
274 <list_item>
275 <paragraph>
276 Item 2
277 <enumerated_list>
278 <list_item>
279 <paragraph>
280 Item 2.1
281 <enumerated_list>
282 <list_item>
283 <paragraph>
284 Item 2.1.1
285 <list_item>
286 <paragraph>
287 Item 2.1.2
288 <list_item>
289 <paragraph>
290 Item 2.1.3
291 <list_item>
292 <paragraph>
293 Item 2.2
294 <list_item>
295 <paragraph>
296 Item 2.3
297 <list_item>
298 <paragraph>
299 Item 3.
300 """],
301 ["""\
302 1. Item one: line 1,
303 line 2.
304 2. Item two: line 1,
305 line 2.
306 3. Item three: paragraph 1, line 1,
307 line 2.
308
309 Item three: paragraph 2.
310 """,
311 """\
312 <document source="test data">
313 <enumerated_list>
314 <list_item>
315 <paragraph>
316 Item one: line 1,
317 line 2.
318 <list_item>
319 <paragraph>
320 Item two: line 1,
321 line 2.
322 <list_item>
323 <paragraph>
324 Item three: paragraph 1, line 1,
325 line 2.
326 <paragraph>
327 Item three: paragraph 2.
328 """],
329 ["""\
330 3-space indent, with a trailing space:
331
332 1. \n\
333 list item 1
334
335 3-space indent, no trailing space:
336
337 1.
338 list item 1
339
340 2-space indent, empty list item:
341
342 1.
343 foo
344
345 1-space indent, empty list item:
346
347 1.
348 foo
349
350 0-space indent, empty list item:
351
352 1.
353 foo
354
355 No item content:
356
357 1.
358 """,
359 """\
360 <document source="test data">
361 <paragraph>
362 3-space indent, with a trailing space:
363 <enumerated_list>
364 <list_item>
365 <paragraph>
366 list item 1
367 <paragraph>
368 3-space indent, no trailing space:
369 <enumerated_list>
370 <list_item>
371 <paragraph>
372 list item 1
373 <paragraph>
374 2-space indent, empty list item:
375 <enumerated_list>
376 <list_item>
377 <paragraph>
378 foo
379 <paragraph>
380 1-space indent, empty list item:
381 <enumerated_list>
382 <list_item>
383 <paragraph>
384 foo
385 <paragraph>
386 0-space indent, empty list item:
387 <enumerated_list>
388 <list_item>
389 <paragraph>
390 foo
391 <paragraph>
392 No item content:
393 <enumerated_list>
394 <list_item>
395 """],
396 ]
397
398
399 if __name__ == '__main__':
400 unittest.main()