Fix hashbang line in "smartquotes" script.
[docutils.git] / docutils / test / test_parsers / test_rst / test_interpreted.py
blob0a301edf504b19733325422b3bafe012d9b0b859
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 interpreted text in docutils/parsers/rst/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
23 from docutils.utils.code_analyzer import with_pygments
26 class ParserTestCase(unittest.TestCase):
27 def test_parser(self):
28 if not with_pygments:
29 del totest['code_parsing']
31 parser = Parser()
32 settings = get_default_settings(Parser)
33 settings.warning_stream = ''
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)
43 totest = {}
45 totest['basics'] = [
46 ["""\
47 `interpreted`
48 """,
49 """\
50 <document source="test data">
51 <paragraph>
52 <title_reference>
53 interpreted
54 """],
55 ["""\
56 :title:`interpreted`
57 """,
58 """\
59 <document source="test data">
60 <paragraph>
61 <title_reference>
62 interpreted
63 """],
64 ["""\
65 `interpreted`:title:
66 """,
67 """\
68 <document source="test data">
69 <paragraph>
70 <title_reference>
71 interpreted
72 """],
73 ["""\
74 `interpreted \\`title``
75 """,
76 """\
77 <document source="test data">
78 <paragraph>
79 <title_reference>
80 interpreted `title`
81 """],
82 ["""\
83 :title:`:not-role: interpreted`
84 """,
85 """\
86 <document source="test data">
87 <paragraph>
88 <title_reference>
89 :not-role: interpreted
90 """],
91 ["""\
92 `interpreted` but not \\`interpreted` [`] or ({[`] or [`]}) or `
93 """,
94 """\
95 <document source="test data">
96 <paragraph>
97 <title_reference>
98 interpreted
99 but not `interpreted` [`] or ({[`] or [`]}) or `
100 """],
101 ["""\
102 `interpreted`-text `interpreted`: text `interpreted`:text `text`'s interpreted
103 """,
104 """\
105 <document source="test data">
106 <paragraph>
107 <title_reference>
108 interpreted
109 -text \n\
110 <title_reference>
111 interpreted
112 : text \n\
113 <title_reference>
114 interpreted
115 :text \n\
116 <title_reference>
117 text
118 's interpreted
119 """],
120 ["""\
121 `interpreted without closing backquote
122 """,
123 """\
124 <document source="test data">
125 <paragraph>
126 <problematic ids="problematic-1" refid="system-message-1">
128 interpreted without closing backquote
129 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING">
130 <paragraph>
131 Inline interpreted text or phrase reference start-string without end-string.
132 """],
133 ["""\
134 `interpreted`:not a role if it contains whitespace:
135 """,
136 """\
137 <document source="test data">
138 <paragraph>
139 <title_reference>
140 interpreted
141 :not a role if it contains whitespace:
142 """],
143 ["""\
144 :title:`` (empty interpreted text not recognized)
145 """,
146 """\
147 <document source="test data">
148 <paragraph>
149 :title:`` (empty interpreted text not recognized)
150 """],
151 ["""\
152 :title:`\\ ` (interpreted text containing empty string)
153 """,
154 """\
155 <document source="test data">
156 <paragraph>
157 <title_reference>
158 (interpreted text containing empty string)
159 """],
160 ["""\
161 `\\ `:title: (interpreted text containing empty string (postfix))
162 """,
163 """\
164 <document source="test data">
165 <paragraph>
166 <title_reference>
167 (interpreted text containing empty string (postfix))
168 """],
169 ["""\
170 :title:`\\ non-empty`
171 """,
172 """\
173 <document source="test data">
174 <paragraph>
175 <title_reference>
176 non-empty
177 """],
178 ["""\
179 :title:`\\ ` (trailing unquoted space)
180 """,
181 """\
182 <document source="test data">
183 <paragraph>
184 :title:
185 <problematic ids="problematic-1" refid="system-message-1">
187 ` (trailing unquoted space)
188 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING">
189 <paragraph>
190 Inline interpreted text or phrase reference start-string without end-string.
191 """],
192 ["""\
193 Explicit roles for standard inline markup:
194 :emphasis:`emphasis`,
195 :strong:`strong`,
196 :literal:`inline literal text`.
197 """,
198 """\
199 <document source="test data">
200 <paragraph>
201 Explicit roles for standard inline markup:
202 <emphasis>
203 emphasis
205 <strong>
206 strong
208 <literal>
209 inline literal text
211 """],
212 ["""\
213 Simple explicit roles:
214 :ab:`abbreviation`,
215 :ac:`acronym`,
216 :sup:`superscript`,
217 :sub:`subscript`,
218 :title:`title reference`.
219 """,
220 """\
221 <document source="test data">
222 <paragraph>
223 Simple explicit roles:
224 <abbreviation>
225 abbreviation
227 <acronym>
228 acronym
230 <superscript>
231 superscript
233 <subscript>
234 subscript
236 <title_reference>
237 title reference
239 """],
242 totest['code'] = [
243 ["""\
244 Code role for inline code snippets:
245 :code:`$\alpha = \\int_0^\\infty f(x) dx$`.
246 """,
247 """\
248 <document source="test data">
249 <paragraph>
250 Code role for inline code snippets:
251 <literal classes="code">
252 $\x07lpha = \\int_0^\\infty f(x) dx$
254 """],
257 totest['code_parsing'] = [
258 ["""\
259 .. role:: tex(code)
260 :language: latex
262 Custom role based on code role:
263 :tex:`$\alpha = f(x)$`.
264 """,
265 """\
266 <document source="test data">
267 <paragraph>
268 Custom role based on code role:
269 <literal classes="code tex latex">
270 <inline classes="literal string">
272 <inline classes="name builtin">
273 \x07lpha \n\
274 <inline classes="operator">
276 <inline classes="name builtin">
278 <inline classes="operator">
280 <inline classes="name builtin">
282 <inline classes="operator">
284 <inline classes="literal string">
287 """],
288 ["""\
289 Custom role based on code role:
291 .. role:: python(code)
292 :language: python3
293 :class: testclass
295 Python code :python:`print("The end")`.
296 """,
297 """\
298 <document source="test data">
299 <paragraph>
300 Custom role based on code role:
301 <paragraph>
302 Python code \n\
303 <literal classes="code testclass python3">
304 <inline classes="name builtin">
305 print
306 <inline classes="punctuation">
308 <inline classes="literal string double">
309 "The end"
310 <inline classes="punctuation">
313 """],
316 totest['references'] = [
317 ["""\
318 :PEP:`0`
319 """,
320 """\
321 <document source="test data">
322 <paragraph>
323 <reference refuri="https://peps.python.org/pep-0000">
324 PEP 0
325 """],
326 ["""\
327 :PEP:`-1`
328 """,
329 """\
330 <document source="test data">
331 <paragraph>
332 <problematic ids="problematic-1" refid="system-message-1">
333 :PEP:`-1`
334 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
335 <paragraph>
336 PEP number must be a number from 0 to 9999; "-1" is invalid.
337 """],
338 ["""\
339 :RFC:`2822`
340 """,
341 """\
342 <document source="test data">
343 <paragraph>
344 <reference refuri="https://tools.ietf.org/html/rfc2822.html">
345 RFC 2822
346 """],
347 ["""\
348 :RFC:`0`
349 """,
350 """\
351 <document source="test data">
352 <paragraph>
353 <problematic ids="problematic-1" refid="system-message-1">
354 :RFC:`0`
355 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
356 <paragraph>
357 RFC number must be a number greater than or equal to 1; "0" is invalid.
358 """],
359 ["""\
360 :RFC:`2822#section1`
361 """,
362 """\
363 <document source="test data">
364 <paragraph>
365 <reference refuri="https://tools.ietf.org/html/rfc2822.html#section1">
366 RFC 2822
367 """],
370 totest['unknown_roles'] = [
371 ["""\
372 :role:`interpreted`
373 """,
374 """\
375 <document source="test data">
376 <paragraph>
377 <problematic ids="problematic-1" refid="system-message-1">
378 :role:`interpreted`
379 <system_message level="1" line="1" source="test data" type="INFO">
380 <paragraph>
381 No role entry for "role" in module "docutils.parsers.rst.languages.en".
382 Trying "role" as canonical role name.
383 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
384 <paragraph>
385 Unknown interpreted text role "role".
386 """],
387 ["""\
388 `interpreted`:role:
389 """,
390 """\
391 <document source="test data">
392 <paragraph>
393 <problematic ids="problematic-1" refid="system-message-1">
394 `interpreted`:role:
395 <system_message level="1" line="1" source="test data" type="INFO">
396 <paragraph>
397 No role entry for "role" in module "docutils.parsers.rst.languages.en".
398 Trying "role" as canonical role name.
399 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
400 <paragraph>
401 Unknown interpreted text role "role".
402 """],
403 ["""\
404 :role:`interpreted`:role:
405 """,
406 """\
407 <document source="test data">
408 <paragraph>
409 <problematic ids="problematic-1" refid="system-message-1">
410 :role:`interpreted`:role:
411 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING">
412 <paragraph>
413 Multiple roles in interpreted text (both prefix and suffix present; only one allowed).
414 """],
415 ["""\
416 :very.long-role_name:`interpreted`
417 """,
418 """\
419 <document source="test data">
420 <paragraph>
421 <problematic ids="problematic-1" refid="system-message-1">
422 :very.long-role_name:`interpreted`
423 <system_message level="1" line="1" source="test data" type="INFO">
424 <paragraph>
425 No role entry for "very.long-role_name" in module "docutils.parsers.rst.languages.en".
426 Trying "very.long-role_name" as canonical role name.
427 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
428 <paragraph>
429 Unknown interpreted text role "very.long-role_name".
430 """],
431 ["""\
432 :restructuredtext-unimplemented-role:`interpreted`
433 """,
434 """\
435 <document source="test data">
436 <paragraph>
437 <problematic ids="problematic-1" refid="system-message-1">
438 :restructuredtext-unimplemented-role:`interpreted`
439 <system_message level="1" line="1" source="test data" type="INFO">
440 <paragraph>
441 No role entry for "restructuredtext-unimplemented-role" in module "docutils.parsers.rst.languages.en".
442 Trying "restructuredtext-unimplemented-role" as canonical role name.
443 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
444 <paragraph>
445 Interpreted text role "restructuredtext-unimplemented-role" not implemented.
446 """],
450 if __name__ == '__main__':
451 unittest.main()