From 918b82fd8cd4977c7ebd8a17256702014bd43a60 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 29 Oct 2024 18:18:35 +0000 Subject: [PATCH] test: add more tests for the inventory api --- .../tests/on_load/test_server.py | 4 ++- .../tests/on_player_join/test_inventory.py | 36 +++++++++++++++++++ .../tests/on_player_join/test_player.py | 20 ----------- .../tests/on_player_join/test_scoreboard.py | 14 +++++++- 4 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 src/endstone_test/tests/on_player_join/test_inventory.py diff --git a/src/endstone_test/tests/on_load/test_server.py b/src/endstone_test/tests/on_load/test_server.py index 850075c..d1218ac 100644 --- a/src/endstone_test/tests/on_load/test_server.py +++ b/src/endstone_test/tests/on_load/test_server.py @@ -38,7 +38,9 @@ def test_max_players(server: Server) -> None: def test_online_mode(plugin: Plugin, server: Server) -> None: - properties_file = Path(plugin.data_folder, "..", "..", "server.properties").resolve() + properties_file = Path( + plugin.data_folder, "..", "..", "server.properties" + ).resolve() with properties_file.open(mode="r") as file: for line in file: if line.startswith("online-mode="): diff --git a/src/endstone_test/tests/on_player_join/test_inventory.py b/src/endstone_test/tests/on_player_join/test_inventory.py new file mode 100644 index 0000000..457b4eb --- /dev/null +++ b/src/endstone_test/tests/on_player_join/test_inventory.py @@ -0,0 +1,36 @@ +import pytest +from endstone import Player, Server +from endstone.inventory import ItemStack + + +@pytest.fixture +def server(player: Player) -> Server: + return player.server + + +def test_inventory_sizes(player: Player): + assert player.inventory.size == 36 + assert player.inventory.max_stack_size == 254 + + +def test_set_invalid_item(player: Player): + player.inventory.set_item(35, ItemStack("item_that_does_not_exist", 1)) + assert player.inventory.get_item(35) is None + + +def test_set_empty_item(player: Player): + player.inventory.set_item(35, None) + assert player.inventory.get_item(35) is None + + +def test_set_item(player: Player): + player.inventory.set_item(35, ItemStack("minecraft:clock", 7)) + + item = player.inventory.get_item(35) + assert item.type == "minecraft:clock" + assert item.amount == 7 + + player.inventory.set_item(35, item) + item = player.inventory.get_item(35) + assert item.type == "minecraft:clock" + assert item.amount == 7 diff --git a/src/endstone_test/tests/on_player_join/test_player.py b/src/endstone_test/tests/on_player_join/test_player.py index 5d833b0..a600332 100644 --- a/src/endstone_test/tests/on_player_join/test_player.py +++ b/src/endstone_test/tests/on_player_join/test_player.py @@ -16,14 +16,6 @@ def test_player_in_online_players(player: Player, server: Server): 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 - - player.inventory.set_item(35, ItemStack("minecraft:clock", 1)) - assert player.inventory.get_item(35).type == "minecraft:clock" - - 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) @@ -88,18 +80,6 @@ def test_player_game_mode(player: Player): assert player.game_mode == current_game_mode -def test_player_scoreboard(player: Player, server: Server): - assert player.scoreboard is server.scoreboard - - new_scoreboard = server.create_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 - - def test_player_get_tags(player: Player, server: Server): server.dispatch_command(server.command_sender, f'tag "{player.name}" add test_tag') assert "test_tag" in player.scoreboard_tags diff --git a/src/endstone_test/tests/on_player_join/test_scoreboard.py b/src/endstone_test/tests/on_player_join/test_scoreboard.py index 30b24e4..143e3e4 100644 --- a/src/endstone_test/tests/on_player_join/test_scoreboard.py +++ b/src/endstone_test/tests/on_player_join/test_scoreboard.py @@ -13,8 +13,20 @@ def scoreboard(server: Server) -> Scoreboard: return server.scoreboard +def test_set_scoreboard(player: Player, server: Server): + assert player.scoreboard is server.scoreboard + + new_scoreboard = server.create_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 + + def test_scoreboard_value( - player: Player, server: Server, scoreboard: Scoreboard + player: Player, server: Server, scoreboard: Scoreboard ) -> None: server.dispatch_command( server.command_sender, "scoreboard objectives add test_objective dummy"