126 lines
3.9 KiB
Python
126 lines
3.9 KiB
Python
import pytest
|
|
import sys
|
|
import os
|
|
|
|
# S'assurer que la racine du projet est dans le path
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
# Import en tant que module (évite les références figées)
|
|
import slic.utils.marker as marker_mod
|
|
|
|
|
|
# Dummy class to simulate an adjustable device
|
|
class DummyDevice:
|
|
def __init__(self, name="Device", units="V", current=1.23):
|
|
self.name = name
|
|
self.units = units
|
|
self.current = current
|
|
self.set_calls = []
|
|
|
|
def get_current_value(self):
|
|
return self.current
|
|
|
|
def set_target_value(self, value, hold=False):
|
|
self.set_calls.append((value, hold))
|
|
self.current = value
|
|
return f"Set to {value}{self.units}, hold={hold}"
|
|
|
|
|
|
# --- format_value() tests
|
|
|
|
def test_format_value_with_units():
|
|
assert marker_mod.format_value(1.5, "V") == "1.5 V"
|
|
|
|
def test_format_value_without_units():
|
|
assert marker_mod.format_value(1.5, None) == "1.5"
|
|
|
|
|
|
# --- Marker tests
|
|
|
|
def test_marker_name_default():
|
|
device = DummyDevice(name="Device_TND")
|
|
m = marker_mod.Marker(device, value=2.5)
|
|
assert m.name == "Device_TND at 2.5 V"
|
|
|
|
def test_marker_name_custom():
|
|
device = DummyDevice(name="Device_TNC")
|
|
m = marker_mod.Marker(device, value=2.5, name="TestName_TNC")
|
|
assert m.name == "TestName_TNC"
|
|
|
|
def test_marker_repr_format():
|
|
device = DummyDevice(name="Volt_TRF", units="mV", current=1.0)
|
|
m = marker_mod.Marker(device, value=2.5)
|
|
out = repr(m)
|
|
assert 'Marker "Volt_TRF at 2.5 mV"' in out
|
|
assert "(currently at 1.0 mV)" in out
|
|
|
|
def test_marker_update_changes_value():
|
|
device = DummyDevice(name="Device_TMU", current=4.2)
|
|
m = marker_mod.Marker(device, value=1.0)
|
|
assert m.value == 1.0
|
|
m.update()
|
|
assert m.value == 4.2
|
|
|
|
def test_marker_update_with_explicit_value():
|
|
device = DummyDevice(name="Device_TUE")
|
|
m = marker_mod.Marker(device, value=0.0)
|
|
m.update(7.7)
|
|
assert m.value == 7.7
|
|
|
|
def test_marker_goto_sets_value_and_returns_result():
|
|
device = DummyDevice(name="Device_TGG", current=1.0)
|
|
m = marker_mod.Marker(device, value=3.3)
|
|
result = m.goto()
|
|
assert result == "Set to 3.3V, hold=False"
|
|
assert device.current == 3.3
|
|
assert device.set_calls[-1] == (3.3, False)
|
|
|
|
def test_marker_call_is_alias_of_goto():
|
|
device = DummyDevice(name="Device_TAC", current=1.0)
|
|
m = marker_mod.Marker(device, value=3.3)
|
|
result = m(hold=True)
|
|
assert result == "Set to 3.3V, hold=True"
|
|
assert device.set_calls[-1] == (3.3, True)
|
|
|
|
|
|
# --- markers singleton registry
|
|
|
|
def test_markers_register_and_access():
|
|
d1 = DummyDevice(name="Dev1_TMR", current=1.0)
|
|
d2 = DummyDevice(name="Dev2_TMR", current=2.0)
|
|
m1 = marker_mod.Marker(d1, value=10)
|
|
m2 = marker_mod.Marker(d2, value=20)
|
|
|
|
all_markers = marker_mod.markers._get()
|
|
|
|
assert isinstance(all_markers, dict)
|
|
assert m1.name in all_markers
|
|
assert m2.name in all_markers
|
|
assert all_markers[m1.name] is m1
|
|
assert all_markers[m2.name] is m2
|
|
assert marker_mod.markers[m1.name] is m1
|
|
assert marker_mod.markers[m2.name] is m2
|
|
|
|
def test_markers_repr_contains_all():
|
|
d1 = DummyDevice(name="Dev1_TMR2", current=1.0)
|
|
d2 = DummyDevice(name="Dev2_TMR2", current=2.0)
|
|
d1 = marker_mod.Marker(d1, value=10)
|
|
d2 = marker_mod.Marker(d2, value=20)
|
|
|
|
repr_str = repr(marker_mod.markers)
|
|
assert "Dev1_TMR2 at 10 V" in repr_str
|
|
assert "Dev2_TMR2 at 20 V" in repr_str
|
|
assert isinstance(repr_str, str)
|
|
|
|
def test_marker_registry_dict_is_printable_dict():
|
|
d = DummyDevice(name="DevP_TPR", current=7.0)
|
|
marker_mod.Marker(d, value=70)
|
|
ms = marker_mod.markers._get()
|
|
assert isinstance(ms, dict)
|
|
assert isinstance(repr(marker_mod.markers), str)
|
|
assert isinstance(marker_mod.printable_dict(ms), str)
|
|
|
|
def test_markers_getitem_invalid_key_raises():
|
|
with pytest.raises(KeyError):
|
|
_ = marker_mod.markers["this marker does not exist"]
|