Skip to content

Commit

Permalink
Merge pull request Textualize#334 from phase06/master
Browse files Browse the repository at this point in the history
add tests for syntax
  • Loading branch information
willmcgugan authored Oct 10, 2020
2 parents af7fbf9 + eb49f84 commit 9c5d790
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tests/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def replace_link_ids(render: str) -> str:
return re_link_ids.sub("id=0;foo\x1b", render)


def render(renderable: RenderableType) -> str:
def render(renderable: RenderableType, no_wrap: bool = False) -> str:
console = Console(
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False
)
console.print(renderable)
console.print(renderable, no_wrap=no_wrap)
output = replace_link_ids(console.file.getvalue())
return output
7 changes: 6 additions & 1 deletion tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from rich.console import Console
from rich.pretty import install, pretty_repr
from rich.pretty import install, pretty_repr, Node


def test_install():
Expand Down Expand Up @@ -72,3 +72,8 @@ def test_array():

def test_tuple_of_one():
assert pretty_repr((1,)) == "(1,)"


def test_node():
node = Node("abc")
assert pretty_repr(node) == "abc: "
107 changes: 106 additions & 1 deletion tests/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

from rich.panel import Panel
from rich.style import Style
from rich.syntax import Syntax, ANSISyntaxTheme
from rich.syntax import (
Syntax,
ANSISyntaxTheme,
PygmentsSyntaxTheme,
Color,
Console,
ConsoleOptions,
)


CODE = '''
Expand Down Expand Up @@ -46,6 +53,104 @@ def test_python_render():
assert rendered_syntax == expected


def test_pygments_syntax_theme_non_str():
from pygments.style import Style as PygmentsStyle

style = PygmentsSyntaxTheme(PygmentsStyle())
assert style.get_background_style().bgcolor == Color.parse("#ffffff")


def test_pygments_syntax_theme():
style = PygmentsSyntaxTheme("default")
assert style.get_style_for_token("abc") == Style.parse("none")


def test_get_line_color_none():
style = PygmentsSyntaxTheme("default")
style._background_style = Style(bgcolor=None)
syntax = Syntax(
CODE,
lexer_name="python",
line_numbers=True,
line_range=(2, 10),
theme=style,
code_width=60,
word_wrap=True,
background_color="red",
)
assert syntax._get_line_numbers_color() == Color.default()


def test_highlight_background_color():
syntax = Syntax(
CODE,
lexer_name="python",
line_numbers=True,
line_range=(2, 10),
theme="foo",
code_width=60,
word_wrap=True,
background_color="red",
)
assert syntax.highlight(CODE).style == Style.parse("on red")


def test_get_number_styles():
syntax = Syntax(CODE, "python", theme="monokai", line_numbers=True)
console = Console(color_system="windows")
assert syntax._get_number_styles(console=console) == (
Style.parse("on #272822"),
Style.parse("dim on #272822"),
Style.parse("not dim on #272822"),
)


def test_get_style_for_token():
# from pygments.style import Style as PygmentsStyle
# pygments_style = PygmentsStyle()
from pygments.style import Token

style = PygmentsSyntaxTheme("default")
style_dict = {Token.Text: Style(color=None)}
style._style_cache = style_dict
syntax = Syntax(
CODE,
lexer_name="python",
line_numbers=True,
line_range=(2, 10),
theme=style,
code_width=60,
word_wrap=True,
background_color="red",
)
assert syntax._get_line_numbers_color() == Color.default()


def test_option_no_wrap():

from rich.console import Console

console = Console

syntax = Syntax(
CODE,
lexer_name="python",
line_numbers=True,
line_range=(2, 10),
code_width=60,
word_wrap=False,
background_color="red",
)

rendered_syntax = render(syntax, True)
# print(repr(rendered_syntax))

expected = '\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 2 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;230;219;116;41m"""Iterate and generate a tuple with a flag for first and last value."""\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 3 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41miter_values\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;249;38;114;41m=\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41miter\x1b[0m\x1b[38;2;248;248;242;41m(\x1b[0m\x1b[38;2;248;248;242;41mvalues\x1b[0m\x1b[38;2;248;248;242;41m)\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 4 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mtry\x1b[0m\x1b[38;2;248;248;242;41m:\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 5 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mprevious_value\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;249;38;114;41m=\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mnext\x1b[0m\x1b[38;2;248;248;242;41m(\x1b[0m\x1b[38;2;248;248;242;41miter_values\x1b[0m\x1b[38;2;248;248;242;41m)\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 6 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mexcept\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;166;226;46;41mStopIteration\x1b[0m\x1b[38;2;248;248;242;41m:\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 7 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mreturn\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 8 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mfirst\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;249;38;114;41m=\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mTrue\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m 9 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mfor\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mvalue\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;249;38;114;41min\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41miter_values\x1b[0m\x1b[38;2;248;248;242;41m:\x1b[0m\n\x1b[1;38;2;227;227;221;48;2;39;40;34m \x1b[0m\x1b[38;2;101;102;96;48;2;39;40;34m10 \x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41myield\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mfirst\x1b[0m\x1b[38;2;248;248;242;41m,\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;102;217;239;41mFalse\x1b[0m\x1b[38;2;248;248;242;41m,\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;248;248;242;41mprevious_value\x1b[0m\n'
# console.print(syntax, no_wrap=True)

assert rendered_syntax == expected


def test_ansi_theme():
style = Style(color="red")
theme = ANSISyntaxTheme({("foo", "bar"): style})
Expand Down

0 comments on commit 9c5d790

Please sign in to comment.