rich/tests/test_style.py

268 lines
7.3 KiB
Python
Raw Permalink Normal View History

2019-12-11 18:35:25 +00:00
import pytest
from rich import errors
2022-06-20 10:19:45 +00:00
from rich.color import Color, ColorSystem, ColorType
2019-12-11 18:35:25 +00:00
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():
2021-06-18 19:24:05 +00:00
assert (
repr(Style(bold=True, color="red"))
== "Style(color=Color('red', ColorType.STANDARD, number=1), bold=True)"
)
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():
2021-02-20 11:05:21 +00:00
expected = "color: #7f7fbf; text-decoration-color: #7f7fbf; background-color: #800000; font-weight: bold; font-style: italic; text-decoration: underline; text-decoration: line-through; text-decoration: overline"
html_style = Style(
reverse=True,
dim=True,
color="red",
bgcolor="blue",
bold=True,
italic=True,
underline=True,
strike=True,
overline=True,
).get_html_style()
print(repr(html_style))
assert html_style == 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")
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()
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()
2021-01-09 16:15:15 +00:00
assert null_style.without_color == null_style
2021-06-18 19:24:05 +00:00
def test_meta():
style = Style(bold=True, meta={"foo": "bar"})
assert style.meta["foo"] == "bar"
style += Style(meta={"egg": "baz"})
assert style.meta == {"foo": "bar", "egg": "baz"}
assert repr(style) == "Style(bold=True, meta={'foo': 'bar', 'egg': 'baz'})"
2021-07-19 20:34:41 +00:00
def test_from_meta():
style = Style.from_meta({"foo": "bar"})
assert style.color is None
2021-07-19 20:50:22 +00:00
assert style.bold is None
2021-07-28 08:10:23 +00:00
def test_on():
style = Style.on({"foo": "bar"}, click="CLICK") + Style(color="red")
2021-07-28 08:18:21 +00:00
assert style.meta == {"foo": "bar", "@click": "CLICK"}
2023-03-27 10:38:32 +00:00
def test_clear_meta_and_links():
style = Style.parse("bold red on black link https://example.org") + Style.on(
click="CLICK"
)
assert style.meta == {"@click": "CLICK"}
assert style.link == "https://example.org"
assert style.color == Color.parse("red")
assert style.bgcolor == Color.parse("black")
assert style.bold
assert not style.italic
clear_style = style.clear_meta_and_links()
assert clear_style.meta == {}
assert clear_style.link == None
assert clear_style.color == Color.parse("red")
assert clear_style.bgcolor == Color.parse("black")
assert clear_style.bold
assert not clear_style.italic
def test_clear_meta_and_links_clears_hash():
"""Regression test for https://github.com/Textualize/rich/issues/2942."""
style = Style.parse("bold red on black link https://example.org") + Style.on(
click="CLICK"
)
hash(style) # Force hash caching.
assert style._hash is not None
clear_style = style.clear_meta_and_links()
assert clear_style._hash is None