1"""
2    test_ext_autosectionlabel
3    ~~~~~~~~~~~~~~~~~~~~~~~~~
4
5    Test sphinx.ext.autosectionlabel extension.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13import pytest
14
15from sphinx.util import docutils
16
17
18@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
19                    reason='docutils-0.13 or above is required')
20@pytest.mark.sphinx('html', testroot='ext-autosectionlabel')
21def test_autosectionlabel_html(app, status, warning, skipped_labels=False):
22    app.builder.build_all()
23
24    content = (app.outdir / 'index.html').read_text()
25    html = ('<li><p><a class="reference internal" href="#introduce-of-sphinx">'
26            '<span class=".*?">Introduce of Sphinx</span></a></p></li>')
27    assert re.search(html, content, re.S)
28
29    html = ('<li><p><a class="reference internal" href="#installation">'
30            '<span class="std std-ref">Installation</span></a></p></li>')
31    assert re.search(html, content, re.S)
32
33    html = ('<li><p><a class="reference internal" href="#for-windows-users">'
34            '<span class="std std-ref">For Windows users</span></a></p></li>')
35    assert re.search(html, content, re.S)
36
37    html = ('<li><p><a class="reference internal" href="#for-unix-users">'
38            '<span class="std std-ref">For UNIX users</span></a></p></li>')
39    assert re.search(html, content, re.S)
40
41    html = ('<li><p><a class="reference internal" href="#linux">'
42            '<span class="std std-ref">Linux</span></a></p></li>')
43    assert re.search(html, content, re.S)
44
45    html = ('<li><p><a class="reference internal" href="#freebsd">'
46            '<span class="std std-ref">FreeBSD</span></a></p></li>')
47    assert re.search(html, content, re.S)
48
49    # for smart_quotes (refs: #4027)
50    html = ('<li><p><a class="reference internal" '
51            'href="#this-one-s-got-an-apostrophe">'
52            '<span class="std std-ref">This one’s got an apostrophe'
53            '</span></a></p></li>')
54    assert re.search(html, content, re.S)
55
56
57# Re-use test definition from above, just change the test root directory
58@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
59                    reason='docutils-0.13 or above is required')
60@pytest.mark.sphinx('html', testroot='ext-autosectionlabel-prefix-document')
61def test_autosectionlabel_prefix_document_html(app, status, warning):
62    test_autosectionlabel_html(app, status, warning)
63
64
65@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
66                    reason='docutils-0.13 or above is required')
67@pytest.mark.sphinx('html', testroot='ext-autosectionlabel',
68                    confoverrides={'autosectionlabel_maxdepth': 3})
69def test_autosectionlabel_maxdepth(app, status, warning):
70    app.builder.build_all()
71
72    content = (app.outdir / 'index.html').read_text()
73
74    # depth: 1
75    html = ('<li><p><a class="reference internal" href="#test-ext-autosectionlabel">'
76            '<span class=".*?">test-ext-autosectionlabel</span></a></p></li>')
77    assert re.search(html, content, re.S)
78
79    # depth: 2
80    html = ('<li><p><a class="reference internal" href="#installation">'
81            '<span class="std std-ref">Installation</span></a></p></li>')
82    assert re.search(html, content, re.S)
83
84    # depth: 3
85    html = ('<li><p><a class="reference internal" href="#for-windows-users">'
86            '<span class="std std-ref">For Windows users</span></a></p></li>')
87    assert re.search(html, content, re.S)
88
89    # depth: 4
90    html = '<li><p><span class="xref std std-ref">Linux</span></p></li>'
91    assert re.search(html, content, re.S)
92
93    assert 'WARNING: undefined label: linux' in warning.getvalue()
94