diff options
author | Paweł Fertyk <pfertyk@users.noreply.github.com> | 2020-07-04 18:10:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-04 18:10:03 +0200 |
commit | 728fd19ebcb69ddb2c7af159939e1a19ae53a892 (patch) | |
tree | ed0c18f61887b61a9c4f41cf021a202dc92c3ad1 /tests/test_html_formatter.py | |
parent | b5577a68a9c286f4bce157d63e8c83ed8d70e704 (diff) | |
download | pygments-git-728fd19ebcb69ddb2c7af159939e1a19ae53a892.tar.gz |
Fix Solarized line number colors (#1477)
* Add font and background colors to Style
* Move all styles to get_style_defs, add tests
* Remove hardcoded styles, add special lineno style
* Add styles for special line numbers in tables
* Update noclasses documentation
* Refactor linenos elements and styles, add tests
* Update AUTHORS
* Fix multiple CSS prefixes, add tests
Diffstat (limited to 'tests/test_html_formatter.py')
-rw-r--r-- | tests/test_html_formatter.py | 113 |
1 files changed, 78 insertions, 35 deletions
diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index 83b08e1f..75986db5 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -11,14 +11,15 @@ import io import os import re import tempfile -from os import path from io import StringIO +from os import path -from pytest import raises +import pytest -from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter, NullFormatter from pygments.formatters.html import escape_html +from pygments.lexers import PythonLexer +from pygments.style import Style TESTDIR = path.dirname(path.abspath(__file__)) TESTFILE = path.join(TESTDIR, 'test_html_formatter.py') @@ -94,24 +95,6 @@ def test_all_options(): check(optdict) -def test_linenos(): - optdict = dict(linenos=True) - outfile = StringIO() - fmt = HtmlFormatter(**optdict) - fmt.format(tokensource, outfile) - html = outfile.getvalue() - assert re.search(r"<pre>\s+1\s+2\s+3", html) - - -def test_linenos_with_startnum(): - optdict = dict(linenos=True, linenostart=5) - outfile = StringIO() - fmt = HtmlFormatter(**optdict) - fmt.format(tokensource, outfile) - html = outfile.getvalue() - assert re.search(r"<pre>\s+5\s+6\s+7", html) - - def test_lineanchors(): optdict = dict(lineanchors="foo") outfile = StringIO() @@ -157,19 +140,77 @@ def test_valid_output(): os.unlink(pathname) -def test_get_style_defs(): - fmt = HtmlFormatter() - sd = fmt.get_style_defs() - assert sd.startswith('.') - - fmt = HtmlFormatter(cssclass='foo') - sd = fmt.get_style_defs() - assert sd.startswith('.foo') - sd = fmt.get_style_defs('.bar') - assert sd.startswith('.bar') - sd = fmt.get_style_defs(['.bar', '.baz']) - fl = sd.splitlines()[0] - assert '.bar' in fl and '.baz' in fl +def test_get_style_defs_contains_pre_style(): + style_defs = HtmlFormatter().get_style_defs().splitlines() + assert style_defs[0] == 'pre { line-height: 125%; margin: 0; }' + + +def test_get_style_defs_contains_default_line_numbers_styles(): + style_defs = HtmlFormatter().get_style_defs().splitlines() + + assert style_defs[1] == ( + 'td.linenos pre ' + '{ color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }' + ) + assert style_defs[2] == ( + 'span.linenos ' + '{ color: #000000; background-color: #f0f0f0; padding: 0 5px 0 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 pre ' + '{ color: #ff0000; background-color: #0000ff; padding: 0 5px 0 5px; }' + ) + assert style_defs[2] == ( + 'span.linenos ' + '{ color: #ff0000; background-color: #0000ff; padding: 0 5px 0 5px; }' + ) + assert style_defs[3] == ( + 'td.linenos pre.special ' + '{ color: #00ff00; background-color: #ffffff; padding: 0 5px 0 5px; }' + ) + assert style_defs[4] == ( + 'span.linenos.special ' + '{ color: #00ff00; background-color: #ffffff; padding: 0 5px 0 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(): @@ -187,7 +228,9 @@ def test_ctags(): import ctags except ImportError: # we can't check without the ctags module, but at least check the exception - assert raises(RuntimeError, HtmlFormatter, tagsfile='support/tags') + 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 |