| 1 |
"""Test sphinx.ext.autosectionlabel extension.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from sphinx.testing.util import SphinxTestApp |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.sphinx('html', testroot='ext-autosectionlabel') |
| 15 |
def test_autosectionlabel_html(app: SphinxTestApp) -> None: |
| 16 |
app.build(force_all=True) |
| 17 |
|
| 18 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 19 |
html = ( |
| 20 |
'<li><p><a class="reference internal" href="#introduce-of-sphinx">' |
| 21 |
'<span class=".*?">Introduce of Sphinx</span></a></p></li>' |
| 22 |
) |
| 23 |
assert re.search(html, content, re.DOTALL) |
| 24 |
|
| 25 |
html = ( |
| 26 |
'<li><p><a class="reference internal" href="#installation">' |
| 27 |
'<span class="std std-ref">Installation</span></a></p></li>' |
| 28 |
) |
| 29 |
assert re.search(html, content, re.DOTALL) |
| 30 |
|
| 31 |
html = ( |
| 32 |
'<li><p><a class="reference internal" href="#for-windows-users">' |
| 33 |
'<span class="std std-ref">For Windows users</span></a></p></li>' |
| 34 |
) |
| 35 |
assert re.search(html, content, re.DOTALL) |
| 36 |
|
| 37 |
html = ( |
| 38 |
'<li><p><a class="reference internal" href="#for-unix-users">' |
| 39 |
'<span class="std std-ref">For UNIX users</span></a></p></li>' |
| 40 |
) |
| 41 |
assert re.search(html, content, re.DOTALL) |
| 42 |
|
| 43 |
html = ( |
| 44 |
'<li><p><a class="reference internal" href="#linux">' |
| 45 |
'<span class="std std-ref">Linux</span></a></p></li>' |
| 46 |
) |
| 47 |
assert re.search(html, content, re.DOTALL) |
| 48 |
|
| 49 |
html = ( |
| 50 |
'<li><p><a class="reference internal" href="#freebsd">' |
| 51 |
'<span class="std std-ref">FreeBSD</span></a></p></li>' |
| 52 |
) |
| 53 |
assert re.search(html, content, re.DOTALL) |
| 54 |
|
| 55 |
# for smart_quotes |
| 56 |
# See: https://github.com/sphinx-doc/sphinx/issues/4027 |
| 57 |
html = ( |
| 58 |
'<li><p><a class="reference internal" ' |
| 59 |
'href="#this-one-s-got-an-apostrophe">' |
| 60 |
'<span class="std std-ref">This one’s got an apostrophe' |
| 61 |
'</span></a></p></li>' |
| 62 |
) |
| 63 |
assert re.search(html, content, re.DOTALL) |
| 64 |
|
| 65 |
|
| 66 |
# Reuse test definition from above, just change the test root directory |
| 67 |
@pytest.mark.sphinx('html', testroot='ext-autosectionlabel-prefix-document') |
| 68 |
def test_autosectionlabel_prefix_document_html(app: SphinxTestApp) -> None: |
| 69 |
test_autosectionlabel_html(app) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.sphinx( |
| 73 |
'html', |
| 74 |
testroot='ext-autosectionlabel', |
| 75 |
confoverrides={'autosectionlabel_maxdepth': 3}, |
| 76 |
) |
| 77 |
def test_autosectionlabel_maxdepth(app: SphinxTestApp) -> None: |
| 78 |
app.build(force_all=True) |
| 79 |
|
| 80 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 81 |
|
| 82 |
# depth: 1 |
| 83 |
html = ( |
| 84 |
'<li><p><a class="reference internal" href="#test-ext-autosectionlabel">' |
| 85 |
'<span class=".*?">test-ext-autosectionlabel</span></a></p></li>' |
| 86 |
) |
| 87 |
assert re.search(html, content, re.DOTALL) |
| 88 |
|
| 89 |
# depth: 2 |
| 90 |
html = ( |
| 91 |
'<li><p><a class="reference internal" href="#installation">' |
| 92 |
'<span class="std std-ref">Installation</span></a></p></li>' |
| 93 |
) |
| 94 |
assert re.search(html, content, re.DOTALL) |
| 95 |
|
| 96 |
# depth: 3 |
| 97 |
html = ( |
| 98 |
'<li><p><a class="reference internal" href="#for-windows-users">' |
| 99 |
'<span class="std std-ref">For Windows users</span></a></p></li>' |
| 100 |
) |
| 101 |
assert re.search(html, content, re.DOTALL) |
| 102 |
|
| 103 |
# depth: 4 |
| 104 |
html = '<li><p><span class="xref std std-ref">Linux</span></p></li>' |
| 105 |
assert re.search(html, content, re.DOTALL) |
| 106 |
|
| 107 |
assert "WARNING: undefined label: 'linux'" in app.warning.getvalue() |