rich/tests/test_repr.py

33 lines
835 B
Python
Raw Normal View History

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
from rich.repr import rich_repr
@rich_repr
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-03-25 21:08:36 +00:00
yield 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
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
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