Skip to content

Commit 5ff3740

Browse files
committed
Adapt tests for Pygments 2.19
1 parent d59084a commit 5ff3740

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

tests/test_builders/test_build_html_code.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import pygments
34
import pytest
45

56

@@ -34,11 +35,16 @@ def test_html_codeblock_linenos_style_inline(app):
3435

3536
@pytest.mark.sphinx('html', testroot='reST-code-role')
3637
def test_html_code_role(app):
38+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
39+
sp = '<span class="w"> </span>'
40+
else:
41+
sp = ' '
42+
3743
app.build()
3844
content = (app.outdir / 'index.html').read_text(encoding='utf8')
3945

4046
common_content = (
41-
'<span class="k">def</span> <span class="nf">foo</span>'
47+
f'<span class="k">def</span>{sp}<span class="nf">foo</span>'
4248
'<span class="p">(</span>'
4349
'<span class="mi">1</span> '
4450
'<span class="o">+</span> '

tests/test_builders/test_build_latex.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from shutil import copyfile
1212
from subprocess import CalledProcessError
1313

14+
import pygments
1415
import pytest
1516

1617
from sphinx.builders.latex import default_latex_documents
@@ -2115,12 +2116,16 @@ def test_latex_container(app):
21152116

21162117
@pytest.mark.sphinx('latex', testroot='reST-code-role')
21172118
def test_latex_code_role(app):
2119+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
2120+
sp = r'\PYG{+w}{ }'
2121+
else:
2122+
sp = ' '
2123+
21182124
app.build()
21192125
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
21202126

21212127
common_content = (
2122-
r'\PYG{k}{def} '
2123-
r'\PYG{n+nf}{foo}'
2128+
r'\PYG{k}{def}' + sp + r'\PYG{n+nf}{foo}'
21242129
r'\PYG{p}{(}'
21252130
r'\PYG{l+m+mi}{1} '
21262131
r'\PYG{o}{+} '

tests/test_directives/test_directive_code.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import pygments
56
import pytest
67
from docutils import nodes
78

@@ -394,6 +395,11 @@ def test_literal_include_block_start_with_comment_or_brank(app):
394395

395396
@pytest.mark.sphinx('html', testroot='directive-code')
396397
def test_literal_include_linenos(app):
398+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
399+
sp = '<span class="w"> </span>'
400+
else:
401+
sp = ' '
402+
397403
app.build(filenames=[app.srcdir / 'linenos.rst'])
398404
html = (app.outdir / 'linenos.html').read_text(encoding='utf8')
399405

@@ -411,7 +417,7 @@ def test_literal_include_linenos(app):
411417

412418
# :lines: 5-9
413419
assert (
414-
'<span class="linenos">5</span><span class="k">class</span> '
420+
f'<span class="linenos">5</span><span class="k">class</span>{sp}'
415421
'<span class="nc">Foo</span><span class="p">:</span>'
416422
) in html
417423

@@ -556,12 +562,17 @@ def test_code_block_highlighted(app):
556562

557563
@pytest.mark.sphinx('html', testroot='directive-code')
558564
def test_linenothreshold(app):
565+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
566+
sp = '<span class="w"> </span>'
567+
else:
568+
sp = ' '
569+
559570
app.build(filenames=[app.srcdir / 'linenothreshold.rst'])
560571
html = (app.outdir / 'linenothreshold.html').read_text(encoding='utf8')
561572

562573
# code-block using linenothreshold
563574
assert (
564-
'<span class="linenos">1</span><span class="k">class</span> '
575+
f'<span class="linenos">1</span><span class="k">class</span>{sp}'
565576
'<span class="nc">Foo</span><span class="p">:</span>'
566577
) in html
567578

tests/test_extensions/test_ext_viewcode.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
import shutil
77
from typing import TYPE_CHECKING
88

9+
import pygments
910
import pytest
1011

1112
if TYPE_CHECKING:
1213
from sphinx.testing.util import SphinxTestApp
1314

1415

1516
def check_viewcode_output(app: SphinxTestApp) -> str:
17+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
18+
sp = '<span> </span>'
19+
else:
20+
sp = ' '
21+
1622
warnings = re.sub(r'\\+', '/', app.warning.getvalue())
1723
assert re.findall(
1824
r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
@@ -41,7 +47,7 @@ def check_viewcode_output(app: SphinxTestApp) -> str:
4147
'<a class="viewcode-back" href="../../index.html#spam.Class1">[docs]</a>\n'
4248
) in result
4349
assert '<span>@decorator</span>\n' in result
44-
assert '<span>class</span> <span>Class1</span><span>:</span>\n' in result
50+
assert f'<span>class</span>{sp}<span>Class1</span><span>:</span>\n' in result
4551
assert '<span> </span><span>&quot;&quot;&quot;</span>\n' in result
4652
assert '<span> this is Class1</span>\n' in result
4753
assert '<span> &quot;&quot;&quot;</span>\n' in result

tests/test_highlighting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from sphinx.highlighting import PygmentsBridge
1414

15-
if tuple(map(int, pygments.__version__.split('.')[:2]))[:2] < (2, 18):
15+
if tuple(map(int, pygments.__version__.split('.')[:2])) < (2, 18):
1616
from pygments.formatter import Formatter
1717

1818
Formatter.__class_getitem__ = classmethod(lambda cls, name: cls) # type: ignore[attr-defined]

tests/test_intl/test_intl.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import time
1212
from typing import TYPE_CHECKING
1313

14+
import pygments
1415
import pytest
1516
from babel.messages import mofile, pofile
1617
from babel.messages.catalog import Catalog
@@ -1487,6 +1488,11 @@ def test_xml_strange_markup(app):
14871488
@pytest.mark.sphinx('html', testroot='intl')
14881489
@pytest.mark.test_params(shared_result='test_intl_basic')
14891490
def test_additional_targets_should_not_be_translated(app):
1491+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
1492+
sp = '<span class="w"> </span>'
1493+
else:
1494+
sp = ' '
1495+
14901496
app.build()
14911497
# [literalblock.txt]
14921498
result = (app.outdir / 'literalblock.html').read_text(encoding='utf8')
@@ -1525,7 +1531,7 @@ def test_additional_targets_should_not_be_translated(app):
15251531
# doctest block should not be translated but be highlighted
15261532
expected_expr = (
15271533
"""<span class="gp">&gt;&gt;&gt; </span>"""
1528-
"""<span class="kn">import</span> <span class="nn">sys</span> """
1534+
f"""<span class="kn">import</span>{sp}<span class="nn">sys</span> """
15291535
"""<span class="c1"># sys importing</span>"""
15301536
)
15311537
assert_count(expected_expr, result, 1)
@@ -1570,6 +1576,11 @@ def test_additional_targets_should_not_be_translated(app):
15701576
},
15711577
)
15721578
def test_additional_targets_should_be_translated(app):
1579+
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
1580+
sp = '<span class="w"> </span>'
1581+
else:
1582+
sp = ' '
1583+
15731584
app.build()
15741585
# [literalblock.txt]
15751586
result = (app.outdir / 'literalblock.html').read_text(encoding='utf8')
@@ -1619,7 +1630,7 @@ def test_additional_targets_should_be_translated(app):
16191630
# doctest block should not be translated but be highlighted
16201631
expected_expr = (
16211632
"""<span class="gp">&gt;&gt;&gt; </span>"""
1622-
"""<span class="kn">import</span> <span class="nn">sys</span> """
1633+
f"""<span class="kn">import</span>{sp}<span class="nn">sys</span> """
16231634
"""<span class="c1"># SYS IMPORTING</span>"""
16241635
)
16251636
assert_count(expected_expr, result, 1)

0 commit comments

Comments
 (0)