Debian Codegraph @

test/test_parsers/test_rst/test_substitutions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_substitutions.py 10254 2025-11-02 17:50:57Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for states.py.
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[3]))
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 maxDiff = None
27
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.warning_stream = ''
32 for name, cases in totest.items():
33 for casenum, (case_input, case_expected) in enumerate(cases):
34 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
35 document = new_document('test data', settings.copy())
36 parser.parse(case_input, document)
37 output = document.pformat()
38 self.assertEqual(case_expected, output)
39
40
41 totest = {}
42
43 totest['substitution_definitions'] = [
44 ["""\
45 Here's an image substitution definition:
46
47 .. |symbol| image:: symbol.png
48 """,
49 """\
50 <document source="test data">
51 <paragraph>
52 Here's an image substitution definition:
53 <substitution_definition names="symbol">
54 <image alt="symbol" uri="symbol.png">
55 """],
56 ["""\
57 Embedded directive starts on the next line:
58
59 .. |symbol|
60 image:: symbol.png
61 """,
62 """\
63 <document source="test data">
64 <paragraph>
65 Embedded directive starts on the next line:
66 <substitution_definition names="symbol">
67 <image alt="symbol" uri="symbol.png">
68 """],
69 ["""\
70 Trailing spaces should not be significant:
71
72 .. |symbol| image:: \n\
73 symbol.png
74 """,
75 """\
76 <document source="test data">
77 <paragraph>
78 Trailing spaces should not be significant:
79 <substitution_definition names="symbol">
80 <image alt="symbol" uri="symbol.png">
81 """],
82 ["""\
83 Here's a series of substitution definitions:
84
85 .. |symbol 1| image:: symbol1.png
86 .. |SYMBOL 2| image:: symbol2.png
87 :height: 50
88 :width: 100
89 .. |symbol 3| image:: symbol3.png
90 """,
91 """\
92 <document source="test data">
93 <paragraph>
94 Here's a series of substitution definitions:
95 <substitution_definition names="symbol\\ 1">
96 <image alt="symbol 1" uri="symbol1.png">
97 <substitution_definition names="SYMBOL\\ 2">
98 <image alt="SYMBOL 2" height="50" uri="symbol2.png" width="100">
99 <substitution_definition names="symbol\\ 3">
100 <image alt="symbol 3" uri="symbol3.png">
101 """],
102 ["""\
103 .. |very long substitution text,
104 split across lines| image:: symbol.png
105 """,
106 """\
107 <document source="test data">
108 <substitution_definition names="very\\ long\\ substitution\\ text,\\ split\\ across\\ lines">
109 <image alt="very long substitution text, split across lines" uri="symbol.png">
110 """],
111 ["""\
112 .. |symbol 1| image:: symbol.png
113
114 Followed by a block quote.
115 """,
116 """\
117 <document source="test data">
118 <system_message level="3" line="1" source="test data" type="ERROR">
119 <paragraph>
120 Error in "image" directive:
121 no content permitted.
122 <literal_block xml:space="preserve">
123 image:: symbol.png
124 \n\
125 Followed by a block quote.
126 <system_message level="2" line="1" source="test data" type="WARNING">
127 <paragraph>
128 Substitution definition "symbol 1" empty or invalid.
129 <literal_block xml:space="preserve">
130 .. |symbol 1| image:: symbol.png
131 \n\
132 Followed by a block quote.
133 """],
134 ["""\
135 .. |symbol 1| image:: symbol.png
136
137 Followed by a paragraph.
138
139 .. |symbol 2| image:: symbol.png
140
141 ..
142
143 Followed by a block quote.
144 """,
145 """\
146 <document source="test data">
147 <substitution_definition names="symbol\\ 1">
148 <image alt="symbol 1" uri="symbol.png">
149 <paragraph>
150 Followed by a paragraph.
151 <substitution_definition names="symbol\\ 2">
152 <image alt="symbol 2" uri="symbol.png">
153 <comment xml:space="preserve">
154 <block_quote>
155 <paragraph>
156 Followed by a block quote.
157 """],
158 ["""\
159 Substitutions support case differences:
160
161 .. |eacute| replace:: \u00E9
162 .. |Eacute| replace:: \u00C9
163 """,
164 """\
165 <document source="test data">
166 <paragraph>
167 Substitutions support case differences:
168 <substitution_definition names="eacute">
169 \u00E9
170 <substitution_definition names="Eacute">
171 \u00C9
172 """],
173 ["""\
174 Raw substitution, backslashes should be preserved:
175
176 .. |alpha| raw:: latex
177
178 $\\\\alpha$
179 """,
180 """\
181 <document source="test data">
182 <paragraph>
183 Raw substitution, backslashes should be preserved:
184 <substitution_definition names="alpha">
185 <raw format="latex" xml:space="preserve">
186 $\\\\alpha$
187 """],
188 ["""\
189 Here are some duplicate substitution definitions:
190
191 .. |symbol| image:: symbol.png
192 .. |symbol| image:: symbol.png
193 """,
194 """\
195 <document source="test data">
196 <paragraph>
197 Here are some duplicate substitution definitions:
198 <substitution_definition dupnames="symbol">
199 <image alt="symbol" uri="symbol.png">
200 <system_message level="3" line="4" source="test data" type="ERROR">
201 <paragraph>
202 Duplicate substitution definition name: "symbol".
203 <substitution_definition names="symbol">
204 <image alt="symbol" uri="symbol.png">
205 """],
206 ["""\
207 Here are some bad cases:
208
209 .. |symbol| image:: symbol.png
210 No blank line after.
211
212 .. |empty|
213
214 .. |unknown| directive:: symbol.png
215
216 .. |invalid 1| there's no directive here
217 .. |invalid 2| there's no directive here
218 With some block quote text, line 1.
219 And some more, line 2.
220
221 .. |invalid 3| there's no directive here
222
223 .. | bad name | bad data
224
225 .. |
226 """,
227 """\
228 <document source="test data">
229 <paragraph>
230 Here are some bad cases:
231 <substitution_definition names="symbol">
232 <image alt="symbol" uri="symbol.png">
233 <system_message level="2" line="4" source="test data" type="WARNING">
234 <paragraph>
235 Explicit markup ends without a blank line; unexpected unindent.
236 <paragraph>
237 No blank line after.
238 <system_message level="2" line="6" source="test data" type="WARNING">
239 <paragraph>
240 Substitution definition "empty" missing contents.
241 <literal_block xml:space="preserve">
242 .. |empty|
243 <system_message level="1" line="8" source="test data" type="INFO">
244 <paragraph>
245 No directive entry for "directive" in module "docutils.parsers.rst.languages.en".
246 Trying "directive" as canonical directive name.
247 <system_message level="3" line="8" source="test data" type="ERROR">
248 <paragraph>
249 Unknown directive type "directive".
250 <literal_block xml:space="preserve">
251 directive:: symbol.png
252 <system_message level="2" line="8" source="test data" type="WARNING">
253 <paragraph>
254 Substitution definition "unknown" empty or invalid.
255 <literal_block xml:space="preserve">
256 .. |unknown| directive:: symbol.png
257 <system_message level="2" line="10" source="test data" type="WARNING">
258 <paragraph>
259 Substitution definition "invalid 1" empty or invalid.
260 <literal_block xml:space="preserve">
261 .. |invalid 1| there's no directive here
262 <system_message level="2" line="11" source="test data" type="WARNING">
263 <paragraph>
264 Substitution definition "invalid 2" empty or invalid.
265 <literal_block xml:space="preserve">
266 .. |invalid 2| there's no directive here
267 With some block quote text, line 1.
268 And some more, line 2.
269 <system_message level="2" line="15" source="test data" type="WARNING">
270 <paragraph>
271 Substitution definition "invalid 3" empty or invalid.
272 <literal_block xml:space="preserve">
273 .. |invalid 3| there's no directive here
274 <comment xml:space="preserve">
275 | bad name | bad data
276 <comment xml:space="preserve">
277 |
278 """],
279 ["""\
280 Elements that are prohibited inside of substitution definitions:
281
282 .. |target| replace:: _`target`
283 .. |reference| replace:: anonymous__
284 .. |auto-numbered footnote| replace:: [#]_
285 """,
286 """\
287 <document source="test data">
288 <paragraph>
289 Elements that are prohibited inside of substitution definitions:
290 <system_message level="3" line="3" source="test data" type="ERROR">
291 <paragraph>
292 Targets (names and identifiers) are not supported in a substitution definition.
293 <literal_block xml:space="preserve">
294 .. |target| replace:: _`target`
295 <system_message level="3" line="4" source="test data" type="ERROR">
296 <paragraph>
297 Anonymous references are not supported in a substitution definition.
298 <literal_block xml:space="preserve">
299 .. |reference| replace:: anonymous__
300 <system_message level="3" line="5" source="test data" type="ERROR">
301 <paragraph>
302 References to auto-numbered and auto-symbol footnotes are not supported in a substitution definition.
303 <literal_block xml:space="preserve">
304 .. |auto-numbered footnote| replace:: [#]_
305 """],
306 ]
307
308
309 if __name__ == '__main__':
310 unittest.main()