Add <target> to one more testcase (see r8206).
[docutils.git] / docutils / test / test_transforms / test_substitutions.py
bloba67c9bad4ad2e8711933c7da14d46b0821c0cb44
1 #! /usr/bin/env python
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 __init__ import DocutilsTestSupport
12 from docutils.transforms.references import Substitutions
13 from docutils.parsers.rst import Parser
16 def suite():
17 parser = Parser()
18 s = DocutilsTestSupport.TransformTestSuite(parser)
19 s.generateTests(totest)
20 return s
22 totest = {}
24 totest['substitutions'] = ((Substitutions,), [
25 ["""\
26 The |biohazard| symbol is deservedly scary-looking.
28 .. |biohazard| image:: biohazard.png
29 """,
30 """\
31 <document source="test data">
32 <paragraph>
33 The \n\
34 <image alt="biohazard" uri="biohazard.png">
35 symbol is deservedly scary-looking.
36 <substitution_definition names="biohazard">
37 <image alt="biohazard" uri="biohazard.png">
38 """],
39 ["""\
40 Here's an |unknown| substitution.
41 """,
42 """\
43 <document source="test data">
44 <paragraph>
45 Here's an \n\
46 <problematic ids="id2" refid="id1">
47 |unknown|
48 substitution.
49 <system_message backrefs="id2" ids="id1" level="3" line="1" source="test data" type="ERROR">
50 <paragraph>
51 Undefined substitution referenced: "unknown".
52 """],
53 [u"""\
54 Substitutions support case differences:
56 .. |eacute| replace:: \u00E9
57 .. |Eacute| replace:: \u00C9
59 |Eacute|\\t\\ |eacute|, and even |EACUTE|.
60 """,
61 u"""\
62 <document source="test data">
63 <paragraph>
64 Substitutions support case differences:
65 <substitution_definition names="eacute">
66 \u00E9
67 <substitution_definition names="Eacute">
68 \u00C9
69 <paragraph>
70 \u00C9
72 \u00E9
73 , and even \n\
74 \u00C9
76 """],
77 [u"""\
78 Indirect substitution definitions with multiple references:
80 |substitute| my coke for gin
81 |substitute| you for my mum
82 at least I'll get my washing done
84 .. |substitute| replace:: |replace|
85 .. |replace| replace:: swap
86 """,
87 u"""\
88 <document source="test data">
89 <paragraph>
90 Indirect substitution definitions with multiple references:
91 <paragraph>
92 swap
93 my coke for gin
94 swap
95 you for my mum
96 at least I'll get my washing done
97 <substitution_definition names="substitute">
98 swap
99 <substitution_definition names="replace">
100 swap
101 """],
102 ["""\
103 .. |l| unicode:: U+00AB .. left chevron
104 .. |r| unicode:: U+00BB .. right chevron
105 .. |.| replace:: |l|\ ``.``\ |r|
107 .. Delete either of the following lines, and there is no error.
109 Regular expression |.| will match any character
111 .. Note:: Note that |.| matches *exactly* one character
112 """,
113 u"""\
114 <document source="test data">
115 <substitution_definition names="l">
116 \xab
117 <substitution_definition names="r">
118 \xbb
119 <substitution_definition names=".">
120 \xab
121 <literal>
123 \xbb
124 <comment xml:space="preserve">
125 Delete either of the following lines, and there is no error.
126 <paragraph>
127 Regular expression \n\
128 \xab
129 <literal>
131 \xbb
132 will match any character
133 <note>
134 <paragraph>
135 Note that \n\
136 \xab
137 <literal>
139 \xbb
140 matches \n\
141 <emphasis>
142 exactly
143 one character
144 """],
145 ["""\
146 .. |sub| replace:: |sub|
147 """,
148 """\
149 <document source="test data">
150 <system_message level="3" line="1" names="sub" source="test data" type="ERROR">
151 <paragraph>
152 Circular substitution definition detected:
153 <literal_block xml:space="preserve">
154 .. |sub| replace:: |sub|
155 """],
156 ["""\
157 .. |sub| replace:: |indirect1|
158 .. |indirect1| replace:: |indirect2|
159 .. |indirect2| replace:: |Sub|
160 """,
161 """\
162 <document source="test data">
163 <system_message level="3" line="1" names="sub" source="test data" type="ERROR">
164 <paragraph>
165 Circular substitution definition detected:
166 <literal_block xml:space="preserve">
167 .. |sub| replace:: |indirect1|
168 <system_message level="3" line="2" names="indirect1" source="test data" type="ERROR">
169 <paragraph>
170 Circular substitution definition detected:
171 <literal_block xml:space="preserve">
172 .. |indirect1| replace:: |indirect2|
173 <system_message level="3" line="3" names="indirect2" source="test data" type="ERROR">
174 <paragraph>
175 Circular substitution definition detected:
176 <literal_block xml:space="preserve">
177 .. |indirect2| replace:: |Sub|
178 """],
179 ["""\
180 .. |indirect1| replace:: |indirect2|
181 .. |indirect2| replace:: |Sub|
182 .. |sub| replace:: |indirect1|
184 Use |sub| and |indirect1| and |sub| again (and |sub| one more time).
185 """,
186 """\
187 <document source="test data">
188 <system_message level="3" line="1" names="indirect1" source="test data" type="ERROR">
189 <paragraph>
190 Circular substitution definition detected:
191 <literal_block xml:space="preserve">
192 .. |indirect1| replace:: |indirect2|
193 <system_message level="3" line="2" names="indirect2" source="test data" type="ERROR">
194 <paragraph>
195 Circular substitution definition detected:
196 <literal_block xml:space="preserve">
197 .. |indirect2| replace:: |Sub|
198 <system_message level="3" line="3" names="sub" source="test data" type="ERROR">
199 <paragraph>
200 Circular substitution definition detected:
201 <literal_block xml:space="preserve">
202 .. |sub| replace:: |indirect1|
203 <paragraph>
204 Use \n\
205 <problematic ids="id8" refid="id7">
206 |Sub|
207 and \n\
208 <problematic ids="id2" refid="id1">
209 |indirect1|
210 and \n\
211 <problematic ids="id4" refid="id3">
212 |sub|
213 again (and \n\
214 <problematic ids="id6" refid="id5">
215 |sub|
216 one more time).
217 <system_message backrefs="id2" ids="id1" level="3" line="5" source="test data" type="ERROR">
218 <paragraph>
219 Circular substitution definition referenced: "indirect1".
220 <system_message backrefs="id4" ids="id3" level="3" line="5" source="test data" type="ERROR">
221 <paragraph>
222 Circular substitution definition referenced: "sub".
223 <system_message backrefs="id6" ids="id5" level="3" line="5" source="test data" type="ERROR">
224 <paragraph>
225 Circular substitution definition referenced: "sub".
226 <system_message backrefs="id8" ids="id7" level="3" line="5" source="test data" type="ERROR">
227 <paragraph>
228 Circular substitution definition referenced: "Sub".
229 """],
232 totest['unicode'] = ((Substitutions,), [
233 ["""\
234 Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking
235 space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|).
237 .. |mdash| unicode:: 0x02014
238 .. |copy| unicode:: \\u00A9
239 .. |nbsp| unicode:: &#x000A0;
240 .. |bne| unicode:: U0003D U020E5
241 .. |Omega| unicode:: U+003A9
242 """,
243 u"""\
244 <document source="test data">
245 <paragraph>
246 Insert an em-dash (
247 \u2014
248 ), a copyright symbol (
249 \u00a9
250 ), a non-breaking
251 space (
252 \u00a0
253 ), a backwards-not-equals (
255 \u20e5
256 ), and a captial omega (
257 \u03a9
259 <substitution_definition names="mdash">
260 \u2014
261 <substitution_definition names="copy">
262 \u00a9
263 <substitution_definition names="nbsp">
264 \u00a0
265 <substitution_definition names="bne">
267 \u20e5
268 <substitution_definition names="Omega">
269 \u03a9
270 """],
271 ["""
272 Testing comments and extra text.
274 Copyright |copy| 2003, |BogusMegaCorp (TM)|.
276 .. |copy| unicode:: 0xA9 .. copyright sign
277 .. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122
278 .. with trademark sign
279 """,
280 u"""\
281 <document source="test data">
282 <paragraph>
283 Testing comments and extra text.
284 <paragraph>
285 Copyright \n\
286 \u00a9
287 2003, \n\
288 BogusMegaCorp
289 \u2122
291 <substitution_definition names="copy">
292 \u00a9
293 <substitution_definition names="BogusMegaCorp\ (TM)">
294 BogusMegaCorp
295 \u2122
296 """],
297 ["""\
298 Insert an em-dash |---| automatically trimming whitespace.
299 Some substitutions |TM| only need |rarrow| trimming on one side.
301 .. |---| unicode:: U+02014
302 :trim:
303 .. |TM| unicode:: U+02122
304 :ltrim:
305 .. |rarrow| unicode:: U+2192
306 :rtrim:
307 """,
308 u"""\
309 <document source="test data">
310 <paragraph>
311 Insert an em-dash
312 \u2014
313 automatically trimming whitespace.
314 Some substitutions
315 \u2122
316 only need \n\
317 \u2192
318 trimming on one side.
319 <substitution_definition ltrim="1" names="---" rtrim="1">
320 \u2014
321 <substitution_definition ltrim="1" names="TM">
322 \u2122
323 <substitution_definition names="rarrow" rtrim="1">
324 \u2192
325 """],
326 ["""\
327 Substitution definition with an illegal element:
329 .. |target| replace:: _`target`
331 Make sure this substitution definition is not registered: |target|
332 """,
333 """\
334 <document source="test data">
335 <paragraph>
336 Substitution definition with an illegal element:
337 <system_message level="3" line="3" source="test data" type="ERROR">
338 <paragraph>
339 Substitution definition contains illegal element <target>:
340 <literal_block xml:space="preserve">
341 <target ids="target" names="target">
342 target
343 <literal_block xml:space="preserve">
344 .. |target| replace:: _`target`
345 <paragraph>
346 Make sure this substitution definition is not registered: \n\
347 <problematic ids="id2" refid="id1">
348 |target|
349 <system_message backrefs="id2" ids="id1" level="3" line="5" source="test data" type="ERROR">
350 <paragraph>
351 Undefined substitution referenced: "target".
352 """],
356 if __name__ == '__main__':
357 import unittest
358 unittest.main(defaultTest='suite')