rich/tests/test_repr.py

126 lines
2.9 KiB
Python
Raw Normal View History

2021-06-18 19:24:05 +00:00
import pytest
2021-11-28 13:05:03 +00:00
import sys
2021-04-23 02:56:02 +00:00
from typing import Optional
2021-03-22 20:26:01 +00:00
from rich.console import Console
2021-06-18 19:24:05 +00:00
import rich.repr
2021-03-22 20:26:01 +00:00
2021-11-28 13:05:03 +00:00
skip_py36 = pytest.mark.skipif(
sys.version_info.minor == 6 and sys.version_info.major == 3,
reason="rendered differently on py3.6",
)
skip_py37 = pytest.mark.skipif(
sys.version_info.minor == 7 and sys.version_info.major == 3,
reason="rendered differently on py3.7",
)
2021-06-18 19:24:05 +00:00
@rich.repr.auto
2021-03-22 20:26:01 +00:00
class Foo:
2021-04-23 02:56:02 +00:00
def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
2021-03-22 20:26:01 +00:00
self.foo = foo
self.bar = bar
2021-03-25 21:08:36 +00:00
self.egg = egg
2021-03-22 20:26:01 +00:00
def __rich_repr__(self):
yield self.foo
2021-06-09 14:39:58 +00:00
yield None, self.foo,
2021-03-22 20:26:01 +00:00
yield "bar", self.bar, None
2021-03-25 21:08:36 +00:00
yield "egg", self.egg
2021-03-22 20:26:01 +00:00
2021-06-18 19:24:05 +00:00
@rich.repr.auto
class Egg:
2021-06-18 19:37:33 +00:00
def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
2021-06-18 19:24:05 +00:00
self.foo = foo
self.bar = bar
self.egg = egg
@rich.repr.auto
class BrokenEgg:
def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.fubar = bar
self.egg = egg
@rich.repr.auto(angular=True)
class AngularEgg:
def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.bar = bar
self.egg = egg
@rich.repr.auto
2021-06-09 14:39:58 +00:00
class Bar(Foo):
def __rich_repr__(self):
yield (self.foo,)
yield None, self.foo,
yield "bar", self.bar, None
yield "egg", self.egg
__rich_repr__.angular = True
2021-03-22 20:26:01 +00:00
def test_rich_repr() -> None:
2021-03-25 21:08:36 +00:00
assert (repr(Foo("hello"))) == "Foo('hello', 'hello', egg=1)"
assert (repr(Foo("hello", bar=3))) == "Foo('hello', 'hello', bar=3, egg=1)"
2021-03-22 20:26:01 +00:00
2021-11-28 13:05:03 +00:00
@skip_py36
@skip_py37
2021-11-27 19:41:29 +00:00
def test_rich_repr_positional_only() -> None:
2021-11-28 13:05:03 +00:00
_locals = locals().copy()
exec(
"""\
@rich.repr.auto
class PosOnly:
def __init__(self, foo, /):
self.foo = 1
""",
globals(),
_locals,
)
p = _locals["PosOnly"](1)
2021-11-27 19:41:29 +00:00
assert repr(p) == "PosOnly(1)"
2021-06-09 14:39:58 +00:00
def test_rich_angular() -> None:
assert (repr(Bar("hello"))) == "<Bar 'hello' 'hello' egg=1>"
assert (repr(Bar("hello", bar=3))) == "<Bar 'hello' 'hello' bar=3 egg=1>"
2021-06-18 19:24:05 +00:00
def test_rich_repr_auto() -> None:
assert repr(Egg("hello", egg=2)) == "Egg('hello', egg=2)"
def test_rich_repr_auto_angular() -> None:
assert repr(AngularEgg("hello", egg=2)) == "<AngularEgg 'hello' egg=2>"
def test_broken_egg() -> None:
with pytest.raises(rich.repr.ReprError):
repr(BrokenEgg("foo"))
2021-03-22 20:26:01 +00:00
def test_rich_pretty() -> None:
console = Console()
with console.capture() as capture:
console.print(Foo("hello", bar=3))
result = capture.get()
2021-03-25 21:08:36 +00:00
expected = "Foo('hello', 'hello', bar=3, egg=1)\n"
2021-03-22 20:26:01 +00:00
assert result == expected
2021-06-09 14:39:58 +00:00
def test_rich_pretty_angular() -> None:
console = Console()
with console.capture() as capture:
console.print(Bar("hello", bar=3))
result = capture.get()
expected = "<Bar 'hello' 'hello' bar=3 egg=1>\n"
2021-06-09 17:22:24 +00:00
assert result == expected