Debian Codegraph @

test/test_parsers/test_rst/test_root_prefix.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_root_prefix.py 9906 2024-08-15 08:43:38Z grubert $
3 # Author: Günter Milde
4 # Maintainer: [email protected]
5 # :Copyright: 2022 Günter Milde,
6 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
7 #
8 # Copying and distribution of this file, with or without modification,
9 # are permitted in any medium without royalty provided the copyright
10 # notice and this notice are preserved.
11 # This file is offered as-is, without any warranty.
12 #
13 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
14
15 """Test configurable root directory for included files
16
17 Cf. feature-requests #91.
18 """
19
20 import os
21 from pathlib import Path
22 import sys
23 import unittest
24
25 if __name__ == '__main__':
26 # prepend the "docutils root" to the Python library path
27 # so we import the local `docutils` package.
28 sys.path.insert(0, str(Path(__file__).parents[3]))
29
30 from docutils.frontend import get_default_settings
31 from docutils.parsers.rst import Parser
32 from docutils.utils import new_document
33
34 ROOT_PREFIX = (Path(__file__).parent/'test_directives').as_posix()
35 REL_PREFIX = os.path.relpath(ROOT_PREFIX).replace('\\', '/')
36
37
38 class ParserTestCase(unittest.TestCase):
39
40 def test_parser(self):
41 parser = Parser()
42 settings = get_default_settings(Parser)
43 settings.warning_stream = ''
44 settings.root_prefix = ROOT_PREFIX
45 for name, cases in totest.items():
46 for casenum, (case_input, case_expected) in enumerate(cases):
47 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
48 document = new_document('test data', settings.copy())
49 parser.parse(case_input, document)
50 output = document.pformat()
51 self.assertEqual(output, case_expected)
52
53
54 totest = {}
55
56 totest['include-root'] = [
57 # locate included files using "include-root" setting value
58 ["""\
59 .. include:: /include 11.rst
60 """,
61 """\
62 <document source="test data">
63 <paragraph>
64 some text
65 """],
66 # Do we want to support a leading backslash to signify a path root?
67 # ["""\
68 # .. include:: \include 11.rst
69 # """,
70 # """\
71 # <document source="test data">
72 # <paragraph>
73 # some text
74 # """],
75 ["""\
76 .. include:: /include 11.rst
77 :parser: rst
78 """,
79 """\
80 <document source="test data">
81 <paragraph>
82 some text
83 """],
84 ["""\
85 .. include:: /include 11.rst
86 :literal:
87 """,
88 f"""\
89 <document source="test data">
90 <literal_block source="{REL_PREFIX}/include 11.rst" xml:space="preserve">
91 some text
92 """],
93 ["""\
94 .. include:: /include 11.rst
95 :code:
96 """,
97 f"""\
98 <document source="test data">
99 <literal_block classes="code" source="{REL_PREFIX}/include 11.rst" xml:space="preserve">
100 some text
101 """],
102 # search included CSV files using "include-root" setting value
103 ["""\
104 .. CSV-table::
105 :file: /include 11.rst
106 """,
107 """\
108 <document source="test data">
109 <table>
110 <tgroup cols="1">
111 <colspec colwidth="100">
112 <tbody>
113 <row>
114 <entry>
115 <paragraph>
116 some text
117 """],
118 ["""\
119 .. raw:: txt
120 :file: /include 11.rst
121 """,
122 f"""\
123 <document source="test data">
124 <raw format="txt" source="{REL_PREFIX}/include 11.rst" xml:space="preserve">
125 some text
126 """],
127 ]
128
129
130 if __name__ == '__main__':
131 unittest.main()