4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Tests for docutils.transforms.references.Hyperlinks.
11 from __init__
import DocutilsTestSupport
12 from docutils
.transforms
.references
import PropagateTargets
, \
13 AnonymousHyperlinks
, IndirectHyperlinks
, ExternalTargets
, \
14 InternalTargets
, DanglingReferences
16 from docutils
.parsers
.rst
import Parser
21 s
= DocutilsTestSupport
.TransformTestSuite(parser
)
22 s
.generateTests(totest
)
27 # Exhaustive listing of hyperlink variations: every combination of
28 # target/reference, direct/indirect, internal/external, and named/anonymous,
30 totest
['exhaustive_hyperlinks'] = ((PropagateTargets
, AnonymousHyperlinks
,
32 ExternalTargets
, InternalTargets
,
33 DanglingReferences
), [
37 .. _direct: http://direct
40 <document source="test data">
42 <reference name="direct" refuri="http://direct">
45 <target ids="direct" names="direct" refuri="http://direct">
50 .. _indirect: xtarget_
51 .. _xtarget: http://indirect
54 <document source="test data">
56 <reference name="indirect" refuri="http://indirect">
59 <target ids="indirect" names="indirect" refuri="http://indirect">
60 <target ids="xtarget" names="xtarget" refuri="http://indirect">
68 <document source="test data">
69 <target refid="direct">
70 <paragraph ids="direct" names="direct">
71 <reference name="direct" refid="direct">
80 .. _indirect2: ztarget_
81 .. _indirect: indirect2_
84 <document source="test data">
85 <target refid="ztarget">
86 <paragraph ids="ztarget" names="ztarget">
87 <reference name="indirect" refid="ztarget">
90 <target ids="indirect2" names="indirect2" refid="ztarget">
91 <target ids="indirect" names="indirect" refid="ztarget">
99 .. _indirect: implicit_
102 <document source="test data">
103 <section ids="implicit" names="implicit">
107 <reference name="indirect" refid="implicit">
110 <target ids="indirect" names="indirect" refid="implicit">
116 `multiply-indirect`_ internal
118 .. _multiply-indirect: indirect_
119 .. _indirect: implicit_
122 <document source="test data">
123 <section ids="implicit" names="implicit">
127 <reference name="multiply-indirect" refid="implicit">
130 <target ids="multiply-indirect" names="multiply-indirect" refid="implicit">
131 <target ids="indirect" names="indirect" refid="implicit">
134 circular_ indirect reference
136 .. _circular: indirect_
137 .. _indirect: circular_
140 <document source="test data">
142 <problematic ids="id2" refid="id1">
145 <target ids="circular" names="circular" refid="circular">
146 <problematic ids="id3 indirect" names="indirect" refid="id1">
147 .. _indirect: circular_
148 <system_message backrefs="id2 id3" ids="id1" level="3" line="3" source="test data" type="ERROR">
150 Indirect hyperlink target "circular" (id="circular") refers to target "indirect", forming a circular reference.
156 Duplicate implicit targets.
163 .. _indirect: implicit_
165 Direct internal reference: Implicit_
168 <document source="test data">
169 <section dupnames="implicit" ids="implicit">
173 Duplicate implicit targets.
174 <section dupnames="implicit" ids="id1">
177 <system_message backrefs="id1" level="1" line="7" source="test data" type="INFO">
179 Duplicate implicit target name: "implicit".
181 <problematic ids="id3" refid="id2">
184 <target ids="indirect" names="indirect" refname="implicit">
186 Direct internal reference:
187 <problematic ids="id5" refid="id4">
189 <system_message backrefs="id3" ids="id2" level="3" line="11" source="test data" type="ERROR">
191 Indirect hyperlink target "indirect" (id="indirect") refers to target "implicit", which is a duplicate, and cannot be used as a unique reference.
192 <system_message backrefs="id5" ids="id4" level="3" line="13" source="test data" type="ERROR">
194 Duplicate target name, cannot be used as a unique reference: "implicit".
202 <document source="test data">
204 <reference anonymous="1" name="direct external" refuri="http://direct">
206 <target anonymous="1" ids="id1" refuri="http://direct">
209 `indirect external`__
212 .. _xtarget: http://indirect
215 <document source="test data">
217 <reference anonymous="1" name="indirect external" refuri="http://indirect">
219 <target anonymous="1" ids="id1" refuri="http://indirect">
220 <target ids="xtarget" names="xtarget" refuri="http://indirect">
228 <document source="test data">
229 <target anonymous="1" refid="id1">
230 <paragraph ids="id1">
231 <reference anonymous="1" name="direct internal" refid="id1">
237 `indirect internal`__
242 <document source="test data">
243 <target refid="ztarget">
244 <paragraph ids="ztarget" names="ztarget">
245 <reference anonymous="1" name="indirect internal" refid="ztarget">
247 <target anonymous="1" ids="id1" refid="ztarget">
258 `indirect internal`__
263 <document source="test data">
264 <target dupnames="ztarget" refid="ztarget">
265 <paragraph ids="ztarget">
267 <system_message backrefs="id1" level="2" line="5" source="test data" type="WARNING">
269 Duplicate explicit target name: "ztarget".
270 <target dupnames="ztarget" refid="id1">
271 <paragraph ids="id1">
274 <problematic ids="id4" refid="id3">
275 `indirect internal`__
276 <target anonymous="1" ids="id2" refname="ztarget">
277 <system_message backrefs="id4" ids="id3" level="3" line="11" source="test data" type="ERROR">
279 Indirect hyperlink target (id="id2") refers to target "ztarget", which is a duplicate, and cannot be used as a unique reference.
282 The next anonymous hyperlink reference is parsed (and discarded) at
283 some point, but nonetheless anonymous hyperlink references and targets
284 match in this snippet.
286 .. |invalid| replace:: anonymous__
293 <document source="test data">
295 The next anonymous hyperlink reference is parsed (and discarded) at
296 some point, but nonetheless anonymous hyperlink references and targets
297 match in this snippet.
298 <system_message level="3" line="5" source="test data" type="ERROR">
300 Substitution definition contains illegal element:
301 <literal_block xml:space="preserve">
302 <reference anonymous="1" name="anonymous">
304 <literal_block xml:space="preserve">
305 .. |invalid| replace:: anonymous__
307 <reference anonymous="1" name="hyperlink" refuri="URL">
309 <target anonymous="1" ids="id1" refuri="URL">
312 An `embedded uri <http://direct>`_.
314 Another reference to the same `embedded URI`_.
317 <document source="test data">
320 <reference name="embedded uri" refuri="http://direct">
322 <target ids="embedded-uri" names="embedded\ uri" refuri="http://direct">
325 Another reference to the same \n\
326 <reference name="embedded URI" refuri="http://direct">
331 An `anonymous embedded uri <http://direct>`__.
334 <document source="test data">
337 <reference name="anonymous embedded uri" refuri="http://direct">
338 anonymous embedded uri
342 An `embedded alias <alias_>`_.
344 Another reference to the same `embedded alias`_.
349 <document source="test data">
352 <reference name="embedded alias" refuri="ham.py">
354 <target names="embedded\ alias" refuri="ham.py">
357 Another reference to the same \n\
358 <reference name="embedded alias" refuri="ham.py">
361 <target ids="alias" names="alias" refuri="ham.py">
364 An `anonymous embedded alias <redirect_>`__.
366 .. _redirect: spam.py
369 <document source="test data">
372 <reference name="anonymous embedded alias" refuri="spam.py">
373 anonymous embedded alias
375 <target ids="redirect" names="redirect" refuri="spam.py">
378 An `embedded alias <alias_>`_ with unknown reference.
381 <document source="test data">
384 <problematic ids="id3" refid="id2">
385 `embedded alias <alias_>`_
386 <target names="embedded\ alias" refname="alias">
387 with unknown reference.
388 <system_message ids="id1" level="3" line="1" source="test data" type="ERROR">
390 Indirect hyperlink target "embedded alias" refers to target "alias", which does not exist.
391 <system_message backrefs="id3" ids="id2" level="3" line="1" source="test data" type="ERROR">
393 Unknown target name: "alias".
394 <system_message level="1" line="1" source="test data" type="INFO">
396 Hyperlink target "embedded alias" is not referenced.\
399 An embedded URI with trailing underline:
400 `__init__ <http:example.py.html#__init__>`__.
403 <document source="test data">
405 An embedded URI with trailing underline:
406 <reference name="__init__" refuri="http:example.py.html#__init__">
411 Hyperlinks with angle-bracketed text need escaping.
413 See `Element \<a>`_, `Element <b\>`_, and `Element <c>\ `_.
417 .. _`Element <c>`: elements.txt
420 <document source="test data">
422 Hyperlinks with angle-bracketed text need escaping.
425 <reference name="Element <a>" refuri="elements.txt">
428 <reference name="Element <b>" refuri="elements.txt">
431 <reference name="Element <c>" refuri="elements.txt">
434 <target refid="element-a">
435 <target refid="element-b">
436 <target ids="element-c element-b element-a" names="element\ <c> element\ <b> element\ <a>" refuri="elements.txt">
441 .. [1] Footnote; target_
444 <document source="test data">
445 <target ids="target" names="target">
446 <footnote ids="id1" names="1">
451 <reference name="target" refid="target">
457 .. [cit] Citation; target_
460 <document source="test data">
461 <target ids="target" names="target">
462 <citation ids="cit" names="cit">
467 <reference name="target" refid="target">
472 totest
['hyperlinks'] = ((PropagateTargets
, AnonymousHyperlinks
,
473 IndirectHyperlinks
, ExternalTargets
,
474 InternalTargets
, DanglingReferences
), [
476 .. _internal hyperlink:
478 This paragraph referenced.
480 By this `internal hyperlink`_ reference.
483 <document source="test data">
484 <target refid="internal-hyperlink">
485 <paragraph ids="internal-hyperlink" names="internal\ hyperlink">
486 This paragraph referenced.
489 <reference name="internal hyperlink" refid="internal-hyperlink">
495 .. _internal hyperlink:
497 This paragraph referenced.
499 By this `internal hyperlink`_ reference
500 as well as by this chained_ reference.
502 The results of the transform are not visible at the XML level.
505 <document source="test data">
506 <target refid="chained">
507 <target refid="internal-hyperlink">
508 <paragraph ids="internal-hyperlink chained" names="internal\ hyperlink chained">
509 This paragraph referenced.
512 <reference name="internal hyperlink" refid="internal-hyperlink">
515 as well as by this \n\
516 <reference name="chained" refid="chained">
520 The results of the transform are not visible at the XML level.
526 Anonymous__ and chained_ both refer to the same URI.
529 <document source="test data">
530 <target refid="chained">
531 <target anonymous="1" ids="id1 chained" names="chained" refuri="http://anonymous">
533 <reference anonymous="1" name="Anonymous" refuri="http://anonymous">
536 <reference name="chained" refuri="http://anonymous">
538 both refer to the same URI.
547 <document source="test data">
550 <paragraph ids="b a" names="b a">
552 <system_message level="1" line="1" source="test data" type="INFO">
554 Hyperlink target "a" is not referenced.
555 <system_message level="1" line="2" source="test data" type="INFO">
557 Hyperlink target "b" is not referenced.
566 <document source="test data">
569 <paragraph ids="b a" names="b a">
570 <reference name="a" refid="a">
572 <system_message level="1" line="2" source="test data" type="INFO">
574 Hyperlink target "b" is not referenced.
583 <document source="test data">
586 <paragraph ids="b a" names="b a">
587 <reference name="b" refid="b">
589 <system_message level="1" line="1" source="test data" type="INFO">
591 Hyperlink target "a" is not referenced.
600 <document source="test data">
603 <paragraph ids="b a" names="b a">
604 <reference name="a" refid="a">
606 <reference name="b" refid="b">
610 .. _external hyperlink: http://uri
612 `External hyperlink`_ reference.
615 <document source="test data">
616 <target ids="external-hyperlink" names="external\ hyperlink" refuri="http://uri">
618 <reference name="External hyperlink" refuri="http://uri">
623 .. _external hyperlink: http://uri
624 .. _indirect target: `external hyperlink`_
627 <document source="test data">
628 <target ids="external-hyperlink" names="external\ hyperlink" refuri="http://uri">
629 <target ids="indirect-target" names="indirect\ target" refuri="http://uri">
630 <system_message level="1" line="2" source="test data" type="INFO">
632 Hyperlink target "indirect target" is not referenced.
636 .. _external hyperlink: http://uri
638 `External hyperlink`_ reference
639 and a chained_ reference too.
642 <document source="test data">
643 <target refid="chained">
644 <target ids="external-hyperlink chained" names="external\ hyperlink chained" refuri="http://uri">
646 <reference name="External hyperlink" refuri="http://uri">
650 <reference name="chained" refuri="http://uri">
655 .. _external hyperlink: http://uri
656 .. _indirect hyperlink: `external hyperlink`_
658 `Indirect hyperlink`_ reference.
661 <document source="test data">
662 <target ids="external-hyperlink" names="external\ hyperlink" refuri="http://uri">
663 <target ids="indirect-hyperlink" names="indirect\ hyperlink" refuri="http://uri">
665 <reference name="Indirect hyperlink" refuri="http://uri">
670 .. _external hyperlink: http://uri
672 .. _indirect hyperlink: `external hyperlink`_
674 Chained_ `indirect hyperlink`_ reference.
677 <document source="test data">
678 <target ids="external-hyperlink" names="external\ hyperlink" refuri="http://uri">
679 <target refuri="http://uri">
680 <target ids="indirect-hyperlink chained" names="indirect\ hyperlink chained" refuri="http://uri">
682 <reference name="Chained" refuri="http://uri">
685 <reference name="indirect hyperlink" refuri="http://uri">
693 .. _external: http://indirect.external
697 `Full syntax anonymous external hyperlink reference`__,
698 `chained anonymous external reference`__,
699 `simplified syntax anonymous external hyperlink reference`__,
700 `indirect anonymous hyperlink reference`__,
701 `internal anonymous hyperlink reference`__.
704 <document source="test data">
705 <target anonymous="1" ids="id1" refuri="http://full">
706 <target anonymous="1" refid="id2">
707 <target anonymous="1" ids="id3 id2" refuri="http://simplified">
708 <target ids="external" names="external" refuri="http://indirect.external">
709 <target anonymous="1" ids="id4" refuri="http://indirect.external">
710 <target anonymous="1" refid="id5">
711 <paragraph ids="id5">
712 <reference anonymous="1" name="Full syntax anonymous external hyperlink reference" refuri="http://full">
713 Full syntax anonymous external hyperlink reference
715 <reference anonymous="1" name="chained anonymous external reference" refuri="http://simplified">
716 chained anonymous external reference
718 <reference anonymous="1" name="simplified syntax anonymous external hyperlink reference" refuri="http://simplified">
719 simplified syntax anonymous external hyperlink reference
721 <reference anonymous="1" name="indirect anonymous hyperlink reference" refuri="http://indirect.external">
722 indirect anonymous hyperlink reference
724 <reference anonymous="1" name="internal anonymous hyperlink reference" refid="id5">
725 internal anonymous hyperlink reference
729 Duplicate external target_'s (different URIs):
736 <document source="test data">
738 Duplicate external \n\
739 <problematic ids="id3" refid="id2">
742 <target dupnames="target" ids="target" refuri="first">
743 <system_message backrefs="id1" level="2" line="5" source="test data" type="WARNING">
745 Duplicate explicit target name: "target".
746 <target dupnames="target" ids="id1" refuri="second">
747 <system_message backrefs="id3" ids="id2" level="3" line="1" source="test data" type="ERROR">
749 Duplicate target name, cannot be used as a unique reference: "target".
752 Duplicate external targets (different URIs) without reference:
759 <document source="test data">
761 Duplicate external targets (different URIs) without reference:
762 <target dupnames="target" ids="target" refuri="first">
763 <system_message backrefs="id1" level="2" line="5" source="test data" type="WARNING">
765 Duplicate explicit target name: "target".
766 <target dupnames="target" ids="id1" refuri="second">
769 Several__ anonymous__ hyperlinks__, but not enough targets.
771 __ http://example.org
774 <document source="test data">
776 <problematic ids="id3" refid="id2">
779 <problematic ids="id4" refid="id2">
782 <problematic ids="id5" refid="id2">
784 , but not enough targets.
785 <target anonymous="1" ids="id1" refuri="http://example.org">
786 <system_message backrefs="id3 id4 id5" ids="id2" level="3" source="test data" type="ERROR">
788 Anonymous hyperlink mismatch: 3 references but 1 targets.
789 See "backrefs" attribute for IDs.
792 .. _external: http://uri
793 .. _indirect: external_
796 .. image:: picture.png
799 .. image:: picture.png
802 .. image:: picture.png
806 <document source="test data">
807 <target ids="external" names="external" refuri="http://uri">
808 <target ids="indirect" names="indirect" refuri="http://uri">
809 <target refid="internal">
810 <reference ids="internal" name="external" names="internal" refuri="http://uri">
811 <image uri="picture.png">
812 <reference name="indirect" refuri="http://uri">
813 <image uri="picture.png">
814 <reference name="internal" refid="internal">
815 <image uri="picture.png">
818 .. contents:: Table of Contents
819 .. _indirect reference to the table of contents: `table of contents`_
824 Testing an `indirect reference to the table of contents`_.
827 <document source="test data">
828 <topic classes="contents" ids="table-of-contents" names="table\ of\ contents">
834 <reference ids="id1" refid="section">
836 <target ids="indirect-reference-to-the-table-of-contents" names="indirect\ reference\ to\ the\ table\ of\ contents" refid="table-of-contents">
837 <section ids="section" names="section">
842 <reference name="indirect reference to the table of contents" refid="table-of-contents">
843 indirect reference to the table of contents
852 Let's reference it (`explicit target`_) to avoid an irrelevant error.
855 <document source="test data">
856 <target refid="explicit-target">
857 <section ids="title explicit-target" names="title explicit\ target">
862 <reference name="explicit target" refid="explicit-target">
864 ) to avoid an irrelevant error.
867 target1_ should refer to target2_, not the Title.
876 <document source="test data">
878 <reference name="target1" refuri="URI">
881 <reference name="target2" refuri="URI">
884 <target refid="target1">
885 <target ids="target2 target1" names="target2 target1" refuri="URI">
886 <section ids="title" names="title">
894 <document source="test data">
897 <problematic ids="id2" refid="id1">
900 <system_message backrefs="id2" ids="id1" level="3" line="1" source="test data" type="ERROR">
902 Unknown target name: "reference".
905 Duplicate manual footnote labels, with reference ([1]_):
912 <document source="test data">
914 Duplicate manual footnote labels, with reference (
915 <problematic ids="id5 id1" refid="id4">
918 <footnote dupnames="1" ids="id2">
923 <footnote dupnames="1" ids="id3">
926 <system_message backrefs="id3" level="2" line="5" source="test data" type="WARNING">
928 Duplicate explicit target name: "1".
931 <system_message backrefs="id5" ids="id4" level="3" line="1" source="test data" type="ERROR">
933 Duplicate target name, cannot be used as a unique reference: "1".
938 if __name__
== '__main__':
940 unittest
.main(defaultTest
='suite')