Skip to content

Commit

Permalink
feat: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Oct 5, 2024
1 parent 21b1582 commit ec26121
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/endstone_test/event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def on_thunder_change(self, event: ThunderChangeEvent):
def on_weather_change(self, event: WeatherChangeEvent):
self._plugin.logger.info(f"Weather state changed to {event.to_weather_state}")

@event_handler
def on_server_command(self, event: ServerCommandEvent):
self._plugin.logger.info(f"{event.sender.name} executed command: {event.command}")

@event_handler
def on_server_list_ping(self, event: ServerListPingEvent):
event.motd = ColorFormat.BOLD + event.motd
Expand Down
7 changes: 7 additions & 0 deletions src/endstone_test/tests/on_load/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ def test_create_block_data_custom_block_states(server: Server) -> None:
assert "ground_sign_direction" in block_data.block_states
assert block_data.block_states["ground_sign_direction"] == 8
assert "block_light_level" not in block_data.block_states


def test_get_block_outside_world_boundaries(server: Server) -> None:
with pytest.raises(RuntimeError) as exec_info:
server.level.dimensions[0].get_block_at(x=0, y=-256, z=0)

assert "outside of the world boundaries" in str(exec_info.value)
5 changes: 5 additions & 0 deletions src/endstone_test/tests/on_load/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ def test_command_executor(plugin: Plugin, command: PluginCommand) -> None:
executor = TestCommandExecutor()
command.executor = executor
assert command.executor is executor


def test_server_command_sender(plugin: Plugin) -> None:
server = plugin.server
assert server.command_sender.name == "Server"
22 changes: 22 additions & 0 deletions src/endstone_test/tests/on_load/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
from endstone.plugin import Plugin

from endstone_test import EndstoneTest
from endstone.command import CommandSenderWrapper


def test_instance(plugin: Plugin):
assert plugin is not None
assert isinstance(plugin, EndstoneTest)


def test_schedule_task(plugin: Plugin) -> None:
# case study: https://github.com/EndstoneMC/endstone/issues/31
server = plugin.server
assert server.dispatch_command(server.command_sender, "save hold")

# since /save hold takes time to save, we run /save query in a delayed task to avoid blocking the thread
def save_query():
messages = []
sender = CommandSenderWrapper(server.command_sender,
on_message=lambda msg: messages.extend([msg.translate, *msg.with_]))
ready = server.dispatch_command(sender, "save query")
if not ready:
server.scheduler.run_task(plugin, save_query, delay=5)
return

assert 'commands.save-all.success' in messages
assert server.dispatch_command(server.command_sender, "save resume")

server.scheduler.run_task(plugin, save_query, delay=5)
10 changes: 8 additions & 2 deletions src/endstone_test/tests/on_load/test_scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ def scoreboard(server: Server) -> Scoreboard:
@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(server: Server, scoreboard: Scoreboard):
# Before each test run
server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective")
objective = scoreboard.get_objective("test_objective")
if objective is not None:
server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective")

objective = scoreboard.get_objective("test_objective")
assert objective is None

yield

# After each test run
server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective")
objective = scoreboard.get_objective("test_objective")
if objective is not None:
server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective")
objective = scoreboard.get_objective("test_objective")
assert objective is None

Expand All @@ -35,6 +40,7 @@ def test_add_objective(server: Server, scoreboard: Scoreboard) -> None:
assert objective.criteria.name == "dummy"
assert objective.display_name == objective.name
assert objective.display_slot is None
assert objective.sort_order is None
assert objective.name == "test_objective"
assert objective.scoreboard is scoreboard

Expand Down
7 changes: 5 additions & 2 deletions src/endstone_test/tests/on_load/test_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import time

import pytest
from endstone import Server, __minecraft_version__
from endstone import Server, Translatable, __minecraft_version__
from endstone.plugin import Plugin
from endstone.command import CommandSenderWrapper


@pytest.fixture
Expand All @@ -19,7 +22,7 @@ def test_server_level(server: Server) -> None:


def test_dispatch_command(server: Server) -> None:
server.dispatch_command(server.command_sender, "say Hello, World!")
assert server.dispatch_command(server.command_sender, "say Hello, World!")


def test_max_players(server: Server) -> None:
Expand Down

0 comments on commit ec26121

Please sign in to comment.