LevelUpCommandTest.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.gmail.nossr50.commands.levelup;
  2. import com.gmail.nossr50.MMOTestEnvironmentBasic;
  3. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
  6. import com.gmail.nossr50.mcMMO;
  7. import org.junit.jupiter.api.AfterEach;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import org.mockito.Mockito;
  11. import java.util.Set;
  12. import java.util.function.Predicate;
  13. import static org.mockito.ArgumentMatchers.any;
  14. import static org.mockito.Mockito.*;
  15. class LevelUpCommandTest extends MMOTestEnvironmentBasic {
  16. @BeforeEach
  17. void setUp() {
  18. mockBaseEnvironment();
  19. }
  20. @AfterEach
  21. void tearDown() {
  22. cleanupBaseEnvironment();
  23. }
  24. @Test
  25. void levelInMiningShouldRunCommandFiveTimes() {
  26. // GIVEN level up command for Mining should always execute for Mining level up
  27. assert mcMMO.p.getLevelUpCommandManager().isEmpty();
  28. final PrimarySkillType skillType = PrimarySkillType.MINING;
  29. final Predicate<Integer> predicate = (i) -> true;
  30. final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
  31. predicate,
  32. "say hello",
  33. Set.of(skillType),
  34. true));
  35. mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
  36. int levelsGained = 5;
  37. // WHEN player gains 5 levels in mining
  38. McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
  39. selfListener.onPlayerLevelUp(event);
  40. // THEN the command should be checked for execution
  41. verify(levelUpCommandManager).apply(any(), any(), any());
  42. verify(levelUpCommand).process(any(), any(), any());
  43. // THEN the command should have executed
  44. verify(levelUpCommand, times(levelsGained)).executeCommand();
  45. }
  46. @Test
  47. void levelInMiningShouldRunCommandAtLeastOnce() {
  48. // GIVEN level up command for Mining should always execute for Mining level up
  49. assert mcMMO.p.getLevelUpCommandManager().isEmpty();
  50. final PrimarySkillType skillType = PrimarySkillType.MINING;
  51. final Predicate<Integer> predicate = (i) -> true;
  52. final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
  53. predicate,
  54. "say hello",
  55. Set.of(skillType),
  56. true));
  57. mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
  58. int levelsGained = 1;
  59. // WHEN player gains 5 levels in mining
  60. McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
  61. selfListener.onPlayerLevelUp(event);
  62. // THEN the command should be checked for execution
  63. verify(levelUpCommandManager).apply(any(), any(), any());
  64. verify(levelUpCommand).process(any(), any(), any());
  65. // THEN the command should have executed
  66. verify(levelUpCommand).executeCommand();
  67. }
  68. @Test
  69. void levelInMiningShouldNotRunCommand() {
  70. // GIVEN level up command for Woodcutting should not execute for Mining level up
  71. assert mcMMO.p.getLevelUpCommandManager().isEmpty();
  72. final PrimarySkillType skillType = PrimarySkillType.WOODCUTTING;
  73. final Predicate<Integer> predicate = (i) -> true;
  74. final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
  75. predicate,
  76. "say hello",
  77. Set.of(skillType),
  78. true));
  79. mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
  80. int levelsGained = 5;
  81. // WHEN player gains 5 levels in mining
  82. McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
  83. selfListener.onPlayerLevelUp(event);
  84. // THEN the command should be checked for execution
  85. verify(levelUpCommandManager).apply(any(), any(), any());
  86. verify(levelUpCommand).process(any(), any(), any());
  87. // THEN the command should not be run
  88. verify(levelUpCommand, never()).executeCommand();
  89. }
  90. }