Debian Codegraph @

tests/test_builders/test_build_html_tocdepth.py

raw
1 """Test the HTML builder and check output against XPath."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from tests.test_builders.xpath_html_util import _intradocument_hyperlink_check
10 from tests.test_builders.xpath_util import check_xpath
11
12 if TYPE_CHECKING:
13 from collections.abc import Callable
14 from pathlib import Path
15 from xml.etree.ElementTree import ElementTree
16
17 from sphinx.testing.util import SphinxTestApp
18
19
20 @pytest.mark.parametrize(
21 ('fname', 'path', 'check', 'be_found'),
22 [
23 # index.html
24 ('index.html', ".//li[@class='toctree-l3']/a", '1.1.1. Foo A1', True),
25 ('index.html', ".//li[@class='toctree-l3']/a", '1.2.1. Foo B1', True),
26 ('index.html', ".//li[@class='toctree-l3']/a", '2.1.1. Bar A1', False),
27 ('index.html', ".//li[@class='toctree-l3']/a", '2.2.1. Bar B1', False),
28 # foo.html
29 ('foo.html', './/h1', 'Foo', True),
30 ('foo.html', './/h2', 'Foo A', True),
31 ('foo.html', './/h3', 'Foo A1', True),
32 ('foo.html', './/h2', 'Foo B', True),
33 ('foo.html', './/h3', 'Foo B1', True),
34 # foo.html section-number
35 ('foo.html', ".//h1//span[@class='section-number']", '1. ', True),
36 ('foo.html', ".//h2//span[@class='section-number']", '1.1. ', True),
37 ('foo.html', ".//h3//span[@class='section-number']", '1.1.1. ', True),
38 ('foo.html', ".//h2//span[@class='section-number']", '1.2. ', True),
39 ('foo.html', ".//h3//span[@class='section-number']", '1.2.1. ', True),
40 # foo.html sphinxsidebarwrapper
41 ('foo.html', ".//div[@class='sphinxsidebarwrapper']//li/a", '1.1. Foo A', True),
42 (
43 'foo.html',
44 ".//div[@class='sphinxsidebarwrapper']//li/a",
45 '1.1.1. Foo A1',
46 True,
47 ),
48 ('foo.html', ".//div[@class='sphinxsidebarwrapper']//li/a", '1.2. Foo B', True),
49 (
50 'foo.html',
51 ".//div[@class='sphinxsidebarwrapper']//li/a",
52 '1.2.1. Foo B1',
53 True,
54 ),
55 # bar.html
56 ('bar.html', './/h1', 'Bar', True),
57 ('bar.html', './/h2', 'Bar A', True),
58 ('bar.html', './/h2', 'Bar B', True),
59 ('bar.html', './/h3', 'Bar B1', True),
60 ('bar.html', ".//h1//span[@class='section-number']", '2. ', True),
61 ('bar.html', ".//h2//span[@class='section-number']", '2.1. ', True),
62 ('bar.html', ".//h2//span[@class='section-number']", '2.2. ', True),
63 ('bar.html', ".//h3//span[@class='section-number']", '2.2.1. ', True),
64 ('bar.html', ".//div[@class='sphinxsidebarwrapper']//li/a", '2. Bar', True),
65 ('bar.html', ".//div[@class='sphinxsidebarwrapper']//li/a", '2.1. Bar A', True),
66 ('bar.html', ".//div[@class='sphinxsidebarwrapper']//li/a", '2.2. Bar B', True),
67 (
68 'bar.html',
69 ".//div[@class='sphinxsidebarwrapper']//li/a",
70 '2.2.1. Bar B1',
71 False,
72 ),
73 # baz.html
74 ('baz.html', './/h1', 'Baz A', True),
75 ('baz.html', ".//h1//span[@class='section-number']", '2.1.1. ', True),
76 ],
77 )
78 @pytest.mark.sphinx('html', testroot='tocdepth')
79 @pytest.mark.test_params(shared_result='test_build_html_tocdepth')
80 def test_tocdepth(
81 app: SphinxTestApp,
82 cached_etree_parse: Callable[[Path], ElementTree],
83 fname: str,
84 path: str,
85 check: str,
86 be_found: bool,
87 ) -> None:
88 app.build()
89 # https://github.com/sphinx-doc/sphinx/issues/1251
90 check_xpath(cached_etree_parse(app.outdir / fname), fname, path, check, be_found)
91
92
93 @pytest.mark.parametrize(
94 'expect',
95 [
96 (".//li[@class='toctree-l3']/a", '1.1.1. Foo A1', True),
97 (".//li[@class='toctree-l3']/a", '1.2.1. Foo B1', True),
98 (".//li[@class='toctree-l3']/a", '2.1.1. Bar A1', False),
99 (".//li[@class='toctree-l3']/a", '2.2.1. Bar B1', False),
100 (".//ul/li[@class='toctree-l1']/..//a", _intradocument_hyperlink_check),
101 # index.rst
102 ('.//h1', 'test-tocdepth', True),
103 # foo.rst
104 ('.//h2', 'Foo', True),
105 ('.//h3', 'Foo A', True),
106 ('.//h4', 'Foo A1', True),
107 ('.//h3', 'Foo B', True),
108 ('.//h4', 'Foo B1', True),
109 (".//h2//span[@class='section-number']", '1. ', True),
110 (".//h3//span[@class='section-number']", '1.1. ', True),
111 (".//h4//span[@class='section-number']", '1.1.1. ', True),
112 (".//h3//span[@class='section-number']", '1.2. ', True),
113 (".//h4//span[@class='section-number']", '1.2.1. ', True),
114 # bar.rst
115 ('.//h2', 'Bar', True),
116 ('.//h3', 'Bar A', True),
117 ('.//h3', 'Bar B', True),
118 ('.//h4', 'Bar B1', True),
119 (".//h2//span[@class='section-number']", '2. ', True),
120 (".//h3//span[@class='section-number']", '2.1. ', True),
121 (".//h3//span[@class='section-number']", '2.2. ', True),
122 (".//h4//span[@class='section-number']", '2.2.1. ', True),
123 # baz.rst
124 ('.//h4', 'Baz A', True),
125 (".//h4//span[@class='section-number']", '2.1.1. ', True),
126 ],
127 )
128 @pytest.mark.sphinx('singlehtml', testroot='tocdepth')
129 @pytest.mark.test_params(shared_result='test_build_html_tocdepth')
130 def test_tocdepth_singlehtml(
131 app: SphinxTestApp,
132 cached_etree_parse: Callable[[Path], ElementTree],
133 expect: tuple[str, str, bool],
134 ) -> None:
135 app.build()
136 check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)