summaryrefslogtreecommitdiff
path: root/tests/test_html_formatter_linenos_elements.py
blob: af43c4b49d25928ee540d18ab19ee8957620fb8c (plain)
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
import os
from io import StringIO

import pytest

from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer

from .support import structural_diff

TESTDIR = os.path.dirname(os.path.abspath(__file__))
EXPECTED_OUTPUT_DIR = os.path.join(TESTDIR, "html_linenos_expected_output")
CODE = list(PythonLexer().get_tokens("# a\n# b\n# c"))


def single_line(text):
    return "".join(l.strip() for l in text.splitlines())


# Note: option `anchorlinenos` is currently ignored for `linenos=inline`
@pytest.mark.parametrize("linenos", ["inline", "table"])
@pytest.mark.parametrize("noclasses", ["False", "True"])
@pytest.mark.parametrize("linenostep", ["1", "2"])
@pytest.mark.parametrize("linenostart", ["1", "8"])
@pytest.mark.parametrize("linenospecial", ["0", "3"])
@pytest.mark.parametrize("anchorlinenos", ["False", "True"])
@pytest.mark.parametrize("filename", ["", "testfilename"])
def test_linenos_elements(
    linenos, noclasses, linenostep, linenostart, linenospecial, anchorlinenos, filename
):
    options = dict(
        linenos=linenos,
        noclasses=noclasses,
        linenostep=linenostep,
        linenostart=linenostart,
        linenospecial=linenospecial,
        anchorlinenos=anchorlinenos,
        filename=filename,
    )

    output = StringIO()
    fmt = HtmlFormatter(**options)
    fmt.format(CODE, output)
    html = output.getvalue()

    filename_parts = []
    filename_parts.append(linenos)
    filename_parts.append("nocls" if noclasses == "True" else "cls")
    filename_parts.append("step_" + linenostep)
    filename_parts.append("start_" + linenostart)
    filename_parts.append("special_" + linenospecial)
    filename_parts.append("anchor" if anchorlinenos == "True" else "noanchor")
    filename_parts.append("filename" if filename else "nofilename")
    expected_html_filename = "_".join(filename_parts) + ".html"

    # with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename), 'w') as f:
    #     import bs4 as BeautifulSoup
    #     f.write(str(BeautifulSoup.BeautifulSoup(html, 'html.parser')))

    with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename),
              encoding="utf-8") as f:
        expected_html = f.read()

    structural_diff.structural_diff(html, expected_html)