Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 committed Oct 10, 2024
1 parent 217d84b commit a69ea18
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 68 deletions.
12 changes: 5 additions & 7 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ void SetActiveStore(TalkID talkId)
ActiveStore = talkId;
}

// FIXME: Put in anonymous namespace
int GetItemCount(TalkID talkId)
{
TownerStore *towner = townerStores[TownerId];
Expand All @@ -249,7 +248,6 @@ int GetItemCount(TalkID talkId)
return playerItems.size();

Check warning on line 248 in Source/stores.cpp

View workflow job for this annotation

GitHub Actions / build

'return': conversion from 'size_t' to 'int', possible loss of data
}

// FIXME: Put in anonymous namespace
bool HasScrollbar()
{
if (!IsAnyOf(ActiveStore, TalkID::BasicBuy, TalkID::Buy, TalkID::Sell, TalkID::Repair, TalkID::Recharge, TalkID::Identify))
Expand Down Expand Up @@ -852,7 +850,7 @@ void FilterSellableItems(TalkID talkId)
return true; // Remove this item

// Common conditions for both Smith and Witch
if (pI->isEmpty() || pI->_itype == ItemType::Gold || pI->_iClass == ICLASS_QUEST || pI->IDidx == IDI_LAZSTAFF)
if (pI->_itype == ItemType::Gold || pI->_iClass == ICLASS_QUEST || pI->IDidx == IDI_LAZSTAFF)
return true; // Remove this item

switch (TownerId) {
Expand Down Expand Up @@ -883,7 +881,7 @@ void FilterRepairableItems()
playerItems.erase(std::remove_if(playerItems.begin(), playerItems.end(),
[](const IndexedItem &indexedItem) {
const Item &itemPtr = *indexedItem.itemPtr;
return itemPtr.isEmpty() || itemPtr._iDurability == itemPtr._iMaxDur || itemPtr._iMaxDur == DUR_INDESTRUCTIBLE;
return itemPtr._iDurability == itemPtr._iMaxDur || itemPtr._iMaxDur == DUR_INDESTRUCTIBLE;
}),
playerItems.end());
}
Expand All @@ -894,7 +892,7 @@ void FilterRechargeableItems()
playerItems.erase(std::remove_if(playerItems.begin(), playerItems.end(),
[](const IndexedItem &indexedItem) {
const Item &itemPtr = *indexedItem.itemPtr;
return itemPtr.isEmpty() || itemPtr._iCharges == itemPtr._iMaxCharges || (itemPtr._itype != ItemType::Staff && itemPtr._iMiscId != IMISC_UNIQUE && itemPtr._iMiscId != IMISC_STAFF);
return itemPtr._iCharges == itemPtr._iMaxCharges || (itemPtr._itype != ItemType::Staff && itemPtr._iMiscId != IMISC_UNIQUE && itemPtr._iMiscId != IMISC_STAFF);
}),
playerItems.end());
}
Expand All @@ -905,7 +903,7 @@ void FilterIdentifiableItems()
playerItems.erase(std::remove_if(playerItems.begin(), playerItems.end(),
[](const IndexedItem &indexedItem) {
const Item &itemPtr = *indexedItem.itemPtr;
return itemPtr.isEmpty() || itemPtr._iMagical == ITEM_QUALITY_NORMAL || itemPtr._iIdentified;
return itemPtr._iMagical == ITEM_QUALITY_NORMAL || itemPtr._iIdentified;
}),
playerItems.end());
}
Expand Down Expand Up @@ -1436,7 +1434,7 @@ void ConfirmEnter(Item &item)
RechargeItem();
break;
case TalkID::Identify:
IdentifyItem(item);
IdentifyItem();
StartStore(TalkID::IdentifyShow);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/stores.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ extern TownerStore Boy;
extern TownerStore Storyteller;
extern TownerStore Barmaid;

void ExitStore();
/* Clears premium items sold by Griswold and Wirt. */
void InitStores();
/* Spawns items sold by vendors, including premium items sold by Griswold and Wirt. */
void SetupTownStores();
void FreeStoreMem();
void ExitStore();
void PrintSString(const Surface &out, int margin, int line, std::string_view text, UiFlags flags, int price = 0, int cursId = -1, bool cursIndent = false);
void DrawSLine(const Surface &out, int sy);
void DrawSTextHelp();
Expand Down
146 changes: 86 additions & 60 deletions test/stores_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,95 @@ using namespace devilution;

