diff --git a/tests/test_utils_richcfg.py b/tests/test_utils_richcfg.py index 72a757ff6..92559b225 100644 --- a/tests/test_utils_richcfg.py +++ b/tests/test_utils_richcfg.py @@ -1,9 +1,13 @@ import io import types +import sys import inspect as std_inspect -from slic.utils.richcfg import replace_ipython_inspect -# Class to inspect +from IPython.core.oinspect import Inspector +from slic.utils.richcfg import replace_ipython_inspect # ← adapt this to your actual module path + + +# A realistic class to inspect class User: """Represents a user in the system.""" @@ -19,7 +23,7 @@ class User: def test_rich_inspector_outputs_more_than_builtin(monkeypatch): - # Simulate an IPython shell + # --- Simulate a fake IPython shell --- class FakeInspector: def __init__(self): self.pinfo = None @@ -31,36 +35,43 @@ def test_rich_inspector_outputs_more_than_builtin(monkeypatch): fake_ipy = FakeIPython() monkeypatch.setattr("slic.utils.richcfg.get_ipython", lambda: fake_ipy) - # Apply the Rich-based inspector patch + # --- Apply your 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) + # --- 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_out.getvalue() + rich_text = rich_buf.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() + # --- Capture original IPython inspector output --- + builtin_buf = io.StringIO() + user = User("Alice", 30) + original_stdout = sys.stdout + sys.stdout = builtin_buf + inspector = Inspector() + inspector.pinfo(user, oname="user", detail_level=1) + sys.stdout = original_stdout + builtin_text = builtin_buf.getvalue() - # Assertions showing that Rich adds stuff compared to standard inspection - assert "User" in rich_text - assert "User" in builtin_text + print(rich_text, '\n\n\n') + print(builtin_text) - # Present in rich only + # --- Comparison assertions: Rich adds structure and content --- + # Rich has sections and decoration + assert "╭" in rich_text or "┌" in rich_text # Decorative borders 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 + # Both include method names and basic info assert "greet" in rich_text assert "greet" in builtin_text - assert "name" in rich_text and "name" 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