| 1 |
import pytest |
| 2 |
|
| 3 |
from rich import errors |
| 4 |
from rich.color import Color, ColorSystem, ColorType |
| 5 |
from rich.style import Style, StyleStack |
| 6 |
|
| 7 |
|
| 8 |
def test_str(): |
| 9 |
assert str(Style(bold=False)) == "not bold" |
| 10 |
assert str(Style(color="red", bold=False)) == "not bold red" |
| 11 |
assert str(Style(color="red", bold=False, italic=True)) == "not bold italic red" |
| 12 |
assert str(Style()) == "none" |
| 13 |
assert str(Style(bold=True)) == "bold" |
| 14 |
assert str(Style(color="red", bold=True)) == "bold red" |
| 15 |
assert str(Style(color="red", bgcolor="black", bold=True)) == "bold red on black" |
| 16 |
all_styles = Style( |
| 17 |
color="red", |
| 18 |
bgcolor="black", |
| 19 |
bold=True, |
| 20 |
dim=True, |
| 21 |
italic=True, |
| 22 |
underline=True, |
| 23 |
blink=True, |
| 24 |
blink2=True, |
| 25 |
reverse=True, |
| 26 |
conceal=True, |
| 27 |
strike=True, |
| 28 |
underline2=True, |
| 29 |
frame=True, |
| 30 |
encircle=True, |
| 31 |
overline=True, |
| 32 |
) |
| 33 |
expected = "bold dim italic underline blink blink2 reverse conceal strike underline2 frame encircle overline red on black" |
| 34 |
assert str(all_styles) == expected |
| 35 |
assert str(Style(link="foo")) == "link foo" |
| 36 |
|
| 37 |
|
| 38 |
def test_ansi_codes(): |
| 39 |
all_styles = Style( |
| 40 |
color="red", |
| 41 |
bgcolor="black", |
| 42 |
bold=True, |
| 43 |
dim=True, |
| 44 |
italic=True, |
| 45 |
underline=True, |
| 46 |
blink=True, |
| 47 |
blink2=True, |
| 48 |
reverse=True, |
| 49 |
conceal=True, |
| 50 |
strike=True, |
| 51 |
underline2=True, |
| 52 |
frame=True, |
| 53 |
encircle=True, |
| 54 |
overline=True, |
| 55 |
) |
| 56 |
expected = "1;2;3;4;5;6;7;8;9;21;51;52;53;31;40" |
| 57 |
assert all_styles._make_ansi_codes(ColorSystem.TRUECOLOR) == expected |
| 58 |
|
| 59 |
|
| 60 |
def test_repr(): |
| 61 |
assert ( |
| 62 |
repr(Style(bold=True, color="red")) |
| 63 |
== "Style(color=Color('red', ColorType.STANDARD, number=1), bold=True)" |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def test_eq(): |
| 68 |
assert Style(bold=True, color="red") == Style(bold=True, color="red") |
| 69 |
assert Style(bold=True, color="red") != Style(bold=True, color="green") |
| 70 |
assert Style().__eq__("foo") == NotImplemented |
| 71 |
|
| 72 |
|
| 73 |
def test_hash(): |
| 74 |
assert isinstance(hash(Style()), int) |
| 75 |
|
| 76 |
|
| 77 |
def test_empty(): |
| 78 |
assert Style.null() == Style() |
| 79 |
|
| 80 |
|
| 81 |
def test_bool(): |
| 82 |
assert bool(Style()) is False |
| 83 |
assert bool(Style(bold=True)) is True |
| 84 |
assert bool(Style(color="red")) is True |
| 85 |
assert bool(Style.parse("")) is False |
| 86 |
|
| 87 |
|
| 88 |
def test_color_property(): |
| 89 |
assert Style(color="red").color == Color("red", ColorType.STANDARD, 1, None) |
| 90 |
|
| 91 |
|
| 92 |
def test_bgcolor_property(): |
| 93 |
assert Style(bgcolor="black").bgcolor == Color("black", ColorType.STANDARD, 0, None) |
| 94 |
|
| 95 |
|
| 96 |
def test_parse(): |
| 97 |
assert Style.parse("") == Style() |
| 98 |
assert Style.parse("red") == Style(color="red") |
| 99 |
assert Style.parse("not bold") == Style(bold=False) |
| 100 |
assert Style.parse("bold red on black") == Style( |
| 101 |
color="red", bgcolor="black", bold=True |
| 102 |
) |
| 103 |
assert Style.parse("bold link https://example.org") == Style( |
| 104 |
bold=True, link="https://example.org" |
| 105 |
) |
| 106 |
with pytest.raises(errors.StyleSyntaxError): |
| 107 |
Style.parse("on") |
| 108 |
with pytest.raises(errors.StyleSyntaxError): |
| 109 |
Style.parse("on nothing") |
| 110 |
with pytest.raises(errors.StyleSyntaxError): |
| 111 |
Style.parse("rgb(999,999,999)") |
| 112 |
with pytest.raises(errors.StyleSyntaxError): |
| 113 |
Style.parse("not monkey") |
| 114 |
with pytest.raises(errors.StyleSyntaxError): |
| 115 |
Style.parse("link") |
| 116 |
|
| 117 |
|
| 118 |
def test_link_id(): |
| 119 |
assert Style().link_id == "" |
| 120 |
assert Style.parse("").link_id == "" |
| 121 |
assert Style.parse("red").link_id == "" |
| 122 |
style = Style.parse("red link https://example.org") |
| 123 |
assert isinstance(style.link_id, str) |
| 124 |
assert len(style.link_id) > 1 |
| 125 |
|
| 126 |
|
| 127 |
def test_get_html_style(): |
| 128 |
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" |
| 129 |
html_style = Style( |
| 130 |
reverse=True, |
| 131 |
dim=True, |
| 132 |
color="red", |
| 133 |
bgcolor="blue", |
| 134 |
bold=True, |
| 135 |
italic=True, |
| 136 |
underline=True, |
| 137 |
strike=True, |
| 138 |
overline=True, |
| 139 |
).get_html_style() |
| 140 |
print(repr(html_style)) |
| 141 |
assert html_style == expected |
| 142 |
|
| 143 |
|
| 144 |
def test_chain(): |
| 145 |
assert Style.chain(Style(color="red"), Style(bold=True)) == Style( |
| 146 |
color="red", bold=True |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
def test_copy(): |
| 151 |
style = Style(color="red", bgcolor="black", italic=True) |
| 152 |
assert style == style.copy() |
| 153 |
assert style is not style.copy() |
| 154 |
|
| 155 |
|
| 156 |
def test_render(): |
| 157 |
assert Style(color="red").render("foo", color_system=None) == "foo" |
| 158 |
assert ( |
| 159 |
Style(color="red", bgcolor="black", bold=True).render("foo") |
| 160 |
== "\x1b[1;31;40mfoo\x1b[0m" |
| 161 |
) |
| 162 |
assert Style().render("foo") == "foo" |
| 163 |
|
| 164 |
|
| 165 |
def test_test(): |
| 166 |
Style(color="red").test("hello") |
| 167 |
|
| 168 |
|
| 169 |
def test_add(): |
| 170 |
assert Style(color="red") + None == Style(color="red") |
| 171 |
|
| 172 |
|
| 173 |
def test_iadd(): |
| 174 |
style = Style(color="red") |
| 175 |
style += Style(bold=True) |
| 176 |
assert style == Style(color="red", bold=True) |
| 177 |
style += None |
| 178 |
assert style == Style(color="red", bold=True) |
| 179 |
|
| 180 |
|
| 181 |
def test_style_stack(): |
| 182 |
stack = StyleStack(Style(color="red")) |
| 183 |
repr(stack) |
| 184 |
assert stack.current == Style(color="red") |
| 185 |
stack.push(Style(bold=True)) |
| 186 |
assert stack.current == Style(color="red", bold=True) |
| 187 |
stack.pop() |
| 188 |
assert stack.current == Style(color="red") |
| 189 |
|
| 190 |
|
| 191 |
def test_pick_first(): |
| 192 |
with pytest.raises(ValueError): |
| 193 |
Style.pick_first() |
| 194 |
|
| 195 |
|
| 196 |
def test_background_style(): |
| 197 |
assert Style(bold=True, color="yellow", bgcolor="red").background_style == Style( |
| 198 |
bgcolor="red" |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
def test_without_color(): |
| 203 |
style = Style(bold=True, color="red", bgcolor="blue") |
| 204 |
colorless_style = style.without_color |
| 205 |
assert colorless_style.color == None |
| 206 |
assert colorless_style.bgcolor == None |
| 207 |
assert colorless_style.bold == True |
| 208 |
null_style = Style.null() |
| 209 |
assert null_style.without_color == null_style |
| 210 |
|
| 211 |
|
| 212 |
def test_meta(): |
| 213 |
style = Style(bold=True, meta={"foo": "bar"}) |
| 214 |
assert style.meta["foo"] == "bar" |
| 215 |
|
| 216 |
style += Style(meta={"egg": "baz"}) |
| 217 |
|
| 218 |
assert style.meta == {"foo": "bar", "egg": "baz"} |
| 219 |
|
| 220 |
assert repr(style) == "Style(bold=True, meta={'foo': 'bar', 'egg': 'baz'})" |
| 221 |
|
| 222 |
|
| 223 |
def test_from_meta(): |
| 224 |
style = Style.from_meta({"foo": "bar"}) |
| 225 |
assert style.color is None |
| 226 |
assert style.bold is None |
| 227 |
|
| 228 |
|
| 229 |
def test_on(): |
| 230 |
style = Style.on({"foo": "bar"}, click="CLICK") + Style(color="red") |
| 231 |
assert style.meta == {"foo": "bar", "@click": "CLICK"} |
| 232 |
|
| 233 |
|
| 234 |
def test_clear_meta_and_links(): |
| 235 |
style = Style.parse("bold red on black link https://example.org") + Style.on( |
| 236 |
click="CLICK" |
| 237 |
) |
| 238 |
|
| 239 |
assert style.meta == {"@click": "CLICK"} |
| 240 |
assert style.link == "https://example.org" |
| 241 |
assert style.color == Color.parse("red") |
| 242 |
assert style.bgcolor == Color.parse("black") |
| 243 |
assert style.bold |
| 244 |
assert not style.italic |
| 245 |
|
| 246 |
clear_style = style.clear_meta_and_links() |
| 247 |
|
| 248 |
assert clear_style.meta == {} |
| 249 |
assert clear_style.link == None |
| 250 |
assert clear_style.color == Color.parse("red") |
| 251 |
assert clear_style.bgcolor == Color.parse("black") |
| 252 |
assert clear_style.bold |
| 253 |
assert not clear_style.italic |
| 254 |
|
| 255 |
|
| 256 |
def test_clear_meta_and_links_clears_hash(): |
| 257 |
"""Regression test for https://github.com/Textualize/rich/issues/2942.""" |
| 258 |
|
| 259 |
style = Style.parse("bold red on black link https://example.org") + Style.on( |
| 260 |
click="CLICK" |
| 261 |
) |
| 262 |
hash(style) # Force hash caching. |
| 263 |
|
| 264 |
assert style._hash is not None |
| 265 |
|
| 266 |
clear_style = style.clear_meta_and_links() |
| 267 |
assert clear_style._hash is None |