rich/tests/test_inspect.py

55 lines
1.6 KiB
Python
Raw Normal View History

2020-09-07 15:44:31 +00:00
import io
2020-09-07 15:54:17 +00:00
import sys
2020-09-07 15:44:31 +00:00
2020-09-07 15:54:17 +00:00
import pytest
2020-09-07 15:44:31 +00:00
from rich import inspect
from rich.console import Console
2020-09-07 15:54:17 +00:00
skip_py36 = pytest.mark.skipif(
sys.version_info <= (3, 6), reason="rendered differently on py3.6"
)
2020-09-07 15:49:45 +00:00
class InspectError(Exception):
def __str__(self) -> str:
return "INSPECT ERROR"
2020-09-07 15:44:31 +00:00
class Foo:
"""Foo test
Second line
"""
def __init__(self, foo: int) -> None:
"""constructor docs."""
self.foo = foo
@property
def broken(self):
2020-09-07 15:49:45 +00:00
raise InspectError()
2020-09-07 15:44:31 +00:00
def method(self, a, b) -> str:
"""Multi line
docs.
"""
return "test"
def __dir__(self):
return ["__init__", "broken", "method"]
2020-09-07 15:54:17 +00:00
@skip_py36
2020-09-07 15:44:31 +00:00
def test_render():
console = Console(width=100, file=io.StringIO(), legacy_windows=False)
foo = Foo("hello")
inspect(foo, console=console, all=True)
result = console.file.getvalue()
print(repr(result))
2020-09-07 15:49:45 +00:00
expected = "╭──────────── <class 'tests.test_inspect.Foo'> ────────────╮\n│ Foo test │\n│ │\n│ broken = InspectError() │\n│ __init__ = __init__(foo: int) -> None: constructor docs. │\n│ method = method(a, b) -> str: Multi line │\n╰──────────────────────────────────────────────────────────╯\n"
2020-09-07 15:44:31 +00:00
assert expected == result