def test_should_add_sprite_to_group_on_creation(self): # given when group = Group() sprite = XPGESprite(None, group) # then self.assertTrue(group.has(sprite)) self.assertEqual(0, sprite.groups().index(group))
def test_should_add_sprite_to_two_groups_on_creation(self): # given when group1 = Group() group2 = Group() sprite = XPGESprite(None, group1, group2) # then self.assertTrue(group1.has(sprite)) self.assertTrue(group2.has(sprite)) self.assertEqual(0, sprite.groups().index(group1)) self.assertEqual(1, sprite.groups().index(group2))
def __init__(self, scene_manager): super().__init__(scene_manager) star = pygame.image.load(os.path.join("demo_resources", "star.png")).convert() demo_sprite = XPGESprite(self.scene_manager) demo_sprite.image = star demo_sprite.position = (270, 190) demo_sprite.components.append(DemoSpriteBehaviour(demo_sprite)) self.sprites.append(demo_sprite)
def setUp(self): self.sprite = XPGESprite(None) self.sprite.image = Surface((SPRITE_SIZE_X, SPRITE_SIZE_Y)) self.sprite.position = (SPRITE_X, SPRITE_Y) self.component_1 = Mock(spec=TestComponent1) self.component_2 = Mock(spec=TestComponent2) self.component_3 = Mock(spec=TestComponent3) self.sprite.components.append(self.component_1) self.sprite.components.append(self.component_2) self.sprite.components.append(self.component_3)
def __init__(self, scene_manager): super().__init__(scene_manager) basket_image = pygame.Surface((100, 25)) basket_image.fill((255, 0, 0)) basket = XPGESprite(self.scene_manager) basket.image = basket_image basket.position = (350, 575) basket.name = "basket" basket.components.append(BasketController(basket)) basket.components.append(AppleSpawner(basket)) self.sprites.append(basket)
def test_should_throw_error_on_killing_when_sprite_not_existing(self): # given simple_scene_manager = SimpleSceneManager() test_sprite = XPGESprite(simple_scene_manager) test_sprite.name = "test sprite" mock_component = Mock(spec=SpriteBehaviour) test_sprite.components.append(mock_component) # when then with self.assertRaises(ValueError): simple_scene_manager.kill(test_sprite) mock_component.on_kill.assert_not_called()
def test_should_spawn_sprite(self): # given simple_scene_manager = SimpleSceneManager() test_sprite = XPGESprite(simple_scene_manager) test_sprite.name = "test sprite" mock_component = Mock(spec=SpriteBehaviour) test_sprite.components.append(mock_component) # when simple_scene_manager.spawn(test_sprite) # then self.assertIn(test_sprite, simple_scene_manager._sprites) mock_component.on_spawn.assert_called_once()
def test_should_return_none_when_calling_get_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name_2" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.get_by_name("test_name") # then self.assertIsNone(result)
def test_should_load_scene(self): # given simple_scene_manager = SimpleSceneManager() simple_scene_manager._sprites = MagicMock() test_sprite = XPGESprite(simple_scene_manager) component_mock = Mock(spec=SpriteBehaviour) test_sprite.components.append(component_mock) sprite_list = [test_sprite] simple_scene_manager._sprites.__iter__ = Mock( return_value=iter(sprite_list)) test_scene_name = "test name" class TestSimpleScene(SimpleScene): def __init__(self, scene_manager): super().__init__(scene_manager) self.sprites = sprite_list simple_scene_manager.register_scene(TestSimpleScene, test_scene_name) # when simple_scene_manager.load_scene(test_scene_name) # then self.assertIsInstance(simple_scene_manager._current_scene, TestSimpleScene) simple_scene_manager._sprites.clear.assert_called_once() simple_scene_manager._sprites.append.assert_called_once_with( test_sprite) component_mock.on_scene_loaded.assert_called_once()
def test_should_get_sprite_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.get_by_name("test_name") # then self.assertIsNotNone(result) self.assertEqual(test_sprite_1, result)
def test_should_return_empty_list_when_calling_find_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name_2" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.find_by_name("test_name") # then self.assertIsNotNone(result) self.assertEqual(0, len(result))
def on_update(self): self.counter += 1 if self.counter % 100 == 0: apple_image = pygame.Surface((25, 25)) apple_image.fill((125, 125, 0)) apple = XPGESprite(self.sprite.scene_manager) apple.image = apple_image apple.components.append(AppleController(apple)) x = random.randint(0, 775) y = 0 apple.position = (x, y) self.sprite.scene_manager._sprites.append(apple) self.sprite.scene_manager.static["apples_number"] += 1 print("Number of apples: {}".format( self.sprite.scene_manager.static["apples_number"]))
def __init__(self, scene_manager): super().__init__(scene_manager) star1 = pygame.image.load(os.path.join("demo_resources", "star.png")).convert() star2 = pygame.image.load(os.path.join("demo_resources", "star2.png")).convert() sprite1 = XPGESprite(self.scene_manager) sprite1.image = star1 sprite1.position = (10, 10) sprite1.components.append(ToggleSprite(sprite1)) sprite2 = XPGESprite(self.scene_manager) sprite2.name = "sprite2" sprite2.image = star2 sprite2.position = (200, 10) self.sprites.append(sprite1) self.sprites.append(sprite2)
class XPGESpriteTest(TestCase): """Test class for XPGESprite class.""" def setUp(self): self.sprite = XPGESprite(None) self.sprite.image = Surface((SPRITE_SIZE_X, SPRITE_SIZE_Y)) self.sprite.position = (SPRITE_X, SPRITE_Y) self.component_1 = Mock(spec=TestComponent1) self.component_2 = Mock(spec=TestComponent2) self.component_3 = Mock(spec=TestComponent3) self.sprite.components.append(self.component_1) self.sprite.components.append(self.component_2) self.sprite.components.append(self.component_3) def test_should_add_sprite_to_group_on_creation(self): # given when group = Group() sprite = XPGESprite(None, group) # then self.assertTrue(group.has(sprite)) self.assertEqual(0, sprite.groups().index(group)) def test_should_add_sprite_to_two_groups_on_creation(self): # given when group1 = Group() group2 = Group() sprite = XPGESprite(None, group1, group2) # then self.assertTrue(group1.has(sprite)) self.assertTrue(group2.has(sprite)) self.assertEqual(0, sprite.groups().index(group1)) self.assertEqual(1, sprite.groups().index(group2)) def test_should_update_components(self): # when self.sprite.update() # then self.component_1.on_update.assert_called_once() self.component_2.on_update.assert_called_once() self.component_3.on_update.assert_called_once() def test_should_call_on_click_on_components(self): # given self.sprite._focus = True self.component_1.on_click = Mock(return_value=False) self.component_2.on_click = Mock(return_value=False) self.component_3.on_click = Mock(return_value=False) # when self.sprite.handle_event( TEST_MOUSEBUTTONUP_EVENT_WITH_POS_INSIDE_SPRITE) # then self.component_1.on_click.assert_called_once_with(0) self.component_2.on_click.assert_called_once_with(0) self.component_3.on_click.assert_called_once_with(0) def test_should_not_call_on_click_on_last_component(self): # given self.sprite._focus = True self.component_1.on_click = Mock(return_value=False) self.component_2.on_click = Mock(return_value=True) # when self.sprite.handle_event( TEST_MOUSEBUTTONUP_EVENT_WITH_POS_INSIDE_SPRITE) # then self.component_1.on_click.assert_called_once_with(0) self.component_2.on_click.assert_called_once_with(0) self.component_3.on_click.assert_not_called() def test_should_call_handle_event_on_components(self): # given self.component_1.on_handle_event = Mock(return_value=False) self.component_2.on_handle_event = Mock(return_value=False) self.component_3.on_handle_event = Mock(return_value=False) # when self.sprite.handle_event(TEST_USEREVENT) # then self.component_1.on_handle_event.assert_called_once_with( TEST_USEREVENT) self.component_2.on_handle_event.assert_called_once_with( TEST_USEREVENT) self.component_3.on_handle_event.assert_called_once_with( TEST_USEREVENT) def test_should_not_call_handle_event_on_last_component(self): # given self.component_1.on_handle_event = Mock(return_value=False) self.component_2.on_handle_event = Mock(return_value=True) # when self.sprite.handle_event(TEST_USEREVENT) # then self.component_1.on_handle_event.assert_called_once_with( TEST_USEREVENT) self.component_2.on_handle_event.assert_called_once_with( TEST_USEREVENT) self.component_3.on_handle_event.assert_not_called() def test_should_set_sprite_focus_to_true(self): # given self.sprite._rect = Mock(spec=Rect) self.sprite._rect.collidepoint = Mock(return_value=True) # when self.sprite.handle_event(TEST_MOUSEMOTION_EVENT_WITH_POS_INSIDE_SPRITE) # then x, y = MOUSE_POS_INSIDE_SPRITE self.sprite.rect.collidepoint.assert_called_once_with(x, y) self.assertTrue(self.sprite.focus) def test_should_set_sprite_focus_to_false(self): # given self.sprite._rect = Mock(spec=Rect) self.sprite._rect.collidepoint = Mock(return_value=False) # when self.sprite.handle_event(TEST_MOUSEMOTION_EVENT_WITH_POS_INSIDE_SPRITE) # then x, y = MOUSE_POS_INSIDE_SPRITE self.sprite.rect.collidepoint.assert_called_once_with(x, y) self.assertFalse(self.sprite.focus) def test_should_not_change_focus_when_take_focus_is_false(self): # given self.sprite.takes_focus = False self.sprite._rect = Mock(spec=Rect) # when self.sprite.handle_event(TEST_MOUSEMOTION_EVENT_WITH_POS_INSIDE_SPRITE) # then self.sprite.rect.collidepoint.assert_not_called() self.assertFalse(self.sprite.focus) def test_should_call_on_hover_on_components(self): # when self.sprite.handle_event(TEST_MOUSEMOTION_EVENT_WITH_POS_INSIDE_SPRITE) # then self.component_1.on_hover.assert_called_once() self.component_2.on_hover.assert_called_once() self.component_3.on_hover.assert_called_once() def test_should_call_on_hover_exit_on_components(self): # given self.sprite._focus = True # when self.sprite.handle_event( TEST_MOUSEMOTION_EVENT_WITH_POS_OUTSIDE_SPRITE) # then self.component_1.on_hover_exit.assert_called_once() self.component_2.on_hover_exit.assert_called_once() self.component_3.on_hover_exit.assert_called_once() def test_should_draw_sprite_onto_surface(self): # given surface = Mock(spec=Surface) # when self.sprite.draw(surface) # then surface.blit.assert_called_once_with(self.sprite.image, (SPRITE_X, SPRITE_Y)) def test_should_get_component_by_type(self): # when component = self.sprite.get_component_by_type(TestComponent1) # then self.assertIsNotNone(component) self.assertEqual(self.component_1, component) def test_should_get_component_by_type_name(self): # when component = self.sprite.get_component_by_type_name("TestComponent1") # then self.assertIsNotNone(component) self.assertEqual(self.component_1, component) def test_should_raise_error_when_component_not_present(self): # when then with self.assertRaises(ComponentNotFoundError): self.sprite.get_component_by_type_name("TestComponent4") def test_should_raise_error_when_no_component_present(self): # given self.sprite.components.clear() # when then with self.assertRaises(ComponentNotFoundError): self.sprite.get_component_by_type_name("TestComponent1") def test_should_find_components_by_type(self): # given test_component = TestComponent1(self.sprite) self.sprite.components.append(test_component) # when components = self.sprite.find_components_by_type(TestComponent1) # then self.assertEqual(2, len(components)) self.assertIn(self.component_1, components) self.assertIn(test_component, components) def test_should_find_components_by_type_name(self): # given test_component = TestComponent1(self.sprite) self.sprite.components.append(test_component) # when components = self.sprite.find_components_by_type_name("TestComponent1") # then self.assertEqual(2, len(components)) self.assertIn(self.component_1, components) self.assertIn(test_component, components) def test_should_not_find_component_when_not_present(self): # when components = self.sprite.find_components_by_type_name("TestComponent4") # then self.assertEqual(0, len(components)) def test_should_not_find_component_when_no_component_present(self): # given self.sprite.components.clear() # when components = self.sprite.find_components_by_type_name("TestComponent1") # then self.assertEqual(0, len(components))