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.
13 from __init__
import DocutilsTestSupport
14 from docutils
.parsers
.rst
import states
15 from docutils
._compat
import b
16 from docutils
.utils
.code_analyzer
import with_pygments
19 s
= DocutilsTestSupport
.ParserTestSuite()
21 del(totest
['include-code'])
22 s
.generateTests(totest
)
25 # prepend this directory (relative to the test root):
27 return os
.path
.join('test_parsers/test_rst/test_directives/', path
)
28 # make `path` relative with utils.relative_path():
30 return DocutilsTestSupport
.utils
.relative_path(None, path
)
32 include1
= mydir('include1.txt')
33 include2
= mydir('include2.txt')
34 include3
= mydir('include3.txt')
35 include8
= mydir('include8.txt')
36 include10
= mydir('include10.txt')
37 include11
= mydir('include 11.txt')
38 include12
= mydir('include12.txt')
39 include13
= mydir('include13.txt')
40 include_literal
= mydir('include_literal.txt')
41 utf_16_file
= mydir('utf-16.csv')
42 utf_16_error_str
= ("UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe "
43 "in position 0: ordinal not in range(128)")
44 if sys
.version_info
< (3,0):
45 utf_16_error_str
= ("UnicodeError: Unable to decode input data. "
46 "Tried the following encodings: 'ascii'.\n"
47 " (%s)" % utf_16_error_str
)
48 nonexistent
= os
.path
.join(os
.path
.dirname(states
.__file
__),
49 'include', 'nonexistent')
50 nonexistent_rel
= DocutilsTestSupport
.utils
.relative_path(
51 os
.path
.join(DocutilsTestSupport
.testroot
, 'dummy'), nonexistent
)
53 # Different error for path with 8bit chars with locale == C or None:
55 open(u
'\u043c\u0438\u0440.txt')
56 except UnicodeEncodeError:
57 errstr_8bit_path
= u
"""\
58 Cannot encode input file path "\u043c\u0438\u0440.txt" (wrong locale?).\
61 errstr_8bit_path
= u
"""\
62 InputError: [Errno 2] No such file or directory: '\u043c\u0438\u0440.txt'.\
77 <document source="test data">
78 <section ids="include-test" names="include\ test">
81 <section ids="inclusion-1" names="inclusion\ 1">
85 This file is used by \n\
104 <document source="test data">
105 <section ids="include-test" names="include\ test">
108 <literal_block classes="test" ids="my-name" names="my\ name" source="%s" xml:space="preserve">
112 This file is used by ``test_include.py``.
115 """ % reldir(include1
)],
117 Literal include, add line numbers
124 <document source="test data">
126 Literal include, add line numbers
127 <literal_block source="%s" xml:space="preserve">
128 <inline classes="ln">
131 <inline classes="ln">
134 <inline classes="ln">
137 <inline classes="ln">
139 This file is used by ``test_include.py``.
140 """ % reldir(include1
)],
150 <document source="test data">
153 <literal_block classes="code test" ids="my-name" names="my\ name" source="%s" xml:space="preserve">
157 This file is used by ``test_include.py``.
158 """ % reldir(include1
)],
160 Include code, add line numbers
167 <document source="test data">
169 Include code, add line numbers
170 <literal_block classes="code" source="%s" xml:space="preserve">
171 <inline classes="ln">
174 <inline classes="ln">
177 <inline classes="ln">
180 <inline classes="ln">
182 This file is used by ``test_include.py``.
183 """ % reldir(include1
)],
185 Let's test the parse context.
187 This paragraph is in a block quote.
191 The included paragraphs should also be in the block quote.
194 <document source="test data">
196 Let's test the parse context.
199 This paragraph is in a block quote.
201 Here are some paragraphs
202 that can appear at any level.
204 This file (include2.txt) is used by
209 The included paragraphs should also be in the block quote.
215 .. include:: nonexistent.txt
220 <document source="test data">
221 <section ids="include-test" names="include\ test">
224 <system_message level="4" line="4" source="test data" type="SEVERE">
226 Problems with "include" directive path:
227 InputError: [Errno 2] No such file or directory: 'nonexistent.txt'.
228 <literal_block xml:space="preserve">
229 .. include:: nonexistent.txt
242 """ % (include1
, include1
),
244 <document source="test data">
245 <section ids="include-test" names="include\ test">
248 <section dupnames="inclusion\ 1" ids="inclusion-1">
256 <section dupnames="inclusion\ 1" ids="id1">
259 <system_message backrefs="id1" level="1" line="2" source="%s" type="INFO">
261 Duplicate implicit target name: "inclusion 1".
269 """ % reldir(include1
)],
281 """ % (include1
, include1
),
283 <document source="test data">
284 <section ids="include-test" names="include\ test">
287 <section dupnames="inclusion\ 1" ids="inclusion-1">
291 This file is used by \n\
296 <section dupnames="inclusion\ 1" ids="id1">
299 <system_message backrefs="id1" level="1" line="2" source="%s" type="INFO">
301 Duplicate implicit target name: "inclusion 1".
303 This file is used by \n\
309 """ % reldir(include1
)],
316 <document source="test data">
322 In includes/include4.txt
324 In includes/include5.txt
326 In includes/more/include6.txt
328 In includes/sibling/include7.txt
336 (Section contents in nested parse; slice of input_lines ViewList.)
341 <document source="test data">
344 <section ids="section" names="section">
348 (Section contents in nested parse; slice of input_lines ViewList.)
352 In includes/include4.txt
354 In includes/include5.txt
356 In includes/more/include6.txt
358 In includes/sibling/include7.txt
361 Testing relative includes:
366 <document source="test data">
368 Testing relative includes:
372 In ../includes/include9.txt.
374 Here are some paragraphs
375 that can appear at any level.
377 This file (include2.txt) is used by
387 """ % reldir(utf_16_file
),
389 <document source="test data">
393 "Treat", "Quantity", "Description"
394 "Albatr\xb0\xdf", 2.99, "\xa1On a \\u03c3\\u03c4\\u03b9\\u03ba!"
395 "Crunchy Frog", 1.49, "If we took the b\xf6nes out, it wouldn\\u2019t be
396 crunchy, now would it?"
397 "Gannet Ripple", 1.99, "\xbfOn a \\u03c3\\u03c4\\u03b9\\u03ba?"
398 """).decode('raw_unicode_escape')],
400 Include file is UTF-16-encoded, and is not valid ASCII.
404 """ % reldir(utf_16_file
),
406 <document source="test data">
408 Include file is UTF-16-encoded, and is not valid ASCII.
409 <system_message level="4" line="3" source="test data" type="SEVERE">
411 Problem with "include" directive:
413 <literal_block xml:space="preserve">
416 """ % (utf_16_error_str
, reldir(utf_16_file
))],
420 .. include:: \u043c\u0438\u0440.txt
423 <document source="test data">
426 <system_message level="4" line="3" source="test data" type="SEVERE">
428 Problems with "include" directive path:
430 <literal_block xml:space="preserve">
431 .. include:: \u043c\u0438\u0440.txt
432 """ % errstr_8bit_path
],
434 Testing errors in included file:
439 <document source="test data">
441 Testing errors in included file:
442 <system_message level="3" line="1" source="%(source)s" type="ERROR">
444 Invalid character code: 0x11111111
446 <literal_block xml:space="preserve">
448 <system_message level="2" line="1" source="%(source)s" type="WARNING">
450 Substitution definition "bad" empty or invalid.
451 <literal_block xml:space="preserve">
452 .. |bad| unicode:: 0x11111111
453 <section dupnames="hi" ids="hi">
459 <system_message level="2" line="7" source="%(source)s" type="WARNING">
461 Block quote ends without a blank line; unexpected unindent.
464 <section dupnames="hi" ids="id1">
467 <system_message backrefs="id1" level="1" line="10" source="%(source)s" type="INFO">
469 Duplicate implicit target name: "hi".
470 <system_message level="4" line="12" source="%(source)s" type="SEVERE">
472 Problems with "include" directive path:
473 InputError: [Errno 2] No such file or directory: '%(nonexistent)s'.
474 <literal_block xml:space="preserve">
475 .. include:: <nonexistent>
476 <system_message level="3" line="14" source="%(source)s" type="ERROR">
478 Content block expected for the "note" directive; none found.
479 <literal_block xml:space="preserve">
481 <system_message level="3" line="16" source="%(source)s" type="ERROR">
483 Content block expected for the "admonition" directive; none found.
484 <literal_block xml:space="preserve">
487 <system_message level="3" line="19" source="%(source)s" type="ERROR">
489 Content block expected for the "epigraph" directive; none found.
490 <literal_block xml:space="preserve">
492 <system_message level="3" line="21" source="%(source)s" type="ERROR">
494 Content block expected for the "highlights" directive; none found.
495 <literal_block xml:space="preserve">
497 <system_message level="3" line="23" source="%(source)s" type="ERROR">
499 Content block expected for the "pull-quote" directive; none found.
500 <literal_block xml:space="preserve">
502 <system_message level="3" line="25" source="%(source)s" type="ERROR">
504 Invalid context: the "date" directive can only be used within a substitution definition.
505 <literal_block xml:space="preserve">
510 <system_message level="3" line="29" source="%(source)s" type="ERROR">
512 Unexpected indentation.
515 as a term may only be one line long.
516 <system_message level="3" line="31" source="%(source)s" type="ERROR">
518 Error in "admonition" directive:
519 1 argument(s) required, 0 supplied.
520 <literal_block xml:space="preserve">
523 without title and content following a blank line
524 <section ids="section-underline-too-short" names="section\ underline\ too\ short">
526 section underline too short
527 <system_message level="2" line="36" source="%(source)s" type="WARNING">
529 Title underline too short.
530 <literal_block xml:space="preserve">
531 section underline too short
535 <colspec colwidth="14">
536 <colspec colwidth="6">
553 <system_message level="2" line="43" source="%(source)s" type="WARNING">
555 Blank line required after table.
557 No blank line after table.
558 <system_message level="3" line="45" source="%(source)s" type="ERROR">
560 Error in "unicode" directive:
561 1 argument(s) required, 0 supplied.
562 <literal_block xml:space="preserve">
564 <system_message level="2" line="45" source="%(source)s" type="WARNING">
566 Substitution definition "empty" empty or invalid.
567 <literal_block xml:space="preserve">
569 <system_message level="3" line="47" source="%(source)s" type="ERROR">
571 Error in "topic" directive:
572 1 argument(s) required, 0 supplied.
573 <literal_block xml:space="preserve">
575 <system_message level="3" line="49" source="%(source)s" type="ERROR">
577 Error in "rubric" directive:
578 1 argument(s) required, 0 supplied.
579 <literal_block xml:space="preserve">
582 A rubric has no content
583 <comment xml:space="preserve">
584 _`target: No matching backquote.
585 <system_message level="2" line="52" source="%(source)s" type="WARNING">
587 malformed hyperlink target.
588 <comment xml:space="preserve">
590 <system_message level="2" line="53" source="%(source)s" type="WARNING">
592 malformed hyperlink target.
594 <definition_list_item>
598 <system_message level="1" line="57" source="%(source)s" type="INFO">
600 Blank line missing before literal block (after the "::")? Interpreted as a definition list item.
602 with no blank line above.
603 <literal_block xml:space="preserve">
605 <system_message level="3" line="61" source="%(source)s" type="ERROR">
607 Inconsistent literal block quoting.
609 $ with inconsistent quoting.
611 <problematic ids="id3" refid="id2">
615 <problematic ids="id5" refid="id4">
618 <problematic ids="id7" refid="id6">
621 <problematic ids="id9" refid="id8">
624 <system_message level="1" line="63" source="%(source)s" type="INFO">
626 No role entry for "unknown-role" in module "docutils.parsers.rst.languages.en".
627 Trying "unknown-role" as canonical role name.
628 <system_message backrefs="id3" ids="id2" level="3" line="63" source="%(source)s" type="ERROR">
630 Unknown interpreted text role "unknown-role".
631 <system_message backrefs="id5" ids="id4" level="2" line="63" source="%(source)s" type="WARNING">
633 Inline emphasis start-string without end-string.
634 <system_message backrefs="id7" ids="id6" level="2" line="63" source="%(source)s" type="WARNING">
636 Inline interpreted text or phrase reference start-string without end-string.
637 <system_message backrefs="id9" ids="id8" level="2" line="63" source="%(source)s" type="WARNING">
639 Inline strong start-string without end-string.
641 <problematic ids="id11" refid="id10">
643 <system_message backrefs="id11" ids="id10" level="3" line="68" source="%(source)s" type="ERROR">
645 PEP number must be a number from 0 to 9999; "-1" is invalid.
646 <system_message level="1" line="66" source="%(source)s" type="INFO">
648 No directive entry for "unknown" in module "docutils.parsers.rst.languages.en".
649 Trying "unknown" as canonical directive name.
650 <system_message level="3" line="70" source="%(source)s" type="ERROR">
652 Unknown directive type "unknown".
653 <literal_block xml:space="preserve">
654 .. unknown:: directive (info still reported with wrong line)
655 <system_message level="3" line="72" source="%(source)s" type="ERROR">
658 No bottom table border found.
659 <literal_block xml:space="preserve">
660 ============== ======
663 """ % {'source': reldir(include10
), 'nonexistent': reldir(nonexistent
),
665 DocutilsTestSupport
.exception_data(unichr, int("11111111", 16))[2]
668 Include file with whitespace in the path:
671 """ % reldir(include11
),
673 <document source="test data">
675 Include file with whitespace in the path:
680 Standard include data file:
682 .. include:: <isogrk4.txt>
685 <document source="test data">
687 Standard include data file:
688 <comment xml:space="preserve">
689 This data file has been placed in the public domain.
690 <comment xml:space="preserve">
691 Derived from the Unicode character mappings available from
692 <http://www.w3.org/2003/entities/xml/>.
693 Processed by unicode2rstsubs.py, part of Docutils:
694 <http://docutils.sourceforge.net>.
695 <substitution_definition names="b.Gammad">
697 <substitution_definition names="b.gammad">
699 """).decode('raw_unicode_escape')],
701 Nonexistent standard include data file:
703 .. include:: <nonexistent>
706 <document source="test data">
708 Nonexistent standard include data file:
709 <system_message level="4" line="3" source="test data" type="SEVERE">
711 Problems with "include" directive path:
712 InputError: [Errno 2] No such file or directory: '%s'.
713 <literal_block xml:space="preserve">
714 .. include:: <nonexistent>
715 """ % nonexistent_rel
],
717 Include start-line/end-line Test
724 <document source="test data">
726 Include start-line/end-line Test
728 This file (include2.txt) is used by
731 Include start-line/end-line + start-after Test
738 Text search is limited to the specified lines.
741 <document source="test data">
743 Include start-line/end-line + start-after Test
745 In include12.txt (after "start here", before "stop here")
747 Text search is limited to the specified lines.
750 Include start-after/end-before Test
753 :start-after: .. start here
754 :end-before: .. stop here
759 <document source="test data">
761 Include start-after/end-before Test
763 In include12.txt (after "start here", before "stop here")
768 Include start-after/end-before Test, single option variant
771 :end-before: .. start here
774 :start-after: .. stop here
777 """ % (include12
, include12
),
779 <document source="test data">
781 Include start-after/end-before Test, single option variant
783 In include12.txt (but before "start here")
785 In include12.txt (after "stop here")
790 Include start-after/end-before multi-line test.
793 :start-after: From: me
799 :start-after: From: me
806 """ % (include13
, include13
),
808 <document source="test data">
810 Include start-after/end-before multi-line test.
811 <system_message level="4" line="3" source="test data" type="SEVERE">
813 Problem with "end-before" option of "include" directive:
815 <literal_block xml:space="preserve">
817 :start-after: From: me
822 In include13.txt (between header and signature)
827 Error handling test; "end-before" error handling tested in previous test.
830 :start-after: bad string
831 :end-before: mork of ork
834 <document source="test data">
836 Error handling test; "end-before" error handling tested in previous test.
837 <system_message level="4" line="3" source="test data" type="SEVERE">
839 Problem with "start-after" option of "include" directive:
841 <literal_block xml:space="preserve">
843 :start-after: bad string
844 :end-before: mork of ork
847 TAB expansion with literal include:
851 """ % include_literal
,
853 <document source="test data">
855 TAB expansion with literal include:
856 <literal_block source="%s" xml:space="preserve">
857 Literal included this should **not** be *marked* `up`.
863 """ % include_literal
],
865 Custom TAB expansion with literal include:
870 """ % include_literal
,
872 <document source="test data">
874 Custom TAB expansion with literal include:
875 <literal_block source="%s" xml:space="preserve">
876 Literal included this should **not** be *marked* `up`.
882 """ % include_literal
],
884 No TAB expansion with literal include:
889 """ % include_literal
,
891 <document source="test data">
893 No TAB expansion with literal include:
894 <literal_block source="%s" xml:space="preserve">
895 Literal included this should **not** be *marked* `up`.
896 \t<- leading raw tab.
901 """ % include_literal
],
904 totest
['include-code'] = [
912 <document source="test data">
915 <literal_block classes="code rst" source="%s" xml:space="preserve">
916 <inline classes="generic heading">
919 <inline classes="generic heading">
923 This file is used by \n\
924 <inline classes="literal string">
927 """ % reldir(include1
)],
936 <document source="test data">
939 <literal_block classes="code rst" source="%s" xml:space="preserve">
940 <inline classes="ln">
942 <inline classes="generic heading">
945 <inline classes="ln">
947 <inline classes="generic heading">
950 <inline classes="ln">
953 <inline classes="ln">
955 This file is used by \n\
956 <inline classes="literal string">
959 """ % reldir(include1
)],
962 if __name__
== '__main__':
964 unittest
.main(defaultTest
='suite')