75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
import pytest
|
|
import sys
|
|
import os
|
|
from slic.core.adjustable import Adjustable
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from slic.utils.get_adj import *
|
|
|
|
def unregister_adj(target):
|
|
weak_set = instances(Adjustable, weak=True)
|
|
removed = 0
|
|
|
|
# Normaliser en liste
|
|
targets = target if isinstance(target, (list, tuple, set)) else [target]
|
|
|
|
for item in targets:
|
|
if isinstance(item, Adjustable):
|
|
if item in weak_set:
|
|
weak_set.discard(item)
|
|
removed += 1
|
|
elif isinstance(item, str):
|
|
to_remove = [obj for obj in weak_set if obj.name == item]
|
|
for obj in to_remove:
|
|
weak_set.discard(obj)
|
|
removed += len(to_remove)
|
|
else:
|
|
raise TypeError("target must be Adjustable, str, or iterable thereof")
|
|
|
|
return removed
|
|
|
|
|
|
class SubAdjustable(Adjustable):
|
|
def __init__(self, *a, **kw):
|
|
super().__init__(*a, **kw)
|
|
self._val = 0
|
|
|
|
def get_current_value(self):
|
|
return self._val
|
|
|
|
def set_target_value(self, v, *args, **kwargs):
|
|
self._val = v
|
|
return v
|
|
|
|
def is_moving(self):
|
|
return False
|
|
|
|
@pytest.fixture
|
|
def real_adjustables():
|
|
a1 = SubAdjustable("brightness", units="%", limit_low=0, limit_high=100)
|
|
a2 = SubAdjustable("contrast", units="%", limit_low=0, limit_high=100)
|
|
a3 = SubAdjustable("mid_contrast", units="%", limit_low=0, limit_high=100)
|
|
a4 = SubAdjustable("mid_brightness", units="%", limit_low=0, limit_high=100)
|
|
a5 = SubAdjustable("debug_internal", internal=True)
|
|
return a1, a2, a3, a4, a5
|
|
|
|
|
|
def test_get_adj_success(real_adjustables):
|
|
a1, _, _, _, _ = real_adjustables
|
|
assert get_adj("brightness") is a1
|
|
|
|
def test_get_adj_not_found(capfd):
|
|
with pytest.raises(ValueError, match='could not find Adjustable with name "nonexistent"') as exc_info:
|
|
get_adj("nonexistent")
|
|
|
|
def test_ensure_adjs_mixed(real_adjustables):
|
|
_, a2, a3, _, _ = real_adjustables
|
|
result = ensure_adjs([a3, "contrast"])
|
|
assert result == (a3, a2)
|
|
|
|
def test_get_adjs_filter(real_adjustables):
|
|
_, _, _, a4, a5 = real_adjustables
|
|
public = get_adjs()
|
|
assert set(public) == {'brightness', 'contrast', 'mid_contrast', 'mid_brightness'}
|
|
all_ = get_adjs(include_internal=True)
|
|
assert set(all_) == {'brightness', 'contrast', 'mid_contrast', 'mid_brightness', 'debug_internal'}
|