| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package cz.davza.studentadventure.commands;
- import cz.davza.studentadventure.Items.Book;
- import cz.davza.studentadventure.Items.EnergyDrink;
- import cz.davza.studentadventure.TestHelpers;
- import cz.davza.studentadventure.enums.CommandState;
- import cz.davza.studentadventure.objects.CommandResult;
- import cz.davza.studentadventure.objects.GameData;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import static cz.davza.studentadventure.TestHelpers.execute;
- import static org.junit.jupiter.api.Assertions.*;
- class DropCommandTest {
- private GameData context;
- @BeforeEach
- void setUp() {
- context = TestHelpers.buildGameData();
- context.getPlayer().getInventory().Add(new Book(), 3);
- context.getPlayer().getInventory().Add(new EnergyDrink(), 2);
- }
- @Test
- void dropRemovesItemFromInventory() {
- execute("drop book 1", context);
- assertEquals(2, context.getPlayer().getInventory().GetByItemCode("book").getAmount());
- }
- @Test
- void dropAddsItemToRoom() {
- int before = context.getMap().getCurrentRoom().getItems().GetRemainingCapacity();
- execute("drop book 1", context);
- assertTrue(context.getMap().getCurrentRoom().getItems().GetRemainingCapacity() < before);
- }
- @Test
- void dropEntireStackClearsInventoryEntry() {
- execute("drop book 3", context);
- assertNull(context.getPlayer().getInventory().GetByItemCode("book"));
- }
- @Test
- void dropRestoresInventoryCapacity() {
- int before = context.getPlayer().getInventory().GetRemainingCapacity();
- execute("drop book 1", context);
- assertEquals(before + new Book().getBulk(), context.getPlayer().getInventory().GetRemainingCapacity());
- }
- @Test
- void dropReturnsSuccessState() {
- CommandResult result = execute("drop book 1", context);
- assertEquals(CommandState.SUCCESS, result.getResultState());
- }
- @Test
- void dropItemNotInInventoryReturnsItemNotFound() {
- CommandResult result = execute("drop mouldy_cheese 1", context);
- assertEquals(CommandState.ITEM_NOT_FOUND, result.getResultState());
- }
- @Test
- void dropWithNoParamsReturnsInvalidParams() {
- CommandResult result = execute("drop", context);
- assertEquals(CommandState.INVALID_PARAMS, result.getResultState());
- }
- @Test
- void dropMoreThanStackDropsWholeStack() {
- execute("drop book 100", context);
- assertNull(context.getPlayer().getInventory().GetByItemCode("book"));
- }
- @Test
- void dropWhenRoomFullDropsPartially() {
- // Bedroom capacity is 20, already has Bed(1) and Money(1) = 2 used
- // Fill the room to near capacity then try to drop more
- context.getMap().getCurrentRoom().getItems().Add(new Book(), 5); // 5*3=15 + 2 existing = 17 used, 3 remaining
- // room has 3 bulk left, book bulk=3 so exactly 1 more book fits
- // drop 2 books — only 1 should go through
- CommandResult result = execute("drop book 2", context);
- assertEquals(CommandState.SUCCESS, result.getResultState());
- assertTrue(result.getResultMessage().contains("cluttered"));
- }
- @Test
- void dropWithNonNumberAmountDefaultsToOne() {
- // "drop book abc" — second param parse fails → INVALID_PARAMS
- CommandResult result = execute("drop book abc", context);
- assertEquals(CommandState.INVALID_PARAMS, result.getResultState());
- }
- }
|