Browse Source

More unit tests

nossr50 1 năm trước cách đây
mục cha
commit
2d8bdbded8

+ 21 - 4
src/test/java/com/gmail/nossr50/MMOTestEnvironmentBasic.java

@@ -6,6 +6,7 @@ import com.gmail.nossr50.config.experience.ExperienceConfig;
 import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.datatypes.player.PlayerProfile;
 import com.gmail.nossr50.datatypes.skills.SubSkillType;
+import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
 import com.gmail.nossr50.listeners.SelfListener;
 import com.gmail.nossr50.util.*;
 import com.gmail.nossr50.util.blockmeta.ChunkManager;
@@ -21,6 +22,8 @@ import org.bukkit.entity.Player;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.PlayerInventory;
 import org.bukkit.plugin.PluginManager;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 import org.mockito.MockedStatic;
 import org.mockito.Mockito;
 
@@ -50,7 +53,7 @@ public abstract class MMOTestEnvironmentBasic {
     protected GeneralConfig generalConfig;
     protected RankConfig rankConfig;
     protected SkillTools skillTools;
-    protected Server server;
+    protected Server mockedServer;
     protected PluginManager pluginManager;
     protected World world;
 
@@ -69,6 +72,16 @@ public abstract class MMOTestEnvironmentBasic {
 
     protected ConsoleCommandSender consoleCommandSender;
 
+    @BeforeEach
+    void setUp() {
+        mockBaseEnvironment();
+    }
+
+    @AfterEach
+    void tearDown() {
+        cleanupBaseEnvironment();
+    }
+
     protected void mockBaseEnvironment() {
         mockedMcMMO = Mockito.mockStatic(mcMMO.class);
         mcMMO.p = mock(mcMMO.class);
@@ -113,12 +126,16 @@ public abstract class MMOTestEnvironmentBasic {
         mockedRankUtils = Mockito.mockStatic(RankUtils.class);
 
         // wire server
-        this.server = mock(Server.class);
-        when(mcMMO.p.getServer()).thenReturn(server);
+        this.mockedServer = mock(Server.class);
+        when(mcMMO.p.getServer()).thenReturn(mockedServer);
 
         // wire plugin manager
         this.pluginManager = mock(PluginManager.class);
-        when(server.getPluginManager()).thenReturn(pluginManager);
+        when(mockedServer.getPluginManager()).thenReturn(pluginManager);
+        Mockito.doAnswer(invocation -> {
+            selfListener.onPlayerLevelUp(invocation.getArgument(0));
+            return null;
+        }).when(pluginManager).callEvent(any(McMMOPlayerLevelUpEvent.class));
 
         // wire world
         this.world = mock(World.class);

+ 85 - 31
src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java

@@ -6,41 +6,101 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
 import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
 import com.gmail.nossr50.mcMMO;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
+import com.gmail.nossr50.util.EventUtils;
+import org.bukkit.Bukkit;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
 import java.util.Set;
 import java.util.function.BiPredicate;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.*;
 
 class LevelUpCommandTest extends MMOTestEnvironmentBasic {
+    private final PrimarySkillType skill = PrimarySkillType.MINING;
+    private final PrimarySkillType otherSkill = PrimarySkillType.WOODCUTTING;
 
-    @BeforeEach
-    void setUp() {
-        mockBaseEnvironment();
-    }
+    @Test
+    void levelUpShouldRunCommandFiveTimes() {
+        // GIVEN level up command for Mining should always execute for Mining level up
+        assert mcMMO.p.getLevelUpCommandManager().isEmpty();
+        final String commandStr = "say hello";
+        final LevelUpCommand levelUpCommand
+                = buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
+        mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
+
+        // WHEN player gains 5 levels in mining via command
+        assertEquals(0, mmoPlayer.getSkillLevel(skill));
+        int levelsGained = 5;
+        EventUtils.tryLevelChangeEvent(
+                player,
+                skill,
+                levelsGained,
+                mmoPlayer.getProfile().getSkillXpLevelRaw(skill),
+                true,
+                XPGainReason.COMMAND);
 
-    @AfterEach
-    void tearDown() {
-        cleanupBaseEnvironment();
+        // THEN the command should be checked for execution
+        verify(levelUpCommandManager).apply(any(), any(), any());
+        verify(levelUpCommand).process(any(), any(), any());
+        // THEN the command should have executed
+        verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), any()), atLeast(5));
     }
 
     @Test
-    void levelInMiningShouldRunCommandFiveTimes() {
+    void levelUpShouldRunCommandFiveTimesWithPlaceholders() {
         // GIVEN level up command for Mining should always execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
-        final String commandStr = "say hello";
+        String playerName = "Momshroom";
+        when (player.getName()).thenReturn(playerName);
+        assertEquals(player.getName(), playerName);
+
+        final String commandStr = "say hello %player%";
+        final String expectedStr = "say hello " + playerName;
         final LevelUpCommand levelUpCommand
-                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
+                = buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
+        int levelsGained = 5;
 
+        // WHEN player gains 5 levels in mining
+        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
+        selfListener.onPlayerLevelUp(event);
+
+        // THEN the command should be checked for execution
+        verify(levelUpCommandManager).apply(any(), any(), any());
+        verify(levelUpCommand).process(any(), any(), any());
+        // THEN the command should have executed
+        verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
+        // verify that Bukkit.dispatchCommand got executed at least 5 times with the correct injectedCommand
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr)), atLeast(5));
+    }
+
+    @Test
+    void levelUpShouldRunCommandFiveTimesWithPlaceholdersForLevel() {
+        // GIVEN level up command for Mining should always execute for Mining level up
+        assert mcMMO.p.getLevelUpCommandManager().isEmpty();
+        String playerName = "Momshroom";
+        when (player.getName()).thenReturn(playerName);
+        assertEquals(player.getName(), playerName);
+
+        final String commandStr = "say hello %player%, you have reached level %level%";
+        final String expectedStr1 = "say hello " + playerName + ", you have reached level 1";
+        final String expectedStr2 = "say hello " + playerName + ", you have reached level 2";
+        final String expectedStr3 = "say hello " + playerName + ", you have reached level 3";
+        final String expectedStr4 = "say hello " + playerName + ", you have reached level 4";
+        final String expectedStr5 = "say hello " + playerName + ", you have reached level 5";
+
+        final LevelUpCommand levelUpCommand
+                = buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
+        mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
         int levelsGained = 5;
+
         // WHEN player gains 5 levels in mining
-        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
+        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
         selfListener.onPlayerLevelUp(event);
 
         // THEN the command should be checked for execution
@@ -48,20 +108,26 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
         verify(levelUpCommand).process(any(), any(), any());
         // THEN the command should have executed
         verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
+        // verify that Bukkit.dispatchCommand got executed at least 5 times with the correct injectedCommand
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr1)));
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr2)));
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr3)));
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr4)));
+        mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr5)));
     }
 
     @Test
