97 lines
2.6 KiB
Python
97 lines
2.6 KiB
Python
import pytest
|
|
from slic.core.adjustable import Adjustable
|
|
|
|
class FakeAdjustable(Adjustable):
|
|
def __init__(self, ID, name=None, internal=False):
|
|
super().__init__(ID=ID, name=name or ID, internal=internal)
|
|
self._value = 0
|
|
|
|
def get_current_value(self):
|
|
return self._value
|
|
|
|
def is_moving(self):
|
|
return False
|
|
|
|
def set_target_value(self, value, *args, **kwargs):
|
|
return value
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
# Patched version of snapshot that takes direct adjustable list input to avoid registry.instances patching issues
|
|
def snapshot_testable(adjustables, include_internal=False, sort_key=str):
|
|
visible = [a for a in adjustables if include_internal or not a.internal]
|
|
return sorted(visible, key=sort_key)
|
|
|
|
test_cases = [
|
|
pytest.param(
|
|
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
|
["Visible"],
|
|
{},
|
|
id="exclude_internals"
|
|
),
|
|
|
|
pytest.param(
|
|
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
|
["Visible", "Hidden"],
|
|
{"include_internal": True},
|
|
id="include_internals"
|
|
),
|
|
|
|
pytest.param(
|
|
[],
|
|
[],
|
|
{},
|
|
id="empty_case"
|
|
),
|
|
|
|
pytest.param(
|
|
[("3", "Charlie"), ("1", "Alpha"), ("2", "Beta")],
|
|
["Alpha", "Beta", "Charlie"],
|
|
{"sort_key": str},
|
|
id="sort_str"
|
|
),
|
|
|
|
pytest.param(
|
|
[("z3", "C"), ("a1", "A"), ("m2", "B")],
|
|
["A", "B", "C"],
|
|
{"sort_key": lambda a: a.ID},
|
|
id="sort_id"
|
|
),
|
|
|
|
pytest.param(
|
|
[("3", "Charlie"), ("1", "alpha"), ("2", "Beta")],
|
|
["alpha", "Beta", "Charlie"],
|
|
{"sort_key": lambda a: a.name.lower()},
|
|
id="sort_case_insensitive"
|
|
),
|
|
|
|
pytest.param(
|
|
[("1", "A"), ("2", "BB"), ("3", "CCC")],
|
|
["A", "BB", "CCC"],
|
|
{"sort_key": lambda a: len(a.name)},
|
|
id="sort_length"
|
|
),
|
|
|
|
pytest.param(
|
|
[("1", "A"), ("2", "B"), ("3", "C")],
|
|
["C", "B", "A"],
|
|
{"sort_key": lambda a: -ord(a.name)},
|
|
id="sort_reverse"
|
|
)
|
|
]
|
|
|
|
@pytest.mark.parametrize("test_input,expected,kwargs", test_cases)
|
|
def test_snapshot(test_input, expected, kwargs):
|
|
|
|
adjustables = [FakeAdjustable(*args) for args in test_input]
|
|
|
|
result = snapshot_testable(adjustables, **kwargs)
|
|
|
|
if not expected:
|
|
assert result == []
|
|
elif 'sort_key' in kwargs:
|
|
obtained = [x.name for x in result]
|
|
assert obtained == expected
|
|
else:
|
|
assert {x.name for x in result} == set(expected) |