Debian Codegraph @

tests/test_domains/test_domain_std.py

raw
1 """Tests the std domain"""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6 from unittest import mock
7
8 import pytest
9 from docutils import nodes
10 from docutils.nodes import definition, definition_list, definition_list_item, term
11
12 from sphinx import addnodes
13 from sphinx.addnodes import (
14 desc,
15 desc_addname,
16 desc_content,
17 desc_name,
18 desc_signature,
19 glossary,
20 index,
21 pending_xref,
22 )
23 from sphinx.domains.std import StandardDomain
24 from sphinx.testing import restructuredtext
25 from sphinx.testing.util import assert_node
26
27 from tests.utils import extract_node
28
29 if TYPE_CHECKING:
30 from sphinx.testing.util import SphinxTestApp
31
32
33 def test_process_doc_handle_figure_caption() -> None:
34 env = mock.Mock(domaindata={})
35 env._registry.enumerable_nodes = {}
36 figure_node = nodes.figure(
37 '',
38 nodes.caption('caption text', 'caption text'),
39 )
40 document = mock.Mock(
41 nametypes={'testname': True},
42 nameids={'testname': 'testid'},
43 ids={'testid': figure_node},
44 citation_refs={},
45 )
46 document.findall.return_value = []
47
48 domain = StandardDomain(env)
49 if 'testname' in domain.data['labels']:
50 del domain.data['labels']['testname']
51 domain.process_doc(env, 'testdoc', document)
52 assert 'testname' in domain.data['labels']
53 assert domain.data['labels']['testname'] == ('testdoc', 'testid', 'caption text')
54
55
56 def test_process_doc_handle_table_title() -> None:
57 env = mock.Mock(domaindata={})
58 env._registry.enumerable_nodes = {}
59 table_node = nodes.table(
60 '',
61 nodes.title('title text', 'title text'),
62 )
63 document = mock.Mock(
64 nametypes={'testname': True},
65 nameids={'testname': 'testid'},
66 ids={'testid': table_node},
67 citation_refs={},
68 )
69 document.findall.return_value = []
70
71 domain = StandardDomain(env)
72 if 'testname' in domain.data['labels']:
73 del domain.data['labels']['testname']
74 domain.process_doc(env, 'testdoc', document)
75 assert 'testname' in domain.data['labels']
76 assert domain.data['labels']['testname'] == ('testdoc', 'testid', 'title text')
77
78
79 def test_get_full_qualified_name() -> None:
80 env = mock.Mock(domaindata={})
81 env._registry.enumerable_nodes = {}
82 domain = StandardDomain(env)
83
84 # normal references
85 node = nodes.reference()
86 assert domain.get_full_qualified_name(node) is None
87
88 # simple reference to options
89 node = nodes.reference(reftype='option', reftarget='-l')
90 assert domain.get_full_qualified_name(node) is None
91
92 # options with std:program context
93 kwargs = {'std:program': 'ls'}
94 node = nodes.reference(reftype='option', reftarget='-l', **kwargs)
95 assert domain.get_full_qualified_name(node) == 'ls.-l'
96
97
98 @pytest.mark.sphinx('html', testroot='_blank')
99 def test_cmd_option_with_optional_value(app: SphinxTestApp) -> None:
100 text = '.. option:: -j[=N]'
101 doctree = restructuredtext.parse(app, text)
102 assert_node(
103 doctree,
104 (
105 index,
106 [
107 desc,
108 (
109 [desc_signature, ([desc_name, '-j'], [desc_addname, '[=N]'])],
110 [desc_content, ()],
111 ),
112 ],
113 ),
114 )
115 assert_node(
116 doctree[0],
117 addnodes.index,
118 entries=[('pair', 'command line option; -j', 'cmdoption-j', '', None)],
119 )
120
121 objects = list(app.env.domains.standard_domain.get_objects())
122 assert ('-j', '-j', 'cmdoption', 'index', 'cmdoption-j', 1) in objects
123
124
125 @pytest.mark.sphinx('html', testroot='_blank')
126 def test_cmd_option_starting_with_bracket(app: SphinxTestApp) -> None:
127 text = '.. option:: [enable=]PATTERN'
128 doctree = restructuredtext.parse(app, text)
129 assert_node(
130 doctree,
131 (
132 index,
133 [
134 desc,
135 (
136 [
137 desc_signature,
138 ([desc_name, '[enable'], [desc_addname, '=]PATTERN']),
139 ],
140 [desc_content, ()],
141 ),
142 ],
143 ),
144 )
145 objects = list(app.env.domains.standard_domain.get_objects())
146 assert (
147 '[enable',
148 '[enable',
149 'cmdoption',
150 'index',
151 'cmdoption-arg-enable',
152 1,
153 ) in objects
154
155
156 @pytest.mark.sphinx('html', testroot='_blank')
157 def test_glossary(app):
158 text = (
159 '.. glossary::\n'
160 '\n'
161 ' term1\n'
162 ' TERM2\n'
163 ' description\n'
164 '\n'
165 ' term3 : classifier\n'
166 ' description\n'
167 ' description\n'
168 '\n'
169 ' term4 : class1 : class2\n'
170 ' description\n'
171 )
172
173 # doctree
174 doctree = restructuredtext.parse(app, text)
175 assert_node(
176 doctree,
177 (
178 [
179 glossary,
180 definition_list,
181 (
182 [
183 definition_list_item,
184 (
185 [term, ('term1', index)],
186 [term, ('TERM2', index)],
187 definition,
188 ),
189 ],
190 [definition_list_item, ([term, ('term3', index)], definition)],
191 [definition_list_item, ([term, ('term4', index)], definition)],
192 ),
193 ],
194 ),
195 )
196 assert_node(
197 extract_node(doctree, 0, 0, 0, 0, 1),
198 entries=[('single', 'term1', 'term-term1', 'main', None)],
199 )
200 assert_node(
201 extract_node(doctree, 0, 0, 0, 1, 1),
202 entries=[('single', 'TERM2', 'term-TERM2', 'main', None)],
203 )
204 assert_node(
205 extract_node(doctree, 0, 0, 0, 2), [definition, nodes.paragraph, 'description']
206 )
207 assert_node(
208 extract_node(doctree, 0, 0, 1, 0, 1),
209 entries=[('single', 'term3', 'term-term3', 'main', 'classifier')],
210 )
211 assert_node(
212 extract_node(doctree, 0, 0, 1, 1),
213 [definition, nodes.paragraph, 'description\ndescription'],
214 )
215 assert_node(
216 extract_node(doctree, 0, 0, 2, 0, 1),
217 entries=[('single', 'term4', 'term-term4', 'main', 'class1')],
218 )
219 assert_node(
220 extract_node(doctree, 0, 0, 2, 1),
221 [nodes.definition, nodes.paragraph, 'description'],
222 )
223
224 # index
225 domain = app.env.domains.standard_domain
226 objects = list(domain.get_objects())
227 assert ('term1', 'term1', 'term', 'index', 'term-term1', -1) in objects
228 assert ('TERM2', 'TERM2', 'term', 'index', 'term-TERM2', -1) in objects
229 assert ('term3', 'term3', 'term', 'index', 'term-term3', -1) in objects
230 assert ('term4', 'term4', 'term', 'index', 'term-term4', -1) in objects
231
232 # term reference (case sensitive)
233 refnode = domain.resolve_xref(
234 app.env,
235 'index',
236 app.builder,
237 'term',
238 'term1',
239 pending_xref(),
240 nodes.paragraph(),
241 )
242 assert_node(refnode, nodes.reference, refid='term-term1')
243
244 # term reference (case insensitive)
245 refnode = domain.resolve_xref(
246 app.env,
247 'index',
248 app.builder,
249 'term',
250 'term2',
251 pending_xref(),
252 nodes.paragraph(),
253 )
254 assert_node(refnode, nodes.reference, refid='term-TERM2')
255
256
257 @pytest.mark.sphinx('html', testroot='_blank')
258 def test_glossary_warning(app: SphinxTestApp) -> None:
259 # empty line between terms
260 text = '.. glossary::\n\n term1\n\n term2\n'
261 restructuredtext.parse(app, text, 'case1')
262 assert (
263 'case1.rst:4: WARNING: glossary terms must not be separated by empty lines'
264 ) in app.warning.getvalue()
265
266 # glossary starts with indented item
267 text = '.. glossary::\n\n description\n term\n'
268 restructuredtext.parse(app, text, 'case2')
269 assert (
270 'case2.rst:3: WARNING: glossary term must be preceded by empty line'
271 ) in app.warning.getvalue()
272
273 # empty line between terms
274 text = '.. glossary::\n\n term1\n description\n term2\n'
275 restructuredtext.parse(app, text, 'case3')
276 assert (
277 'case3.rst:4: WARNING: glossary term must be preceded by empty line'
278 ) in app.warning.getvalue()
279
280 # duplicated terms
281 text = '.. glossary::\n\n term-case4\n term-case4\n'
282 restructuredtext.parse(app, text, 'case4')
283 assert (
284 'case4.rst:3: WARNING: duplicate term description of term-case4, '
285 'other instance in case4'
286 ) in app.warning.getvalue()
287
288
289 @pytest.mark.sphinx('html', testroot='_blank')
290 def test_glossary_comment(app):
291 text = (
292 '.. glossary::\n'
293 '\n'
294 ' term1\n'
295 ' description\n'
296 ' .. term2\n'
297 ' description\n'
298 ' description\n'
299 )
300 doctree = restructuredtext.parse(app, text)
301 assert_node(
302 doctree,
303 (
304 [
305 glossary,
306 definition_list,
307 definition_list_item,
308 ([term, ('term1', index)], definition),
309 ],
310 ),
311 )
312 assert_node(
313 extract_node(doctree, 0, 0, 0, 1),
314 [nodes.definition, nodes.paragraph, 'description'],
315 )
316
317
318 @pytest.mark.sphinx('html', testroot='_blank')
319 def test_glossary_comment2(app):
320 text = (
321 '.. glossary::\n'
322 '\n'
323 ' term1\n'
324 ' description\n'
325 '\n'
326 ' .. term2\n'
327 ' term3\n'
328 ' description\n'
329 ' description\n'
330 )
331 doctree = restructuredtext.parse(app, text)
332 assert_node(
333 doctree,
334 (
335 [
336 glossary,
337 definition_list,
338 (
339 [definition_list_item, ([term, ('term1', index)], definition)],
340 [definition_list_item, ([term, ('term3', index)], definition)],
341 ),
342 ],
343 ),
344 )
345 assert_node(
346 extract_node(doctree, 0, 0, 0, 1),
347 [nodes.definition, nodes.paragraph, 'description'],
348 )
349 assert_node(
350 extract_node(doctree, 0, 0, 1, 1),
351 [nodes.definition, nodes.paragraph, 'description\ndescription'],
352 )
353
354
355 @pytest.mark.sphinx('html', testroot='_blank')
356 def test_glossary_sorted(app):
357 text = (
358 '.. glossary::\n'
359 ' :sorted:\n'
360 '\n'
361 ' term3\n'
362 ' description\n'
363 '\n'
364 ' term2\n'
365 ' term1\n'
366 ' description\n'
367 )
368 doctree = restructuredtext.parse(app, text)
369 assert_node(
370 doctree,
371 (
372 [
373 glossary,
374 definition_list,
375 (
376 [
377 definition_list_item,
378 (
379 [term, ('term2', index)],
380 [term, ('term1', index)],
381 definition,
382 ),
383 ],
384 [definition_list_item, ([term, ('term3', index)], definition)],
385 ),
386 ],
387 ),
388 )
389 assert_node(
390 extract_node(doctree, 0, 0, 0, 2),
391 [nodes.definition, nodes.paragraph, 'description'],
392 )
393 assert_node(
394 extract_node(doctree, 0, 0, 1, 1),
395 [nodes.definition, nodes.paragraph, 'description'],
396 )
397
398
399 @pytest.mark.sphinx('html', testroot='_blank')
400 def test_glossary_alphanumeric(app: SphinxTestApp) -> None:
401 text = '.. glossary::\n\n 1\n /\n'
402 restructuredtext.parse(app, text)
403 objects = list(app.env.domains.standard_domain.get_objects())
404 assert ('1', '1', 'term', 'index', 'term-1', -1) in objects
405 assert ('/', '/', 'term', 'index', 'term-0', -1) in objects
406
407
408 @pytest.mark.sphinx('html', testroot='_blank')
409 def test_glossary_conflicted_labels(app: SphinxTestApp) -> None:
410 text = '.. _term-foo:\n.. glossary::\n\n foo\n'
411 restructuredtext.parse(app, text)
412 objects = list(app.env.domains.standard_domain.get_objects())
413 assert ('foo', 'foo', 'term', 'index', 'term-0', -1) in objects
414
415
416 @pytest.mark.sphinx('html', testroot='_blank')
417 def test_cmdoption(app: SphinxTestApp) -> None:
418 text = '.. program:: ls\n\n.. option:: -l\n'
419 domain = app.env.domains.standard_domain
420 doctree = restructuredtext.parse(app, text)
421 assert_node(
422 doctree,
423 (
424 addnodes.index,
425 [
426 desc,
427 (
428 [desc_signature, ([desc_name, '-l'], [desc_addname, ()])],
429 [desc_content, ()],
430 ),
431 ],
432 ),
433 )
434 assert_node(
435 doctree[0],
436 addnodes.index,
437 entries=[('pair', 'ls command line option; -l', 'cmdoption-ls-l', '', None)],
438 )
439 assert ('ls', '-l') in domain.progoptions
440 assert domain.progoptions['ls', '-l'] == ('index', 'cmdoption-ls-l')
441
442
443 @pytest.mark.sphinx('html', testroot='_blank')
444 def test_cmdoption_for_None(app: SphinxTestApp) -> None:
445 text = '.. program:: ls\n.. program:: None\n\n.. option:: -l\n'
446 domain = app.env.domains.standard_domain
447 doctree = restructuredtext.parse(app, text)
448 assert_node(
449 doctree,
450 (
451 addnodes.index,
452 [
453 desc,
454 (
455 [desc_signature, ([desc_name, '-l'], [desc_addname, ()])],
456 [desc_content, ()],
457 ),
458 ],
459 ),
460 )
461 assert_node(
462 doctree[0],
463 addnodes.index,
464 entries=[('pair', 'command line option; -l', 'cmdoption-l', '', None)],
465 )
466 assert (None, '-l') in domain.progoptions
467 assert domain.progoptions[None, '-l'] == ('index', 'cmdoption-l')
468
469
470 @pytest.mark.sphinx('html', testroot='_blank')
471 def test_multiple_cmdoptions(app: SphinxTestApp) -> None:
472 text = '.. program:: cmd\n\n.. option:: -o directory, --output directory\n'
473 domain = app.env.domains.standard_domain
474 doctree = restructuredtext.parse(app, text)
475 assert_node(
476 doctree,
477 (
478 addnodes.index,
479 [
480 desc,
481 (
482 [
483 desc_signature,
484 (
485 [desc_name, '-o'],
486 [desc_addname, ' directory'],
487 [desc_addname, ', '],
488 [desc_name, '--output'],
489 [desc_addname, ' directory'],
490 ),
491 ],
492 [desc_content, ()],
493 ),
494 ],
495 ),
496 )
497 assert_node(
498 doctree[0],
499 addnodes.index,
500 entries=[
501 ('pair', 'cmd command line option; -o', 'cmdoption-cmd-o', '', None),
502 ('pair', 'cmd command line option; --output', 'cmdoption-cmd-o', '', None),
503 ],
504 )
505 assert ('cmd', '-o') in domain.progoptions
506 assert ('cmd', '--output') in domain.progoptions
507 assert domain.progoptions['cmd', '-o'] == ('index', 'cmdoption-cmd-o')
508 assert domain.progoptions['cmd', '--output'] == ('index', 'cmdoption-cmd-o')
509
510
511 @pytest.mark.sphinx('html', testroot='_blank')
512 def test_disabled_docref(app: SphinxTestApp) -> None:
513 text = ':doc:`index`\n:doc:`!index`\n'
514 doctree = restructuredtext.parse(app, text)
515 assert_node(
516 doctree,
517 (
518 [
519 nodes.paragraph,
520 ([pending_xref, nodes.inline, 'index'], '\n', [nodes.inline, 'index']),
521 ],
522 ),
523 )
524
525
526 @pytest.mark.sphinx('html', testroot='_blank')
527 def test_labeled_rubric(app: SphinxTestApp) -> None:
528 text = '.. _label:\n.. rubric:: blah *blah* blah\n'
529 restructuredtext.parse(app, text)
530
531 domain = app.env.domains.standard_domain
532 assert 'label' in domain.labels
533 assert domain.labels['label'] == ('index', 'label', 'blah blah blah')
534
535
536 @pytest.mark.sphinx('html', testroot='_blank')
537 def test_labeled_definition(app: SphinxTestApp) -> None:
538 text = (
539 '.. _label1:\n'
540 '\n'
541 'Foo blah *blah* blah\n'
542 ' Definition\n'
543 '\n'
544 '.. _label2:\n'
545 '\n'
546 'Bar blah *blah* blah\n'
547 ' Definition\n'
548 '\n'
549 )
550 restructuredtext.parse(app, text)
551
552 domain = app.env.domains.standard_domain
553 assert 'label1' in domain.labels
554 assert domain.labels['label1'] == ('index', 'label1', 'Foo blah blah blah')
555 assert 'label2' in domain.labels
556 assert domain.labels['label2'] == ('index', 'label2', 'Bar blah blah blah')
557
558
559 @pytest.mark.sphinx('html', testroot='_blank')
560 def test_labeled_field(app: SphinxTestApp) -> None:
561 text = (
562 '.. _label1:\n'
563 '\n'
564 ':Foo blah *blah* blah:\n'
565 ' Definition\n'
566 '\n'
567 '.. _label2:\n'
568 '\n'
569 ':Bar blah *blah* blah:\n'
570 ' Definition\n'
571 '\n'
572 )
573 restructuredtext.parse(app, text)
574
575 domain = app.env.domains.standard_domain
576 assert 'label1' in domain.labels
577 assert domain.labels['label1'] == ('index', 'label1', 'Foo blah blah blah')
578 assert 'label2' in domain.labels
579 assert domain.labels['label2'] == ('index', 'label2', 'Bar blah blah blah')
580
581
582 @pytest.mark.sphinx(
583 'html',
584 testroot='manpage_url',
585 confoverrides={'manpages_url': 'https://example.com/{page}.{section}'},
586 )
587 def test_html_manpage(app: SphinxTestApp) -> None:
588 app.build(force_all=True)
589
590 content = (app.outdir / 'index.html').read_text(encoding='utf8')
591 assert (
592 '<em class="manpage">'
593 '<a class="manpage reference external" href="https://example.com/man.1">man(1)</a>'
594 '</em>'
595 ) in content
596 assert (
597 '<em class="manpage">'
598 '<a class="manpage reference external" href="https://example.com/ls.1">ls.1</a>'
599 '</em>'
600 ) in content
601 assert (
602 '<em class="manpage">'
603 '<a class="manpage reference external" href="https://example.com/sphinx.">sphinx</a>'
604 '</em>'
605 ) in content
606 assert (
607 '<em class="manpage">'
608 '<a class="manpage reference external" href="https://example.com/bsd-mailx/mailx.1">mailx(1)</a>'
609 '</em>'
610 ) in content
611 assert '<em class="manpage">man(1)</em>' in content