Fix hashbang line in "smartquotes" script.
[docutils.git] / docutils / test / test_parsers / test_rst / test_definition_lists.py
blobeaca0854cbaf30f2c31d6ecb6c38bfaadb819e52
1 #! /usr/bin/env python3
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for states.py.
9 """
11 from pathlib import Path
12 import sys
13 import unittest
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]))
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
25 class ParserTestCase(unittest.TestCase):
27 maxDiff = None
29 def test_parser(self):
30 parser = Parser()
31 settings = get_default_settings(Parser)
32 settings.warning_stream = ''
33 for name, cases in totest.items():
34 for casenum, (case_input, case_expected) in enumerate(cases):
35 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
36 document = new_document('test data', settings.copy())
37 parser.parse(case_input, document)
38 output = document.pformat()
39 self.assertEqual(case_expected, output)
42 totest = {}
44 totest['definition_lists'] = [
45 ["""\
46 term
47 definition
48 """,
49 """\
50 <document source="test data">
51 <definition_list>
52 <definition_list_item>
53 <term>
54 term
55 <definition>
56 <paragraph>
57 definition
58 """],
59 ["""\
60 term
61 definition
63 paragraph
64 """,
65 """\
66 <document source="test data">
67 <definition_list>
68 <definition_list_item>
69 <term>
70 term
71 <definition>
72 <paragraph>
73 definition
74 <paragraph>
75 paragraph
76 """],
77 ["""\
78 term
79 definition
80 no blank line
81 """,
82 """\
83 <document source="test data">
84 <definition_list>
85 <definition_list_item>
86 <term>
87 term
88 <definition>
89 <paragraph>
90 definition
91 <system_message level="2" line="3" source="test data" type="WARNING">
92 <paragraph>
93 Definition list ends without a blank line; unexpected unindent.
94 <paragraph>
95 no blank line
96 """],
97 ["""\
98 A paragraph::
99 A literal block without a blank line first?
100 """,
101 """\
102 <document source="test data">
103 <definition_list>
104 <definition_list_item>
105 <term>
106 A paragraph::
107 <definition>
108 <system_message level="1" line="2" source="test data" type="INFO">
109 <paragraph>
110 Blank line missing before literal block (after the "::")? Interpreted as a definition list item.
111 <paragraph>
112 A literal block without a blank line first?
113 """],
114 ["""\
115 this is not a term;
116 a term may only be one line long
117 this is not a definition
118 """,
119 """\
120 <document source="test data">
121 <paragraph>
122 this is not a term;
123 a term may only be one line long
124 <system_message level="3" line="3" source="test data" type="ERROR">
125 <paragraph>
126 Unexpected indentation.
127 <block_quote>
128 <paragraph>
129 this is not a definition
130 """],
131 ["""\
132 term 1
133 definition 1
135 term 2
136 definition 2
137 """,
138 """\
139 <document source="test data">
140 <definition_list>
141 <definition_list_item>
142 <term>
143 term 1
144 <definition>
145 <paragraph>
146 definition 1
147 <definition_list_item>
148 <term>
149 term 2
150 <definition>
151 <paragraph>
152 definition 2
153 """],
154 ["""\
155 term 1
156 definition 1 (no blank line below)
157 term 2
158 definition 2
159 """,
160 """\
161 <document source="test data">
162 <definition_list>
163 <definition_list_item>
164 <term>
165 term 1
166 <definition>
167 <paragraph>
168 definition 1 (no blank line below)
169 <definition_list_item>
170 <term>
171 term 2
172 <definition>
173 <paragraph>
174 definition 2
175 """],
176 ["""\
177 term 1
178 definition 1 (no blank line below)
179 term 2
180 definition 2
181 No blank line after the definition list.
182 """,
183 """\
184 <document source="test data">
185 <definition_list>
186 <definition_list_item>
187 <term>
188 term 1
189 <definition>
190 <paragraph>
191 definition 1 (no blank line below)
192 <definition_list_item>
193 <term>
194 term 2
195 <definition>
196 <paragraph>
197 definition 2
198 <system_message level="2" line="5" source="test data" type="WARNING">
199 <paragraph>
200 Definition list ends without a blank line; unexpected unindent.
201 <paragraph>
202 No blank line after the definition list.
203 """],
204 ["""\
205 term 1
206 definition 1
208 term 1a
209 definition 1a
211 term 1b
212 definition 1b
214 term 2
215 definition 2
217 paragraph
218 """,
219 """\
220 <document source="test data">
221 <definition_list>
222 <definition_list_item>
223 <term>
224 term 1
225 <definition>
226 <paragraph>
227 definition 1
228 <definition_list>
229 <definition_list_item>
230 <term>
231 term 1a
232 <definition>
233 <paragraph>
234 definition 1a
235 <definition_list_item>
236 <term>
237 term 1b
238 <definition>
239 <paragraph>
240 definition 1b
241 <definition_list_item>
242 <term>
243 term 2
244 <definition>
245 <paragraph>
246 definition 2
247 <paragraph>
248 paragraph
249 """],
250 ["""\
251 Term : classifier
252 The ' : ' indicates a classifier in
253 definition list item terms only.
254 """,
255 """\
256 <document source="test data">
257 <definition_list>
258 <definition_list_item>
259 <term>
260 Term
261 <classifier>
262 classifier
263 <definition>
264 <paragraph>
265 The ' : ' indicates a classifier in
266 definition list item terms only.
267 """],
268 ["""\
269 Term: not a classifier
270 Because there's no space before the colon.
271 Term :not a classifier
272 Because there's no space after the colon.
273 Term \\: not a classifier
274 Because the colon is escaped.
275 """,
276 """\
277 <document source="test data">
278 <definition_list>
279 <definition_list_item>
280 <term>
281 Term: not a classifier
282 <definition>
283 <paragraph>
284 Because there's no space before the colon.
285 <definition_list_item>
286 <term>
287 Term :not a classifier
288 <definition>
289 <paragraph>
290 Because there's no space after the colon.
291 <definition_list_item>
292 <term>
293 Term : not a classifier
294 <definition>
295 <paragraph>
296 Because the colon is escaped.
297 """],
298 ["""\
299 ``Term : not a classifier``
300 Because the ' : ' is inside an inline literal.
301 """,
302 """\
303 <document source="test data">
304 <definition_list>
305 <definition_list_item>
306 <term>
307 <literal>
308 Term : not a classifier
309 <definition>
310 <paragraph>
311 Because the ' : ' is inside an inline literal.
312 """],
313 ["""\
314 Term `with *inline ``text **errors : classifier `with *errors ``too
315 Definition `with *inline ``text **markup errors.
316 """,
317 """\
318 <document source="test data">
319 <definition_list>
320 <definition_list_item>
321 <term>
322 Term \n\
323 <problematic ids="problematic-1" refid="system-message-1">
325 with \n\
326 <problematic ids="problematic-2" refid="system-message-2">
328 inline \n\
329 <problematic ids="problematic-3" refid="system-message-3">
331 text \n\
332 <problematic ids="problematic-4" refid="system-message-4">
334 errors
335 <classifier>
336 classifier \n\
337 <problematic ids="problematic-5" refid="system-message-5">
339 with \n\
340 <problematic ids="problematic-6" refid="system-message-6">
342 errors \n\
343 <problematic ids="problematic-7" refid="system-message-7">
346 <definition>
347 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING">
348 <paragraph>
349 Inline interpreted text or phrase reference start-string without end-string.
350 <system_message backrefs="problematic-2" ids="system-message-2" level="2" line="1" source="test data" type="WARNING">
351 <paragraph>
352 Inline emphasis start-string without end-string.
353 <system_message backrefs="problematic-3" ids="system-message-3" level="2" line="1" source="test data" type="WARNING">
354 <paragraph>
355 Inline literal start-string without end-string.
356 <system_message backrefs="problematic-4" ids="system-message-4" level="2" line="1" source="test data" type="WARNING">
357 <paragraph>
358 Inline strong start-string without end-string.
359 <system_message backrefs="problematic-5" ids="system-message-5" level="2" line="1" source="test data" type="WARNING">
360 <paragraph>
361 Inline interpreted text or phrase reference start-string without end-string.
362 <system_message backrefs="problematic-6" ids="system-message-6" level="2" line="1" source="test data" type="WARNING">
363 <paragraph>
364 Inline emphasis start-string without end-string.
365 <system_message backrefs="problematic-7" ids="system-message-7" level="2" line="1" source="test data" type="WARNING">
366 <paragraph>
367 Inline literal start-string without end-string.
368 <paragraph>
369 Definition \n\
370 <problematic ids="problematic-8" refid="system-message-8">
372 with \n\
373 <problematic ids="problematic-9" refid="system-message-9">
375 inline \n\
376 <problematic ids="problematic-10" refid="system-message-10">
378 text \n\
379 <problematic ids="problematic-11" refid="system-message-11">
381 markup errors.
382 <system_message backrefs="problematic-8" ids="system-message-8" level="2" line="2" source="test data" type="WARNING">
383 <paragraph>
384 Inline interpreted text or phrase reference start-string without end-string.
385 <system_message backrefs="problematic-9" ids="system-message-9" level="2" line="2" source="test data" type="WARNING">
386 <paragraph>
387 Inline emphasis start-string without end-string.
388 <system_message backrefs="problematic-10" ids="system-message-10" level="2" line="2" source="test data" type="WARNING">
389 <paragraph>
390 Inline literal start-string without end-string.
391 <system_message backrefs="problematic-11" ids="system-message-11" level="2" line="2" source="test data" type="WARNING">
392 <paragraph>
393 Inline strong start-string without end-string.
394 """],
395 ["""\
396 Term : `reference`_
397 classifier starting with a reference crashes from release 8197 to ...
398 """,
399 """\
400 <document source="test data">
401 <definition_list>
402 <definition_list_item>
403 <term>
404 Term
405 <classifier>
406 <reference name="reference" refname="reference">
407 reference
408 <definition>
409 <paragraph>
410 classifier starting with a reference crashes from release 8197 to ...
411 """],
412 ["""\
413 Term : a `reference`_ in text : second
414 classifier with reference crashes from release 8197 to ...
415 """,
416 """\
417 <document source="test data">
418 <definition_list>
419 <definition_list_item>
420 <term>
421 Term
422 <classifier>
423 a \n\
424 <reference name="reference" refname="reference">
425 reference
426 in text
427 <classifier>
428 second
429 <definition>
430 <paragraph>
431 classifier with reference crashes from release 8197 to ...
432 """],
433 ["""\
434 Term : classifier one : classifier two
435 Definition
436 """,
437 """\
438 <document source="test data">
439 <definition_list>
440 <definition_list_item>
441 <term>
442 Term
443 <classifier>
444 classifier one
445 <classifier>
446 classifier two
447 <definition>
448 <paragraph>
449 Definition
450 """],
453 if __name__ == '__main__':
454 unittest.main()