Esempio n. 1
0
 def setUp(self):
     self.sound_test_file = "unittest_files/Pop.wav"
     self.valid_sound_yaml = "- yaml_sound:\n    filename: {}\n".format(
         self.sound_test_file) + "    sound_type: music\n    preload: False\n"
     self.valid_sound_object = Sound("yaml_sound",
                                     filename=self.sound_test_file, sound_type="music",
                                     preload=False)
Esempio n. 2
0
 def setup(self, screen):
     """Handle the game setup phase, prior to the main game loop."""
     self.screen = screen
     self.game_engine.screen = screen
     self.game_engine.draw_surface = screen
     res = self.game_engine.resources
     res['sprites']['spr_test'] = object_sprite.ObjectSprite(
         "spr_test",
         filename="unittest_files/ball2.png",
         collision_type="precise")
     res['sprites']['spr_solid'] = object_sprite.ObjectSprite(
         "spr_solid",
         filename="unittest_files/solid.png",
         collision_type="precise")
     res['sprites']['spr_spaceship'] = object_sprite.ObjectSprite(
         "spr_spaceship",
         filename="unittest_files/spaceship_strip07.png",
         collision_type="precise")
     res['sounds']['snd_test'] = Sound("snd_test",
                                       filename="unittest_files/Pop.wav")
     res['sounds']['snd_explosion'] = Sound(
         "snd_explosion", filename="unittest_files/explosion.wav")
     with open(OBJ_TEST_FILE, "r") as yaml_f:
         res['objects']['obj_test'] = ObjectType.load_from_yaml(
             yaml_f, self.game_engine)[0]
     res['objects']['obj_solid'] = CollideableObjectType("obj_solid",
                                                         self.game_engine,
                                                         solid=True,
                                                         sprite='spr_solid')
     # this doubles as a solid object and as the manager object
     res['objects']['obj_solid'].create_instance(self.screen,
                                                 position=(308, 228))
     with open(OBJ_TEST_FILE2, "r") as yaml_f:
         res['objects']['obj_spaceship'] = ObjectType.load_from_yaml(
             yaml_f, self.game_engine)[0]
     res['objects']['obj_spaceship'].create_instance(self.screen,
                                                     position=(308, 450))
     res['objects']['obj_solid']['kb_enter_keydn'] = ActionSequence()
     res['objects']['obj_solid']['kb_enter_keydn'].append_action(
         action.ObjectAction(
             "create_object", {
                 'object': 'obj_test',
                 'position.x': "=randint({})".format(
                     self.screen.get_width()),
                 'position.y': "=randint({})".format(
                     self.screen.get_height())
             }))
     res['objects']['obj_solid'][
         'mouse_global_left_pressed'] = ActionSequence()
     res['objects']['obj_solid']['mouse_global_left_pressed'].append_action(
         action.ObjectAction(
             "create_object", {
                 'object': 'obj_test',
                 'position.x': "=mouse.x",
                 'position.y': "=mouse.y"
             }))
     print "Setup complete"
Esempio n. 3
0
 def test_015missing_sound_file(self):
     """
     Test that the appropriate exception is raised for missing sound files.
     """
     missing_music1 = Sound("missing1", sound_type="music", filename="unittest/missing1.wav")
     with self.assertRaises(SoundException):
         missing_music1.setup()
     missing_sound1 = Sound("missing2", preload=False, filename="unittest/missing2.wav")
     missing_sound1.setup()
     with self.assertRaises(SoundException):
         missing_sound1.play_sound()
Esempio n. 4
0
 def test_025yaml_to_sound(self):
     """Test loading of YAML-formatted sound info into a new instance."""
     tmpf_info = tempfile.mkstemp(dir="/tmp")
     tmp_file = os.fdopen(tmpf_info[0], 'w')
     tmp_file.write(self.valid_sound_yaml)
     tmp_file.close()
     loaded_sound1 = None
     with open(tmpf_info[1], "r") as yaml_f:
         loaded_sound1 = Sound.load_from_yaml(yaml_f)[0]
     os.unlink(tmpf_info[1])
     self.assertEqual(self.valid_sound_object, loaded_sound1)