namespace {

TEST(Stores, AddPlayerItemToRepairList_magic)
// Helper function to reset the playerItems vector before each test
void ResetPlayerItems()
{
Item *item;

item = &playerItem[0];

item->_iMaxDur = 60;
item->_iDurability = item->_iMaxDur;
item->_iMagical = ITEM_QUALITY_MAGIC;
item->_iIdentified = true;
item->_ivalue = 2000;
item->_iIvalue = 19000;

for (int i = 1; i < item->_iMaxDur; i++) {
item->_ivalue = 2000;
item->_iIvalue = 19000;
item->_iDurability = i;
currentItemIndex = 0;
AddPlayerItemToRepairList(item, 0);
EXPECT_EQ(1, currentItemIndex);
EXPECT_EQ(95 * (item->_iMaxDur - i) / 2, item->_ivalue);
}

item->_iDurability = 59;
currentItemIndex = 0;
item->_ivalue = 500;
item->_iIvalue = 30; // To cheap to repair
AddPlayerItemToRepairList(item, 0);
EXPECT_EQ(0, currentItemIndex);
EXPECT_EQ(30, item->_iIvalue);
EXPECT_EQ(500, item->_ivalue);
playerItems.clear();
}

TEST(Stores, AddPlayerItemToRepairList_normal)
// This is a direct copy of FilterRepairableItems logic for testing purposes
void Test_FilterRepairableItems()
{
Item *item;

item = &playerItem[0];

item->_iMaxDur = 20;
item->_iDurability = item->_iMaxDur;
item->_iMagical = ITEM_QUALITY_NORMAL;
item->_iIdentified = true;
item->_ivalue = 2000;
item->_iIvalue = item->_ivalue;

for (int i = 1; i < item->_iMaxDur; i++) {
item->_ivalue = 2000;
item->_iIvalue = item->_ivalue;
item->_iDurability = i;
currentItemIndex = 0;
AddPlayerItemToRepairList(item, 0);
EXPECT_EQ(1, currentItemIndex);
EXPECT_EQ(50 * (item->_iMaxDur - i), item->_ivalue);
}

item->_iDurability = 19;
currentItemIndex = 0;
item->_ivalue = 10; // less than 1 per dur
item->_iIvalue = item->_ivalue;
AddPlayerItemToRepairList(item, 0);
EXPECT_EQ(1, currentItemIndex);
EXPECT_EQ(1, item->_ivalue);
EXPECT_EQ(1, item->_iIvalue);
playerItems.erase(std::remove_if(playerItems.begin(), playerItems.end(),
[](const IndexedItem &indexedItem) {
const Item &itemPtr = *indexedItem.itemPtr;
return itemPtr._iDurability == itemPtr._iMaxDur || itemPtr._iMaxDur == DUR_INDESTRUCTIBLE;
}),
playerItems.end());
}

TEST(Stores, FilterRepairableItems_magic)
{
// Reset playerItems before starting the test
ResetPlayerItems();

// Create a magic item with durability and add it to the player's inventory
Item magicItem;
magicItem._iMaxDur = 60;
magicItem._iDurability = magicItem._iMaxDur - 1;
magicItem._iMagical = ITEM_QUALITY_MAGIC;
magicItem._iIdentified = true;
magicItem._ivalue = 2000;
magicItem._iIvalue = 19000;

// Add the item to the player's inventory
playerItems.push_back({ &magicItem, ItemLocation::Inventory, 0 });

// Call the filtering function to remove non-repairable items
Test_FilterRepairableItems();

// Check that the playerItems vector contains the magic item and its values are correct
ASSERT_EQ(playerItems.size(), 1);
EXPECT_EQ(playerItems[0].itemPtr->_ivalue, 2000); // Item's value should not change
EXPECT_EQ(playerItems[0].itemPtr->_iDurability, 59); // Durability should match
}

TEST(Stores, FilterRepairableItems_normal)
{
// Reset playerItems before starting the test
ResetPlayerItems();

// Create a normal item with durability and add it to the player's inventory
Item normalItem;
normalItem._iMaxDur = 20;
normalItem._iDurability = normalItem._iMaxDur - 1;
normalItem._iMagical = ITEM_QUALITY_NORMAL;
normalItem._iIdentified = true;
normalItem._ivalue = 2000;

// Add the item to the player's inventory
playerItems.push_back({ &normalItem, ItemLocation::Inventory, 0 });

// Call the filtering function to remove non-repairable items
Test_FilterRepairableItems();

// Check that the playerItems vector contains the normal item and its values are correct
ASSERT_EQ(playerItems.size(), 1);
EXPECT_EQ(playerItems[0].itemPtr->_ivalue, 2000); // Item's value should not change
EXPECT_EQ(playerItems[0].itemPtr->_iDurability, 19); // Durability should match
}

TEST(Stores, FilterRepairableItems_no_repair)
{
// Reset playerItems before starting the test
ResetPlayerItems();

// Create an item that cannot be repaired (already at max durability)
Item indestructibleItem;
indestructibleItem._iMaxDur = DUR_INDESTRUCTIBLE; // Indestructible item
indestructibleItem._iDurability = 100;
indestructibleItem._iMagical = ITEM_QUALITY_MAGIC;
indestructibleItem._iIdentified = true;
indestructibleItem._ivalue = 5000;

// Add the item to the player's inventory
playerItems.push_back({ &indestructibleItem, ItemLocation::Inventory, 0 });

// Call the filtering function to remove non-repairable items
Test_FilterRepairableItems();

// Check that the playerItems vector is empty since the item is indestructible
ASSERT_EQ(playerItems.size(), 0);
}

} // namespace

0 comments on commit a69ea18

Please sign in to comment.