Debian Codegraph @

tests/test_environment/test_environment_indexentries.py

raw
1 """Test the sphinx.environment.adapters.indexentries."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from sphinx.environment.adapters.indexentries import IndexEntries
10 from sphinx.testing import restructuredtext
11
12 if TYPE_CHECKING:
13 from sphinx.testing.util import SphinxTestApp
14
15
16 @pytest.mark.sphinx(
17 'dummy',
18 testroot='root',
19 freshenv=True,
20 )
21 def test_create_single_index(app: SphinxTestApp) -> None:
22 text = (
23 '.. index:: docutils\n'
24 '.. index:: Python\n'
25 '.. index:: pip; install\n'
26 '.. index:: pip; upgrade\n'
27 '.. index:: Sphinx\n'
28 '.. index:: Ель\n'
29 '.. index:: ёлка\n'
30 '.. index:: \N{RIGHT-TO-LEFT MARK}\u05e2\u05d1\u05e8\u05d9\u05ea\N{LEFT-TO-RIGHT MARK}\n'
31 '.. index:: 9-symbol\n'
32 '.. index:: &-symbol\n'
33 '.. index:: £100\n'
34 )
35 restructuredtext.parse(app, text)
36 index = IndexEntries(app.env).create_index(app.builder)
37 assert len(index) == 6
38 assert index[0] == (
39 'Symbols',
40 [
41 ('&-symbol', ([('', '#index-9')], [], None)),
42 ('9-symbol', ([('', '#index-8')], [], None)),
43 ('£100', ([('', '#index-10')], [], None)),
44 ],
45 )
46 assert index[1] == ('D', [('docutils', ([('', '#index-0')], [], None))])
47 assert index[2] == (
48 'P',
49 [
50 (
51 'pip',
52 (
53 [],
54 [('install', [('', '#index-2')]), ('upgrade', [('', '#index-3')])],
55 None,
56 ),
57 ),
58 ('Python', ([('', '#index-1')], [], None)),
59 ],
60 )
61 assert index[3] == ('S', [('Sphinx', ([('', '#index-4')], [], None))])
62 assert index[4] == (
63 'Е',
64 [
65 ('ёлка', ([('', '#index-6')], [], None)),
66 ('Ель', ([('', '#index-5')], [], None)),
67 ],
68 )
69 # Here the word starts with U+200F RIGHT-TO-LEFT MARK, which should be
70 # ignored when getting the first letter.
71 assert index[5] == (
72 '\u05e2',
73 [
74 (
75 '\N{RIGHT-TO-LEFT MARK}\u05e2\u05d1\u05e8\u05d9\u05ea\N{LEFT-TO-RIGHT MARK}',
76 ([('', '#index-7')], [], None),
77 )
78 ],
79 )
80
81
82 @pytest.mark.sphinx(
83 'dummy',
84 testroot='root',
85 freshenv=True,
86 )
87 def test_create_pair_index(app: SphinxTestApp) -> None:
88 text = (
89 '.. index:: pair: docutils; reStructuredText\n'
90 '.. index:: pair: Python; interpreter\n'
91 '.. index:: pair: Sphinx; documentation tool\n'
92 '.. index:: pair: Sphinx; :+1:\n'
93 '.. index:: pair: Sphinx; Ель\n'
94 '.. index:: pair: Sphinx; ёлка\n'
95 )
96 restructuredtext.parse(app, text)
97 index = IndexEntries(app.env).create_index(app.builder)
98 assert len(index) == 7
99 assert index[0] == (
100 'Symbols',
101 [(':+1:', ([], [('Sphinx', [('', '#index-3')])], None))],
102 )
103 assert index[1] == (
104 'D',
105 [
106 ('documentation tool', ([], [('Sphinx', [('', '#index-2')])], None)),
107 ('docutils', ([], [('reStructuredText', [('', '#index-0')])], None)),
108 ],
109 )
110 assert index[2] == (
111 'I',
112 [('interpreter', ([], [('Python', [('', '#index-1')])], None))],
113 )
114 assert index[3] == (
115 'P',
116 [('Python', ([], [('interpreter', [('', '#index-1')])], None))],
117 )
118 assert index[4] == (
119 'R',
120 [('reStructuredText', ([], [('docutils', [('', '#index-0')])], None))],
121 )
122 assert index[5] == (
123 'S',
124 [
125 (
126 'Sphinx',
127 (
128 [],
129 [
130 (':+1:', [('', '#index-3')]),
131 ('documentation tool', [('', '#index-2')]),
132 ('ёлка', [('', '#index-5')]),
133 ('Ель', [('', '#index-4')]),
134 ],
135 None,
136 ),
137 )
138 ],
139 )
140 assert index[6] == (
141 'Е',
142 [
143 ('ёлка', ([], [('Sphinx', [('', '#index-5')])], None)),
144 ('Ель', ([], [('Sphinx', [('', '#index-4')])], None)),
145 ],
146 )
147
148
149 @pytest.mark.sphinx(
150 'dummy',
151 testroot='root',
152 freshenv=True,
153 )
154 def test_create_triple_index(app: SphinxTestApp) -> None:
155 text = '.. index:: triple: foo; bar; baz\n.. index:: triple: Python; Sphinx; reST\n'
156 restructuredtext.parse(app, text)
157 index = IndexEntries(app.env).create_index(app.builder)
158 assert len(index) == 5
159 assert index[0] == (
160 'B',
161 [
162 ('bar', ([], [('baz, foo', [('', '#index-0')])], None)),
163 ('baz', ([], [('foo bar', [('', '#index-0')])], None)),
164 ],
165 )
166 assert index[1] == ('F', [('foo', ([], [('bar baz', [('', '#index-0')])], None))])
167 assert index[2] == (
168 'P',
169 [('Python', ([], [('Sphinx reST', [('', '#index-1')])], None))],
170 )
171 assert index[3] == (
172 'R',
173 [('reST', ([], [('Python Sphinx', [('', '#index-1')])], None))],
174 )
175 assert index[4] == (
176 'S',
177 [('Sphinx', ([], [('reST, Python', [('', '#index-1')])], None))],
178 )
179
180
181 @pytest.mark.sphinx(
182 'dummy',
183 testroot='root',
184 freshenv=True,
185 )
186 def test_create_see_index(app: SphinxTestApp) -> None:
187 text = (
188 '.. index:: see: docutils; reStructuredText\n'
189 '.. index:: see: Python; interpreter\n'
190 '.. index:: see: Sphinx; documentation tool\n'
191 )
192 restructuredtext.parse(app, text)
193 index = IndexEntries(app.env).create_index(app.builder)
194 assert len(index) == 3
195 assert index[0] == ('D', [('docutils', ([], [('see reStructuredText', [])], None))])
196 assert index[1] == ('P', [('Python', ([], [('see interpreter', [])], None))])
197 assert index[2] == ('S', [('Sphinx', ([], [('see documentation tool', [])], None))])
198
199
200 @pytest.mark.sphinx(
201 'dummy',
202 testroot='root',
203 freshenv=True,
204 )
205 def test_create_seealso_index(app: SphinxTestApp) -> None:
206 text = (
207 '.. index:: seealso: docutils; reStructuredText\n'
208 '.. index:: seealso: Python; interpreter\n'
209 '.. index:: seealso: Sphinx; documentation tool\n'
210 )
211 restructuredtext.parse(app, text)
212 index = IndexEntries(app.env).create_index(app.builder)
213 assert len(index) == 3
214 assert index[0] == (
215 'D',
216 [('docutils', ([], [('see also reStructuredText', [])], None))],
217 )
218 assert index[1] == ('P', [('Python', ([], [('see also interpreter', [])], None))])
219 assert index[2] == (
220 'S',
221 [('Sphinx', ([], [('see also documentation tool', [])], None))],
222 )
223
224
225 @pytest.mark.sphinx(
226 'dummy',
227 testroot='root',
228 freshenv=True,
229 )
230 def test_create_main_index(app: SphinxTestApp) -> None:
231 text = (
232 '.. index:: !docutils\n'
233 '.. index:: docutils\n'
234 '.. index:: pip; install\n'
235 '.. index:: !pip; install\n'
236 )
237 restructuredtext.parse(app, text)
238 index = IndexEntries(app.env).create_index(app.builder)
239 assert len(index) == 2
240 assert index[0] == (
241 'D',
242 [('docutils', ([('main', '#index-0'), ('', '#index-1')], [], None))],
243 )
244 assert index[1] == (
245 'P',
246 [('pip', ([], [('install', [('main', '#index-3'), ('', '#index-2')])], None))],
247 )
248
249
250 @pytest.mark.sphinx(
251 'dummy',
252 testroot='root',
253 freshenv=True,
254 )
255 def test_create_index_with_name(app: SphinxTestApp) -> None:
256 text = (
257 '.. index:: single: docutils\n'
258 ' :name: ref1\n'
259 '.. index:: single: Python\n'
260 ' :name: ref2\n'
261 '.. index:: Sphinx\n'
262 )
263 restructuredtext.parse(app, text)
264 index = IndexEntries(app.env).create_index(app.builder)
265
266 # check index is created correctly
267 assert len(index) == 3
268 assert index[0] == ('D', [('docutils', ([('', '#ref1')], [], None))])
269 assert index[1] == ('P', [('Python', ([('', '#ref2')], [], None))])
270 assert index[2] == ('S', [('Sphinx', ([('', '#index-0')], [], None))])
271
272 # check the reference labels are created correctly
273 std = app.env.domains.standard_domain
274 assert std.anonlabels['ref1'] == ('index', 'ref1')
275 assert std.anonlabels['ref2'] == ('index', 'ref2')
276
277
278 @pytest.mark.sphinx(
279 'dummy',
280 testroot='root',
281 freshenv=True,
282 )
283 def test_create_index_by_key(app: SphinxTestApp) -> None:
284 # At present, only glossary directive is able to create index key
285 text = '.. glossary::\n\n docutils\n Python\n スフィンクス : ス\n'
286 restructuredtext.parse(app, text)
287 index = IndexEntries(app.env).create_index(app.builder)
288 assert len(index) == 3
289 assert index[0] == ('D', [('docutils', ([('main', '#term-docutils')], [], None))])
290 assert index[1] == ('P', [('Python', ([('main', '#term-Python')], [], None))])
291 assert index[2] == ('ス', [('スフィンクス', ([('main', '#term-0')], [], 'ス'))])