-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add tests for listeners and commands #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scheuraa007
wants to merge
2
commits into
main
Choose a base branch
from
testing/add-some-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/test/java/net/onelitefeather/labyrinth/commands/CenterCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package net.onelitefeather.labyrinth.commands; | ||
|
|
||
| import net.kyori.adventure.text.minimessage.MiniMessage; | ||
| import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; | ||
| import net.onelitefeather.labyrinth.service.api.ValidationService; | ||
| import net.onelitefeather.labyrinth.utils.Constants; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.entity.Player; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.jupiter.api.*; | ||
| import org.mockbukkit.mockbukkit.world.WorldMock; | ||
|
|
||
| class CenterCommandTest extends CommandPluginTestBase { | ||
|
|
||
| private CenterCommand command; | ||
| private MockValidationService validationService; | ||
|
|
||
| public static class MockValidationService implements ValidationService | ||
| { | ||
| private boolean isValid; | ||
|
|
||
| @Override | ||
| public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) { | ||
| return isValid; | ||
| } | ||
|
|
||
| public void setValid(boolean valid) { | ||
| isValid = valid; | ||
| } | ||
|
|
||
| public boolean isValid() { | ||
| return isValid; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @BeforeEach | ||
| void setUp() | ||
| { | ||
| super.setUp(); | ||
| validationService = new MockValidationService(); | ||
| command = new CenterCommand(plugin, validationService); | ||
| } | ||
|
|
||
|
|
||
| @DisplayName("Test if the player location is not zero") | ||
| @Test | ||
| void testIsYLocationFromPlayerNotZero() | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The project follows the standard Java code conventions. Therefore, each method declaration should include the opening brace on the same line as the method declaration. Correct style: void myTestMethod() {
} |
||
| var player = server.addPlayer(); | ||
| var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); | ||
| player.setLocation(location); | ||
|
|
||
| command.centerCommand(player, "Test"); | ||
| Assertions.assertNotEquals(0, location.getY()); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWrong() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); | ||
| player.setLocation(location); | ||
|
|
||
| validationService.setValid(false); | ||
| command.centerCommand(player, "Test"); | ||
| var playerMessage = player.nextComponentMessage(); | ||
| Assertions.assertNotNull(playerMessage); | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| Assertions.assertEquals(expectedMessage, playerMessage); | ||
| Assertions.assertFalse(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted("Test"))); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationTrue() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CENTER_COMMAND_MESSAGE_SUCCESS, | ||
| Placeholder.unparsed("zone", zoneName), | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
|
|
||
| player.setLocation(location); | ||
| validationService.setValid(true); | ||
| command.centerCommand(player, zoneName); | ||
| location.setY(0); | ||
| var playerMessage = player.nextComponentMessage(); | ||
|
|
||
| Assertions.assertNotNull(playerMessage); | ||
| Assertions.assertEquals(expectedMessage, playerMessage); | ||
| Assertions.assertTrue(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName))); | ||
| Assertions.assertEquals(location, plugin.getConfig().getLocation(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName))); | ||
| } | ||
|
|
||
| } | ||
38 changes: 38 additions & 0 deletions
38
src/test/java/net/onelitefeather/labyrinth/commands/CommandPluginTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package net.onelitefeather.labyrinth.commands; | ||
|
|
||
| import net.onelitefeather.labyrinth.Labyrinth; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.mockbukkit.mockbukkit.MockBukkit; | ||
| import org.mockbukkit.mockbukkit.ServerMock; | ||
|
|
||
| public abstract class CommandPluginTestBase { | ||
| protected @NotNull ServerMock server; | ||
| protected Labyrinth plugin; | ||
|
|
||
| public static class MockLabyrinthPlugin extends Labyrinth { | ||
| @Override | ||
| public void onEnable() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onDisable() { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setUp() | ||
| { | ||
| server = MockBukkit.mock(); | ||
| plugin = MockBukkit.load(CommandPluginTestBase.MockLabyrinthPlugin.class); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() | ||
| { | ||
| MockBukkit.unmock(); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/test/java/net/onelitefeather/labyrinth/commands/CreateZoneCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package net.onelitefeather.labyrinth.commands; | ||
|
|
||
| import net.kyori.adventure.text.minimessage.MiniMessage; | ||
| import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; | ||
| import net.onelitefeather.labyrinth.utils.Constants; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class CreateZoneCommandTest extends CommandPluginTestBase { | ||
|
|
||
| private CreateZoneCommand command; | ||
|
|
||
| @Override | ||
| @BeforeEach | ||
| void setUp() | ||
| { | ||
| super.setUp(); | ||
| command = new CreateZoneCommand(plugin); | ||
| } | ||
|
|
||
| @Test | ||
| void testZoneNameNotMatchesPattern() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test%"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| command.createZone(player, zoneName); | ||
|
|
||
| assertFalse(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName))); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testZoneCreated() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CREATE_ZONE_MESSAGE_SUCCESS, | ||
| Placeholder.unparsed("zone", zoneName), | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| command.createZone(player, zoneName); | ||
|
|
||
| assertTrue(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName))); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
src/test/java/net/onelitefeather/labyrinth/commands/DeleteZoneCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package net.onelitefeather.labyrinth.commands; | ||
|
|
||
| import net.kyori.adventure.text.minimessage.MiniMessage; | ||
| import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; | ||
| import net.onelitefeather.labyrinth.utils.Constants; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class DeleteZoneCommandTest extends CommandPluginTestBase{ | ||
|
|
||
| private DeleteZoneCommand command; | ||
|
|
||
| @Override | ||
| @BeforeEach | ||
| void setUp() | ||
| { | ||
| super.setUp(); | ||
| command = new DeleteZoneCommand(plugin); | ||
| } | ||
|
|
||
| @Test | ||
| void testZoneDeleted() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.DELETE_ZONE_MESSAGE_SUCCESS, | ||
| Placeholder.unparsed("zone", zoneName), | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); | ||
| plugin.getConfig().createSection(configSectionName); | ||
| command.deleteZone(player, zoneName); | ||
| assertFalse(plugin.getConfig().isSet(configSectionName)); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testZoneNotDeleted() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| command.deleteZone(player, zoneName); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
|
|
||
| } | ||
|
|
||
| } |
95 changes: 95 additions & 0 deletions
95
src/test/java/net/onelitefeather/labyrinth/commands/SetRadiusCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package net.onelitefeather.labyrinth.commands; | ||
|
|
||
| import net.kyori.adventure.text.minimessage.MiniMessage; | ||
| import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; | ||
| import net.onelitefeather.labyrinth.service.api.ValidationService; | ||
| import net.onelitefeather.labyrinth.utils.Constants; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.entity.Player; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockbukkit.mockbukkit.world.WorldMock; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class SetRadiusCommandTest extends CommandPluginTestBase{ | ||
|
|
||
| private SetRadiusCommand command; | ||
| private SetRadiusCommandTest.MockValidationService validationService; | ||
|
|
||
| public static class MockValidationService implements ValidationService | ||
| { | ||
| private boolean isValid; | ||
|
|
||
| @Override | ||
| public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) { | ||
| return isValid; | ||
| } | ||
|
|
||
| public void setValid(boolean valid) { | ||
| isValid = valid; | ||
| } | ||
|
|
||
| public boolean isValid() { | ||
| return isValid; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @BeforeEach | ||
| void setUp() | ||
| { | ||
| super.setUp(); | ||
| validationService = new SetRadiusCommandTest.MockValidationService(); | ||
| command = new SetRadiusCommand(plugin, validationService); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWrong() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| validationService.setValid(false); | ||
| command.setRadius(player, zoneName); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoLocationSet() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| validationService.setValid(true); | ||
| command.setRadius(player, zoneName); | ||
| assertNull(player.nextComponentMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRadiusSet() | ||
| { | ||
| var player = server.addPlayer(); | ||
| var zoneName = "Test"; | ||
| var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.SET_RADIUS_MESSAGE, | ||
| Placeholder.unparsed("zone", zoneName), | ||
| Placeholder.component("prefix", Constants.PREFIX)); | ||
| var world = new WorldMock(Material.GRASS_BLOCK, 64); | ||
| var centerLocation = new Location(world, 120, 0, 120); | ||
| var playerLocation = new Location(world, 240, 64, 240); | ||
|
|
||
| validationService.setValid(true); | ||
| plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); | ||
| player.setLocation(playerLocation); | ||
|
|
||
| command.setRadius(player,zoneName); | ||
|
|
||
| playerLocation.setY(0); | ||
| var expectedDistance = centerLocation.distance(playerLocation); | ||
| assertEquals(expectedDistance, plugin.getConfig().getDouble(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName))); | ||
| assertEquals(expectedMessage, player.nextComponentMessage()); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lines should be swapped to maintain a consistent definition of the test dependencies. The goal is to group all
testImplementationcalls together and do the same fortestRuntimeOnlycalls. This improves the readability and organization of the dependency definitions.