2020-04-27 15:22:39 +00:00
|
|
|
import io
|
|
|
|
|
2020-11-19 18:22:10 +00:00
|
|
|
from rich.abc import RichRenderable
|
2020-04-27 15:22:39 +00:00
|
|
|
from rich.console import Console
|
2020-08-24 16:26:30 +00:00
|
|
|
from rich.panel import Panel
|
2020-04-27 15:22:39 +00:00
|
|
|
from rich.text import Text
|
|
|
|
|
|
|
|
|
|
|
|
class Foo:
|
2020-11-19 18:22:10 +00:00
|
|
|
def __rich__(self) -> Text:
|
2020-04-27 15:22:39 +00:00
|
|
|
return Text("Foo")
|
|
|
|
|
|
|
|
|
2020-08-24 16:26:30 +00:00
|
|
|
def test_rich_cast():
|
2020-04-27 15:22:39 +00:00
|
|
|
foo = Foo()
|
|
|
|
console = Console(file=io.StringIO())
|
|
|
|
console.print(foo)
|
|
|
|
assert console.file.getvalue() == "Foo\n"
|
2020-08-24 16:26:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_rich_cast_container():
|
|
|
|
foo = Foo()
|
2020-08-25 16:23:06 +00:00
|
|
|
console = Console(file=io.StringIO(), legacy_windows=False)
|
2020-10-15 20:17:03 +00:00
|
|
|
console.print(Panel.fit(foo, padding=0))
|
2020-08-24 16:26:30 +00:00
|
|
|
assert console.file.getvalue() == "╭───╮\n│Foo│\n╰───╯\n"
|
2020-11-19 18:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2021-11-16 12:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast_deep():
|
|
|
|
class B:
|
|
|
|
def __rich__(self) -> Foo:
|
|
|
|
return Foo()
|
|
|
|
|
|
|
|
class A:
|
|
|
|
def __rich__(self) -> B:
|
|
|
|
return B()
|
|
|
|
|
|
|
|
console = Console(file=io.StringIO())
|
|
|
|
console.print(A())
|
|
|
|
assert console.file.getvalue() == "Foo\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_cast_recursive():
|
|
|
|
class B:
|
|
|
|
def __rich__(self) -> "A":
|
|
|
|
return A()
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "<B>"
|
|
|
|
|
|
|
|
class A:
|
|
|
|
def __rich__(self) -> B:
|
|
|
|
return B()
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "<A>"
|
|
|
|
|
|
|
|
console = Console(file=io.StringIO())
|
|
|
|
console.print(A())
|
|
|
|
assert console.file.getvalue() == "<B>\n"
|