Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_topics.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_topics.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 the "topic" directive.
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[4]))
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['topics'] = [
42 ["""\
43 .. topic::
44 """,
45 """\
46 <document source="test data">
47 <system_message level="3" line="1" source="test data" type="ERROR">
48 <paragraph>
49 Error in "topic" directive:
50 1 argument(s) required, 0 supplied.
51 <literal_block xml:space="preserve">
52 .. topic::
53 """],
54 ["""\
55 .. topic:: Title
56 """,
57 """\
58 <document source="test data">
59 <system_message level="3" line="1" source="test data" type="ERROR">
60 <paragraph>
61 Content block expected for the "topic" directive; none found.
62 <literal_block xml:space="preserve">
63 .. topic:: Title
64 """],
65 ["""\
66 .. topic:: Title
67
68 Body.
69 """,
70 """\
71 <document source="test data">
72 <topic>
73 <title>
74 Title
75 <paragraph>
76 Body.
77 """],
78 ["""\
79 .. topic:: With Options
80 :class: custom
81 :name: my point
82
83 Body.
84 """,
85 """\
86 <document source="test data">
87 <topic classes="custom" ids="my-point" names="my\\ point">
88 <title>
89 With Options
90 <paragraph>
91 Body.
92 """],
93 ["""\
94 .. topic::
95
96 Title
97
98 Body.
99 """,
100 """\
101 <document source="test data">
102 <system_message level="3" line="1" source="test data" type="ERROR">
103 <paragraph>
104 Error in "topic" directive:
105 1 argument(s) required, 0 supplied.
106 <literal_block xml:space="preserve">
107 .. topic::
108 \n\
109 Title
110 \n\
111 Body.
112 """],
113 ["""\
114 .. topic:: Title
115 Body.
116 """,
117 """\
118 <document source="test data">
119 <system_message level="3" line="1" source="test data" type="ERROR">
120 <paragraph>
121 Content block expected for the "topic" directive; none found.
122 <literal_block xml:space="preserve">
123 .. topic:: Title
124 Body.
125 """],
126 ["""\
127 .. topic::
128
129 Title
130 Body.
131 """,
132 """\
133 <document source="test data">
134 <system_message level="3" line="1" source="test data" type="ERROR">
135 <paragraph>
136 Error in "topic" directive:
137 1 argument(s) required, 0 supplied.
138 <literal_block xml:space="preserve">
139 .. topic::
140 \n\
141 Title
142 Body.
143 """],
144 ["""\
145 .. topic:: Title
146
147 .. topic:: Nested
148
149 Body.
150 """,
151 """\
152 <document source="test data">
153 <topic>
154 <title>
155 Title
156 <system_message level="3" line="3" source="test data" type="ERROR">
157 <paragraph>
158 The "topic" directive may not be used within topics or body elements.
159 <literal_block xml:space="preserve">
160 .. topic:: Nested
161 \n\
162 Body.
163 """],
164 ["""\
165 .. topic:: Title
166
167 .. topic:: Nested
168
169 Body.
170 More.
171 """,
172 """\
173 <document source="test data">
174 <topic>
175 <title>
176 Title
177 <system_message level="3" line="3" source="test data" type="ERROR">
178 <paragraph>
179 The "topic" directive may not be used within topics or body elements.
180 <literal_block xml:space="preserve">
181 .. topic:: Nested
182 \n\
183 Body.
184 <system_message level="2" line="6" source="test data" type="WARNING">
185 <paragraph>
186 Explicit markup ends without a blank line; unexpected unindent.
187 <paragraph>
188 More.
189 """],
190 ["""\
191 .. topic:: Title
192
193 .. topic:: Nested
194
195 Body.
196
197 More.
198
199 More.
200 """,
201 """\
202 <document source="test data">
203 <topic>
204 <title>
205 Title
206 <system_message level="3" line="3" source="test data" type="ERROR">
207 <paragraph>
208 The "topic" directive may not be used within topics or body elements.
209 <literal_block xml:space="preserve">
210 .. topic:: Nested
211 \n\
212 Body.
213 <paragraph>
214 More.
215 <paragraph>
216 More.
217 """],
218 ["""\
219 .. topic:: First
220
221 Body
222
223 .. topic:: Second
224
225 Body.
226 """,
227 """\
228 <document source="test data">
229 <topic>
230 <title>
231 First
232 <paragraph>
233 Body
234 <topic>
235 <title>
236 Second
237 <paragraph>
238 Body.
239 """],
240 ["""\
241 .. sidebar:: Title
242 :subtitle: Outer
243
244 .. topic:: Nested
245
246 Body.
247
248 More.
249
250 More.
251 """,
252 """\
253 <document source="test data">
254 <sidebar>
255 <title>
256 Title
257 <subtitle>
258 Outer
259 <topic>
260 <title>
261 Nested
262 <paragraph>
263 Body.
264 <paragraph>
265 More.
266 <paragraph>
267 More.
268 """],
269 ]
270
271
272 if __name__ == '__main__':
273 unittest.main()