From a9a77494fbad06ea53ed5613943043823b4a73f4 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 4 Sep 2024 21:44:05 +0100 Subject: [PATCH] refactor: separate tests into modules from event_listener --- src/endstone_test/command_executor.py | 4 +- src/endstone_test/event_listener.py | 38 +-------- src/endstone_test/plugin.py | 12 +-- src/endstone_test/test_helper.py | 19 +++++ src/endstone_test/tests/on_load/__init__.py | 0 .../tests/{ => on_load}/test_block.py | 0 .../tests/{ => on_load}/test_command.py | 0 .../tests/{ => on_load}/test_plugin.py | 0 .../tests/{ => on_load}/test_scoreboard.py | 0 .../tests/{ => on_load}/test_server.py | 0 .../tests/on_player_join/__init__.py | 0 .../tests/on_player_join/test_player.py | 84 +++++++++++++++++++ 12 files changed, 110 insertions(+), 47 deletions(-) create mode 100644 src/endstone_test/test_helper.py create mode 100644 src/endstone_test/tests/on_load/__init__.py rename src/endstone_test/tests/{ => on_load}/test_block.py (100%) rename src/endstone_test/tests/{ => on_load}/test_command.py (100%) rename src/endstone_test/tests/{ => on_load}/test_plugin.py (100%) rename src/endstone_test/tests/{ => on_load}/test_scoreboard.py (100%) rename src/endstone_test/tests/{ => on_load}/test_server.py (100%) create mode 100644 src/endstone_test/tests/on_player_join/__init__.py create mode 100644 src/endstone_test/tests/on_player_join/test_player.py diff --git a/src/endstone_test/command_executor.py b/src/endstone_test/command_executor.py index 281142a..cf46376 100644 --- a/src/endstone_test/command_executor.py +++ b/src/endstone_test/command_executor.py @@ -80,13 +80,15 @@ def on_command(self, sender: CommandSender, command: Command, args: list[str]) - else: sender.send_error_message(f"Unknown sender: {sender.__class__}") - case ["player", ("toast" | "kick") as test_type]: + case ["player", ("toast" | "title" | "kick") as test_type]: if not isinstance(sender, Player): sender.send_error_message("You must execute this command as a player") return False if test_type == "toast": sender.send_toast("This is the title", "This is the content") + elif test_type == "title": + sender.send_title("Welcome!", sender.name) elif test_type == "kick": sender.kick("kick is working!") diff --git a/src/endstone_test/event_listener.py b/src/endstone_test/event_listener.py index 7545bc8..eeb418e 100644 --- a/src/endstone_test/event_listener.py +++ b/src/endstone_test/event_listener.py @@ -4,6 +4,7 @@ from endstone import ColorFormat, Server, Translatable from endstone.event import * from endstone.plugin import Plugin +from endstone_test.test_helper import run_tests class EventListener: @@ -12,18 +13,11 @@ def __init__(self, plugin: Plugin): @event_handler def on_player_login(self, event: PlayerLoginEvent) -> None: - player = event.player self.server.broadcast_message(ColorFormat.YELLOW + f"{event.player.name} logged in.") @event_handler def on_player_join(self, event: PlayerJoinEvent) -> None: self.server.broadcast_message(ColorFormat.YELLOW + f"{event.player.name} joined the game.") - event.player.send_message(Translatable("commands.give.success", ["Secret Item", "233", "Secret Man"])) - event.player.send_title("Welcome!", event.player.name) - - assert event.player in self.server.online_players - assert self.server.get_player(event.player.name) is event.player - assert self.server.get_player(event.player.unique_id) is event.player self._plugin.logger.info("===========================") self._plugin.logger.info(f"Name: {event.player.name}") @@ -40,35 +34,7 @@ def on_player_join(self, event: PlayerJoinEvent) -> None: self._plugin.logger.info(f"Device: {event.player.device_os} {event.player.device_id}") self._plugin.logger.info("===========================") - assert event.player.inventory.size == 36 - assert event.player.inventory.max_stack_size == 254 - - assert event.player.has_permission("minecraft.command.me") is True - event.player.add_attachment(self._plugin, "minecraft.command.me", False) - assert event.player.has_permission("minecraft.command.me") is False - event.player.update_commands() - - # event.player.allow_flight = True - # assert event.player.allow_flight is True - - current_exp_lvl = event.player.exp_level - event.player.give_exp_levels(2) - assert event.player.exp_level == current_exp_lvl + 2 - assert 0.0 <= event.player.exp_progress <= 1.0 - event.player.exp_level = current_exp_lvl + 1 - assert event.player.exp_level == current_exp_lvl + 1 - - assert abs(event.player.fly_speed - 0.05) <= 0.00001 - assert abs(event.player.walk_speed - 0.10) <= 0.00001 - print(event.player.fly_speed, event.player.walk_speed) - - assert Locale.parse(event.player.locale) is not None, event.player.locale - - skin = event.player.skin - self._plugin.logger.info(f"Skin Id: {skin.skin_id}, Cape Id: {skin.cape_id}") - assert skin.skin_data.shape[2] == 4, f"Bad shape for skin data: {skin.skin_data.shape}" - if skin.cape_data is not None: - assert skin.cape_data.shape[2] == 4, f"Bad shape for cape data: {skin.cape_data.shape}" + run_tests("on_player_join", player=event.player, plugin=self._plugin) @event_handler def on_player_interact(self, event: PlayerInteractEvent): diff --git a/src/endstone_test/plugin.py b/src/endstone_test/plugin.py index 55339ea..5a6fe24 100644 --- a/src/endstone_test/plugin.py +++ b/src/endstone_test/plugin.py @@ -4,15 +4,7 @@ from endstone.plugin import Plugin from endstone_test.event_listener import EventListener - - -class FixtureInjection: - def __init__(self, obj): - self.obj = obj - - @pytest.fixture(scope='session') - def plugin(self): - return self.obj +from endstone_test.test_helper import run_tests class EndstoneTest(Plugin): @@ -45,7 +37,7 @@ def on_load(self) -> None: def on_enable(self) -> None: self.logger.info("on_enable is called!") self.register_events(EventListener(self)) - self.run_tests() + run_tests("on_load", plugin=self) def on_disable(self) -> None: self.logger.info("on_disable is called!") diff --git a/src/endstone_test/test_helper.py b/src/endstone_test/test_helper.py new file mode 100644 index 0000000..e358e34 --- /dev/null +++ b/src/endstone_test/test_helper.py @@ -0,0 +1,19 @@ +import pytest +from _pytest.config import ExitCode + + +class FixtureInjection: + def __init__(self, **kwargs): + for name, obj in kwargs.items(): + setattr(self, name, pytest.fixture(scope='session')(self._create_fixture(obj))) + + @staticmethod + def _create_fixture(obj): + def fixture_func(): + return obj + + return fixture_func + + +def run_tests(name: str, **kwargs) -> int | ExitCode: + return pytest.main(["-s", "--pyargs", f"endstone_test.tests.{name}"], plugins=[FixtureInjection(**kwargs)]) diff --git a/src/endstone_test/tests/on_load/__init__.py b/src/endstone_test/tests/on_load/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/endstone_test/tests/test_block.py b/src/endstone_test/tests/on_load/test_block.py similarity index 100% rename from src/endstone_test/tests/test_block.py rename to src/endstone_test/tests/on_load/test_block.py diff --git a/src/endstone_test/tests/test_command.py b/src/endstone_test/tests/on_load/test_command.py similarity index 100% rename from src/endstone_test/tests/test_command.py rename to src/endstone_test/tests/on_load/test_command.py diff --git a/src/endstone_test/tests/test_plugin.py b/src/endstone_test/tests/on_load/test_plugin.py similarity index 100% rename from src/endstone_test/tests/test_plugin.py rename to src/endstone_test/tests/on_load/test_plugin.py diff --git a/src/endstone_test/tests/test_scoreboard.py b/src/endstone_test/tests/on_load/test_scoreboard.py similarity index 100% rename from src/endstone_test/tests/test_scoreboard.py rename to src/endstone_test/tests/on_load/test_scoreboard.py diff --git a/src/endstone_test/tests/test_server.py b/src/endstone_test/tests/on_load/test_server.py similarity index 100% rename from src/endstone_test/tests/test_server.py rename to src/endstone_test/tests/on_load/test_server.py diff --git a/src/endstone_test/tests/on_player_join/__init__.py b/src/endstone_test/tests/on_player_join/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/endstone_test/tests/on_player_join/test_player.py b/src/endstone_test/tests/on_player_join/test_player.py new file mode 100644 index 0000000..7127f97 --- /dev/null +++ b/src/endstone_test/tests/on_player_join/test_player.py @@ -0,0 +1,84 @@ +from babel import Locale +from endstone import Player, Server, GameMode +from endstone.plugin import Plugin +import pytest + + +@pytest.fixture +def server(player: Player) -> Server: + return player.server + + +def test_player_in_online_players(player: Player, server: Server): + assert player in server.online_players + assert server.get_player(player.name) is player + assert server.get_player(player.unique_id) is player + + +def test_player_inventory(player: Player): + assert player.inventory.size == 36 + assert player.inventory.max_stack_size == 254 + + +def test_player_permissions(player: Player, plugin: Plugin): + assert player.has_permission("minecraft.command.me") is True + player.add_attachment(plugin, "minecraft.command.me", False) + + assert player.has_permission("minecraft.command.me") is False + player.update_commands() + + +def test_player_experience(player: Player): + current_exp_lvl = player.exp_level + current_exp_progress = player.exp_progress + + player.give_exp_levels(2) + assert player.exp_level == current_exp_lvl + 2 + + assert 0.0 <= player.exp_progress <= 1.0 + player.exp_level = current_exp_lvl + 1 + assert player.exp_level == current_exp_lvl + 1 + + player.exp_progress = 1.0 - player.exp_progress + assert abs(player.exp_progress + current_exp_progress - 1.0) <= 0.00001 + + player.exp_level = current_exp_lvl + player.exp_progress = current_exp_progress + assert player.exp_level == current_exp_lvl + assert player.exp_progress == current_exp_progress + + +def test_player_attributes(player: Player): + assert abs(player.fly_speed - 0.05) <= 0.00001 + assert abs(player.walk_speed - 0.10) <= 0.00001 + + +def test_player_locale(player: Player): + assert Locale.parse(player.locale) is not None + + +def test_player_skin(player: Player): + skin = player.skin + assert skin.skin_data.shape[2] == 4 + if skin.cape_data is not None: + assert skin.cape_data.shape[2] == 4 + + +def test_player_game_mode(player: Player): + current_game_mode = player.game_mode + player.game_mode = GameMode.SPECTATOR + assert player.game_mode == GameMode.SPECTATOR + player.game_mode = current_game_mode + assert player.game_mode == current_game_mode + + +def test_player_scoreboard(player: Player, server: Server): + assert player.scoreboard is server.scoreboard + + new_scoreboard = server.get_new_scoreboard() + player.scoreboard = new_scoreboard + assert player.scoreboard is new_scoreboard + assert player.scoreboard is not server.scoreboard + + player.scoreboard = server.scoreboard + assert player.scoreboard is server.scoreboard