import pytest from IPython import get_ipython from slic.utils.ipy import devices from slic.core.device.device import Device import types @pytest.fixture(scope="module") def fake_ipython(): class FakeIPython: def __init__(self): self.user_ns = {} return FakeIPython() @pytest.fixture(autouse=True) def simulate_ipython(fake_ipython, monkeypatch): monkeypatch.setattr("slic.utils.ipy.get_ipython", lambda: fake_ipython) return fake_ipython def test_devices_repr_fallback_and_ignore(simulate_ipython): ipy = simulate_ipython assert ipy is not None # Clean namespace for k in list(ipy.user_ns): if k.startswith(("test_", "_test_")): del ipy.user_ns[k] # Test device classes class WithDescription(Device): def __init__(self, ID): super().__init__(ID) self.ID = ID self.description = "desc ok" self.name = None class WithDoc(Device): """doc ok""" def __init__(self, ID): super().__init__(ID) self.ID = ID self.description = None self.name = None class WithName(Device): def __init__(self, ID): super().__init__(ID) self.ID = ID self.name = "name ok" self.description = None class WithOnlyID(Device): def __init__(self, ID): super().__init__(ID) self.ID = ID self.name = None self.description = None # Setup test environment ipy.user_ns.update({ # Valid devices "test_desc": WithDescription("D1"), "test_doc": WithDoc("D2"), "test_name": WithName("D3"), "test_id": WithOnlyID("D4"), # Invalid objects "test_str": "string", "_hidden": WithDescription("HIDDEN") }) out = repr(devices) print(out) # Core assertions assert "test_desc" in out assert "desc ok" in out assert "test_doc" in out assert "doc ok" in out assert "test_name" in out assert "name ok" in out assert "test_id" in out assert "D4" in out assert "test_str" not in out assert "_hidden" not in out # None checks assert getattr(ipy.user_ns["test_desc"], "name") is None assert getattr(ipy.user_ns["test_doc"], "name") is None assert getattr(ipy.user_ns["test_doc"], "description") is None assert getattr(ipy.user_ns["test_id"], "name") is None assert getattr(ipy.user_ns["test_id"], "description") is None