manpage writer: Remove code for unused emdash bullets.
[docutils.git] / docutils / test / test_transforms / test_substitutions.py
blobb3f7d7384bbfdadb3f535fbd3f994fa65f35f224
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 docutils.transforms.references.Substitutions.
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[2]))
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.transforms.references import Substitutions
23 from docutils.transforms.universal import TestMessages
24 from docutils.utils import new_document
27 class TransformTestCase(unittest.TestCase):
28 def test_transforms(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.warning_stream = ''
32 for name, (transforms, 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 # Don't do a ``populate_from_components()`` because that
38 # would enable the Transformer's default transforms.
39 document.transformer.add_transforms(transforms)
40 document.transformer.add_transform(TestMessages)
41 document.transformer.apply_transforms()
42 output = document.pformat()
43 self.assertEqual(case_expected, output)
46 totest = {}
48 totest['substitutions'] = ((Substitutions,), [
49 ["""\
50 The |biohazard| symbol is deservedly scary-looking.
52 .. |biohazard| image:: biohazard.png
53 """,
54 """\
55 <document source="test data">
56 <paragraph>
57 The \n\
58 <image alt="biohazard" uri="biohazard.png">
59 symbol is deservedly scary-looking.
60 <substitution_definition names="biohazard">
61 <image alt="biohazard" uri="biohazard.png">
62 """],
63 ["""\
64 Here's an |unknown| substitution.
65 """,
66 """\
67 <document source="test data">
68 <paragraph>
69 Here's an \n\
70 <problematic ids="problematic-1" refid="system-message-1">
71 |unknown|
72 substitution.
73 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
74 <paragraph>
75 Undefined substitution referenced: "unknown".
76 """],
77 ["""\
78 Substitutions support case differences:
80 .. |eacute| replace:: \u00E9
81 .. |Eacute| replace:: \u00C9
83 |Eacute|\\t\\ |eacute|, and even |EACUTE|.
84 """,
85 """\
86 <document source="test data">
87 <paragraph>
88 Substitutions support case differences:
89 <substitution_definition names="eacute">
90 \u00E9
91 <substitution_definition names="Eacute">
92 \u00C9
93 <paragraph>
94 \u00C9
96 \u00E9
97 , and even \n\
98 \u00C9
100 """],
101 ["""\
102 Indirect substitution definitions with multiple references:
104 |substitute| my coke for gin
105 |substitute| you for my mum
106 at least I'll get my washing done
108 .. |substitute| replace:: |replace|
109 .. |replace| replace:: swap
110 """,
111 """\
112 <document source="test data">
113 <paragraph>
114 Indirect substitution definitions with multiple references:
115 <paragraph>
116 swap
117 my coke for gin
118 swap
119 you for my mum
120 at least I'll get my washing done
121 <substitution_definition names="substitute">
122 swap
123 <substitution_definition names="replace">
124 swap
125 """],
126 ["""\
127 .. |l| unicode:: U+00AB .. left chevron
128 .. |r| unicode:: U+00BB .. right chevron
129 .. |.| replace:: |l|\\ ``.``\\ |r|
131 .. Delete either of the following lines, and there is no error.
133 Regular expression |.| will match any character
135 .. Note:: Note that |.| matches *exactly* one character
136 """,
137 """\
138 <document source="test data">
139 <substitution_definition names="l">
140 \xab
141 <substitution_definition names="r">
142 \xbb
143 <substitution_definition names=".">
144 \xab
145 <literal>
147 \xbb
148 <comment xml:space="preserve">
149 Delete either of the following lines, and there is no error.
150 <paragraph>
151 Regular expression \n\
152 \xab
153 <literal>
155 \xbb
156 will match any character
157 <note>
158 <paragraph>
159 Note that \n\
160 \xab
161 <literal>
163 \xbb
164 matches \n\
165 <emphasis>
166 exactly
167 one character
168 """],
169 ["""\
170 .. |sub| replace:: |sub|
171 """,
172 """\
173 <document source="test data">
174 <system_message level="3" line="1" names="sub" source="test data" type="ERROR">
175 <paragraph>
176 Circular substitution definition detected:
177 <literal_block xml:space="preserve">
178 .. |sub| replace:: |sub|
179 """],
180 ["""\
181 .. |sub| replace:: |indirect1|
182 .. |indirect1| replace:: |indirect2|
183 .. |indirect2| replace:: |Sub|
184 """,
185 """\
186 <document source="test data">
187 <system_message level="3" line="1" names="sub" source="test data" type="ERROR">
188 <paragraph>
189 Circular substitution definition detected:
190 <literal_block xml:space="preserve">
191 .. |sub| replace:: |indirect1|
192 <system_message level="3" line="2" names="indirect1" source="test data" type="ERROR">
193 <paragraph>
194 Circular substitution definition detected:
195 <literal_block xml:space="preserve">
196 .. |indirect1| replace:: |indirect2|
197 <system_message level="3" line="3" names="indirect2" source="test data" type="ERROR">
198 <paragraph>
199 Circular substitution definition detected:
200 <literal_block xml:space="preserve">
201 .. |indirect2| replace:: |Sub|
202 """],
203 ["""\
204 .. |indirect1| replace:: |indirect2|
205 .. |indirect2| replace:: |Sub|
206 .. |sub| replace:: |indirect1|
208 Use |sub| and |indirect1| and |sub| again (and |sub| one more time).
209 """,
210 """\
211 <document source="test data">
212 <system_message level="3" line="1" names="indirect1" source="test data" type="ERROR">
213 <paragraph>
214 Circular substitution definition detected:
215 <literal_block xml:space="preserve">
216 .. |indirect1| replace:: |indirect2|
217 <system_message level="3" line="2" names="indirect2" source="test data" type="ERROR">
218 <paragraph>
219 Circular substitution definition detected:
220 <literal_block xml:space="preserve">
221 .. |indirect2| replace:: |Sub|
222 <system_message level="3" line="3" names="sub" source="test data" type="ERROR">
223 <paragraph>
224 Circular substitution definition detected:
225 <literal_block xml:space="preserve">
226 .. |sub| replace:: |indirect1|
227 <paragraph>
228 Use \n\
229 <problematic ids="problematic-4" refid="system-message-4">
230 |Sub|
231 and \n\
232 <problematic ids="problematic-1" refid="system-message-1">
233 |indirect1|
234 and \n\
235 <problematic ids="problematic-2" refid="system-message-2">
236 |sub|
237 again (and \n\
238 <problematic ids="problematic-3" refid="system-message-3">
239 |sub|
240 one more time).
241 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="5" source="test data" type="ERROR">
242 <paragraph>
243 Circular substitution definition referenced: "indirect1".
244 <system_message backrefs="problematic-2" ids="system-message-2" level="3" line="5" source="test data" type="ERROR">
245 <paragraph>
246 Circular substitution definition referenced: "sub".
247 <system_message backrefs="problematic-3" ids="system-message-3" level="3" line="5" source="test data" type="ERROR">
248 <paragraph>
249 Circular substitution definition referenced: "sub".
250 <system_message backrefs="problematic-4" ids="system-message-4" level="3" line="5" source="test data" type="ERROR">
251 <paragraph>
252 Circular substitution definition referenced: "Sub".
253 """],
254 ["""\
255 Substitution reference with |reference-in-content|.
257 .. |reference-in-content| replace:: text and hyperlink-reference_
258 """,
259 """\
260 <document source="test data">
261 <paragraph>
262 Substitution reference with \n\
263 text and \n\
264 <reference name="hyperlink-reference" refname="hyperlink-reference">
265 hyperlink-reference
267 <substitution_definition names="reference-in-content">
268 text and \n\
269 <reference name="hyperlink-reference" refname="hyperlink-reference">
270 hyperlink-reference
271 """],
274 totest['unicode'] = ((Substitutions,), [
275 ["""\
276 Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking
277 space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|).
279 .. |mdash| unicode:: 0x02014
280 .. |copy| unicode:: \\u00A9
281 .. |nbsp| unicode:: &#x000A0;
282 .. |bne| unicode:: U0003D U020E5
283 .. |Omega| unicode:: U+003A9
284 """,
285 """\
286 <document source="test data">
287 <paragraph>
288 Insert an em-dash (
289 \u2014
290 ), a copyright symbol (
291 \u00a9
292 ), a non-breaking
293 space (
294 \u00a0
295 ), a backwards-not-equals (
297 \u20e5
298 ), and a captial omega (
299 \u03a9
301 <substitution_definition names="mdash">
302 \u2014
303 <substitution_definition names="copy">
304 \u00a9
305 <substitution_definition names="nbsp">
306 \u00a0
307 <substitution_definition names="bne">
309 \u20e5
310 <substitution_definition names="Omega">
311 \u03a9
312 """],
313 ["""
314 Testing comments and extra text.
316 Copyright |copy| 2003, |BogusMegaCorp (TM)|.
318 .. |copy| unicode:: 0xA9 .. copyright sign
319 .. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122
320 .. with trademark sign
321 """,
322 """\
323 <document source="test data">
324 <paragraph>
325 Testing comments and extra text.
326 <paragraph>
327 Copyright \n\
328 \u00a9
329 2003, \n\
330 BogusMegaCorp
331 \u2122
333 <substitution_definition names="copy">
334 \u00a9
335 <substitution_definition names="BogusMegaCorp\\ (TM)">
336 BogusMegaCorp
337 \u2122
338 """],
339 ["""\
340 Insert an em-dash |---| automatically trimming whitespace.
341 Some substitutions |TM| only need |rarrow| trimming on one side.
343 .. |---| unicode:: U+02014
344 :trim:
345 .. |TM| unicode:: U+02122
346 :ltrim:
347 .. |rarrow| unicode:: U+2192
348 :rtrim:
349 """,
350 """\
351 <document source="test data">
352 <paragraph>
353 Insert an em-dash
354 \u2014
355 automatically trimming whitespace.
356 Some substitutions
357 \u2122
358 only need \n\
359 \u2192
360 trimming on one side.
361 <substitution_definition ltrim="1" names="---" rtrim="1">
362 \u2014
363 <substitution_definition ltrim="1" names="TM">
364 \u2122
365 <substitution_definition names="rarrow" rtrim="1">
366 \u2192
367 """],
368 ["""\
369 Substitution definition with an illegal element:
371 .. |target| replace:: _`target`
373 Make sure this substitution definition is not registered: |target|
374 """,
375 """\
376 <document source="test data">
377 <paragraph>
378 Substitution definition with an illegal element:
379 <system_message level="3" line="3" source="test data" type="ERROR">
380 <paragraph>
381 Substitution definition contains illegal element <target>:
382 <literal_block xml:space="preserve">
383 <target ids="target" names="target">
384 target
385 <literal_block xml:space="preserve">
386 .. |target| replace:: _`target`
387 <paragraph>
388 Make sure this substitution definition is not registered: \n\
389 <problematic ids="problematic-1" refid="system-message-1">
390 |target|
391 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="5" source="test data" type="ERROR">
392 <paragraph>
393 Undefined substitution referenced: "target".
394 """],
398 if __name__ == '__main__':
399 unittest.main()