test: Add tests for slider behavior with equal min and max values (#310)

This commit is contained in:
Talley Lambert
2025-10-13 13:09:52 -04:00
committed by GitHub
parent b11b511c4b
commit 5335004cfb
3 changed files with 66 additions and 1 deletions

View File

@@ -349,7 +349,7 @@ class _GenericSlider(QSlider):
_max = _max or self.MAX_DISPLAY
range_ = self._maximum - self._minimum
if range_ == 0:
return self._minimum
return 0
return int(min(QOVERFLOW, val / range_ * _max))
def _pick(self, pt: QPoint) -> int:

View File

@@ -254,3 +254,39 @@ def test_rangeslider_signals(cls, orientation, qtbot):
sld.setRange(1, 2)
mock.assert_called_once_with(1, 2)
_assert_types(mock.call_args.args, type_)
@pytest.mark.parametrize("cls, orientation", ALL_SLIDER_COMBOS)
def test_range_slider_with_equal_min_max(cls, orientation, qtbot):
"""Test that slider works when min == max (issue #307).
Previously, this would raise a TypeError: 'float' object cannot be
interpreted as an integer when calling show() because _to_qinteger_space
returned a float instead of an int when range was 0.
"""
sld = cls(orientation)
qtbot.addWidget(sld)
# Test with min=max=99 (the specific case from issue #307)
with qtbot.waitSignal(sld.rangeChanged):
sld.setMinimum(99)
# This should not raise a TypeError
sld.show()
# Verify the slider state
assert sld.minimum() == 99
assert sld.maximum() == 99
assert sld.value() == (99, 99)
# Test that we can also set max first
sld2 = cls(orientation)
qtbot.addWidget(sld2)
with qtbot.waitSignal(sld2.rangeChanged):
sld2.setMaximum(0)
sld2.show()
assert sld2.minimum() == 0
assert sld2.maximum() == 0
assert sld2.value() == (0, 0)

View File

@@ -229,3 +229,32 @@ def test_slider_extremes(sld: _GenericSlider, mag, qtbot):
for i in _linspace(-_mag, _mag, 10):
sld.setValue(i)
assert math.isclose(sld.value(), i, rel_tol=1e-8)
def test_slider_with_equal_min_max(sld: _GenericSlider, qtbot):
"""Test that slider works when min == max (issue #307).
Previously, this would raise a TypeError: 'float' object cannot be
interpreted as an integer when calling show() because _to_qinteger_space
returned a float instead of an int when range was 0.
"""
# Test with min=max=99 (the specific case from issue #307)
with qtbot.waitSignal(sld.rangeChanged, timeout=400):
sld.setMinimum(99)
# This should not raise a TypeError
sld.show()
# Verify the slider state
assert sld.minimum() == 99
assert sld.maximum() == 99
assert sld.value() == 99
# Test that we can also set max first
with qtbot.waitSignal(sld.rangeChanged, timeout=400):
sld.setMaximum(0)
sld.show()
assert sld.minimum() == 0
assert sld.maximum() == 0
assert sld.value() == 0