Drop 2.4 and 2.5 compatibility code, part 2.
[docutils.git] / docutils / test / test_parsers / test_rst / test_targets.py
blobfe44a73776bd24feae552e5ad66710ff7cd2460b
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 states.py.
9 """
11 from __init__ import DocutilsTestSupport
13 def suite():
14 s = DocutilsTestSupport.ParserTestSuite()
15 s.generateTests(totest)
16 return s
18 totest = {}
20 totest['targets'] = [
21 ["""\
22 .. _target:
24 (Internal hyperlink target.)
25 """,
26 """\
27 <document source="test data">
28 <target ids="target" names="target">
29 <paragraph>
30 (Internal hyperlink target.)
31 """],
32 ["""\
33 .. _optional space before colon :
34 """,
35 """\
36 <document source="test data">
37 <target ids="optional-space-before-colon" names="optional\ space\ before\ colon">
38 """],
39 [r"""
40 External hyperlink targets:
42 .. _one-liner: http://structuredtext.sourceforge.net
44 .. _starts-on-this-line: http://
45 structuredtext.
46 sourceforge.net
48 .. _entirely-below:
49 http://structuredtext.
50 sourceforge.net
52 .. _escaped-whitespace: http://example.org/a\ path\ with\
53 spaces.html
55 .. _not-indirect: uri\_
56 """,
57 """\
58 <document source="test data">
59 <paragraph>
60 External hyperlink targets:
61 <target ids="one-liner" names="one-liner" refuri="http://structuredtext.sourceforge.net">
62 <target ids="starts-on-this-line" names="starts-on-this-line" refuri="http://structuredtext.sourceforge.net">
63 <target ids="entirely-below" names="entirely-below" refuri="http://structuredtext.sourceforge.net">
64 <target ids="escaped-whitespace" names="escaped-whitespace" refuri="http://example.org/a path with spaces.html">
65 <target ids="not-indirect" names="not-indirect" refuri="uri_">
66 """],
67 ["""\
68 Indirect hyperlink targets:
70 .. _target1: reference_
72 .. _target2: `phrase-link reference`_
73 """,
74 """\
75 <document source="test data">
76 <paragraph>
77 Indirect hyperlink targets:
78 <target ids="target1" names="target1" refname="reference">
79 <target ids="target2" names="target2" refname="phrase-link reference">
80 """],
81 ["""\
82 .. _a long target name:
84 .. _`a target name: including a colon (quoted)`:
86 .. _a target name\: including a colon (escaped):
87 """,
88 """\
89 <document source="test data">
90 <target ids="a-long-target-name" names="a\ long\ target\ name">
91 <target ids="a-target-name-including-a-colon-quoted" names="a\ target\ name:\ including\ a\ colon\ (quoted)">
92 <target ids="a-target-name-including-a-colon-escaped" names="a\ target\ name:\ including\ a\ colon\ (escaped)">
93 """],
94 ["""\
95 .. _`target: No matching backquote.
96 .. _`: No matching backquote either.
97 """,
98 """\
99 <document source="test data">
100 <comment xml:space="preserve">
101 _`target: No matching backquote.
102 <system_message level="2" line="1" source="test data" type="WARNING">
103 <paragraph>
104 malformed hyperlink target.
105 <comment xml:space="preserve">
106 _`: No matching backquote either.
107 <system_message level="2" line="2" source="test data" type="WARNING">
108 <paragraph>
109 malformed hyperlink target.
110 """],
111 ["""\
112 .. _a very long target name,
113 split across lines:
114 .. _`and another,
115 with backquotes`:
116 """,
117 """\
118 <document source="test data">
119 <target ids="a-very-long-target-name-split-across-lines" names="a\ very\ long\ target\ name,\ split\ across\ lines">
120 <target ids="and-another-with-backquotes" names="and\ another,\ with\ backquotes">
121 """],
122 ["""\
123 External hyperlink:
125 .. _target: http://www.python.org/
126 """,
127 """\
128 <document source="test data">
129 <paragraph>
130 External hyperlink:
131 <target ids="target" names="target" refuri="http://www.python.org/">
132 """],
133 ["""\
134 .. _email: jdoe@example.com
136 .. _multi-line email: jdoe
137 @example.com
138 """,
139 """\
140 <document source="test data">
141 <target ids="email" names="email" refuri="mailto:jdoe@example.com">
142 <target ids="multi-line-email" names="multi-line\ email" refuri="mailto:jdoe@example.com">
143 """],
144 ["""\
145 Malformed target:
147 .. __malformed: no good
149 Target beginning with an underscore:
151 .. _`_target`: OK
152 """,
153 """\
154 <document source="test data">
155 <paragraph>
156 Malformed target:
157 <comment xml:space="preserve">
158 __malformed: no good
159 <system_message level="2" line="3" source="test data" type="WARNING">
160 <paragraph>
161 malformed hyperlink target.
162 <paragraph>
163 Target beginning with an underscore:
164 <target ids="target" names="_target" refuri="OK">
165 """],
166 ["""\
167 Duplicate external targets (different URIs):
169 .. _target: first
171 .. _target: second
172 """,
173 """\
174 <document source="test data">
175 <paragraph>
176 Duplicate external targets (different URIs):
177 <target dupnames="target" ids="target" refuri="first">
178 <system_message backrefs="id1" level="2" line="5" source="test data" type="WARNING">
179 <paragraph>
180 Duplicate explicit target name: "target".
181 <target dupnames="target" ids="id1" refuri="second">
182 """],
183 ["""\
184 Duplicate external targets (same URIs):
186 .. _target: first
188 .. _target: first
189 """,
190 """\
191 <document source="test data">
192 <paragraph>
193 Duplicate external targets (same URIs):
194 <target ids="target" names="target" refuri="first">
195 <system_message backrefs="id1" level="1" line="5" source="test data" type="INFO">
196 <paragraph>
197 Duplicate explicit target name: "target".
198 <target dupnames="target" ids="id1" refuri="first">
199 """],
200 ["""\
201 Duplicate implicit targets.
203 Title
204 =====
206 Paragraph.
208 Title
209 =====
211 Paragraph.
212 """,
213 """\
214 <document source="test data">
215 <paragraph>
216 Duplicate implicit targets.
217 <section dupnames="title" ids="title">
218 <title>
219 Title
220 <paragraph>
221 Paragraph.
222 <section dupnames="title" ids="id1">
223 <title>
224 Title
225 <system_message backrefs="id1" level="1" line="9" source="test data" type="INFO">
226 <paragraph>
227 Duplicate implicit target name: "title".
228 <paragraph>
229 Paragraph.
230 """],
231 ["""\
232 Duplicate implicit/explicit targets.
234 Title
235 =====
237 .. _title:
239 Paragraph.
240 """,
241 """\
242 <document source="test data">
243 <paragraph>
244 Duplicate implicit/explicit targets.
245 <section dupnames="title" ids="title">
246 <title>
247 Title
248 <system_message backrefs="id1" level="1" line="6" source="test data" type="INFO">
249 <paragraph>
250 Duplicate implicit target name: "title".
251 <target ids="id1" names="title">
252 <paragraph>
253 Paragraph.
254 """],
255 ["""\
256 Duplicate implicit/directive targets.
258 Title
259 =====
261 .. target-notes::
262 :name: title
263 """,
264 """\
265 <document source="test data">
266 <paragraph>
267 Duplicate implicit/directive targets.
268 <section dupnames="title" ids="title">
269 <title>
270 Title
271 <pending ids="id1" names="title">
272 <system_message backrefs="id1" level="1" line="4" source="test data" type="INFO">
273 <paragraph>
274 Duplicate implicit target name: "title".
275 .. internal attributes:
276 .transform: docutils.transforms.references.TargetNotes
277 .details:
278 """],
279 ["""\
280 Duplicate explicit targets.
282 .. _title:
284 First.
286 .. _title:
288 Second.
290 .. _title:
292 Third.
293 """,
294 """\
295 <document source="test data">
296 <paragraph>
297 Duplicate explicit targets.
298 <target dupnames="title" ids="title">
299 <paragraph>
300 First.
301 <system_message backrefs="id1" level="2" line="7" source="test data" type="WARNING">
302 <paragraph>
303 Duplicate explicit target name: "title".
304 <target dupnames="title" ids="id1">
305 <paragraph>
306 Second.
307 <system_message backrefs="id2" level="2" line="11" source="test data" type="WARNING">
308 <paragraph>
309 Duplicate explicit target name: "title".
310 <target dupnames="title" ids="id2">
311 <paragraph>
312 Third.
313 """],
314 ["""\
315 Duplicate explicit/directive targets.
317 .. _title:
319 First.
321 .. rubric:: this is a title too
322 :name: title
324 """,
325 """\
326 <document source="test data">
327 <paragraph>
328 Duplicate explicit/directive targets.
329 <target dupnames="title" ids="title">
330 <paragraph>
331 First.
332 <rubric dupnames="title" ids="id1">
333 this is a title too
334 <system_message backrefs="id1" level="2" line="9" source="test data" type="WARNING">
335 <paragraph>
336 Duplicate explicit target name: "title".
337 """],
338 ["""\
339 Duplicate targets:
341 Target
342 ======
344 Implicit section header target.
346 .. [TARGET] Citation target.
348 .. [#target] Autonumber-labeled footnote target.
350 .. _target:
352 Explicit internal target.
354 .. _target: Explicit_external_target
356 .. rubric:: directive with target
357 :name: Target
358 """,
359 """\
360 <document source="test data">
361 <paragraph>
362 Duplicate targets:
363 <section dupnames="target" ids="target">
364 <title>
365 Target
366 <paragraph>
367 Implicit section header target.
368 <citation dupnames="target" ids="id1">
369 <label>
370 TARGET
371 <system_message backrefs="id1" level="1" line="8" source="test data" type="INFO">
372 <paragraph>
373 Duplicate implicit target name: "target".
374 <paragraph>
375 Citation target.
376 <footnote auto="1" dupnames="target" ids="id2">
377 <system_message backrefs="id2" level="2" line="10" source="test data" type="WARNING">
378 <paragraph>
379 Duplicate explicit target name: "target".
380 <paragraph>
381 Autonumber-labeled footnote target.
382 <system_message backrefs="id3" level="2" line="12" source="test data" type="WARNING">
383 <paragraph>
384 Duplicate explicit target name: "target".
385 <target dupnames="target" ids="id3">
386 <paragraph>
387 Explicit internal target.
388 <system_message backrefs="id4" level="2" line="16" source="test data" type="WARNING">
389 <paragraph>
390 Duplicate explicit target name: "target".
391 <target dupnames="target" ids="id4" refuri="Explicit_external_target">
392 <rubric dupnames="target" ids="id5">
393 directive with target
394 <system_message backrefs="id5" level="2" line="4" source="test data" type="WARNING">
395 <paragraph>
396 Duplicate explicit target name: "target".
397 """],
398 ["""\
399 .. _unescaped colon at end:: no good
401 .. _:: no good either
403 .. _escaped colon\:: OK
405 .. _`unescaped colon, quoted:`: OK
406 """,
407 """\
408 <document source="test data">
409 <comment xml:space="preserve">
410 _unescaped colon at end:: no good
411 <system_message level="2" line="1" source="test data" type="WARNING">
412 <paragraph>
413 malformed hyperlink target.
414 <comment xml:space="preserve">
415 _:: no good either
416 <system_message level="2" line="3" source="test data" type="WARNING">
417 <paragraph>
418 malformed hyperlink target.
419 <target ids="escaped-colon" names="escaped\ colon:" refuri="OK">
420 <target ids="unescaped-colon-quoted" names="unescaped\ colon,\ quoted:" refuri="OK">
421 """],
424 totest['anonymous_targets'] = [
425 ["""\
426 Anonymous external hyperlink target:
428 .. __: http://w3c.org/
429 """,
430 """\
431 <document source="test data">
432 <paragraph>
433 Anonymous external hyperlink target:
434 <target anonymous="1" ids="id1" refuri="http://w3c.org/">
435 """],
436 ["""\
437 Anonymous external hyperlink target:
439 __ http://w3c.org/
440 """,
441 """\
442 <document source="test data">
443 <paragraph>
444 Anonymous external hyperlink target:
445 <target anonymous="1" ids="id1" refuri="http://w3c.org/">
446 """],
447 ["""\
448 Anonymous indirect hyperlink target:
450 .. __: reference_
451 """,
452 """\
453 <document source="test data">
454 <paragraph>
455 Anonymous indirect hyperlink target:
456 <target anonymous="1" ids="id1" refname="reference">
457 """],
458 ["""\
459 Anonymous external hyperlink target, not indirect:
461 __ uri\\_
463 __ this URI ends with an underscore_
464 """,
465 """\
466 <document source="test data">
467 <paragraph>
468 Anonymous external hyperlink target, not indirect:
469 <target anonymous="1" ids="id1" refuri="uri_">
470 <target anonymous="1" ids="id2" refuri="thisURIendswithanunderscore_">
471 """],
472 ["""\
473 Anonymous indirect hyperlink targets:
475 __ reference_
476 __ `a very long
477 reference`_
478 """,
479 """\
480 <document source="test data">
481 <paragraph>
482 Anonymous indirect hyperlink targets:
483 <target anonymous="1" ids="id1" refname="reference">
484 <target anonymous="1" ids="id2" refname="a very long reference">
485 """],
486 ["""\
487 Mixed anonymous & named indirect hyperlink targets:
489 __ reference_
490 .. __: reference_
491 __ reference_
492 .. _target1: reference_
493 no blank line
495 .. _target2: reference_
496 __ reference_
497 .. __: reference_
498 __ reference_
499 no blank line
500 """,
501 """\
502 <document source="test data">
503 <paragraph>
504 Mixed anonymous & named indirect hyperlink targets:
505 <target anonymous="1" ids="id1" refname="reference">
506 <target anonymous="1" ids="id2" refname="reference">
507 <target anonymous="1" ids="id3" refname="reference">
508 <target ids="target1" names="target1" refname="reference">
509 <system_message level="2" line="7" source="test data" type="WARNING">
510 <paragraph>
511 Explicit markup ends without a blank line; unexpected unindent.
512 <paragraph>
513 no blank line
514 <target ids="target2" names="target2" refname="reference">
515 <target anonymous="1" ids="id4" refname="reference">
516 <target anonymous="1" ids="id5" refname="reference">
517 <target anonymous="1" ids="id6" refname="reference">
518 <system_message level="2" line="13" source="test data" type="WARNING">
519 <paragraph>
520 Explicit markup ends without a blank line; unexpected unindent.
521 <paragraph>
522 no blank line
523 """],
524 ["""\
525 .. _
526 """,
527 """\
528 <document source="test data">
529 <comment xml:space="preserve">
531 """],
535 if __name__ == '__main__':
536 import unittest
537 unittest.main(defaultTest='suite')