2019-12-11 18:35:25 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-05-21 16:15:08 +00:00
|
|
|
from rich.color import Color, ColorSystem, ColorType
|
2019-12-11 18:35:25 +00:00
|
|
|
from rich import errors
|
|
|
|
from rich.style import Style, StyleStack
|
|
|
|
|
|
|
|
|
|
|
|
def test_str():
|
2020-05-21 16:15:08 +00:00
|
|
|
assert str(Style(bold=False)) == "not bold"
|
|
|
|
assert str(Style(color="red", bold=False)) == "not bold red"
|
|
|
|
assert str(Style(color="red", bold=False, italic=True)) == "not bold italic red"
|
2019-12-11 18:35:25 +00:00
|
|
|
assert str(Style()) == "none"
|
|
|
|
assert str(Style(bold=True)) == "bold"
|
|
|
|
assert str(Style(color="red", bold=True)) == "bold red"
|
2020-05-21 16:15:08 +00:00
|
|
|
assert str(Style(color="red", bgcolor="black", bold=True)) == "bold red on black"
|
|
|
|
all_styles = Style(
|
|
|
|
color="red",
|
|
|
|
bgcolor="black",
|
|
|
|
bold=True,
|
|
|
|
dim=True,
|
|
|
|
italic=True,
|
|
|
|
underline=True,
|
|
|
|
blink=True,
|
|
|
|
blink2=True,
|
|
|
|
reverse=True,
|
|
|
|
conceal=True,
|
|
|
|
strike=True,
|
|
|
|
underline2=True,
|
|
|
|
frame=True,
|
|
|
|
encircle=True,
|
|
|
|
overline=True,
|
2019-12-11 18:35:25 +00:00
|
|
|
)
|
2020-05-21 16:15:08 +00:00
|
|
|
expected = "bold dim italic underline blink blink2 reverse conceal strike underline2 frame encircle overline red on black"
|
|
|
|
assert str(all_styles) == expected
|
2020-05-14 19:15:31 +00:00
|
|
|
assert str(Style(link="foo")) == "link foo"
|
2019-12-11 18:35:25 +00:00
|
|
|
|
|
|
|
|
2020-05-21 16:15:08 +00:00
|
|
|
def test_ansi_codes():
|
|
|
|
all_styles = Style(
|
|
|
|
color="red",
|
|
|
|
bgcolor="black",
|
|
|
|
bold=True,
|
|
|
|
dim=True,
|
|
|
|
italic=True,
|
|
|
|
underline=True,
|
|
|
|
blink=True,
|
|
|
|
blink2=True,
|
|
|
|
reverse=True,
|
|
|
|
conceal=True,
|
|
|
|
strike=True,
|
|
|
|
underline2=True,
|
|
|
|
frame=True,
|
|
|
|
encircle=True,
|
|
|
|
overline=True,
|
|
|
|
)
|
|
|
|
expected = "1;2;3;4;5;6;7;8;9;21;51;52;53;31;40"
|
|
|
|
assert all_styles._make_ansi_codes(ColorSystem.TRUECOLOR) == expected
|
|
|
|
|
|
|
|
|
2019-12-11 18:35:25 +00:00
|
|
|
def test_repr():
|
2020-06-28 10:10:59 +00:00
|
|
|
assert repr(Style(bold=True, color="red")) == 'Style.parse("bold red")'
|
2019-12-11 18:35:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_eq():
|
|
|
|
assert Style(bold=True, color="red") == Style(bold=True, color="red")
|
|
|
|
assert Style(bold=True, color="red") != Style(bold=True, color="green")
|
|
|
|
assert Style().__eq__("foo") == NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
def test_hash():
|
|
|
|
assert isinstance(hash(Style()), int)
|
|
|
|
|
|
|
|
|
2020-09-23 14:53:41 +00:00
|
|
|
def test_empty():
|
2020-09-30 08:34:12 +00:00
|
|
|
assert Style.null() == Style()
|
2020-09-23 14:53:41 +00:00
|
|
|
|
|
|
|
|
2020-07-10 15:52:01 +00:00
|
|
|
def test_bool():
|
|
|
|
assert bool(Style()) is False
|
|
|
|
assert bool(Style(bold=True)) is True
|
|
|
|
assert bool(Style(color="red")) is True
|
|
|
|
assert bool(Style.parse("")) is False
|
|
|
|
|
|
|
|
|
2019-12-11 18:35:25 +00:00
|
|
|
def test_color_property():
|
|
|
|
assert Style(color="red").color == Color("red", ColorType.STANDARD, 1, None)
|
|
|
|
|
|
|
|
|
|
|
|
def test_bgcolor_property():
|
|
|
|
assert Style(bgcolor="black").bgcolor == Color("black", ColorType.STANDARD, 0, None)
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse():
|
|
|
|
assert Style.parse("") == Style()
|
|
|
|
assert Style.parse("red") == Style(color="red")
|
|
|
|
assert Style.parse("not bold") == Style(bold=False)
|
|
|
|
assert Style.parse("bold red on black") == Style(
|
|
|
|
color="red", bgcolor="black", bold=True
|
|
|
|
)
|
2020-05-14 19:15:31 +00:00
|
|
|
assert Style.parse("bold link https://example.org") == Style(
|
|
|
|
bold=True, link="https://example.org"
|
|
|
|
)
|
2019-12-11 18:35:25 +00:00
|
|
|
with pytest.raises(errors.StyleSyntaxError):
|
|
|
|
Style.parse("on")
|
|
|
|
with pytest.raises(errors.StyleSyntaxError):
|
|
|
|
Style.parse("on nothing")
|
|
|
|
with pytest.raises(errors.StyleSyntaxError):
|
|
|
|
Style.parse("rgb(999,999,999)")
|
|
|
|
with pytest.raises(errors.StyleSyntaxError):
|
|
|
|
Style.parse("not monkey")
|
2020-05-14 19:15:31 +00:00
|
|
|
with pytest.raises(errors.StyleSyntaxError):
|
|
|
|
Style.parse("link")
|
2019-12-11 18:35:25 +00:00
|
|
|
|
|
|
|
|
2020-06-30 20:40:11 +00:00
|
|
|
def test_link_id():
|
|
|
|
assert Style().link_id == ""
|
|
|
|
assert Style.parse("").link_id == ""
|
|
|
|
assert Style.parse("red").link_id == ""
|
|
|
|
style = Style.parse("red link https://example.org")
|
|
|
|
assert isinstance(style.link_id, str)
|
|
|
|
assert len(style.link_id) > 1
|
|
|
|
|
|
|
|
|
2019-12-11 18:35:25 +00:00
|
|
|
def test_get_html_style():
|
2020-05-21 16:15:08 +00:00
|
|
|
expected = "color: #7f7fbf; background-color: #800000; font-weight: bold; font-style: italic; text-decoration: underline; text-decoration: line-through; text-decoration: overline"
|
2019-12-11 18:35:25 +00:00
|
|
|
assert (
|
|
|
|
Style(
|
|
|
|
reverse=True,
|
|
|
|
dim=True,
|
|
|
|
color="red",
|
|
|
|
bgcolor="blue",
|
|
|
|
bold=True,
|
|
|
|
italic=True,
|
|
|
|
underline=True,
|
|
|
|
strike=True,
|
2020-05-21 16:15:08 +00:00
|
|
|
overline=True,
|
2019-12-11 18:35:25 +00:00
|
|
|
).get_html_style()
|
2020-05-21 16:15:08 +00:00
|
|
|
== expected
|
2019-12-11 18:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-21 16:40:31 +00:00
|
|
|
def test_chain():
|
|
|
|
assert Style.chain(Style(color="red"), Style(bold=True)) == Style(
|
2019-12-11 18:35:25 +00:00
|
|
|
color="red", bold=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_copy():
|
|
|
|
style = Style(color="red", bgcolor="black", italic=True)
|
|
|
|
assert style == style.copy()
|
|
|
|
assert style is not style.copy()
|
|
|
|
|
|
|
|
|
|
|
|
def test_render():
|
|
|
|
assert Style(color="red").render("foo", color_system=None) == "foo"
|
|
|
|
assert (
|
|
|
|
Style(color="red", bgcolor="black", bold=True).render("foo")
|
2020-03-11 00:14:16 +00:00
|
|
|
== "\x1b[1;31;40mfoo\x1b[0m"
|
2019-12-11 18:35:25 +00:00
|
|
|
)
|
|
|
|
assert Style().render("foo") == "foo"
|
|
|
|
|
|
|
|
|
|
|
|
def test_test():
|
|
|
|
Style(color="red").test("hello")
|
|
|
|
|
|
|
|
|
|
|
|
def test_add():
|
|
|
|
assert Style(color="red") + None == Style(color="red")
|
|
|
|
assert Style().__add__("foo") == NotImplemented
|
|
|
|
|
|
|
|
|
2019-12-24 12:56:38 +00:00
|
|
|
def test_iadd():
|
|
|
|
style = Style(color="red")
|
|
|
|
style += Style(bold=True)
|
|
|
|
assert style == Style(color="red", bold=True)
|
|
|
|
style += None
|
|
|
|
assert style == Style(color="red", bold=True)
|
|
|
|
|
|
|
|
|
2019-12-11 18:35:25 +00:00
|
|
|
def test_style_stack():
|
|
|
|
stack = StyleStack(Style(color="red"))
|
|
|
|
repr(stack)
|
|
|
|
assert stack.current == Style(color="red")
|
|
|
|
stack.push(Style(bold=True))
|
|
|
|
assert stack.current == Style(color="red", bold=True)
|
|
|
|
stack.pop()
|
|
|
|
assert stack.current == Style(color="red")
|
2020-04-02 15:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pick_first():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Style.pick_first()
|
2020-10-15 20:17:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_background_style():
|
|
|
|
assert Style(bold=True, color="yellow", bgcolor="red").background_style == Style(
|
|
|
|
bgcolor="red"
|
|
|
|
)
|
2021-01-09 16:08:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_without_color():
|
|
|
|
style = Style(bold=True, color="red", bgcolor="blue")
|
|
|
|
colorless_style = style.without_color
|
|
|
|
assert colorless_style.color == None
|
|
|
|
assert colorless_style.bgcolor == None
|
|
|
|
assert colorless_style.bold == True
|
|
|
|
null_style = Style.null()
|
|
|
|
assert null_style.without_color == null_style
|