98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
import io
|
|
import types
|
|
import sys
|
|
import inspect as std_inspect
|
|
|
|
from IPython.core.oinspect import Inspector, OInfo
|
|
from slic.utils.richcfg import replace_ipython_inspect
|
|
|
|
|
|
# A realistic 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 dict_to_oinfo(info_dict):
|
|
|
|
info_obj = OInfo(
|
|
ismagic=False,
|
|
isalias=False,
|
|
found=True,
|
|
namespace='',
|
|
parent=None,
|
|
obj=None
|
|
)
|
|
|
|
# Fill in optional fields from dict
|
|
for key, value in info_dict.items():
|
|
if hasattr(info_obj, key):
|
|
setattr(info_obj, key, value)
|
|
return info_obj
|
|
|
|
def test_rich_inspector_outputs_more_than_builtin(monkeypatch):
|
|
# Simulate a fake IPython shell
|
|
class FakeInspector:
|
|
def __init__(self):
|
|
self.pinfo = None
|
|
|
|
class FakeIPython:
|
|
def __init__(self):
|
|
self.inspector = FakeInspector()
|
|
|
|
fake_ipy = FakeIPython()
|
|
monkeypatch.setattr("slic.utils.richcfg.get_ipython", lambda: fake_ipy)
|
|
|
|
# Apply your Rich-based inspector patch
|
|
replace_ipython_inspect()
|
|
assert isinstance(fake_ipy.inspector.pinfo, types.FunctionType)
|
|
|
|
# Capture Rich inspector output
|
|
rich_buf = io.StringIO()
|
|
monkeypatch.setattr("sys.stdout", rich_buf)
|
|
|
|
user = User("Alice", 30)
|
|
fake_ipy.inspector.pinfo(user, oname="user", detail_level=1)
|
|
rich_text = rich_buf.getvalue()
|
|
|
|
# Capture original IPython inspector output
|
|
builtin_buf = io.StringIO()
|
|
user = User("Alice", 30)
|
|
original_stdout = sys.stdout
|
|
sys.stdout = builtin_buf
|
|
inspector = Inspector()
|
|
|
|
info_dict = inspector.info(user)
|
|
info = dict_to_oinfo(info_dict)
|
|
|
|
inspector.pinfo(user, oname="user", info=info, detail_level=1)
|
|
sys.stdout = original_stdout
|
|
builtin_text = builtin_buf.getvalue()
|
|
|
|
print(rich_text, file=sys.__stdout__)
|
|
print('\n\n\n', file=sys.__stdout__)
|
|
print(builtin_text, file=sys.__stdout__)
|
|
|
|
# Comparison assertions: Rich adds structure and content
|
|
# Rich has sections and decoration
|
|
assert "╭" in rich_text or "┌" in rich_text # Decorative borders
|
|
assert "Attributes" in rich_text or "attribute" in rich_text.lower()
|
|
assert "Returns a welcome message." in rich_text
|
|
assert "Returns a welcome message." not in builtin_text
|
|
|
|
# Both include method names and basic info
|
|
assert "greet" in rich_text
|
|
assert "greet" in builtin_text
|
|
assert "name" in rich_text
|
|
assert "name" in builtin_text
|
|
assert "Represents a user in the system." in rich_text
|
|
assert "Represents a user in the system." in builtin_text
|