File: test_build_html_code.py

package info (click to toggle)
sphinx 8.2.3-12
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 26,960 kB
  • sloc: python: 105,864; javascript: 6,474; perl: 449; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (72 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from __future__ import annotations

from typing import TYPE_CHECKING

import pygments
import pytest

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx(
    'html',
    testroot='reST-code-block',
    confoverrides={'html_codeblock_linenos_style': 'table'},
)
def test_html_codeblock_linenos_style_table(app: SphinxTestApp) -> None:
    app.build()
    content = (app.outdir / 'index.html').read_text(encoding='utf8')

    assert (
        '<div class="linenodiv"><pre><span class="normal">1</span>\n'
        '<span class="normal">2</span>\n'
        '<span class="normal">3</span>\n'
        '<span class="normal">4</span></pre></div>'
    ) in content


@pytest.mark.sphinx(
    'html',
    testroot='reST-code-block',
    confoverrides={'html_codeblock_linenos_style': 'inline'},
)
def test_html_codeblock_linenos_style_inline(app: SphinxTestApp) -> None:
    app.build()
    content = (app.outdir / 'index.html').read_text(encoding='utf8')

    assert '<span class="linenos">1</span>' in content


@pytest.mark.sphinx('html', testroot='reST-code-role')
def test_html_code_role(app: SphinxTestApp) -> None:
    if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
        sp = '<span class="w"> </span>'
    else:
        sp = ' '

    app.build()
    content = (app.outdir / 'index.html').read_text(encoding='utf8')

    common_content = (
        f'<span class="k">def</span>{sp}<span class="nf">foo</span>'
        '<span class="p">(</span>'
        '<span class="mi">1</span> '
        '<span class="o">+</span> '
        '<span class="mi">2</span> '
        '<span class="o">+</span> '
        '<span class="kc">None</span> '
        '<span class="o">+</span> '
        '<span class="s2">&quot;abc&quot;</span>'
        '<span class="p">):</span> '
        '<span class="k">pass</span>'
    )
    assert (
        '<p>Inline <code class="code highlight python docutils literal highlight-python">'
        + common_content
        + '</code> code block</p>'
    ) in content
    assert (
        '<div class="highlight-python notranslate">'
        '<div class="highlight"><pre><span></span>' + common_content
    ) in content