Skip to content

Commit

Permalink
improved error handling for invalid inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ericleonardis committed Jan 2, 2025
1 parent ad57162 commit fb403d9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
12 changes: 11 additions & 1 deletion sleap/gui/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,17 @@ def _toVal(self, x: float, center=False) -> float:
if x is None:
raise ValueError("x position cannot be None")

val = x
# Force conversion to float here.
try:
val = float(x)
except (TypeError, ValueError):
raise ValueError(f"x position must be a number, got {x}")

# Only if center=True, adjust by half the handle width.
if center:
val -= self.handle.rect().width() / 2.0

# Proceed with arithmetic only after we have a valid float.
val /= self._slider_width
val *= max(1, self._val_max - self._val_min)
val += self._val_min
Expand Down
91 changes: 89 additions & 2 deletions tests/gui/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,97 @@ def test_slider_width_property(qtbot):
# Test edge cases with very small and large widths
slider.box_rect.setWidth(0)
assert (
slider._slider_width == 0
), "Expected _slider_width to be 0 when box_rect width is 0"
slider._slider_width == 1
), "Expected _slider_width to be 1 when box_rect width is 0"

slider.box_rect.setWidth(10000)
assert (
slider._slider_width == 10000
), "Expected _slider_width to handle large values correctly"

@pytest.mark.parametrize(
"invalid_value, expected_error_msg",
[
(None, "x position cannot be None"),
("invalid", "x position must be a number, got invalid"),
([], "x position must be a number, got []"),
({}, "x position must be a number, got {}"),
]
)
def test_toVal_invalid_input(qtbot, invalid_value, expected_error_msg):
"""
Tests _toVal for invalid inputs to ensure ValueError is raised
with the correct message.
Args:
qtbot: Pytest fixture for Qt applications.
invalid_value: The invalid value for x.
expected_error_msg: The exact error message expected.
Returns:
None
"""
# We use tqdm to track progress across multiple invalid inputs (optional).
for _ in range(1):
slider = VideoSlider(min=0, max=1000, val=15)

with pytest.raises(ValueError) as excinfo:
slider._toVal(invalid_value)

# Verify the exact error message
assert str(excinfo.value) == expected_error_msg

@pytest.mark.parametrize(
"slider_width, x_value, handle_width, min_value, max_value",
[
(1000, 500, 10, 0, 1000),
(1000, 0, 10, 0, 1000), # Minimum x
(1000, 1000, 10, 0, 1000), # Maximum x
( 500, 250, 5, 100, 200), # Different min/max
]
)
def test_toVal_center_true(qtbot, slider_width, x_value, handle_width, min_value, max_value):
"""
Tests that _toVal correctly accounts for handle width offset when center=True.
Args:
qtbot: Pytest fixture for Qt applications.
slider_width: The width of the slider box_rect.
x_value: The x position being converted to a slider value.
handle_width: The desired width of the slider handle rect.
min_value: The slider's minimum value.
max_value: The slider's maximum value.
Returns:
None
"""
slider = VideoSlider(min=min_value, max=max_value, val=(min_value + max_value)//2)
slider.box_rect.setWidth(slider_width)

# Manually set the handle's bounding rect to the desired width.
current_handle_rect = slider.handle.rect()
slider.handle.setRect(
current_handle_rect.x(),
current_handle_rect.y(),
handle_width,
current_handle_rect.height()
)

# Read back the *actual* handle width.
actual_handle_width = slider.handle.rect().width()

# Manually compute the expected slider value for center=True.
val = float(x_value) - (actual_handle_width / 2.0)
effective_width = max(1.0, slider_width) # Prevent zero-division
val /= effective_width
val *= max(1, max_value - min_value)
val += min_value
expected_val = round(val)

# Call the actual function.
actual_val = slider._toVal(x_value, center=True)

assert actual_val == expected_val, (
f"For x={x_value}, handle_width={actual_handle_width}, slider_width={slider_width}, "
f"expected {expected_val}, but got {actual_val}."
)

0 comments on commit fb403d9

Please sign in to comment.