This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import io
|
||||
import types
|
||||
import inspect as std_inspect
|
||||
from slic.utils.richcfg import replace_ipython_inspect
|
||||
|
||||
# Class to inspect
|
||||
class User:
|
||||
"""Represents a user in the system."""
|
||||
|
||||
role = "admin"
|
||||
|
||||
def __init__(self, name: str, age: int):
|
||||
self.name = name
|
||||
self.age = age
|
||||
|
||||
def greet(self):
|
||||
"""Returns a welcome message."""
|
||||
return f"Welcome, {self.name}!"
|
||||
|
||||
|
||||
def test_rich_inspector_outputs_more_than_builtin(monkeypatch):
|
||||
# Simulate an IPython shell
|
||||
class FakeInspector:
|
||||
def __init__(self):
|
||||
self.pinfo = None
|
||||
|
||||
class FakeIPython:
|
||||
def __init__(self):
|
||||
self.inspector = FakeInspector()
|
||||
|
||||
fake_ipy = FakeIPython()
|
||||
monkeypatch.setattr("my_rich.get_ipython", lambda: fake_ipy)
|
||||
|
||||
# Apply the Rich-based inspector patch
|
||||
replace_ipython_inspect()
|
||||
assert isinstance(fake_ipy.inspector.pinfo, types.FunctionType)
|
||||
|
||||
# Run the inspector and capture its output
|
||||
rich_out = io.StringIO()
|
||||
monkeypatch.setattr("sys.stdout", rich_out)
|
||||
|
||||
user = User("Alice", 30)
|
||||
fake_ipy.inspector.pinfo(user, oname="user", detail_level=1)
|
||||
rich_text = rich_out.getvalue()
|
||||
|
||||
# Simulate classic Python inspection output
|
||||
builtin_out = io.StringIO()
|
||||
print("DOCSTRING:", std_inspect.getdoc(user.__class__), file=builtin_out)
|
||||
print("DIR:", dir(user), file=builtin_out)
|
||||
builtin_text = builtin_out.getvalue()
|
||||
|
||||
# Assertions showing that Rich adds stuff compared to standard inspection
|
||||
assert "User" in rich_text
|
||||
assert "User" in builtin_text
|
||||
|
||||
# Present in rich only
|
||||
assert "Methods" in rich_text and "Methods" not in builtin_text
|
||||
assert "Attributes" in rich_text or "attribute" in rich_text.lower()
|
||||
assert "╭" in rich_text or "┌" in rich_text # Rich border decoration
|
||||
assert "Returns a welcome message." in rich_text
|
||||
assert "Returns a welcome message." not in builtin_text
|
||||
|
||||
# Both should contain method names and attributes
|
||||
assert "greet" in rich_text
|
||||
assert "greet" in builtin_text
|
||||
assert "name" in rich_text and "name" in builtin_text
|
||||
Reference in New Issue
Block a user