4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Tests for misc.py "include" directive.
10 from __future__
import absolute_import
14 if __name__
== '__main__':
16 from test_parsers
import DocutilsTestSupport
17 from docutils
.parsers
.rst
import states
18 from docutils
.parsers
import recommonmark_wrapper
19 from docutils
.utils
.code_analyzer
import with_pygments
21 if sys
.version_info
>= (3, 0):
26 s
= DocutilsTestSupport
.ParserTestSuite()
28 del(totest
['include-code'])
29 s
.generateTests(totest
)
32 # prepend this directory (relative to the test root):
34 return os
.path
.join('test_parsers/test_rst/test_directives/', path
)
35 # make `path` relative with utils.relative_path():
37 return DocutilsTestSupport
.utils
.relative_path(None, path
)
39 include1
= mydir('include1.txt')
40 include2
= mydir('include2.txt')
41 include3
= mydir('include3.txt')
42 include6
= mydir('includes/more/include6.txt')
43 include8
= mydir('include8.txt')
44 include10
= mydir('include10.txt')
45 include11
= mydir('include 11.txt')
46 include12
= mydir('include12.txt')
47 include13
= mydir('include13.txt')
48 include14
= mydir('includes/include14.txt')
49 include15
= mydir('includes/include15.txt')
50 include16
= mydir('includes/include16.txt')
51 include_literal
= mydir('include_literal.txt')
52 include_md
= mydir('include.md')
53 utf_16_file
= mydir('utf-16.csv')
54 utf_16_error_str
= ("UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe "
55 "in position 0: ordinal not in range(128)")
56 if sys
.version_info
< (3, 0):
57 utf_16_error_str
= ("UnicodeError: Unable to decode input data. "
58 "Tried the following encodings: 'ascii'.\n"
59 " (%s)" % utf_16_error_str
)
60 nonexistent
= os
.path
.join(os
.path
.dirname(states
.__file
__),
61 'include', 'nonexistent')
62 nonexistent_rel
= DocutilsTestSupport
.utils
.relative_path(
63 os
.path
.join(DocutilsTestSupport
.testroot
, 'dummy'), nonexistent
)
65 # Different error for path with 8bit chars with locale == C or None:
67 open(u
'\u043c\u0438\u0440.txt')
68 except UnicodeEncodeError:
69 errstr_8bit_path
= u
"""\
70 Cannot encode input file path "\u043c\u0438\u0440.txt" (wrong locale?).\
73 errstr_8bit_path
= u
"""\
74 InputError: [Errno 2] No such file or directory: '\u043c\u0438\u0440.txt'.\
77 # Parsing with Markdown (recommonmark) is an optional feature depending
78 # on 3rd-party modules:
79 if recommonmark_wrapper
.CommonMarkParser
:
80 markdown_parsing_result
= """\
81 <section ids="title-1" names="title\\ 1">
91 No whitespace required around a
92 <reference name="phrase reference" refuri="/uri">
96 markdown_parsing_result
= """\
97 <system_message level="2" source="test_parsers/test_rst/test_directives/include.md" type="WARNING">
99 Missing dependency: MarkDown input is processed by a 3rd party parser but Python did not find the required module "recommonmark" (https://pypi.org/project/recommonmark/).\
105 totest
['include'] = [
115 <document source="test data">
116 <section ids="include-test" names="include\\ test">
119 <section ids="inclusion-1" names="inclusion\\ 1">
123 This file is used by \n\
142 <document source="test data">
143 <section ids="include-test" names="include\\ test">
146 <literal_block classes="test" ids="my-name" names="my\\ name" source="%s" xml:space="preserve">
150 This file is used by ``test_include.py``.
153 """ % reldir(include1
)],
155 Literal include, add line numbers
162 <document source="test data">
164 Literal include, add line numbers
165 <literal_block source="%s" xml:space="preserve">
166 <inline classes="ln">
169 <inline classes="ln">
172 <inline classes="ln">
175 <inline classes="ln">
177 This file is used by ``test_include.py``.
178 """ % reldir(include1
)],
188 <document source="test data">
191 <literal_block classes="code test" ids="my-name" names="my\\ name" source="%s" xml:space="preserve">
195 This file is used by ``test_include.py``.
196 """ % reldir(include1
)],
198 Include code, add line numbers
205 <document source="test data">
207 Include code, add line numbers
208 <literal_block classes="code" source="%s" xml:space="preserve">
209 <inline classes="ln">
212 <inline classes="ln">
215 <inline classes="ln">
218 <inline classes="ln">
220 This file is used by ``test_include.py``.
221 """ % reldir(include1
)],
223 Include markdown (recommonmark).
231 <document source="test data">
233 Include markdown (recommonmark).
237 """ % markdown_parsing_result
],
239 Let's test the parse context.
241 This paragraph is in a block quote.
245 The included paragraphs should also be in the block quote.
248 <document source="test data">
250 Let's test the parse context.
253 This paragraph is in a block quote.
255 Here are some paragraphs
256 that can appear at any level.
258 This file (include2.txt) is used by
263 The included paragraphs should also be in the block quote.
269 .. include:: nonexistent.txt
274 <document source="test data">
275 <section ids="include-test" names="include\\ test">
278 <system_message level="4" line="4" source="test data" type="SEVERE">
280 Problems with "include" directive path:
281 InputError: [Errno 2] No such file or directory: 'nonexistent.txt'.
282 <literal_block xml:space="preserve">
283 .. include:: nonexistent.txt
296 """ % (include1
, include1
),
298 <document source="test data">
299 <section ids="include-test" names="include\\ test">
302 <section dupnames="inclusion\\ 1" ids="inclusion-1">
310 <section dupnames="inclusion\\ 1" ids="id1">
313 <system_message backrefs="id1" level="1" line="2" source="%s" type="INFO">
315 Duplicate implicit target name: "inclusion 1".
323 """ % reldir(include1
)],
335 """ % (include1
, include1
),
337 <document source="test data">
338 <section ids="include-test" names="include\\ test">
341 <section dupnames="inclusion\\ 1" ids="inclusion-1">
345 This file is used by \n\
350 <section dupnames="inclusion\\ 1" ids="id1">
353 <system_message backrefs="id1" level="1" line="2" source="%s" type="INFO">
355 Duplicate implicit target name: "inclusion 1".
357 This file is used by \n\
363 """ % reldir(include1
)],
365 Recursive inclusions: adapt paths.
372 <document source="test data">
374 Recursive inclusions: adapt paths.
380 In includes/include4.txt
382 In includes/include5.txt
384 In includes/more/include6.txt
386 In includes/sibling/include7.txt
387 <literal_block source="test_parsers/test_rst/test_directives/includes/include5.txt" xml:space="preserve">
388 In includes/include5.txt
390 .. include:: more/include6.txt
393 <colspec colwidth="50">
394 <colspec colwidth="50">
402 includes/sibling/include7.txt
410 (Section contents in nested parse; slice of input_lines ViewList.)
415 <document source="test data">
418 <section ids="section" names="section">
422 (Section contents in nested parse; slice of input_lines ViewList.)
426 In includes/include4.txt
428 In includes/include5.txt
430 In includes/more/include6.txt
432 In includes/sibling/include7.txt
433 <literal_block source="test_parsers/test_rst/test_directives/includes/include5.txt" xml:space="preserve">
434 In includes/include5.txt
436 .. include:: more/include6.txt
439 <colspec colwidth="50">
440 <colspec colwidth="50">
448 includes/sibling/include7.txt
451 Testing relative includes:
456 <document source="test data">
458 Testing relative includes:
462 In ../includes/include9.txt.
464 Here are some paragraphs
465 that can appear at any level.
467 This file (include2.txt) is used by
477 """ % reldir(utf_16_file
),
479 <document source="test data">
483 "Treat", "Quantity", "Description"
484 "Albatr\xb0\xdf", 2.99, "\xa1On a \\u03c3\\u03c4\\u03b9\\u03ba!"
485 "Crunchy Frog", 1.49, "If we took the b\xf6nes out, it wouldn\\u2019t be
486 crunchy, now would it?"
487 "Gannet Ripple", 1.99, "\xbfOn a \\u03c3\\u03c4\\u03b9\\u03ba?"
488 """.decode('raw_unicode_escape')],
490 Include file is UTF-16-encoded, and is not valid ASCII.
494 """ % reldir(utf_16_file
),
496 <document source="test data">
498 Include file is UTF-16-encoded, and is not valid ASCII.
499 <system_message level="4" line="3" source="test data" type="SEVERE">
501 Problem with "include" directive:
503 <literal_block xml:space="preserve">
506 """ % (utf_16_error_str
, reldir(utf_16_file
))],
510 .. include:: \u043c\u0438\u0440.txt
513 <document source="test data">
516 <system_message level="4" line="3" source="test data" type="SEVERE">
518 Problems with "include" directive path:
520 <literal_block xml:space="preserve">
521 .. include:: \u043c\u0438\u0440.txt
522 """ % errstr_8bit_path
],
524 Testing errors in included file:
529 <document source="test data">
531 Testing errors in included file:
532 <system_message level="3" line="1" source="%(source)s" type="ERROR">
534 Invalid character code: 0x11111111
536 <literal_block xml:space="preserve">
538 <system_message level="2" line="1" source="%(source)s" type="WARNING">
540 Substitution definition "bad" empty or invalid.
541 <literal_block xml:space="preserve">
542 .. |bad| unicode:: 0x11111111
543 <section dupnames="hi" ids="hi">
549 <system_message level="2" line="7" source="%(source)s" type="WARNING">
551 Block quote ends without a blank line; unexpected unindent.
554 <section dupnames="hi" ids="id1">
557 <system_message backrefs="id1" level="1" line="10" source="%(source)s" type="INFO">
559 Duplicate implicit target name: "hi".
560 <system_message level="4" line="12" source="%(source)s" type="SEVERE">
562 Problems with "include" directive path:
563 InputError: [Errno 2] No such file or directory: '%(nonexistent)s'.
564 <literal_block xml:space="preserve">
565 .. include:: <nonexistent>
566 <system_message level="3" line="14" source="%(source)s" type="ERROR">
568 Content block expected for the "note" directive; none found.
569 <literal_block xml:space="preserve">
571 <system_message level="3" line="16" source="%(source)s" type="ERROR">
573 Content block expected for the "admonition" directive; none found.
574 <literal_block xml:space="preserve">
577 <system_message level="3" line="19" source="%(source)s" type="ERROR">
579 Content block expected for the "epigraph" directive; none found.
580 <literal_block xml:space="preserve">
582 <system_message level="3" line="21" source="%(source)s" type="ERROR">
584 Content block expected for the "highlights" directive; none found.
585 <literal_block xml:space="preserve">
587 <system_message level="3" line="23" source="%(source)s" type="ERROR">
589 Content block expected for the "pull-quote" directive; none found.
590 <literal_block xml:space="preserve">
592 <system_message level="3" line="25" source="%(source)s" type="ERROR">
594 Invalid context: the "date" directive can only be used within a substitution definition.
595 <literal_block xml:space="preserve">
600 <system_message level="3" line="29" source="%(source)s" type="ERROR">
602 Unexpected indentation.
605 as a term may only be one line long.
606 <system_message level="3" line="31" source="%(source)s" type="ERROR">
608 Error in "admonition" directive:
609 1 argument(s) required, 0 supplied.
610 <literal_block xml:space="preserve">
613 without title and content following a blank line
614 <section ids="section-underline-too-short" names="section\\ underline\\ too\\ short">
616 section underline too short
617 <system_message level="2" line="36" source="%(source)s" type="WARNING">
619 Title underline too short.
620 <literal_block xml:space="preserve">
621 section underline too short
625 <colspec colwidth="14">
626 <colspec colwidth="6">
643 <system_message level="2" line="43" source="%(source)s" type="WARNING">
645 Blank line required after table.
647 No blank line after table.
648 <system_message level="3" line="45" source="%(source)s" type="ERROR">
650 Error in "unicode" directive:
651 1 argument(s) required, 0 supplied.
652 <literal_block xml:space="preserve">
654 <system_message level="2" line="45" source="%(source)s" type="WARNING">
656 Substitution definition "empty" empty or invalid.
657 <literal_block xml:space="preserve">
659 <system_message level="3" line="47" source="%(source)s" type="ERROR">
661 Error in "topic" directive:
662 1 argument(s) required, 0 supplied.
663 <literal_block xml:space="preserve">
665 <system_message level="3" line="49" source="%(source)s" type="ERROR">
667 Error in "rubric" directive:
668 1 argument(s) required, 0 supplied.
669 <literal_block xml:space="preserve">
672 A rubric has no content
673 <comment xml:space="preserve">
674 _`target: No matching backquote.
675 <system_message level="2" line="52" source="%(source)s" type="WARNING">
677 malformed hyperlink target.
678 <comment xml:space="preserve">
680 <system_message level="2" line="53" source="%(source)s" type="WARNING">
682 malformed hyperlink target.
684 <definition_list_item>
688 <system_message level="1" line="57" source="%(source)s" type="INFO">
690 Blank line missing before literal block (after the "::")? Interpreted as a definition list item.
692 with no blank line above.
693 <literal_block xml:space="preserve">
695 <system_message level="3" line="61" source="%(source)s" type="ERROR">
697 Inconsistent literal block quoting.
699 $ with inconsistent quoting.
701 <problematic ids="id3" refid="id2">
705 <problematic ids="id5" refid="id4">
708 <problematic ids="id7" refid="id6">
711 <problematic ids="id9" refid="id8">
714 <system_message level="1" line="63" source="%(source)s" type="INFO">
716 No role entry for "unknown-role" in module "docutils.parsers.rst.languages.en".
717 Trying "unknown-role" as canonical role name.
718 <system_message backrefs="id3" ids="id2" level="3" line="63" source="%(source)s" type="ERROR">
720 Unknown interpreted text role "unknown-role".
721 <system_message backrefs="id5" ids="id4" level="2" line="63" source="%(source)s" type="WARNING">
723 Inline emphasis start-string without end-string.
724 <system_message backrefs="id7" ids="id6" level="2" line="63" source="%(source)s" type="WARNING">
726 Inline interpreted text or phrase reference start-string without end-string.
727 <system_message backrefs="id9" ids="id8" level="2" line="63" source="%(source)s" type="WARNING">
729 Inline strong start-string without end-string.
731 <problematic ids="id11" refid="id10">
733 <system_message backrefs="id11" ids="id10" level="3" line="68" source="%(source)s" type="ERROR">
735 PEP number must be a number from 0 to 9999; "-1" is invalid.
736 <system_message level="1" line="66" source="%(source)s" type="INFO">
738 No directive entry for "unknown" in module "docutils.parsers.rst.languages.en".
739 Trying "unknown" as canonical directive name.
740 <system_message level="3" line="70" source="%(source)s" type="ERROR">
742 Unknown directive type "unknown".
743 <literal_block xml:space="preserve">
744 .. unknown:: directive (info still reported with wrong line)
745 <system_message level="3" line="72" source="%(source)s" type="ERROR">
748 No bottom table border found.
749 <literal_block xml:space="preserve">
750 ============== ======
753 """ % {'source': reldir(include10
), 'nonexistent': reldir(nonexistent
),
755 DocutilsTestSupport
.exception_data(unichr, int("11111111", 16))[2]
758 Include file with whitespace in the path:
761 """ % reldir(include11
),
763 <document source="test data">
765 Include file with whitespace in the path:
770 Standard include data file:
772 .. include:: <isogrk4.txt>
775 <document source="test data">
777 Standard include data file:
778 <comment xml:space="preserve">
779 This data file has been placed in the public domain.
780 <comment xml:space="preserve">
781 Derived from the Unicode character mappings available from
782 <http://www.w3.org/2003/entities/xml/>.
783 Processed by unicode2rstsubs.py, part of Docutils:
784 <http://docutils.sourceforge.net>.
785 <substitution_definition names="b.Gammad">
787 <substitution_definition names="b.gammad">
789 """.decode('raw_unicode_escape')],
791 Nonexistent standard include data file:
793 .. include:: <nonexistent>
796 <document source="test data">
798 Nonexistent standard include data file:
799 <system_message level="4" line="3" source="test data" type="SEVERE">
801 Problems with "include" directive path:
802 InputError: [Errno 2] No such file or directory: '%s'.
803 <literal_block xml:space="preserve">
804 .. include:: <nonexistent>
805 """ % nonexistent_rel
],
807 Include start-line/end-line Test
814 <document source="test data">
816 Include start-line/end-line Test
818 This file (include2.txt) is used by
821 Include start-line/end-line + start-after Test
828 Text search is limited to the specified lines.
831 <document source="test data">
833 Include start-line/end-line + start-after Test
835 In include12.txt (after "start here", before "stop here")
837 Text search is limited to the specified lines.
840 Include start-after/end-before Test
843 :start-after: .. start here
844 :end-before: .. stop here
849 <document source="test data">
851 Include start-after/end-before Test
853 In include12.txt (after "start here", before "stop here")
858 Include start-after/end-before Test, single option variant
861 :end-before: .. start here
864 :start-after: .. stop here
867 """ % (include12
, include12
),
869 <document source="test data">
871 Include start-after/end-before Test, single option variant
873 In include12.txt (but before "start here")
875 In include12.txt (after "stop here")
880 Include start-after/end-before multi-line test.
883 :start-after: From: me
889 :start-after: From: me
896 """ % (include13
, include13
),
898 <document source="test data">
900 Include start-after/end-before multi-line test.
901 <system_message level="4" line="3" source="test data" type="SEVERE">
903 Problem with "end-before" option of "include" directive:
905 <literal_block xml:space="preserve">
907 :start-after: From: me
912 In include13.txt (between header and signature)
917 Error handling test; "end-before" error handling tested in previous test.
920 :start-after: bad string
921 :end-before: mork of ork
924 <document source="test data">
926 Error handling test; "end-before" error handling tested in previous test.
927 <system_message level="4" line="3" source="test data" type="SEVERE">
929 Problem with "start-after" option of "include" directive:
931 <literal_block xml:space="preserve">
933 :start-after: bad string
934 :end-before: mork of ork
937 TAB expansion with literal include:
941 """ % include_literal
,
943 <document source="test data">
945 TAB expansion with literal include:
946 <literal_block source="%s" xml:space="preserve">
947 Literal included this should **not** be *marked* `up`.
953 """ % include_literal
],
955 Custom TAB expansion with literal include:
960 """ % include_literal
,
962 <document source="test data">
964 Custom TAB expansion with literal include:
965 <literal_block source="%s" xml:space="preserve">
966 Literal included this should **not** be *marked* `up`.
972 """ % include_literal
],
974 No TAB expansion with literal include:
979 """ % include_literal
,
981 <document source="test data">
983 No TAB expansion with literal include:
984 <literal_block source="%s" xml:space="preserve">
985 Literal included this should **not** be *marked* `up`.
986 \t<- leading raw tab.
991 """ % include_literal
],
994 totest
['include-code'] = [
1002 <document source="test data">
1005 <literal_block classes="code rst" source="%s" xml:space="preserve">
1006 <inline classes="generic heading">
1009 <inline classes="generic heading">
1013 This file is used by \n\
1014 <inline classes="literal string">
1017 """ % reldir(include1
)],
1026 <document source="test data">
1029 <literal_block classes="code rst" source="%s" xml:space="preserve">
1030 <inline classes="ln">
1032 <inline classes="generic heading">
1035 <inline classes="ln">
1037 <inline classes="generic heading">
1040 <inline classes="ln">
1043 <inline classes="ln">
1045 This file is used by \n\
1046 <inline classes="literal string">
1049 """ % reldir(include1
)],
1051 TAB expansion with included code:
1055 """ % include_literal
,
1057 <document source="test data">
1059 TAB expansion with included code:
1060 <literal_block classes="code rst" source="%s" xml:space="preserve">
1061 Literal included this should \n\
1062 <inline classes="generic strong">
1065 <inline classes="generic emph">
1068 <inline classes="name variable">
1076 """ % include_literal
],
1078 Custom TAB expansion with included code:
1083 """ % include_literal
,
1085 <document source="test data">
1087 Custom TAB expansion with included code:
1088 <literal_block classes="code rst" source="%s" xml:space="preserve">
1089 Literal included this should \n\
1090 <inline classes="generic strong">
1093 <inline classes="generic emph">
1096 <inline classes="name variable">
1104 """ % include_literal
],
1106 Custom TAB expansion with included code:
1111 """ % include_literal
,
1113 <document source="test data">
1115 Custom TAB expansion with included code:
1116 <literal_block classes="code rst" source="%s" xml:space="preserve">
1117 Literal included this should \n\
1118 <inline classes="generic strong">
1121 <inline classes="generic emph">
1124 <inline classes="name variable">
1127 \t<- leading raw tab.
1132 """ % include_literal
],
1134 Including includes/include14.txt
1139 <document source="test data">
1141 Including includes/include14.txt
1143 Including more/include6.txt as rst-code from includes/include14.txt:
1144 <literal_block classes="code rst" source="%s" xml:space="preserve">
1145 In includes/more/include6.txt
1147 <inline classes="punctuation">
1150 <inline classes="operator word">
1152 <inline classes="punctuation">
1154 ../sibling/include7.txt
1155 """ % reldir(include6
)],
1162 <document source="test data">
1166 File "include15.txt": example of rekursive inclusion.
1168 File "include16.txt": example of rekursive inclusion.
1169 <system_message level="2" line="3" source="%s" type="WARNING">
1171 circular inclusion in "include" directive: %s < %s < %s < test data
1172 <literal_block xml:space="preserve">
1173 .. include:: include15.txt
1175 No loop when clipping before the "include" directive:
1177 File "include15.txt": example of rekursive inclusion.
1178 """ % (reldir(include16
), reldir(include15
),
1179 reldir(include16
), reldir(include15
))],
1181 Circular inclusion with clipping.
1187 <document source="test data">
1189 Circular inclusion with clipping.
1191 File "include15.txt": example of rekursive inclusion.
1193 File "include16.txt": example of rekursive inclusion.
1194 <system_message level="2" line="3" source="%s" type="WARNING">
1196 circular inclusion in "include" directive: %s < %s < %s < %s < test data
1197 <literal_block xml:space="preserve">
1198 .. include:: include15.txt
1200 No loop when clipping before the "include" directive:
1202 File "include15.txt": example of rekursive inclusion.
1204 No loop when clipping before the "include" directive:
1206 File "include15.txt": example of rekursive inclusion.
1207 """ % (reldir(include16
), reldir(include15
), reldir(include16
),
1208 reldir(include15
), reldir(include16
))],
1211 if __name__
== '__main__':
1213 unittest
.main(defaultTest
='suite')