mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 01:58:24 +00:00
added rich cast to protocol
This commit is contained in:
parent
189731826b
commit
20b27d53e4
5 changed files with 80 additions and 16 deletions
|
@ -33,3 +33,37 @@ def test_abc():
|
|||
assert not isinstance(foo, str)
|
||||
assert not isinstance("foo", RichRenderable)
|
||||
assert not isinstance([], RichRenderable)
|
||||
|
||||
|
||||
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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue