ยปCore Development>Code coverage>Lib/test/test_winsound.py

Python code coverage for Lib/test/test_winsound.py

#countcontent
1n/a# Ridiculously simple test of the winsound module for Windows.
2n/a
3n/aimport functools
4n/aimport time
5n/aimport unittest
6n/a
7n/afrom test import support
8n/a
9n/asupport.requires('audio')
10n/awinsound = support.import_module('winsound')
11n/a
12n/a
13n/a# Unless we actually have an ear in the room, we have no idea whether a sound
14n/a# actually plays, and it's incredibly flaky trying to figure out if a sound
15n/a# even *should* play. Instead of guessing, just call the function and assume
16n/a# it either passed or raised the RuntimeError we expect in case of failure.
17n/adef sound_func(func):
18n/a @functools.wraps(func)
19n/a def wrapper(*args, **kwargs):
20n/a try:
21n/a ret = func(*args, **kwargs)
22n/a except RuntimeError as e:
23n/a if support.verbose:
24n/a print(func.__name__, 'failed:', e)
25n/a else:
26n/a if support.verbose:
27n/a print(func.__name__, 'returned')
28n/a return ret
29n/a return wrapper
30n/a
31n/a
32n/asafe_Beep = sound_func(winsound.Beep)
33n/asafe_MessageBeep = sound_func(winsound.MessageBeep)
34n/asafe_PlaySound = sound_func(winsound.PlaySound)
35n/a
36n/a
37n/aclass BeepTest(unittest.TestCase):
38n/a
39n/a def test_errors(self):
40n/a self.assertRaises(TypeError, winsound.Beep)
41n/a self.assertRaises(ValueError, winsound.Beep, 36, 75)
42n/a self.assertRaises(ValueError, winsound.Beep, 32768, 75)
43n/a
44n/a def test_extremes(self):
45n/a safe_Beep(37, 75)
46n/a safe_Beep(32767, 75)
47n/a
48n/a def test_increasingfrequency(self):
49n/a for i in range(100, 2000, 100):
50n/a safe_Beep(i, 75)
51n/a
52n/a def test_keyword_args(self):
53n/a safe_Beep(duration=75, frequency=2000)
54n/a
55n/a
56n/aclass MessageBeepTest(unittest.TestCase):
57n/a
58n/a def tearDown(self):
59n/a time.sleep(0.5)
60n/a
61n/a def test_default(self):
62n/a self.assertRaises(TypeError, winsound.MessageBeep, "bad")
63n/a self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
64n/a safe_MessageBeep()
65n/a
66n/a def test_ok(self):
67n/a safe_MessageBeep(winsound.MB_OK)
68n/a
69n/a def test_asterisk(self):
70n/a safe_MessageBeep(winsound.MB_ICONASTERISK)
71n/a
72n/a def test_exclamation(self):
73n/a safe_MessageBeep(winsound.MB_ICONEXCLAMATION)
74n/a
75n/a def test_hand(self):
76n/a safe_MessageBeep(winsound.MB_ICONHAND)
77n/a
78n/a def test_question(self):
79n/a safe_MessageBeep(winsound.MB_ICONQUESTION)
80n/a
81n/a def test_keyword_args(self):
82n/a safe_MessageBeep(type=winsound.MB_OK)
83n/a
84n/a
85n/aclass PlaySoundTest(unittest.TestCase):
86n/a
87n/a def test_errors(self):
88n/a self.assertRaises(TypeError, winsound.PlaySound)
89n/a self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
90n/a self.assertRaises(
91n/a RuntimeError,
92n/a winsound.PlaySound,
93n/a "none", winsound.SND_ASYNC | winsound.SND_MEMORY
94n/a )
95n/a self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0)
96n/a self.assertRaises(TypeError, winsound.PlaySound, "bad",
97n/a winsound.SND_MEMORY)
98n/a self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
99n/a
100n/a def test_keyword_args(self):
101n/a safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit")
102n/a
103n/a def test_snd_memory(self):
104n/a with open(support.findfile('pluck-pcm8.wav',
105n/a subdir='audiodata'), 'rb') as f:
106n/a audio_data = f.read()
107n/a safe_PlaySound(audio_data, winsound.SND_MEMORY)
108n/a audio_data = bytearray(audio_data)
109n/a safe_PlaySound(audio_data, winsound.SND_MEMORY)
110n/a
111n/a def test_snd_filename(self):
112n/a fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
113n/a safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
114n/a
115n/a def test_aliases(self):
116n/a aliases = [
117n/a "SystemAsterisk",
118n/a "SystemExclamation",
119n/a "SystemExit",
120n/a "SystemHand",
121n/a "SystemQuestion",
122n/a ]
123n/a for alias in aliases:
124n/a with self.subTest(alias=alias):
125n/a safe_PlaySound(alias, winsound.SND_ALIAS)
126n/a
127n/a def test_alias_fallback(self):
128n/a safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
129n/a
130n/a def test_alias_nofallback(self):
131n/a safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT)
132n/a
133n/a def test_stopasync(self):
134n/a safe_PlaySound(
135n/a 'SystemQuestion',
136n/a winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
137n/a )
138n/a time.sleep(0.5)
139n/a safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP)
140n/a # Issue 8367: PlaySound(None, winsound.SND_PURGE)
141n/a # does not raise on systems without a sound card.
142n/a winsound.PlaySound(None, winsound.SND_PURGE)
143n/a
144n/a
145n/aif __name__ == "__main__":
146n/a unittest.main()