mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-24 14:16:45 +01:00
More unit tests
This commit is contained in:
parent
c1a4cc2c97
commit
2d8bdbded8
@ -6,6 +6,7 @@ import com.gmail.nossr50.config.experience.ExperienceConfig;
|
|||||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||||
import com.gmail.nossr50.datatypes.skills.SubSkillType;
|
import com.gmail.nossr50.datatypes.skills.SubSkillType;
|
||||||
|
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
|
||||||
import com.gmail.nossr50.listeners.SelfListener;
|
import com.gmail.nossr50.listeners.SelfListener;
|
||||||
import com.gmail.nossr50.util.*;
|
import com.gmail.nossr50.util.*;
|
||||||
import com.gmail.nossr50.util.blockmeta.ChunkManager;
|
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.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
@ -50,7 +53,7 @@ public abstract class MMOTestEnvironmentBasic {
|
|||||||
protected GeneralConfig generalConfig;
|
protected GeneralConfig generalConfig;
|
||||||
protected RankConfig rankConfig;
|
protected RankConfig rankConfig;
|
||||||
protected SkillTools skillTools;
|
protected SkillTools skillTools;
|
||||||
protected Server server;
|
protected Server mockedServer;
|
||||||
protected PluginManager pluginManager;
|
protected PluginManager pluginManager;
|
||||||
protected World world;
|
protected World world;
|
||||||
|
|
||||||
@ -69,6 +72,16 @@ public abstract class MMOTestEnvironmentBasic {
|
|||||||
|
|
||||||
protected ConsoleCommandSender consoleCommandSender;
|
protected ConsoleCommandSender consoleCommandSender;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
mockBaseEnvironment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
cleanupBaseEnvironment();
|
||||||
|
}
|
||||||
|
|
||||||
protected void mockBaseEnvironment() {
|
protected void mockBaseEnvironment() {
|
||||||
mockedMcMMO = Mockito.mockStatic(mcMMO.class);
|
mockedMcMMO = Mockito.mockStatic(mcMMO.class);
|
||||||
mcMMO.p = mock(mcMMO.class);
|
mcMMO.p = mock(mcMMO.class);
|
||||||
@ -113,12 +126,16 @@ public abstract class MMOTestEnvironmentBasic {
|
|||||||
mockedRankUtils = Mockito.mockStatic(RankUtils.class);
|
mockedRankUtils = Mockito.mockStatic(RankUtils.class);
|
||||||
|
|
||||||
// wire server
|
// wire server
|
||||||
this.server = mock(Server.class);
|
this.mockedServer = mock(Server.class);
|
||||||
when(mcMMO.p.getServer()).thenReturn(server);
|
when(mcMMO.p.getServer()).thenReturn(mockedServer);
|
||||||
|
|
||||||
// wire plugin manager
|
// wire plugin manager
|
||||||
this.pluginManager = mock(PluginManager.class);
|
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
|
// wire world
|
||||||
this.world = mock(World.class);
|
this.world = mock(World.class);
|
||||||
|
@ -6,41 +6,68 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
|||||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||||
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
|
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
|
||||||
import com.gmail.nossr50.mcMMO;
|
import com.gmail.nossr50.mcMMO;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import com.gmail.nossr50.util.EventUtils;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.bukkit.Bukkit;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.BiPredicate;
|
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.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
class LevelUpCommandTest extends MMOTestEnvironmentBasic {
|
class LevelUpCommandTest extends MMOTestEnvironmentBasic {
|
||||||
|
private final PrimarySkillType skill = PrimarySkillType.MINING;
|
||||||
@BeforeEach
|
private final PrimarySkillType otherSkill = PrimarySkillType.WOODCUTTING;
|
||||||
void setUp() {
|
|
||||||
mockBaseEnvironment();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void tearDown() {
|
|
||||||
cleanupBaseEnvironment();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void levelInMiningShouldRunCommandFiveTimes() {
|
void levelUpShouldRunCommandFiveTimes() {
|
||||||
// GIVEN level up command for Mining should always execute for Mining level up
|
// GIVEN level up command for Mining should always execute for Mining level up
|
||||||
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
||||||
final String commandStr = "say hello";
|
final String commandStr = "say hello";
|
||||||
final LevelUpCommand levelUpCommand
|
final LevelUpCommand levelUpCommand
|
||||||
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
|
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
|
||||||
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
||||||
|
|
||||||
|
// WHEN player gains 5 levels in mining via command
|
||||||
|
assertEquals(0, mmoPlayer.getSkillLevel(skill));
|
||||||
int levelsGained = 5;
|
int levelsGained = 5;
|
||||||
|
EventUtils.tryLevelChangeEvent(
|
||||||
|
player,
|
||||||
|
skill,
|
||||||
|
levelsGained,
|
||||||
|
mmoPlayer.getProfile().getSkillXpLevelRaw(skill),
|
||||||
|
true,
|
||||||
|
XPGainReason.COMMAND);
|
||||||
|
|
||||||
|
// 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 levelUpShouldRunCommandFiveTimesWithPlaceholders() {
|
||||||
|
// 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%";
|
||||||
|
final String expectedStr = "say hello " + playerName;
|
||||||
|
final LevelUpCommand levelUpCommand
|
||||||
|
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
|
||||||
|
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
||||||
|
int levelsGained = 5;
|
||||||
|
|
||||||
// WHEN player gains 5 levels in mining
|
// 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);
|
selfListener.onPlayerLevelUp(event);
|
||||||
|
|
||||||
// THEN the command should be checked for execution
|
// THEN the command should be checked for execution
|
||||||
@ -48,20 +75,59 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
|
|||||||
verify(levelUpCommand).process(any(), any(), any());
|
verify(levelUpCommand).process(any(), any(), any());
|
||||||
// THEN the command should have executed
|
// THEN the command should have executed
|
||||||
verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
|
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
|
@Test
|
||||||
void levelInMiningShouldRunCommandAtLeastOnce() {
|
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, 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(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 levelUpShouldRunCommandAtLeastOnce() {
|
||||||
// GIVEN level up command for Mining should always execute for Mining level up
|
// GIVEN level up command for Mining should always execute for Mining level up
|
||||||
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
||||||
final String commandStr = "say hello";
|
final String commandStr = "say hello";
|
||||||
final LevelUpCommand levelUpCommand
|
final LevelUpCommand levelUpCommand
|
||||||
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
|
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
|
||||||
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
||||||
|
|
||||||
int levelsGained = 1;
|
int levelsGained = 1;
|
||||||
// WHEN player gains 5 levels in mining
|
// 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);
|
selfListener.onPlayerLevelUp(event);
|
||||||
|
|
||||||
// THEN the command should be checked for execution
|
// THEN the command should be checked for execution
|
||||||
@ -72,18 +138,18 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void levelInMiningShouldNotRunCommand() {
|
void levelUpShouldNotRunCommand() {
|
||||||
// GIVEN level up command for Woodcutting should not execute for Mining level up
|
// GIVEN level up command for Woodcutting should not execute for Mining level up
|
||||||
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
|
||||||
final String commandStr = "say hello";
|
final String commandStr = "say hello";
|
||||||
final LevelUpCommand levelUpCommand
|
final LevelUpCommand levelUpCommand
|
||||||
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.WOODCUTTING);
|
= buildLevelUpCommand(commandStr, (s, ignored) -> s == otherSkill);
|
||||||
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
||||||
|
|
||||||
|
|
||||||
int levelsGained = 5;
|
int levelsGained = 5;
|
||||||
// WHEN player gains 5 levels in mining
|
// 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);
|
selfListener.onPlayerLevelUp(event);
|
||||||
|
|
||||||
// THEN the command should be checked for execution
|
// 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());
|
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) {
|
private LevelUpCommand buildLevelUpCommand(String commandStr, BiPredicate<PrimarySkillType, Integer> predicate) {
|
||||||
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
|
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
|
||||||
builder.command(commandStr)
|
builder.command(commandStr)
|
||||||
|
Loading…
Reference in New Issue
Block a user