", html)
def test_lineanchors_with_startnum():
optdict = dict(lineanchors="foo", linenostart=5)
outfile = StringIO()
fmt = HtmlFormatter(**optdict)
fmt.format(tokensource, outfile)
html = outfile.getvalue()
assert re.search("\\s*\\s*\\s*", html)
def test_valid_output():
# test all available wrappers
fmt = HtmlFormatter(full=True, linenos=True, noclasses=True,
outencoding='utf-8')
handle, pathname = tempfile.mkstemp('.html')
with os.fdopen(handle, 'w+b') as tfile:
fmt.format(tokensource, tfile)
catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc')
try:
import subprocess
po = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname],
stdout=subprocess.PIPE)
ret = po.wait()
output = po.stdout.read()
po.stdout.close()
except OSError:
# nsgmls not available
pass
else:
if ret:
print(output)
assert not ret, 'nsgmls run reported errors'
os.unlink(pathname)
def test_get_style_defs_contains_pre_style():
style_defs = HtmlFormatter().get_style_defs().splitlines()
assert style_defs[0] == 'pre { line-height: 125%; }'
def test_get_style_defs_contains_default_line_numbers_styles():
style_defs = HtmlFormatter().get_style_defs().splitlines()
assert style_defs[1] == (
'td.linenos .normal '
'{ color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }'
)
assert style_defs[2] == (
'span.linenos '
'{ color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }'
)
def test_get_style_defs_contains_style_specific_line_numbers_styles():
class TestStyle(Style):
line_number_color = '#ff0000'
line_number_background_color = '#0000ff'
line_number_special_color = '#00ff00'
line_number_special_background_color = '#ffffff'
style_defs = HtmlFormatter(style=TestStyle).get_style_defs().splitlines()
assert style_defs[1] == (
'td.linenos .normal '
'{ color: #ff0000; background-color: #0000ff; padding-left: 5px; padding-right: 5px; }'
)
assert style_defs[2] == (
'span.linenos '
'{ color: #ff0000; background-color: #0000ff; padding-left: 5px; padding-right: 5px; }'
)
assert style_defs[3] == (
'td.linenos .special '
'{ color: #00ff00; background-color: #ffffff; padding-left: 5px; padding-right: 5px; }'
)
assert style_defs[4] == (
'span.linenos.special '
'{ color: #00ff00; background-color: #ffffff; padding-left: 5px; padding-right: 5px; }'
)
@pytest.mark.parametrize(
"formatter_kwargs, style_defs_args, assert_starts_with, assert_contains",
[
[{}, [], ".", []],
[{"cssclass": "foo"}, [], ".foo .", []],
[{"cssclass": "foo"}, [".bar"], ".bar .", []],
[{"cssclass": "foo"}, [[".bar", ".baz"]], ".ba", [".bar .", ".baz ."]],
]
)
def test_get_token_style_defs_uses_css_prefix(
formatter_kwargs, style_defs_args, assert_starts_with, assert_contains
):
formatter = HtmlFormatter(**formatter_kwargs)
for line in formatter.get_token_style_defs(*style_defs_args):
assert line.startswith(assert_starts_with)
for s in assert_contains:
assert s in line
def test_get_background_style_defs_uses_multiple_css_prefixes():
formatter = HtmlFormatter()
lines = formatter.get_background_style_defs([".foo", ".bar"])
assert lines[0].startswith(".foo .hll, .bar .hll {")
assert lines[1].startswith(".foo , .bar {")
def test_unicode_options():
fmt = HtmlFormatter(title='Föö',
cssclass='bär',
cssstyles='div:before { content: \'bäz\' }',
encoding='utf-8')
handle, pathname = tempfile.mkstemp('.html')
with os.fdopen(handle, 'w+b') as tfile:
fmt.format(tokensource, tfile)
def test_ctags():
try:
import ctags
except ImportError:
# we can't check without the ctags module, but at least check the exception
assert pytest.raises(
RuntimeError, HtmlFormatter, tagsfile='support/tags'
)
else:
# this tagfile says that test_ctags() is on line 165, even if it isn't
# anymore in the actual source
fmt = HtmlFormatter(tagsfile='support/tags', lineanchors='L',
tagurlformat='%(fname)s%(fext)s')
outfile = StringIO()
fmt.format(tokensource, outfile)
assert 'test_ctags' \
in outfile.getvalue()
def test_filename():
optdict = dict(filename="test.py")
outfile = StringIO()
fmt = HtmlFormatter(**optdict)
fmt.format(tokensource, outfile)
html = outfile.getvalue()
assert re.search("test.py", html)
def test_debug_token_types():
fmt_nod_token_types = HtmlFormatter(debug_token_types=False)
outfile_nod_token_types = StringIO()
fmt_nod_token_types.format(tokensource, outfile_nod_token_types)
html_nod_token_types = outfile_nod_token_types.getvalue()
assert 'TESTDIR' not in html_nod_token_types
fmt_debug_token_types = HtmlFormatter(debug_token_types=True)
outfile_debug_token_types = StringIO()
fmt_debug_token_types.format(tokensource, outfile_debug_token_types)
html_debug_token_types = outfile_debug_token_types.getvalue()
assert 'TESTDIR' in html_debug_token_types