Skip to content

Commit 5335004

Browse files
authored
test: Add tests for slider behavior with equal min and max values (#310)
1 parent b11b511 commit 5335004

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/superqt/sliders/_generic_slider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _to_qinteger_space(self, val, _max=None):
349349
_max = _max or self.MAX_DISPLAY
350350
range_ = self._maximum - self._minimum
351351
if range_ == 0:
352-
return self._minimum
352+
return 0
353353
return int(min(QOVERFLOW, val / range_ * _max))
354354

355355
def _pick(self, pt: QPoint) -> int:

tests/zz_test_sliders/test_range_slider.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,39 @@ def test_rangeslider_signals(cls, orientation, qtbot):
254254
sld.setRange(1, 2)
255255
mock.assert_called_once_with(1, 2)
256256
_assert_types(mock.call_args.args, type_)
257+
258+
259+
@pytest.mark.parametrize("cls, orientation", ALL_SLIDER_COMBOS)
260+
def test_range_slider_with_equal_min_max(cls, orientation, qtbot):
261+
"""Test that slider works when min == max (issue #307).
262+
263+
Previously, this would raise a TypeError: 'float' object cannot be
264+
interpreted as an integer when calling show() because _to_qinteger_space
265+
returned a float instead of an int when range was 0.
266+
"""
267+
sld = cls(orientation)
268+
qtbot.addWidget(sld)
269+
270+
# Test with min=max=99 (the specific case from issue #307)
271+
with qtbot.waitSignal(sld.rangeChanged):
272+
sld.setMinimum(99)
273+
274+
# This should not raise a TypeError
275+
sld.show()
276+
277+
# Verify the slider state
278+
assert sld.minimum() == 99
279+
assert sld.maximum() == 99
280+
assert sld.value() == (99, 99)
281+
282+
# Test that we can also set max first
283+
sld2 = cls(orientation)
284+
qtbot.addWidget(sld2)
285+
286+
with qtbot.waitSignal(sld2.rangeChanged):
287+
sld2.setMaximum(0)
288+
289+
sld2.show()
290+
assert sld2.minimum() == 0
291+
assert sld2.maximum() == 0
292+
assert sld2.value() == (0, 0)

tests/zz_test_sliders/test_single_value_sliders.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,32 @@ def test_slider_extremes(sld: _GenericSlider, mag, qtbot):
229229
for i in _linspace(-_mag, _mag, 10):
230230
sld.setValue(i)
231231
assert math.isclose(sld.value(), i, rel_tol=1e-8)
232+
233+
234+
def test_slider_with_equal_min_max(sld: _GenericSlider, qtbot):
235+
"""Test that slider works when min == max (issue #307).
236+
237+
Previously, this would raise a TypeError: 'float' object cannot be
238+
interpreted as an integer when calling show() because _to_qinteger_space
239+
returned a float instead of an int when range was 0.
240+
"""
241+
# Test with min=max=99 (the specific case from issue #307)
242+
with qtbot.waitSignal(sld.rangeChanged, timeout=400):
243+
sld.setMinimum(99)
244+
245+
# This should not raise a TypeError
246+
sld.show()
247+
248+
# Verify the slider state
249+
assert sld.minimum() == 99
250+
assert sld.maximum() == 99
251+
assert sld.value() == 99
252+
253+
# Test that we can also set max first
254+
with qtbot.waitSignal(sld.rangeChanged, timeout=400):
255+
sld.setMaximum(0)
256+
257+
sld.show()
258+
assert sld.minimum() == 0
259+
assert sld.maximum() == 0
260+
assert sld.value() == 0

0 commit comments

Comments
 (0)