Esempio n. 5
0
 def test_030to_and_from_yaml(self):
     """Test round trip: convert an instance to YAML and back again."""
     generated_sound_yaml = self.valid_sound_object.to_yaml()
     tmpf_info = tempfile.mkstemp(dir="/tmp")
     tmp_file = os.fdopen(tmpf_info[0], 'w')
     tmp_file.write(generated_sound_yaml)
     tmp_file.close()
     loaded_sound1 = None
     with open(tmpf_info[1], "r") as yaml_f:
         loaded_sound1 = Sound.load_from_yaml(yaml_f)[0]
     os.unlink(tmpf_info[1])
     self.assertEqual(self.valid_sound_object, loaded_sound1)
Esempio n. 6
0
 def test_010sound_parameters(self):
     """Test parameter setting via keyword args."""
     pygame.init()
     music1 = Sound("music1", sound_type="music")
     self.assertEqual(music1.sound_type, "music")
     sound_with_file = Sound("dontpanic", filename=self.sound_test_file)
     sound_with_file.play_sound()
     self.assertTrue(sound_with_file.is_sound_playing())
     time.sleep(2)
     self.assertFalse(sound_with_file.is_sound_playing())
     self.assertEqual(sound_with_file.filename, self.sound_test_file)
Esempio n. 7
0
 def test_005valid_sound(self):
     """Test creation of a simple sound instance."""
     sound1 = Sound("sound1")
     self.assertEqual(sound1.name, "sound1")
Esempio n. 8
0
class TestSound(unittest.TestCase):
    """Unit tests for the sound module."""

    def setUp(self):
        self.sound_test_file = "unittest_files/Pop.wav"
        self.valid_sound_yaml = "- yaml_sound:\n    filename: {}\n".format(
            self.sound_test_file) + "    sound_type: music\n    preload: False\n"
        self.valid_sound_object = Sound("yaml_sound",
                                        filename=self.sound_test_file, sound_type="music",
                                        preload=False)

    def test_005valid_sound(self):
        """Test creation of a simple sound instance."""
        sound1 = Sound("sound1")
        self.assertEqual(sound1.name, "sound1")

    def test_010sound_parameters(self):
        """Test parameter setting via keyword args."""
        pygame.init()
        music1 = Sound("music1", sound_type="music")
        self.assertEqual(music1.sound_type, "music")
        sound_with_file = Sound("dontpanic", filename=self.sound_test_file)
        sound_with_file.play_sound()
        self.assertTrue(sound_with_file.is_sound_playing())
        time.sleep(2)
        self.assertFalse(sound_with_file.is_sound_playing())
        self.assertEqual(sound_with_file.filename, self.sound_test_file)

    def test_015missing_sound_file(self):
        """
        Test that the appropriate exception is raised for missing sound files.
        """
        missing_music1 = Sound("missing1", sound_type="music", filename="unittest/missing1.wav")
        with self.assertRaises(SoundException):
            missing_music1.setup()
        missing_sound1 = Sound("missing2", preload=False, filename="unittest/missing2.wav")
        missing_sound1.setup()
        with self.assertRaises(SoundException):
            missing_sound1.play_sound()

    def test_020sound_to_yaml(self):
        """Test conversion of a sound instance to YAML format."""
        self.assertEqual(self.valid_sound_object.to_yaml(), self.valid_sound_yaml)

    def test_025yaml_to_sound(self):
        """Test loading of YAML-formatted sound info into a new instance."""
        tmpf_info = tempfile.mkstemp(dir="/tmp")
        tmp_file = os.fdopen(tmpf_info[0], 'w')
        tmp_file.write(self.valid_sound_yaml)
        tmp_file.close()
        loaded_sound1 = None
        with open(tmpf_info[1], "r") as yaml_f:
            loaded_sound1 = Sound.load_from_yaml(yaml_f)[0]
        os.unlink(tmpf_info[1])
        self.assertEqual(self.valid_sound_object, loaded_sound1)

    def test_030to_and_from_yaml(self):
        """Test round trip: convert an instance to YAML and back again."""
        generated_sound_yaml = self.valid_sound_object.to_yaml()
        tmpf_info = tempfile.mkstemp(dir="/tmp")
        tmp_file = os.fdopen(tmpf_info[0], 'w')
        tmp_file.write(generated_sound_yaml)
        tmp_file.close()
        loaded_sound1 = None
        with open(tmpf_info[1], "r") as yaml_f:
            loaded_sound1 = Sound.load_from_yaml(yaml_f)[0]
        os.unlink(tmpf_info[1])
        self.assertEqual(self.valid_sound_object, loaded_sound1)