Skip to content

Commit

Permalink
Improve menu and sound testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Jul 31, 2019
1 parent cfe5acb commit c9eeb7c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pygameMenu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import pygameMenu.events
import pygameMenu.font
import pygameMenu.locals
import pygameMenu.sound
import pygameMenu.version

from pygameMenu.menu import Menu
from pygameMenu.sound import Sound
from pygameMenu.textmenu import TextMenu

# Other
Expand Down
6 changes: 4 additions & 2 deletions pygameMenu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,10 +867,12 @@ def _check_menu_initialized(self):
"""
Check menu initialization.
:return: None
:return: True if menu is initialized, raise Exception if not
:rtype: bool
"""
if self._top is None:
raise Exception('The menu has not been initialized yet, try using mainloop function')
return True

def mainloop(self, events=None, disable_loop=False):
"""
Expand Down Expand Up @@ -989,7 +991,7 @@ def set_sound(self, sound, recursive=False):
Set sound engine to a menu.
:param sound: Sound object
:type sound: pygameMenu.sound.Sound
:type sound: pygameMenu.sound.Sound, NoneType
:param recursive: Set the sound engine to all submenus
:type recursive: bool
:return: None
Expand Down
22 changes: 22 additions & 0 deletions test/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ def setUp(self):
self.menu = PygameMenuUtils.generic_menu('mainmenu')
self.menu.mainloop()

def test_enabled(self):
"""
Test menu enable/disable feature.
"""
menu = PygameMenuUtils.generic_menu()
self.assert_(menu.is_disabled())
menu.enable()
self.assert_(menu.is_enabled())
self.assert_(not menu.is_disabled())

# Intialize and close
menu.mainloop()
menu._close()

def test_initialization(self):
"""
Test initialization of the menu.
"""
self.assert_(self.menu._check_menu_initialized())
menu_not_initializated = PygameMenuUtils.generic_menu()
self.assertRaises(Exception, lambda: menu_not_initializated._check_menu_initialized())

def test_depth(self):
"""
Test depth of a menu.
Expand Down
16 changes: 16 additions & 0 deletions test/test_sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ def test_example_sounds(self):
self.sound.play_key_add()
self.sound.play_key_del()
self.sound.play_open_menu()

def test_sound_menu(self):
"""
Test sounds in menu.
"""
menu = PygameMenuUtils.generic_menu()
submenu = PygameMenuUtils.generic_menu()

menu.add_option('submenu', submenu)
button = menu.add_option('button', lambda: None)
menu.set_sound(self.sound, True)
self.assertEqual(button.sound, self.sound)

# This will remove the sound engine
menu.set_sound(None, True)
self.assertNotEqual(button.sound, self.sound)

0 comments on commit c9eeb7c

Please sign in to comment.