added warking on --strip-* options
[docutils.git] / test / test_transforms / test_substitutions.py
blob570e354b5a6e946fae3c29634468a38122f56154
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 and \n\
207 <problematic ids="id2" refid="id1">
208 |indirect1|
209 and \n\
210 <problematic ids="id4" refid="id3">
211 |sub|
212 again (and \n\
213 <problematic ids="id6" refid="id5">
214 |sub|
215 one more time).
216 <system_message backrefs="id2" ids="id1" level="3" line="5" source="test data" type="ERROR">
217 <paragraph>
218 Circular substitution definition referenced: "indirect1".
219 <system_message backrefs="id4" ids="id3" level="3" line="5" source="test data" type="ERROR">
220 <paragraph>
221 Circular substitution definition referenced: "sub".
222 <system_message backrefs="id6" ids="id5" level="3" line="5" source="test data" type="ERROR">
223 <paragraph>
224 Circular substitution definition referenced: "sub".
225 <system_message backrefs="id8" ids="id7" level="3" source="test data" type="ERROR">
226 <paragraph>
227 Circular substitution definition referenced: "Sub".
228 """],
231 totest['unicode'] = ((Substitutions,), [
232 ["""\
233 Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking
234 space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|).
236 .. |mdash| unicode:: 0x02014
237 .. |copy| unicode:: \\u00A9
238 .. |nbsp| unicode:: &#x000A0;
239 .. |bne| unicode:: U0003D U020E5
240 .. |Omega| unicode:: U+003A9
241 """,
242 u"""\
243 <document source="test data">
244 <paragraph>
245 Insert an em-dash (
246 \u2014
247 ), a copyright symbol (
248 \u00a9
249 ), a non-breaking
250 space (
251 \u00a0
252 ), a backwards-not-equals (
254 \u20e5
255 ), and a captial omega (
256 \u03a9
258 <substitution_definition names="mdash">
259 \u2014
260 <substitution_definition names="copy">
261 \u00a9
262 <substitution_definition names="nbsp">
263 \u00a0
264 <substitution_definition names="bne">
266 \u20e5
267 <substitution_definition names="Omega">
268 \u03a9
269 """],
270 ["""
271 Testing comments and extra text.
273 Copyright |copy| 2003, |BogusMegaCorp (TM)|.
275 .. |copy| unicode:: 0xA9 .. copyright sign
276 .. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122
277 .. with trademark sign
278 """,
279 u"""\
280 <document source="test data">
281 <paragraph>
282 Testing comments and extra text.
283 <paragraph>
284 Copyright \n\
285 \u00a9
286 2003, \n\
287 BogusMegaCorp
288 \u2122
290 <substitution_definition names="copy">
291 \u00a9
292 <substitution_definition names="BogusMegaCorp\ (TM)">
293 BogusMegaCorp
294 \u2122
295 """],
296 ["""\
297 Insert an em-dash |---| automatically trimming whitespace.
298 Some substitutions |TM| only need |rarrow| trimming on one side.
300 .. |---| unicode:: U+02014
301 :trim:
302 .. |TM| unicode:: U+02122
303 :ltrim:
304 .. |rarrow| unicode:: U+2192
305 :rtrim:
306 """,
307 u"""\
308 <document source="test data">
309 <paragraph>
310 Insert an em-dash
311 \u2014
312 automatically trimming whitespace.
313 Some substitutions
314 \u2122
315 only need \n\
316 \u2192
317 trimming on one side.
318 <substitution_definition ltrim="1" names="---" rtrim="1">
319 \u2014
320 <substitution_definition ltrim="1" names="TM">
321 \u2122
322 <substitution_definition names="rarrow" rtrim="1">
323 \u2192
324 """],
325 ["""\
326 Substitution definition with an illegal element:
328 .. |target| replace:: _`target`
330 Make sure this substitution definition is not registered: |target|
331 """,
332 """\
333 <document source="test data">
334 <paragraph>
335 Substitution definition with an illegal element:
336 <system_message level="3" line="3" source="test data" type="ERROR">
337 <paragraph>
338 Substitution definition contains illegal element:
339 <literal_block xml:space="preserve">
340 <target ids="target" names="target">
341 target
342 <literal_block xml:space="preserve">
343 .. |target| replace:: _`target`
344 <paragraph>
345 Make sure this substitution definition is not registered: \n\
346 <problematic ids="id2" refid="id1">
347 |target|
348 <system_message backrefs="id2" ids="id1" level="3" line="5" source="test data" type="ERROR">
349 <paragraph>
350 Undefined substitution referenced: "target".
351 """],
355 if __name__ == '__main__':
356 import unittest
357 unittest.main(defaultTest='suite')