File size: 919 Bytes
dae14ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import io
from rich.abc import RichRenderable
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
class Foo:
def __rich__(self) -> Text:
return Text("Foo")
def test_rich_cast():
foo = Foo()
console = Console(file=io.StringIO())
console.print(foo)
assert console.file.getvalue() == "Foo\n"
def test_rich_cast_container():
foo = Foo()
console = Console(file=io.StringIO(), legacy_windows=False)
console.print(Panel.fit(foo, padding=0))
assert console.file.getvalue() == "╭───╮\n│Foo│\n╰───╯\n"
def test_abc():
foo = Foo()
assert isinstance(foo, RichRenderable)
assert isinstance(Text("hello"), RichRenderable)
assert isinstance(Panel("hello"), RichRenderable)
assert not isinstance(foo, str)
assert not isinstance("foo", RichRenderable)
assert not isinstance([], RichRenderable)
|