-    void levelInMiningShouldRunCommandAtLeastOnce() {
+    void levelUpShouldRunCommandAtLeastOnce() {
         // GIVEN level up command for Mining should always execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
         final String commandStr = "say hello";
         final LevelUpCommand levelUpCommand
-                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
+                = buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
 
         int levelsGained = 1;
         // WHEN player gains 5 levels in mining
-        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
+        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
         selfListener.onPlayerLevelUp(event);
 
         // THEN the command should be checked for execution
@@ -72,18 +138,18 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
     }
 
     @Test
-    void levelInMiningShouldNotRunCommand() {
+    void levelUpShouldNotRunCommand() {
         // GIVEN level up command for Woodcutting should not execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
         final String commandStr = "say hello";
         final LevelUpCommand levelUpCommand
-                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.WOODCUTTING);
+                = buildLevelUpCommand(commandStr, (s, ignored) -> s == otherSkill);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
 
 
         int levelsGained = 5;
         // WHEN player gains 5 levels in mining
-        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
+        McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
         selfListener.onPlayerLevelUp(event);
 
         // THEN the command should be checked for execution
@@ -93,18 +159,6 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
         verify(levelUpCommand, never()).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
     }
 
-    private LevelUpCommand buildLevelUpCommand(String commandStr, Set<Integer> levels, Set<PrimarySkillType> skillFilter) {
-        LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
-        builder.command(commandStr)
-                .withLevels(levels)
-                .withLogInfo(true);
-        if (skillFilter != null) {
-            builder.withSkillFilter(skillFilter);
-        }
-        builder.withLogInfo(true);
-        return Mockito.spy(builder.build());
-    }
-
     private LevelUpCommand buildLevelUpCommand(String commandStr, BiPredicate<PrimarySkillType, Integer> predicate) {
         LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
         builder.command(commandStr)