Debian Codegraph @

tests/test_environment/test_environment_toctree.py

raw
1 """Test the sphinx.environment.adapters.toctree."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import docutils
8 import pytest
9 from docutils import nodes
10 from docutils.nodes import bullet_list, list_item, literal, reference, title
11
12 from sphinx import addnodes
13 from sphinx.addnodes import compact_paragraph, only
14 from sphinx.builders.html import StandaloneHTMLBuilder
15 from sphinx.environment.adapters.toctree import (
16 _toctree_copy,
17 document_toc,
18 global_toctree_for_doc,
19 )
20 from sphinx.testing.util import assert_node
21 from sphinx.util.tags import Tags
22
23 from tests.utils import extract_node
24
25 if TYPE_CHECKING:
26 from sphinx.testing.util import SphinxTestApp
27
28
29 @pytest.mark.sphinx('xml', testroot='toctree')
30 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
31 def test_process_doc(app):
32 app.build()
33 # tocs
34 toctree = app.env.tocs['index']
35 assert_node(
36 toctree,
37 [
38 bullet_list,
39 (
40 [
41 list_item, # [0]
42 (
43 compact_paragraph, # [0][0]
44 [
45 bullet_list, # [0][1]
46 (
47 addnodes.toctree, # [0][1][0]
48 only, # [0][1][1]
49 list_item, # [0][1][2]
50 ),
51 ],
52 ),
53 ],
54 [
55 list_item, # [1]
56 (
57 compact_paragraph, # [1][0]
58 [
59 bullet_list, # [1][1]
60 (
61 addnodes.toctree, # [1][1][0]
62 addnodes.toctree, # [1][1][1]
63 ),
64 ],
65 ),
66 ],
67 list_item, # [2]
68 ),
69 ],
70 )
71
72 assert_node(
73 extract_node(toctree, 0, 0),
74 [compact_paragraph, reference, 'Welcome to Sphinx Tests’s documentation!'],
75 )
76 assert_node(extract_node(toctree, 0, 0, 0), reference, anchorname='')
77 assert_node(
78 extract_node(toctree, 0, 1, 0),
79 addnodes.toctree,
80 caption='Table of Contents',
81 glob=False,
82 hidden=False,
83 titlesonly=False,
84 maxdepth=2,
85 numbered=999,
86 entries=[
87 (None, 'foo'),
88 (None, 'bar'),
89 (None, 'https://sphinx-doc.org/'),
90 (None, 'self'),
91 ],
92 includefiles=['foo', 'bar'],
93 )
94
95 # only branch
96 assert_node(extract_node(toctree, 0, 1, 1), addnodes.only, expr='html')
97 assert_node(
98 extract_node(toctree, 0, 1, 1),
99 [
100 only,
101 list_item,
102 (
103 [compact_paragraph, reference, 'Section for HTML'],
104 [bullet_list, addnodes.toctree],
105 ),
106 ],
107 )
108 assert_node(
109 extract_node(toctree, 0, 1, 1, 0, 0, 0),
110 reference,
111 anchorname='#section-for-html',
112 )
113 assert_node(
114 extract_node(toctree, 0, 1, 1, 0, 1, 0),
115 addnodes.toctree,
116 caption=None,
117 glob=False,
118 hidden=False,
119 entries=[(None, 'baz')],
120 includefiles=['baz'],
121 titlesonly=False,
122 maxdepth=-1,
123 numbered=0,
124 )
125 assert_node(
126 extract_node(toctree, 0, 1, 2),
127 (
128 [compact_paragraph, reference, 'subsection'],
129 [bullet_list, list_item, compact_paragraph, reference, 'subsubsection'],
130 ),
131 )
132
133 assert_node(
134 extract_node(toctree, 1, 0),
135 [
136 compact_paragraph,
137 reference,
138 'Test for combination of ‘globaltoc.html’ and hidden toctree',
139 ],
140 )
141 assert_node(
142 extract_node(toctree, 1, 0, 0),
143 reference,
144 anchorname='#test-for-combination-of-globaltoc-html-and-hidden-toctree',
145 )
146 assert_node(
147 extract_node(toctree, 1, 1, 0),
148 addnodes.toctree,
149 caption=None,
150 entries=[],
151 glob=False,
152 hidden=False,
153 titlesonly=False,
154 maxdepth=-1,
155 numbered=0,
156 )
157 assert_node(
158 extract_node(toctree, 1, 1, 1),
159 addnodes.toctree,
160 caption=None,
161 glob=False,
162 hidden=True,
163 titlesonly=False,
164 maxdepth=-1,
165 numbered=0,
166 entries=[
167 ('Latest reference', 'https://sphinx-doc.org/latest/'),
168 ('Python', 'https://python.org/'),
169 ],
170 )
171
172 assert_node(
173 extract_node(toctree, 2, 0),
174 [compact_paragraph, reference, 'Indices and tables'],
175 )
176
177 # other collections
178 assert app.env.toc_num_entries['index'] == 6
179 assert app.env.toctree_includes['index'] == ['foo', 'bar', 'baz']
180 assert app.env.files_to_rebuild['foo'] == {'index'}
181 assert app.env.files_to_rebuild['bar'] == {'index'}
182 assert app.env.files_to_rebuild['baz'] == {'index'}
183 assert app.env.glob_toctrees == set()
184 assert app.env.numbered_toctrees == {'index'}
185
186 # qux has no section title
187 assert len(app.env.tocs['qux']) == 0
188 assert_node(app.env.tocs['qux'], nodes.bullet_list)
189 assert app.env.toc_num_entries['qux'] == 0
190 assert 'qux' not in app.env.toctree_includes
191
192
193 @pytest.mark.sphinx('dummy', testroot='toctree-glob')
194 def test_glob(app):
195 includefiles = [
196 'foo',
197 'bar/index',
198 'bar/bar_1',
199 'bar/bar_2',
200 'bar/bar_3',
201 'baz',
202 'qux/index',
203 ]
204
205 app.build()
206
207 # tocs
208 toctree = app.env.tocs['index']
209 assert_node(
210 toctree,
211 [
212 bullet_list,
213 list_item,
214 (
215 compact_paragraph, # [0][0]
216 [
217 bullet_list,
218 (
219 list_item, # [0][1][0]
220 list_item, # [0][1][1]
221 ),
222 ],
223 ),
224 ],
225 )
226
227 assert_node(
228 extract_node(toctree, 0, 0), [compact_paragraph, reference, 'test-toctree-glob']
229 )
230 assert_node(
231 extract_node(toctree, 0, 1, 0),
232 [
233 list_item,
234 (
235 [compact_paragraph, reference, 'normal order'],
236 [bullet_list, addnodes.toctree], # [0][1][0][1][0]
237 ),
238 ],
239 )
240 assert_node(
241 extract_node(toctree, 0, 1, 0, 1, 0),
242 addnodes.toctree,
243 caption=None,
244 glob=True,
245 hidden=False,
246 titlesonly=False,
247 maxdepth=-1,
248 numbered=0,
249 includefiles=includefiles,
250 entries=[
251 (None, 'foo'),
252 (None, 'bar/index'),
253 (None, 'bar/bar_1'),
254 (None, 'bar/bar_2'),
255 (None, 'bar/bar_3'),
256 (None, 'baz'),
257 (None, 'qux/index'),
258 ('hyperref', 'https://sphinx-doc.org/?q=sphinx'),
259 ],
260 )
261 assert_node(
262 extract_node(toctree, 0, 1, 1),
263 [
264 list_item,
265 (
266 [compact_paragraph, reference, 'reversed order'],
267 [bullet_list, addnodes.toctree], # [0][1][1][1][0]
268 ),
269 ],
270 )
271 assert_node(
272 extract_node(toctree, 0, 1, 1, 1, 0),
273 addnodes.toctree,
274 caption=None,
275 glob=True,
276 hidden=False,
277 titlesonly=False,
278 maxdepth=-1,
279 numbered=0,
280 includefiles=list(reversed(includefiles)),
281 entries=[
282 (None, 'qux/index'),
283 (None, 'baz'),
284 (None, 'bar/bar_3'),
285 (None, 'bar/bar_2'),
286 (None, 'bar/bar_1'),
287 (None, 'bar/index'),
288 (None, 'foo'),
289 ],
290 )
291
292 # other collections
293 assert app.env.toc_num_entries['index'] == 3
294 assert app.env.toctree_includes['index'] == includefiles + list(
295 reversed(includefiles)
296 )
297 for file in includefiles:
298 assert 'index' in app.env.files_to_rebuild[file]
299 assert 'index' in app.env.glob_toctrees
300 assert app.env.numbered_toctrees == set()
301
302
303 @pytest.mark.sphinx('dummy', testroot='toctree-domain-objects')
304 def test_domain_objects(app):
305 app.build()
306
307 assert app.env.toc_num_entries['index'] == 0
308 assert app.env.toc_num_entries['domains'] == 9
309 assert app.env.toctree_includes['index'] == ['domains', 'document_scoping']
310 assert 'index' in app.env.files_to_rebuild['domains']
311 assert app.env.glob_toctrees == set()
312 assert app.env.numbered_toctrees == {'index'}
313
314 # tocs
315 toctree = app.env.tocs['domains']
316 assert_node(
317 toctree,
318 [
319 bullet_list,
320 list_item,
321 (
322 compact_paragraph, # [0][0]
323 [
324 bullet_list, # [0][1]
325 (
326 list_item, # [0][1][0]
327 [
328 list_item, # [0][1][1]
329 (
330 compact_paragraph, # [0][1][1][0]
331 [
332 bullet_list, # [0][1][1][1]
333 (
334 list_item, # [0][1][1][1][0]
335 list_item,
336 list_item,
337 list_item, # [0][1][1][1][3]
338 ),
339 ],
340 ),
341 ],
342 list_item,
343 list_item, # [0][1][1]
344 ),
345 ],
346 ),
347 ],
348 )
349
350 assert_node(
351 extract_node(toctree, 0, 0),
352 [compact_paragraph, reference, 'test-domain-objects'],
353 )
354
355 assert_node(
356 extract_node(toctree, 0, 1, 0),
357 [list_item, ([compact_paragraph, reference, literal, 'world()'])],
358 )
359
360 assert_node(
361 extract_node(toctree, 0, 1, 1, 1, 3),
362 [
363 list_item,
364 ([compact_paragraph, reference, literal, 'HelloWorldPrinter.print()']),
365 ],
366 )
367
368
369 @pytest.mark.sphinx('dummy', testroot='toctree-domain-objects')
370 def test_domain_objects_document_scoping(app: SphinxTestApp) -> None:
371 app.build()
372
373 # tocs
374 toctree = app.env.tocs['document_scoping']
375 assert_node(
376 toctree,
377 [
378 bullet_list,
379 list_item,
380 (
381 compact_paragraph, # [0][0]
382 [
383 bullet_list, # [0][1]
384 (
385 [
386 list_item, # [0][1][0]
387 compact_paragraph,
388 reference,
389 literal,
390 'ClassLevel1a',
391 ],
392 [
393 list_item, # [0][1][1]
394 (
395 [
396 compact_paragraph, # [0][1][1][0]
397 reference,
398 literal,
399 'ClassLevel1b',
400 ],
401 [
402 bullet_list,
403 list_item, # [0][1][1][1][0]
404 compact_paragraph,
405 reference,
406 literal,
407 'ClassLevel1b.f()',
408 ],
409 ),
410 ],
411 [
412 list_item, # [0][1][2]
413 compact_paragraph,
414 reference,
415 literal,
416 'ClassLevel1a.g()',
417 ],
418 [
419 list_item, # [0][1][3]
420 compact_paragraph,
421 reference,
422 literal,
423 'ClassLevel1b.g()',
424 ],
425 [
426 list_item, # [0][1][4]
427 (
428 [
429 compact_paragraph, # [0][1][4][0]
430 reference,
431 'Level 2',
432 ],
433 [
434 bullet_list, # [0][1][4][1]
435 (
436 [
437 list_item, # [0][1][4][1][0]
438 compact_paragraph,
439 reference,
440 literal,
441 'ClassLevel2a',
442 ],
443 [
444 list_item, # [0][1][4][1][1]
445 (
446 [
447 compact_paragraph, # [0][1][4][1][1][0]
448 reference,
449 literal,
450 'ClassLevel2b',
451 ],
452 [
453 bullet_list,
454 list_item, # [0][1][4][1][1][1][0]
455 compact_paragraph,
456 reference,
457 literal,
458 'ClassLevel2b.f()',
459 ],
460 ),
461 ],
462 [
463 list_item, # [0][1][4][1][2]
464 compact_paragraph,
465 reference,
466 literal,
467 'ClassLevel2a.g()',
468 ],
469 [
470 list_item, # [0][1][4][1][3]
471 compact_paragraph,
472 reference,
473 literal,
474 'ClassLevel2b.g()',
475 ],
476 ),
477 ],
478 ),
479 ],
480 ),
481 ],
482 ),
483 ],
484 )
485
486
487 @pytest.mark.sphinx('xml', testroot='toctree')
488 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
489 def test_document_toc(app):
490 app.build()
491 toctree = document_toc(app.env, 'index', app.tags)
492
493 assert_node(
494 toctree,
495 [
496 bullet_list,
497 (
498 [
499 list_item,
500 (
501 compact_paragraph, # [0][0]
502 [
503 bullet_list,
504 (
505 addnodes.toctree, # [0][1][0]
506 list_item, # [0][1][1]
507 ),
508 ],
509 ),
510 ],
511 [
512 list_item,
513 (
514 compact_paragraph, # [1][0]
515 [bullet_list, (addnodes.toctree, addnodes.toctree)],
516 ),
517 ],
518 [list_item, compact_paragraph], # [2][0]
519 ),
520 ],
521 )
522 assert_node(
523 extract_node(toctree, 0, 0),
524 [compact_paragraph, reference, 'Welcome to Sphinx Tests’s documentation!'],
525 )
526 assert_node(
527 extract_node(toctree, 0, 1, 1),
528 (
529 [compact_paragraph, reference, 'subsection'],
530 [bullet_list, list_item, compact_paragraph, reference, 'subsubsection'],
531 ),
532 )
533 assert_node(
534 extract_node(toctree, 1, 0),
535 [
536 compact_paragraph,
537 reference,
538 'Test for combination of ‘globaltoc.html’ and hidden toctree',
539 ],
540 )
541 assert_node(
542 extract_node(toctree, 2, 0),
543 [compact_paragraph, reference, 'Indices and tables'],
544 )
545
546
547 @pytest.mark.sphinx('xml', testroot='toctree')
548 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
549 def test_document_toc_only(app):
550 app.build()
551 StandaloneHTMLBuilder(app, app.env) # adds format/builder tags
552 toctree = document_toc(app.env, 'index', app.tags)
553
554 assert_node(
555 toctree,
556 [
557 bullet_list,
558 (
559 [
560 list_item,
561 (
562 compact_paragraph, # [0][0]
563 [
564 bullet_list,
565 (
566 addnodes.toctree, # [0][1][0]
567 list_item, # [0][1][1]
568 list_item, # [0][1][2]
569 ),
570 ],
571 ),
572 ],
573 [
574 list_item,
575 (
576 compact_paragraph, # [1][0]
577 [bullet_list, (addnodes.toctree, addnodes.toctree)],
578 ),
579 ],
580 [list_item, compact_paragraph], # [2][0]
581 ),
582 ],
583 )
584 assert_node(
585 extract_node(toctree, 0, 0),
586 [compact_paragraph, reference, 'Welcome to Sphinx Tests’s documentation!'],
587 )
588 assert_node(
589 extract_node(toctree, 0, 1, 1),
590 (
591 [compact_paragraph, reference, 'Section for HTML'],
592 [bullet_list, addnodes.toctree],
593 ),
594 )
595 assert_node(
596 extract_node(toctree, 0, 1, 2),
597 (
598 [compact_paragraph, reference, 'subsection'],
599 [bullet_list, list_item, compact_paragraph, reference, 'subsubsection'],
600 ),
601 )
602 assert_node(
603 extract_node(toctree, 1, 0),
604 [
605 compact_paragraph,
606 reference,
607 'Test for combination of ‘globaltoc.html’ and hidden toctree',
608 ],
609 )
610 assert_node(
611 extract_node(toctree, 2, 0),
612 [compact_paragraph, reference, 'Indices and tables'],
613 )
614
615
616 @pytest.mark.sphinx('xml', testroot='toctree')
617 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
618 def test_document_toc_tocdepth(app):
619 app.build()
620 toctree = document_toc(app.env, 'tocdepth', app.tags)
621
622 assert_node(
623 toctree,
624 [
625 bullet_list,
626 list_item,
627 (
628 compact_paragraph, # [0][0]
629 bullet_list, # [0][1]
630 ),
631 ],
632 )
633 assert_node(extract_node(toctree, 0, 0), [compact_paragraph, reference, 'level 1'])
634 assert_node(
635 extract_node(toctree, 0, 1),
636 [bullet_list, list_item, compact_paragraph, reference, 'level 2'],
637 )
638
639
640 @pytest.mark.sphinx('xml', testroot='toctree')
641 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
642 def test_global_toctree_for_doc(app):
643 app.build()
644 toctree = global_toctree_for_doc(
645 app.env, 'index', app.builder, tags=app.tags, collapse=False
646 )
647 assert_node(
648 toctree,
649 [
650 compact_paragraph,
651 ([title, 'Table of Contents'], bullet_list, bullet_list, bullet_list),
652 ],
653 )
654
655 assert_node(
656 toctree[1],
657 (
658 [list_item, ([compact_paragraph, reference, 'foo'], bullet_list)],
659 [list_item, compact_paragraph, reference, 'bar'],
660 [list_item, compact_paragraph, reference, 'https://sphinx-doc.org/'],
661 [
662 list_item,
663 compact_paragraph,
664 reference,
665 'Welcome to Sphinx Tests’s documentation!',
666 ],
667 ),
668 )
669 assert_node(
670 extract_node(toctree, 1, 0, 1),
671 (
672 [list_item, compact_paragraph, reference, 'quux'],
673 [list_item, compact_paragraph, reference, 'foo.1'],
674 [list_item, compact_paragraph, reference, 'foo.2'],
675 ),
676 )
677
678 assert_node(
679 extract_node(toctree, 1, 0, 0, 0), reference, refuri='foo', secnumber=[1]
680 )
681 assert_node(
682 extract_node(toctree, 1, 0, 1, 0, 0, 0),
683 reference,
684 refuri='quux',
685 secnumber=[1, 1],
686 )
687 assert_node(
688 extract_node(toctree, 1, 0, 1, 1, 0, 0),
689 reference,
690 refuri='foo#foo-1',
691 secnumber=[1, 2],
692 )
693 assert_node(
694 extract_node(toctree, 1, 0, 1, 2, 0, 0),
695 reference,
696 refuri='foo#foo-2',
697 secnumber=[1, 3],
698 )
699 assert_node(
700 extract_node(toctree, 1, 1, 0, 0), reference, refuri='bar', secnumber=[2]
701 )
702 assert_node(
703 extract_node(toctree, 1, 2, 0, 0), reference, refuri='https://sphinx-doc.org/'
704 )
705 assert_node(extract_node(toctree, 1, 3, 0, 0), reference, refuri='')
706
707 assert_node(
708 toctree[2], [bullet_list, list_item, compact_paragraph, reference, 'baz']
709 )
710 assert_node(
711 toctree[3],
712 (
713 [list_item, compact_paragraph, reference, 'Latest reference'],
714 [list_item, compact_paragraph, reference, 'Python'],
715 ),
716 )
717 assert_node(
718 extract_node(toctree, 3, 0, 0, 0),
719 reference,
720 refuri='https://sphinx-doc.org/latest/',
721 )
722 assert_node(
723 extract_node(toctree, 3, 1, 0, 0), reference, refuri='https://python.org/'
724 )
725
726
727 @pytest.mark.sphinx('xml', testroot='toctree')
728 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
729 def test_global_toctree_for_doc_collapse(app):
730 app.build()
731 toctree = global_toctree_for_doc(
732 app.env, 'index', app.builder, tags=app.tags, collapse=True
733 )
734 assert_node(
735 toctree,
736 [
737 compact_paragraph,
738 ([title, 'Table of Contents'], bullet_list, bullet_list, bullet_list),
739 ],
740 )
741
742 assert_node(
743 toctree[1],
744 (
745 [list_item, compact_paragraph, reference, 'foo'],
746 [list_item, compact_paragraph, reference, 'bar'],
747 [list_item, compact_paragraph, reference, 'https://sphinx-doc.org/'],
748 [
749 list_item,
750 compact_paragraph,
751 reference,
752 'Welcome to Sphinx Tests’s documentation!',
753 ],
754 ),
755 )
756 assert_node(
757 extract_node(toctree, 1, 0, 0, 0), reference, refuri='foo', secnumber=[1]
758 )
759 assert_node(
760 extract_node(toctree, 1, 1, 0, 0), reference, refuri='bar', secnumber=[2]
761 )
762 assert_node(
763 extract_node(toctree, 1, 2, 0, 0), reference, refuri='https://sphinx-doc.org/'
764 )
765 assert_node(extract_node(toctree, 1, 3, 0, 0), reference, refuri='')
766
767 assert_node(
768 toctree[2], [bullet_list, list_item, compact_paragraph, reference, 'baz']
769 )
770 assert_node(
771 toctree[3],
772 (
773 [list_item, compact_paragraph, reference, 'Latest reference'],
774 [list_item, compact_paragraph, reference, 'Python'],
775 ),
776 )
777 assert_node(
778 extract_node(toctree, 3, 0, 0, 0),
779 reference,
780 refuri='https://sphinx-doc.org/latest/',
781 )
782 assert_node(
783 extract_node(toctree, 3, 1, 0, 0), reference, refuri='https://python.org/'
784 )
785
786
787 @pytest.mark.sphinx('xml', testroot='toctree')
788 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
789 def test_global_toctree_for_doc_maxdepth(app):
790 app.build()
791 toctree = global_toctree_for_doc(
792 app.env, 'index', app.builder, tags=app.tags, collapse=False, maxdepth=3
793 )
794 assert_node(
795 toctree,
796 [
797 compact_paragraph,
798 (
799 [title, 'Table of Contents'],
800 bullet_list,
801 bullet_list,
802 bullet_list,
803 ),
804 ],
805 )
806
807 assert_node(
808 toctree[1],
809 (
810 [
811 list_item,
812 (
813 [compact_paragraph, reference, 'foo'],
814 bullet_list,
815 ),
816 ],
817 [list_item, compact_paragraph, reference, 'bar'],
818 [list_item, compact_paragraph, reference, 'https://sphinx-doc.org/'],
819 [
820 list_item,
821 compact_paragraph,
822 reference,
823 'Welcome to Sphinx Tests’s documentation!',
824 ],
825 ),
826 )
827 assert_node(
828 extract_node(toctree, 1, 0, 1),
829 (
830 [list_item, compact_paragraph, reference, 'quux'],
831 [
832 list_item,
833 (
834 [compact_paragraph, reference, 'foo.1'],
835 bullet_list,
836 ),
837 ],
838 [list_item, compact_paragraph, reference, 'foo.2'],
839 ),
840 )
841 assert_node(
842 extract_node(toctree, 1, 0, 1, 1, 1),
843 [bullet_list, list_item, compact_paragraph, reference, 'foo.1-1'],
844 )
845
846 assert_node(
847 extract_node(toctree, 1, 0, 0, 0), reference, refuri='foo', secnumber=[1]
848 )
849 assert_node(
850 extract_node(toctree, 1, 0, 1, 0, 0, 0),
851 reference,
852 refuri='quux',
853 secnumber=[1, 1],
854 )
855 assert_node(
856 extract_node(toctree, 1, 0, 1, 1, 0, 0),
857 reference,
858 refuri='foo#foo-1',
859 secnumber=[1, 2],
860 )
861 assert_node(
862 extract_node(toctree, 1, 0, 1, 1, 1, 0, 0, 0),
863 reference,
864 refuri='foo#foo-1-1',
865 secnumber=[1, 2, 1],
866 )
867 assert_node(
868 extract_node(toctree, 1, 0, 1, 2, 0, 0),
869 reference,
870 refuri='foo#foo-2',
871 secnumber=[1, 3],
872 )
873 assert_node(
874 extract_node(toctree, 1, 1, 0, 0), reference, refuri='bar', secnumber=[2]
875 )
876 assert_node(
877 extract_node(toctree, 1, 2, 0, 0), reference, refuri='https://sphinx-doc.org/'
878 )
879 assert_node(extract_node(toctree, 1, 3, 0, 0), reference, refuri='')
880
881 assert_node(
882 toctree[2], [bullet_list, list_item, compact_paragraph, reference, 'baz']
883 )
884 assert_node(
885 toctree[3],
886 (
887 [list_item, compact_paragraph, reference, 'Latest reference'],
888 [list_item, compact_paragraph, reference, 'Python'],
889 ),
890 )
891 assert_node(
892 extract_node(toctree, 3, 0, 0, 0),
893 reference,
894 refuri='https://sphinx-doc.org/latest/',
895 )
896 assert_node(
897 extract_node(toctree, 3, 1, 0, 0), reference, refuri='https://python.org/'
898 )
899
900
901 @pytest.mark.sphinx('xml', testroot='toctree')
902 @pytest.mark.test_params(shared_result='test_environment_toctree_basic')
903 def test_global_toctree_for_doc_includehidden(app):
904 app.build()
905 toctree = global_toctree_for_doc(
906 app.env,
907 'index',
908 app.builder,
909 tags=app.tags,
910 collapse=False,
911 includehidden=False,
912 )
913 assert_node(
914 toctree,
915 [
916 compact_paragraph,
917 (
918 [title, 'Table of Contents'],
919 bullet_list,
920 bullet_list,
921 ),
922 ],
923 )
924
925 assert_node(
926 toctree[1],
927 (
928 [
929 list_item,
930 (
931 [compact_paragraph, reference, 'foo'],
932 bullet_list,
933 ),
934 ],
935 [list_item, compact_paragraph, reference, 'bar'],
936 [list_item, compact_paragraph, reference, 'https://sphinx-doc.org/'],
937 [
938 list_item,
939 compact_paragraph,
940 reference,
941 'Welcome to Sphinx Tests’s documentation!',
942 ],
943 ),
944 )
945 assert_node(
946 extract_node(toctree, 1, 0, 1),
947 (
948 [list_item, compact_paragraph, reference, 'quux'],
949 [list_item, compact_paragraph, reference, 'foo.1'],
950 [list_item, compact_paragraph, reference, 'foo.2'],
951 ),
952 )
953
954 assert_node(
955 extract_node(toctree, 1, 0, 0, 0), reference, refuri='foo', secnumber=[1]
956 )
957 assert_node(
958 extract_node(toctree, 1, 0, 1, 0, 0, 0),
959 reference,
960 refuri='quux',
961 secnumber=[1, 1],
962 )
963 assert_node(
964 extract_node(toctree, 1, 0, 1, 1, 0, 0),
965 reference,
966 refuri='foo#foo-1',
967 secnumber=[1, 2],
968 )
969 assert_node(
970 extract_node(toctree, 1, 0, 1, 2, 0, 0),
971 reference,
972 refuri='foo#foo-2',
973 secnumber=[1, 3],
974 )
975 assert_node(
976 extract_node(toctree, 1, 1, 0, 0), reference, refuri='bar', secnumber=[2]
977 )
978 assert_node(
979 extract_node(toctree, 1, 2, 0, 0), reference, refuri='https://sphinx-doc.org/'
980 )
981
982 assert_node(
983 toctree[2], [bullet_list, list_item, compact_paragraph, reference, 'baz']
984 )
985
986
987 @pytest.mark.sphinx('xml', testroot='toctree-index')
988 def test_toctree_index(app):
989 app.build()
990 toctree = app.env.tocs['index']
991 assert_node(
992 toctree,
993 [
994 bullet_list,
995 ([
996 list_item,
997 (
998 compact_paragraph, # [0][0]
999 [
1000 bullet_list,
1001 (
1002 addnodes.toctree, # [0][1][0]
1003 addnodes.toctree, # [0][1][1]
1004 ),
1005 ],
1006 ),
1007 ]),
1008 ],
1009 )
1010 assert_node(
1011 extract_node(toctree, 0, 1, 1),
1012 addnodes.toctree,
1013 caption='Indices',
1014 glob=False,
1015 hidden=False,
1016 titlesonly=False,
1017 maxdepth=-1,
1018 numbered=0,
1019 entries=[(None, 'genindex'), (None, 'modindex'), (None, 'search')],
1020 )
1021
1022
1023 @pytest.mark.sphinx('dummy', testroot='toctree-only')
1024 def test_toctree_only(app):
1025 # regression test for https://github.com/sphinx-doc/sphinx/issues/13022
1026 # we mainly care that this doesn't fail
1027
1028 if docutils.__version_info__[:2] >= (0, 22):
1029 true = '1'
1030 else:
1031 true = 'True'
1032 expected_pformat = f"""\
1033 <bullet_list>
1034 <list_item>
1035 <compact_paragraph>
1036 <reference anchorname="" internal="{true}" refuri="#">
1037 test-toctree-only
1038 <bullet_list>
1039 <list_item>
1040 <compact_paragraph skip_section_number="{true}">
1041 <reference anchorname="#test_toctree_only1" internal="{true}" refuri="#test_toctree_only1">
1042 <literal>
1043 test_toctree_only1
1044 <list_item>
1045 <compact_paragraph skip_section_number="{true}">
1046 <reference anchorname="#test_toctree_only2" internal="{true}" refuri="#test_toctree_only2">
1047 <literal>
1048 test_toctree_only2
1049 <list_item>
1050 <compact_paragraph skip_section_number="{true}">
1051 <reference anchorname="#id0" internal="{true}" refuri="#id0">
1052 <literal>
1053 test_toctree_only2
1054 """
1055 app.build()
1056 toc = document_toc(app.env, 'index', app.tags)
1057 assert toc.pformat(' ') == expected_pformat
1058
1059
1060 def test_toctree_copy_only():
1061 # regression test for https://github.com/sphinx-doc/sphinx/issues/13022
1062 # ensure ``_toctree_copy()`` properly filters out ``only`` nodes,
1063 # including nested nodes.
1064 node = nodes.literal('lobster!', 'lobster!')
1065 node = nodes.reference('', '', node, anchorname='', internal=True, refuri='index')
1066 node = addnodes.only('', node, expr='lobster')
1067 node = addnodes.compact_paragraph('', '', node, skip_section_number=True)
1068 node = nodes.list_item('', node)
1069 node = addnodes.only('', node, expr='not spam')
1070 node = addnodes.only('', node, expr='lobster')
1071 node = addnodes.only('', node, expr='not ham')
1072 node = nodes.bullet_list('', node)
1073 # this is a tree of the shape:
1074 # <bullet_list>
1075 # <only expr="not ham">
1076 # <only expr="lobster">
1077 # <only expr="not spam">
1078 # <list_item>
1079 # <compact_paragraph skip_section_number="True">
1080 # <only expr="lobster">
1081 # <reference anchorname="" internal="True" refuri="index">
1082 # <literal>
1083 # lobster!
1084
1085 tags = Tags({'lobster'})
1086 toc = _toctree_copy(node, 2, 0, False, tags)
1087 # the filtered ToC should look like:
1088 # <bullet_list>
1089 # <list_item>
1090 # <compact_paragraph skip_section_number="True">
1091 # <reference anchorname="" internal="True" refuri="index">
1092 # <literal>
1093 # lobster!
1094
1095 # no only nodes should remain
1096 assert list(toc.findall(addnodes.only)) == []
1097
1098 # the tree is preserved
1099 assert isinstance(toc, nodes.bullet_list)
1100 assert isinstance(toc[0], nodes.list_item)
1101 assert isinstance(extract_node(toc, 0, 0), addnodes.compact_paragraph)
1102 assert isinstance(extract_node(toc, 0, 0, 0), nodes.reference)
1103 assert isinstance(extract_node(toc, 0, 0, 0, 0), nodes.literal)
1104 assert extract_node(toc, 0, 0, 0, 0, 0) == nodes.Text('lobster!')