Debian Codegraph @

test/test_parsers/test_recommonmark/test_section_headers.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 section headings 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
26 from docutils.frontend import get_default_settings
27 from docutils.parsers.recommonmark_wrapper import Parser
28 from docutils.utils import new_document
29
30
31 class RecommonmarkParserTestCase(unittest.TestCase):
32 def test_parser(self):
33 parser = Parser()
34 settings = get_default_settings(Parser)
35 for name, cases in totest.items():
36 for casenum, (case_input, case_expected) in enumerate(cases):
37 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
38 document = new_document('test data', settings.copy())
39 parser.parse(case_input, document)
40 output = document.pformat()
41 self.assertEqual(case_expected, output)
42
43
44 totest = {}
45
46 totest['section_headers'] = [
47 ["""\
48 The Title
49 =========
50 Paragraph.
51 """,
52 r"""<document source="test data">
53 <section ids="the-title" names="the\ title">
54 <title>
55 The Title
56 <paragraph>
57 Paragraph.
58 """],
59 ["""\
60 Another Section Title
61 =====================
62 Paragraph (no blank line required).
63 """,
64 r"""<document source="test data">
65 <section ids="another-section-title" names="another\ section\ title">
66 <title>
67 Another Section Title
68 <paragraph>
69 Paragraph (no blank line required).
70 """],
71 ["""\
72 Paragraph.
73
74 Title
75 =====
76
77 Paragraph.
78 """,
79 """\
80 <document source="test data">
81 <paragraph>
82 Paragraph.
83 <section ids="title" names="title">
84 <title>
85 Title
86 <paragraph>
87 Paragraph.
88 """],
89 # ["""\
90 # Test unexpected section titles.
91 #
92 # * Title
93 # =====
94 #
95 # Paragraph.
96 # """,
97 # """\
98 # <document source="test data">
99 # <paragraph>
100 # Test unexpected section titles.
101 # <system_message level="3" source="test data" type="ERROR">
102 # <paragraph>
103 # Parsing with "recommonmark" returned the error:
104 # 'NoneType' object has no attribute 'note_implicit_target'
105 # """],
106 ["""\
107 Title
108 ==
109
110 A short underline is no problem in CommonMark
111 """,
112 """\
113 <document source="test data">
114 <section ids="title" names="title">
115 <title>
116 Title
117 <paragraph>
118 A short underline is no problem in CommonMark
119 """],
120 ["""\
121 Test return to existing, highest-level section (Title 3).
122
123 Title 1
124 =======
125 Paragraph 1.
126
127 Title 2
128 -------
129 Paragraph 2.
130
131 Title 3
132 =======
133 Paragraph 3.
134
135 Title 4
136 -------
137 Paragraph 4.
138 """,
139 r"""<document source="test data">
140 <paragraph>
141 Test return to existing, highest-level section (Title 3).
142 <section ids="title-1" names="title\ 1">
143 <title>
144 Title 1
145 <paragraph>
146 Paragraph 1.
147 <section ids="title-2" names="title\ 2">
148 <title>
149 Title 2
150 <paragraph>
151 Paragraph 2.
152 <section ids="title-3" names="title\ 3">
153 <title>
154 Title 3
155 <paragraph>
156 Paragraph 3.
157 <section ids="title-4" names="title\ 4">
158 <title>
159 Title 4
160 <paragraph>
161 Paragraph 4.
162 """],
163 ["""\
164 Test bad subsection order.
165
166 ## Title 1
167
168 ## Title 2
169
170 # Title 3
171
172 #### Title 4
173
174 ### Title 5
175 """,
176 r"""<document source="test data">
177 <paragraph>
178 Test bad subsection order.
179 <section ids="title-1" names="title\ 1">
180 <title>
181 Title 1
182 <section ids="title-2" names="title\ 2">
183 <title>
184 Title 2
185 <section ids="title-3" names="title\ 3">
186 <title>
187 Title 3
188 <section ids="title-4" names="title\ 4">
189 <title>
190 Title 4
191 <section ids="title-5" names="title\ 5">
192 <title>
193 Title 5
194 """],
195 ["""\
196 Title containing *inline* ``markup``
197 ====================================
198
199 Paragraph.
200 """,
201 """\
202 <document source="test data">
203 <section ids="title-containing-inline-markup" names="title\\ containing\\ inline\\ markup">
204 <title>
205 Title containing \n\
206 <emphasis>
207 inline
208 \n\
209 <literal classes="code">
210 markup
211 <paragraph>
212 Paragraph.
213 """],
214 ["""\
215 1. Not a numbered title but an enumerated list
216 ==============================================
217
218 Paragraph.
219 """,
220 """\
221 <document source="test data">
222 <enumerated_list>
223 <list_item>
224 <paragraph>
225 Not a numbered title but an enumerated list
226 ==============================================
227 <paragraph>
228 Paragraph.
229 """],
230 ["""\
231 ABC
232 ===
233
234 Short title.
235 """,
236 """\
237 <document source="test data">
238 <section ids="abc" names="abc">
239 <title>
240 ABC
241 <paragraph>
242 Short title.
243 """],
244 ["""\
245 Empty Section
246 =============
247 """,
248 """\
249 <document source="test data">
250 <section ids="empty-section" names="empty\\ section">
251 <title>
252 Empty Section
253 """],
254 ]
255
256
257 if __name__ == '__main__':
258 unittest.main()