From 1e8bc664fb924475490a0ca1f4222ca8434afc2a Mon Sep 17 00:00:00 2001 From: tligui_y Date: Fri, 29 Aug 2025 14:34:06 +0200 Subject: [PATCH] Update tests/test_utils_richcfg.py --- tests/test_utils_richcfg.py | 69 ++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/tests/test_utils_richcfg.py b/tests/test_utils_richcfg.py index 59e633a5..4c9bbf51 100644 --- a/tests/test_utils_richcfg.py +++ b/tests/test_utils_richcfg.py @@ -43,8 +43,15 @@ 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 + # Simulate a fake IPython shell class FakeInspector: def __init__(self): self.pinfo = None @@ -56,67 +63,52 @@ def test_rich_inspector_outputs_more_than_builtin(monkeypatch): fake_ipy = FakeIPython() monkeypatch.setattr("slic.utils.richcfg.get_ipython", lambda: fake_ipy) - # Apply your Rich-based inspector patch + # Apply 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) + # --- 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 - 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() - builtin_text = strip_ansi(builtin_text) - - monkeypatch.setattr("sys.stdout", original_stdout) + # --- 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("\n\n\n") print(builtin_text) # Rich output: shows actual instance content - # Rich shows live instance attribute values like: - # age = 30 - # name = 'Alice' - # role = 'admin' assert "age = 30" in rich_text assert "name = 'Alice'" in rich_text # Built-in inspector does NOT show instance values assert "30" not in builtin_text assert "Alice" not in builtin_text - - # # Built-in inspector only shows the brut init function + + # 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 - # Both outputs include the same method and documentation and gives us the texts written outside functions - - # Method name is visible in both + # Method name is visible in both assert "def greet():" in rich_text - assert "def greet(self):" in builtin_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 - # Informations outside functions are visible in both + # Class-level attributes visible in both assert "role = 'admin'" in rich_text assert 'role = "admin"' in builtin_text @@ -125,6 +117,5 @@ def test_rich_inspector_outputs_more_than_builtin(monkeypatch): assert "Represents a user in the system." in builtin_text # Structural difference: Rich wraps values in a visual box - assert "╭─ user =" and "─╮" and "╰─" and "─╯" in rich_text - assert rich_text.count("│") >= 10 - + assert all(token in rich_text for token in ["╭─ user =", "─╮", "╰─", "─╯"]) + assert rich_text.count("│") >= 10 \ No newline at end of file