Skip to content

Commit

Permalink
refactor: separate tests into modules from event_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 4, 2024
1 parent bfabc7a commit a9a7749
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 47 deletions.
4 changes: 3 additions & 1 deletion src/endstone_test/command_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")

Expand Down
38 changes: 2 additions & 36 deletions src/endstone_test/event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}")
Expand All @@ -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):
Expand Down
12 changes: 2 additions & 10 deletions src/endstone_test/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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!")
Expand Down
19 changes: 19 additions & 0 deletions src/endstone_test/test_helper.py
Original file line number Diff line number Diff line change
@@ -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)])
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
84 changes: 84 additions & 0 deletions src/endstone_test/tests/on_player_join/test_player.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a9a7749

Please sign in to comment.