From ecf66c50efa7b96139bd2a8be56e3027f7efeb7c Mon Sep 17 00:00:00 2001 From: tligui_y Date: Fri, 29 Aug 2025 11:05:22 +0200 Subject: [PATCH] Update tests/test_utils_pv.py --- tests/test_utils_pv.py | 45 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/tests/test_utils_pv.py b/tests/test_utils_pv.py index f09e322d6..4d8e91c61 100644 --- a/tests/test_utils_pv.py +++ b/tests/test_utils_pv.py @@ -10,15 +10,17 @@ from contextlib import contextmanager from slic.utils.pv import PV from morbidissimo import MorIOC ''' -# === Utils === +# Utils @pytest.fixture def capture_stdout(monkeypatch): + # Fixture to capture sys.stdout into a buffer buf = StringIO() monkeypatch.setattr("sys.stdout", buf) return buf @contextmanager def use_callback(pv, callback): + # Context manager to add and remove a callback around a block cbid = pv.add_callback(callback) try: yield @@ -28,10 +30,11 @@ def use_callback(pv, callback): import re def strip_ansi(text): + # Remove ANSI escape sequences from text ansi_escape = re.compile(r'\x1b\[[0-9;]*m') return ansi_escape.sub('', text) -# === Tests === +# Tests @pytest.mark.parametrize("value_new, value_before, show_bar, expected_color", [ (25, 0, True, colorama.Fore.GREEN), (50, 25, True, colorama.Fore.GREEN), @@ -40,21 +43,20 @@ def strip_ansi(text): (150, 100, False, colorama.Fore.GREEN), (-50, 150, True, colorama.Fore.GREEN) ]) - def test_put_with_progress_and_repr(value_new, value_before, show_bar, expected_color): pv = PV("TEST:VAL") pv.put(value_before) assert pv.get() == value_before - # Capture tous les prints dans une liste + # Capture all printed lines in a list printed_lines = [] def fake_print(*args, **kwargs): line = " ".join(str(a) for a in args) printed_lines.append(line) - # Monkeypatch print uniquement dans ce contexte + # Monkeypatch print only inside this context original_print = builtins.print builtins.print = fake_print try: @@ -65,29 +67,32 @@ def test_put_with_progress_and_repr(value_new, value_before, show_bar, expected_ printed_lines = [line for line in printed_lines if line.strip()] cleaned_lines = [strip_ansi(line) for line in printed_lines] - # Initialisation bar - if show_bar==True: + # Check progress bar output + if show_bar is True: + # Verify empty bar was shown at least once matches = [line for line in cleaned_lines if f"| |" in line] - assert matches, f"Expected bar not found in:\n" + "\n".join(printed_lines) + assert matches, f"Expected empty bar not found in:\n" + "\n".join(printed_lines) - # Vérifie que la bonne barre a été affichée au moins une fois + # Verify filled bar was shown at least once matches = [line for line in cleaned_lines if f"|██████████████████████████████|" in line] - assert matches, f"Expected bar not found in:\n" + "\n".join(printed_lines) + assert matches, f"Expected filled bar not found in:\n" + "\n".join(printed_lines) - # Vérifie que la couleur est bien utilisée (au moins une fois dans les lignes printées) + # Verify color code was used at least once color_matches = [line for line in printed_lines if expected_color in line] assert color_matches, "Expected color code not found" + # Verify old and new values appear in printed lines assert all(f"{value_new}" in line for line in printed_lines), "new value not in all lines" assert all(f"{value_before}" in line for line in printed_lines), "old value not in all lines" - else : - + else: + # When show_bar=False, nothing should be printed assert len(printed_lines) == 0 - # Vérifie que la valeur finale est correcte + # Verify final PV value is updated assert pv.get() == value_new + def test_use_callback_context_manager(): pv = PV("TEST:VAL") @@ -97,20 +102,17 @@ def test_use_callback_context_manager(): def callback(value=None, **kwargs): seen_values.append(value) + # Use context manager, callback should be triggered with pv.use_callback(callback): pv.put(42.0) - - # Vérifie que le callback a bien été appelé assert 42.0 in seen_values with pv.use_callback(callback): pv.put(24.0) - assert 24.0 in seen_values with pv.use_callback(callback): pv.put(75.0) - assert 75.0 in seen_values @@ -119,12 +121,13 @@ def test_orig_repr_is_not_custom_repr(): custom = repr(pv) original = pv.orig_repr() - # Check that the custom repr is not the same as the original + # Custom repr should not be the same as original repr assert custom != original - # Check that the original repr looks like a real epics PV + # Original repr should look like a standard epics PV repr assert original.startswith("