Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use subTests for the tests
  • Loading branch information
encukou committed Mar 17, 2026
commit 925a17552d8343dcb9cdb149a88e548e5a28f657
58 changes: 31 additions & 27 deletions Lib/test/test_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,38 +207,42 @@ def test_open_in_write_raises(self):
support.gc_collect()
self.assertIsNone(cm.unraisable)

def test_setframerate_rounds_then_validates(self):
"""Test that setframerate rounds before validation"""
# Test that framerates that round to 0 or negative are rejected
@support.subTests('arg', (
# rounds to 0, should raise:
0.5,
0.4,
# Negative values should still raise:
-1,
-0.5,
-0.4,
# 0 should raise:
0,
))
def test_setframerate_validates_rounded_values(self, arg):
"""Test that setframerate that round to 0 or negative are rejected"""
with wave.open(io.BytesIO(), 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
# 0.5 rounds to 0, should raise
with self.assertRaises(wave.Error):
f.setframerate(0.5)
# 0.4 rounds to 0, should raise
f.setframerate(arg)
with self.assertRaises(wave.Error):
f.setframerate(0.4)
# Negative values should still raise
with self.assertRaises(wave.Error):
f.setframerate(-1)
with self.assertRaises(wave.Error):
f.setframerate(-0.5)
# 0 should raise
with self.assertRaises(wave.Error):
f.setframerate(0)

# Valid values that round to positive integers should work
f.setframerate(1.4) # rounds to 1
self.assertEqual(f.getframerate(), 1)
f.setframerate(1.5) # rounds to 2
self.assertEqual(f.getframerate(), 2)
f.setframerate(1.6) # rounds to 2
self.assertEqual(f.getframerate(), 2)
f.setframerate(44100.4) # rounds to 44100
self.assertEqual(f.getframerate(), 44100)
f.setframerate(44100.5) # rounds to 44100
self.assertEqual(f.getframerate(), 44100)
f.close()

@support.subTests(('arg', 'expected'), (
(1.4, 1),
(1.5, 2),
(1.6, 2),
(44100.4, 44100),
(44100.5, 44100),
(44100.6, 44101),
))
def test_setframerate_rounds(self, arg, expected):
"""Test that setframerate is rounded"""
with wave.open(io.BytesIO(), 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(arg)
self.assertEqual(f.getframerate(), expected)


class WaveOpen(unittest.TestCase):
Expand Down
Loading