Debian Codegraph @

tests/test_html_formatter_linenos_elements.py

raw
1 import os
2 from io import StringIO
3
4 import pytest
5
6 from pygments.formatters import HtmlFormatter
7 from pygments.lexers import PythonLexer
8
9 from .support import structural_diff
10
11 TESTDIR = os.path.dirname(os.path.abspath(__file__))
12 EXPECTED_OUTPUT_DIR = os.path.join(TESTDIR, "html_linenos_expected_output")
13 CODE = list(PythonLexer().get_tokens("# a\n# b\n# c"))
14
15
16 def single_line(text):
17 return "".join(line.strip() for line in text.splitlines())
18
19
20 # Note: option `anchorlinenos` is currently ignored for `linenos=inline`
21 @pytest.mark.parametrize("linenos", ["inline", "table"])
22 @pytest.mark.parametrize("noclasses", ["False", "True"])
23 @pytest.mark.parametrize("linenostep", ["1", "2"])
24 @pytest.mark.parametrize("linenostart", ["1", "8"])
25 @pytest.mark.parametrize("linenospecial", ["0", "3"])
26 @pytest.mark.parametrize("anchorlinenos", ["False", "True"])
27 @pytest.mark.parametrize("filename", ["", "testfilename"])
28 def test_linenos_elements(
29 linenos, noclasses, linenostep, linenostart, linenospecial, anchorlinenos, filename
30 ):
31 options = dict(
32 linenos=linenos,
33 noclasses=noclasses,
34 linenostep=linenostep,
35 linenostart=linenostart,
36 linenospecial=linenospecial,
37 anchorlinenos=anchorlinenos,
38 filename=filename,
39 )
40
41 output = StringIO()
42 fmt = HtmlFormatter(**options)
43 fmt.format(CODE, output)
44 html = output.getvalue()
45
46 filename_parts = []
47 filename_parts.append(linenos)
48 filename_parts.append("nocls" if noclasses == "True" else "cls")
49 filename_parts.append("step_" + linenostep)
50 filename_parts.append("start_" + linenostart)
51 filename_parts.append("special_" + linenospecial)
52 filename_parts.append("anchor" if anchorlinenos == "True" else "noanchor")
53 filename_parts.append("filename" if filename else "nofilename")
54 expected_html_filename = "_".join(filename_parts) + ".html"
55
56 # with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename), 'w') as f:
57 # import bs4 as BeautifulSoup
58 # f.write(str(BeautifulSoup.BeautifulSoup(html, 'html.parser')))
59
60 with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename),
61 encoding="utf-8") as f:
62 expected_html = f.read()
63
64 structural_diff.structural_diff(html, expected_html)