Update tests/test_utils_richcfg.py
Run CI Tests / test (push) Has been cancelled

This commit is contained in:
2025-08-29 14:34:06 +02:00
parent c4c0c1432f
commit 1e8bc664fb
+30 -39
View File
@@ -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