From 2c18f05b05f38f64909aeacb9499d18cd5ef0b00 Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Sun, 5 Feb 2023 03:49:05 +0100 Subject: [PATCH] added sellItem with test --- src/c/player.c | 7 +++++++ src/c/player.h | 1 + test/c/test_player.c | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/c/player.c b/src/c/player.c index fea8f91..85602bf 100644 --- a/src/c/player.c +++ b/src/c/player.c @@ -89,6 +89,13 @@ Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer) } } +Player sellItem(int itemIndex, Player actualPlayer) +{ + actualPlayer = addMoneyToPlayer(actualPlayer, actualPlayer.itemInventory[itemIndex].price / 2); + actualPlayer = removeItemFromInventory(itemIndex, actualPlayer); + printf("Item has been sold! Your balance is now: %d$\n", actualPlayer.wallet); + return actualPlayer; +} // show void showInventory(Player actualPlayer) diff --git a/src/c/player.h b/src/c/player.h index 411c2cf..4518f70 100644 --- a/src/c/player.h +++ b/src/c/player.h @@ -22,6 +22,7 @@ Player removeMoneyFromPlayer(Player actualPlayer, int money); //add and remove items with currency Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer); +Player sellItem(int itemIndex, Player actualPlayer); //show void showInventory(Player actualPlayer); diff --git a/test/c/test_player.c b/test/c/test_player.c index 2bd5683..5e86752 100644 --- a/test/c/test_player.c +++ b/test/c/test_player.c @@ -129,5 +129,27 @@ void test_buyItem(void) } +void test_sellItem(void) +{ + /* arrange */ + // Hier die Werte eingeben/deklarieren + int buyItemID = 1; // prepare sell + actualPlayer = addItemToInventory(availableItems, buyItemID, actualPlayer); // prepare sell + + int itemToRemove = 0; // index to remove (we already bought it in function above) + + /* act */ + // Die Funktion wird ausgeführt + Item itemBeforeRemove = actualPlayer.itemInventory[0]; + actualPlayer = sellItem(itemToRemove, actualPlayer); // then remove + Item itemAfterRemove = actualPlayer.itemInventory[0]; + + // OUTPUT + //printf("sellItem | before: %d -> after: %d", itemBeforeRemove.id, itemAfterRemove.id); + + /* assert */ + // Vergleichen mit Inhalt von game.Map File + TEST_ASSERT_NOT_EQUAL_UINT8(itemBeforeRemove.id, itemAfterRemove.id); +} #endif // TEST