Debian Codegraph @

tests/test_builders/test_build_html_code.py

raw
1 from __future__ import annotations
2
3 from typing import TYPE_CHECKING
4
5 import pygments
6 import pytest
7
8 if TYPE_CHECKING:
9 from sphinx.testing.util import SphinxTestApp
10
11
12 @pytest.mark.sphinx(
13 'html',
14 testroot='reST-code-block',
15 confoverrides={'html_codeblock_linenos_style': 'table'},
16 )
17 def test_html_codeblock_linenos_style_table(app: SphinxTestApp) -> None:
18 app.build()
19 content = (app.outdir / 'index.html').read_text(encoding='utf8')
20
21 assert (
22 '<div class="linenodiv"><pre><span class="normal">1</span>\n'
23 '<span class="normal">2</span>\n'
24 '<span class="normal">3</span>\n'
25 '<span class="normal">4</span></pre></div>'
26 ) in content
27
28
29 @pytest.mark.sphinx(
30 'html',
31 testroot='reST-code-block',
32 confoverrides={'html_codeblock_linenos_style': 'inline'},
33 )
34 def test_html_codeblock_linenos_style_inline(app: SphinxTestApp) -> None:
35 app.build()
36 content = (app.outdir / 'index.html').read_text(encoding='utf8')
37
38 assert '<span class="linenos">1</span>' in content
39
40
41 @pytest.mark.sphinx('html', testroot='reST-code-role')
42 def test_html_code_role(app: SphinxTestApp) -> None:
43 if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
44 sp = '<span class="w"> </span>'
45 else:
46 sp = ' '
47
48 app.build()
49 content = (app.outdir / 'index.html').read_text(encoding='utf8')
50
51 common_content = (
52 f'<span class="k">def</span>{sp}<span class="nf">foo</span>'
53 '<span class="p">(</span>'
54 '<span class="mi">1</span> '
55 '<span class="o">+</span> '
56 '<span class="mi">2</span> '
57 '<span class="o">+</span> '
58 '<span class="kc">None</span> '
59 '<span class="o">+</span> '
60 '<span class="s2">&quot;abc&quot;</span>'
61 '<span class="p">):</span> '
62 '<span class="k">pass</span>'
63 )
64 assert (
65 '<p>Inline <code class="code highlight python docutils literal highlight-python">'
66 + common_content
67 + '</code> code block</p>'
68 ) in content
69 assert (
70 '<div class="highlight-python notranslate">'
71 '<div class="highlight"><pre><span></span>' + common_content
72 ) in content