DropCommandTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package cz.davza.studentadventure.commands;
  2. import cz.davza.studentadventure.Items.Book;
  3. import cz.davza.studentadventure.Items.EnergyDrink;
  4. import cz.davza.studentadventure.TestHelpers;
  5. import cz.davza.studentadventure.enums.CommandState;
  6. import cz.davza.studentadventure.objects.CommandResult;
  7. import cz.davza.studentadventure.objects.GameData;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import static cz.davza.studentadventure.TestHelpers.execute;
  11. import static org.junit.jupiter.api.Assertions.*;
  12. class DropCommandTest {
  13. private GameData context;
  14. @BeforeEach
  15. void setUp() {
  16. context = TestHelpers.buildGameData();
  17. context.getPlayer().getInventory().Add(new Book(), 3);
  18. context.getPlayer().getInventory().Add(new EnergyDrink(), 2);
  19. }
  20. @Test
  21. void dropRemovesItemFromInventory() {
  22. execute("drop book 1", context);
  23. assertEquals(2, context.getPlayer().getInventory().GetByItemCode("book").getAmount());
  24. }
  25. @Test
  26. void dropAddsItemToRoom() {
  27. int before = context.getMap().getCurrentRoom().getItems().GetRemainingCapacity();
  28. execute("drop book 1", context);
  29. assertTrue(context.getMap().getCurrentRoom().getItems().GetRemainingCapacity() < before);
  30. }
  31. @Test
  32. void dropEntireStackClearsInventoryEntry() {
  33. execute("drop book 3", context);
  34. assertNull(context.getPlayer().getInventory().GetByItemCode("book"));
  35. }
  36. @Test
  37. void dropRestoresInventoryCapacity() {
  38. int before = context.getPlayer().getInventory().GetRemainingCapacity();
  39. execute("drop book 1", context);
  40. assertEquals(before + new Book().getBulk(), context.getPlayer().getInventory().GetRemainingCapacity());
  41. }
  42. @Test
  43. void dropReturnsSuccessState() {
  44. CommandResult result = execute("drop book 1", context);
  45. assertEquals(CommandState.SUCCESS, result.getResultState());
  46. }
  47. @Test
  48. void dropItemNotInInventoryReturnsItemNotFound() {
  49. CommandResult result = execute("drop mouldy_cheese 1", context);
  50. assertEquals(CommandState.ITEM_NOT_FOUND, result.getResultState());
  51. }
  52. @Test
  53. void dropWithNoParamsReturnsInvalidParams() {
  54. CommandResult result = execute("drop", context);
  55. assertEquals(CommandState.INVALID_PARAMS, result.getResultState());
  56. }
  57. @Test
  58. void dropMoreThanStackDropsWholeStack() {
  59. execute("drop book 100", context);
  60. assertNull(context.getPlayer().getInventory().GetByItemCode("book"));
  61. }
  62. @Test
  63. void dropWhenRoomFullDropsPartially() {
  64. // Bedroom capacity is 20, already has Bed(1) and Money(1) = 2 used
  65. // Fill the room to near capacity then try to drop more
  66. context.getMap().getCurrentRoom().getItems().Add(new Book(), 5); // 5*3=15 + 2 existing = 17 used, 3 remaining
  67. // room has 3 bulk left, book bulk=3 so exactly 1 more book fits
  68. // drop 2 books — only 1 should go through
  69. CommandResult result = execute("drop book 2", context);
  70. assertEquals(CommandState.SUCCESS, result.getResultState());
  71. assertTrue(result.getResultMessage().contains("cluttered"));
  72. }
  73. @Test
  74. void dropWithNonNumberAmountDefaultsToOne() {
  75. // "drop book abc" — second param parse fails → INVALID_PARAMS
  76. CommandResult result = execute("drop book abc", context);
  77. assertEquals(CommandState.INVALID_PARAMS, result.getResultState());
  78. }
  79. }