Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_contents.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_contents.py 10308 2026-03-28 18:54:25Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for parts.py contents directive.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
23
24
25 class ParserTestCase(unittest.TestCase):
26 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 settings.warning_stream = ''
30 settings.legacy_ids = False
31 for name, cases in totest.items():
32 for casenum, (case_input, case_expected) in enumerate(cases):
33 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
34 document = new_document('test data', settings.copy())
35 parser.parse(case_input, document)
36 output = document.pformat()
37 self.assertEqual(case_expected, output)
38
39
40 try:
41 int('two')
42 except ValueError as detail:
43 invalid_literal = detail.args[0]
44 else:
45 invalid_literal = ''
46
47 totest = {}
48
49 totest['contents'] = [
50 ["""\
51 .. contents::
52 """,
53 """\
54 <document source="test data">
55 <topic classes="contents" names="contents">
56 <title>
57 Contents
58 <pending>
59 .. internal attributes:
60 .transform: docutils.transforms.parts.Contents
61 .details:
62 """],
63 ["""\
64 .. contents:: Table of Contents
65 """,
66 """\
67 <document source="test data">
68 <topic classes="contents" names="table\\ of\\ contents">
69 <title>
70 Table of Contents
71 <pending>
72 .. internal attributes:
73 .transform: docutils.transforms.parts.Contents
74 .details:
75 """],
76 ["""\
77 .. contents::
78 Table of Contents
79 """,
80 """\
81 <document source="test data">
82 <topic classes="contents" names="table\\ of\\ contents">
83 <title>
84 Table of Contents
85 <pending>
86 .. internal attributes:
87 .transform: docutils.transforms.parts.Contents
88 .details:
89 """],
90 ["""\
91 .. contents:: Table
92 of
93 Contents
94 """,
95 """\
96 <document source="test data">
97 <topic classes="contents" names="table\\ of\\ contents">
98 <title>
99 Table
100 of
101 Contents
102 <pending>
103 .. internal attributes:
104 .transform: docutils.transforms.parts.Contents
105 .details:
106 """],
107 ["""\
108 .. contents:: *Table* of ``Contents``
109 """,
110 """\
111 <document source="test data">
112 <topic classes="contents" names="table\\ of\\ contents">
113 <title>
114 <emphasis>
115 Table
116 of \n\
117 <literal>
118 Contents
119 <pending>
120 .. internal attributes:
121 .transform: docutils.transforms.parts.Contents
122 .details:
123 """],
124 ["""\
125 .. contents::
126 :depth: 2
127 :local:
128 """,
129 """\
130 <document source="test data">
131 <topic classes="contents local" names="contents">
132 <pending>
133 .. internal attributes:
134 .transform: docutils.transforms.parts.Contents
135 .details:
136 depth: 2
137 local: None
138 """],
139 ["""\
140 .. contents::
141 :local: arg
142 """,
143 """\
144 <document source="test data">
145 <system_message level="3" line="1" source="test data" type="ERROR">
146 <paragraph>
147 Error in "contents" directive:
148 invalid option value: (option: "local"; value: 'arg')
149 no argument is allowed; "arg" supplied.
150 <literal_block xml:space="preserve">
151 .. contents::
152 :local: arg
153 """],
154 ["""\
155 .. contents:: Table of Contents
156 :local:
157 :depth: 2
158 :backlinks: none
159 """,
160 """\
161 <document source="test data">
162 <topic classes="contents local" names="table\\ of\\ contents">
163 <title>
164 Table of Contents
165 <pending>
166 .. internal attributes:
167 .transform: docutils.transforms.parts.Contents
168 .details:
169 backlinks: None
170 depth: 2
171 local: None
172 """],
173 ["""\
174 .. contents::
175 :depth: two
176 """,
177 """\
178 <document source="test data">
179 <system_message level="3" line="1" source="test data" type="ERROR">
180 <paragraph>
181 Error in "contents" directive:
182 invalid option value: (option: "depth"; value: 'two')
183 %s.
184 <literal_block xml:space="preserve">
185 .. contents::
186 :depth: two
187 """ % invalid_literal],
188 ["""\
189 .. contents::
190 :width: 2
191 """,
192 """\
193 <document source="test data">
194 <system_message level="3" line="1" source="test data" type="ERROR">
195 <paragraph>
196 Error in "contents" directive:
197 unknown option: "width".
198 <literal_block xml:space="preserve">
199 .. contents::
200 :width: 2
201 """],
202 ["""\
203 .. contents::
204 :backlinks: no way!
205 """,
206 """\
207 <document source="test data">
208 <system_message level="3" line="1" source="test data" type="ERROR">
209 <paragraph>
210 Error in "contents" directive:
211 invalid option value: (option: "backlinks"; value: 'no way!')
212 "no way!" unknown; choose from "top", "entry", or "none".
213 <literal_block xml:space="preserve">
214 .. contents::
215 :backlinks: no way!
216 """],
217 ["""\
218 .. contents::
219 :backlinks:
220 """,
221 """\
222 <document source="test data">
223 <system_message level="3" line="1" source="test data" type="ERROR">
224 <paragraph>
225 Error in "contents" directive:
226 invalid option value: (option: "backlinks"; value: None)
227 must supply an argument; choose from "top", "entry", or "none".
228 <literal_block xml:space="preserve">
229 .. contents::
230 :backlinks:
231 """],
232 ["""\
233 * .. contents::
234 """,
235 """\
236 <document source="test data">
237 <bullet_list bullet="*">
238 <list_item>
239 <system_message level="3" line="1" source="test data" type="ERROR">
240 <paragraph>
241 The "contents" directive may not be used within topics or body elements.
242 <literal_block xml:space="preserve">
243 .. contents::
244 """],
245 ["""\
246 .. sidebar:: containing contents
247
248 .. contents::
249 """,
250 """\
251 <document source="test data">
252 <sidebar>
253 <title>
254 containing contents
255 <topic classes="contents" names="contents">
256 <title>
257 Contents
258 <pending>
259 .. internal attributes:
260 .transform: docutils.transforms.parts.Contents
261 .details:
262 """],
263 ]
264
265
266 if __name__ == '__main__':
267 unittest.main()