def test_update_collision_should_activate(self, mock_pygame, mock_load_png_sequence): """Test that the powerup does activate when it collides with the paddle. """ mock_image_1 = Mock() mock_image_2 = Mock() mock_load_png_sequence.return_value = [(mock_image_1, Mock()), (mock_image_2, Mock())] mock_rect = Mock() mock_pygame.Rect.return_value = mock_rect mock_rect.move.return_value = mock_rect mock_rect.colliderect.return_value = True mock_screen = Mock() mock_pygame.display.get_surface.return_value = mock_screen mock_area = Mock() mock_screen.get_rect.return_value = mock_area mock_area.contains.return_value = True mock_game = Mock() mock_game.paddle.exploding = False mock_game.paddle.visible = True mock_active_powerup = Mock() mock_game.active_powerup = mock_active_powerup powerup = PowerUp(mock_game, Mock(), 'test_prefix') mock_activate = Mock() powerup._activate = mock_activate powerup.update() mock_rect.colliderect.assert_called_once_with(mock_game.paddle.rect) mock_active_powerup.deactivate.assert_called_once_with() mock_activate.assert_called_once_with() self.assertIsNotNone(mock_game.active_powerup) mock_game.sprites.remove.assert_called_once_with(powerup) self.assertFalse(powerup.visible)
def test_update_collision_no_activate_explode(self, mock_pygame, mock_load_png_sequence): """Test that the powerup does not activate when it collides with the paddle but the paddle is exploding. """ mock_image_1 = Mock() mock_image_2 = Mock() mock_load_png_sequence.return_value = [(mock_image_1, Mock()), (mock_image_2, Mock())] mock_rect = Mock() mock_pygame.Rect.return_value = mock_rect mock_rect.move.return_value = mock_rect mock_rect.colliderect.return_value = True mock_screen = Mock() mock_pygame.display.get_surface.return_value = mock_screen mock_area = Mock() mock_screen.get_rect.return_value = mock_area mock_area.contains.return_value = True mock_game = Mock() mock_game.paddle.exploding = True mock_game.active_powerup = None powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup.update() mock_rect.colliderect.assert_called_once_with(mock_game.paddle.rect) self.assertIsNone(mock_game.active_powerup)
def test_deactivate_is_noop(self, mock_pygame, mock_load_png_sequence): mock_game = _configure_mocks(mock_load_png_sequence, mock_pygame) mock_game.lives = 2 mock_game.active_powerup = ExtraLifePowerUp(mock_game, Mock()) powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup._activate = Mock() powerup.update() self.assertEqual(mock_game.lives, 2)
def test_init_positions_powerup(self, mock_pygame, mock_load_png_sequence): mock_brick = Mock() mock_brick.rect.bottomleft = (10, 10) mock_brick.rect.width = 50 mock_brick.rect.height = 10 PowerUp(Mock(), mock_brick, 'test_prefix') mock_pygame.Rect.assert_called_once_with((10, 10), (50, 10))
def test_update_moves_powerup(self, mock_pygame, mock_load_png_sequence): mock_image = Mock() mock_load_png_sequence.return_value = [(mock_image, Mock())] mock_rect = Mock() mock_pygame.Rect.return_value = mock_rect mock_rect.move.return_value = mock_rect mock_screen = Mock() mock_pygame.display.get_surface.return_value = mock_screen mock_area = Mock() mock_screen.get_rect.return_value = mock_area mock_area.contains.return_value = True powerup = PowerUp(Mock(), Mock(), 'test_prefix') powerup.update() mock_rect.move.assert_called_once_with(0, powerup._speed) self.assertEquals(powerup.image, mock_image)
def test_deactivate(self, mock_pygame, mock_load_png_sequence, mock_receiver): mock_game = _configure_mocks(mock_load_png_sequence, mock_pygame) catch_powerup = CatchPowerUp(mock_game, Mock()) mock_game.active_powerup = catch_powerup mock_game.balls = [Mock(), Mock(), Mock()] powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup._activate = Mock() powerup.update() mock_game.paddle.ball_collide_callbacks.remove.assert_called_once_with( catch_powerup._catch) mock_receiver.unregister_handler.assert_called_once_with( catch_powerup._release_ball) for ball in mock_game.balls: ball.release.assert_called_once_with()
def test_deactivate_reinstates_ball_speed(self, mock_pygame, mock_load_png_sequence): mock_game = _configure_mocks(mock_load_png_sequence, mock_pygame) mock_game.active_powerup = SlowBallPowerUp(mock_game, Mock()) mock_game.active_powerup._orig_speed = 10 mock_game.balls = [Mock(), Mock(), Mock()] for ball in mock_game.balls: ball.speed = SlowBallPowerUp._SLOW_BALL_SPEED ball.base_speed = SlowBallPowerUp._SLOW_BALL_SPEED powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup._activate = Mock() powerup.update() self.assertEqual(len(mock_game.balls), 3) for ball in mock_game.balls: self.assertEqual(ball.speed, 10) self.assertEqual(ball.base_speed, 10)
def test_deactivate_transitions_to_normal_state(self, mock_pygame, mock_load_png_sequence, mock_normal_state): mock_game = _configure_mocks(mock_load_png_sequence, mock_pygame) mock_game.active_powerup = LaserPowerUp(mock_game, Mock()) mock_state = Mock() mock_normal_state.return_value = mock_state mock_game.balls = [Mock(), Mock(), Mock()] for ball in mock_game.balls: ball.base_speed = 11 powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup._activate = Mock() powerup.update() mock_game.paddle.transition.assert_called_once_with(mock_state) self.assertEqual(len(mock_game.balls), 3) for ball in mock_game.balls: self.assertEqual(ball.base_speed, 10)
def test_update_moves_powerup_offscreen(self, mock_pygame, mock_load_png_sequence): mock_image = Mock() mock_load_png_sequence.return_value = [(mock_image, Mock())] mock_rect = Mock() mock_pygame.Rect.return_value = mock_rect mock_rect.move.return_value = mock_rect mock_screen = Mock() mock_pygame.display.get_surface.return_value = mock_screen mock_area = Mock() mock_screen.get_rect.return_value = mock_area mock_area.contains.return_value = False mock_game = Mock() powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup.update() mock_rect.move.assert_called_once_with(0, powerup._speed) mock_game.sprites.remove.assert_called_once_with(powerup) self.assertFalse(powerup.visible)
def test_deactivate(self, mock_pygame, mock_load_png_sequence, mock_receiver): mock_game = _configure_mocks(mock_load_png_sequence, mock_pygame) catch_powerup = CatchPowerUp(mock_game, Mock()) mock_game.active_powerup = catch_powerup mock_game.balls = [Mock(), Mock(), Mock()] powerup = PowerUp(mock_game, Mock(), 'test_prefix') powerup._activate = Mock() powerup.update() mock_game.paddle.ball_collide_callbacks.remove.assert_called_once_with( catch_powerup._catch ) mock_receiver.unregister_handler.assert_called_once_with( catch_powerup._release_ball ) for ball in mock_game.balls: ball.release.assert_called_once_with()
def test_update_animates_powerup(self, mock_pygame, mock_load_png_sequence): mock_image_1 = Mock() mock_image_2 = Mock() mock_load_png_sequence.return_value = [(mock_image_1, Mock()), (mock_image_2, Mock())] mock_rect = Mock() mock_pygame.Rect.return_value = mock_rect mock_rect.move.return_value = mock_rect mock_screen = Mock() mock_pygame.display.get_surface.return_value = mock_screen mock_area = Mock() mock_screen.get_rect.return_value = mock_area mock_area.contains.return_value = True powerup = PowerUp(Mock(), Mock(), 'test_prefix') # The powerup image is changed every 4 cycles. powerup.update() powerup.update() powerup.update() powerup.update() self.assertEquals(powerup.image, mock_image_2)
def test_init_loads_png_sequence(self, mock_pygame, mock_load_png_sequence): PowerUp(Mock(), Mock(), 'test_prefix') mock_load_png_sequence.assert_called_once_with('test_prefix')