Files
slic/tests/test_utils_richcfg.py
T
tligui_y 0fe32701ea
Run CI Tests / test (push) Successful in 2m33s
Update tests/test_utils_richcfg.py
2025-08-29 15:58:44 +02:00

125 lines
3.7 KiB
Python

import io
import types
import sys
import re
import contextlib
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 strip_ansi(text):
ansi_escape = re.compile(r'\x1b\[[0-9;]*m')
return ansi_escape.sub('', text)
import io
import types
import contextlib
from IPython.core.oinspect import Inspector
from slic.utils.richcfg import replace_ipython_inspect
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 Rich-based inspector patch
replace_ipython_inspect()
assert isinstance(fake_ipy.inspector.pinfo, types.FunctionType)
# --- Capture Rich inspector output ---
with contextlib.redirect_stdout(io.StringIO()) as 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 ---
with contextlib.redirect_stdout(io.StringIO()) as builtin_buf:
user = User("Alice", 30)
inspector = Inspector()
info_dict = inspector.info(user)
info = dict_to_oinfo(info_dict)
inspector.pinfo(user, oname="user", info=info, detail_level=1)
builtin_text = strip_ansi(builtin_buf.getvalue())
# Print so pytest captures it
print(rich_text)
print("\n\n\n")
print(builtin_text)
# Rich output: shows actual instance content
assert "age = 30" in rich_text
assert "name = 'Alice'" in rich_text
# Built-in inspector does NOT show instance values
assert "30" not in "\n".join(
line for line in builtin_text.splitlines()
if not line.strip().startswith("String form:") # id in this line that can contain the number 30
)
assert "Alice" not in builtin_text
# Built-in inspector only shows the raw __init__ function
assert "def __init__(self, name: str, age: int):" in builtin_text
assert "self.name = name" in builtin_text
assert "self.age = age" in builtin_text
# Method name is visible in both
assert "def greet():" in rich_text
assert "def greet(self):" in builtin_text
# Method docstring is visible in both
assert "Returns a welcome message." in rich_text
assert "Returns a welcome message." in builtin_text
# Class-level attributes visible in both
assert "role = 'admin'" in rich_text
assert 'role = "admin"' in builtin_text
# Class docstring is shown in both
assert "Represents a user in the system." in rich_text
assert "Represents a user in the system." in builtin_text
# Structural difference: Rich wraps values in a visual box
assert all(token in rich_text for token in ["╭─ user =", "─╮", "╰─", "─╯"])
assert rich_text.count("") >= 10