inventory tests

This commit is contained in:
Connor
2026-02-16 11:27:01 +09:00
parent 01eaebeb71
commit 7ae69ea1ff
3 changed files with 140 additions and 15 deletions

View File

@@ -132,3 +132,115 @@ TEST_SUITE("Resource - Health & Renewing") {
CHECK(inv.GetItemsAmount(woodID) == 2);
}
}
TEST_SUITE("Inventory Entity") {
TEST_CASE("resource harvests into local inventory when present") {
WorldConfig config{};
uint16_t stoneID = config.RegisterItem("Stone");
WorldInstance world{ config };
auto entity = world.GetEcsWorld().entity();
Resource_Ore_Helper(entity, stoneID, 1);
Inventory_Helper(entity, config, 10);
world.ProcessFrame();
auto localInv = entity.get<Inventory>();
CHECK(localInv.GetItemsAmount(stoneID) == 1);
auto& worldInv = world.GetEcsWorld().ensure<WorldInventory>();
CHECK(worldInv.GetItemsAmount(stoneID) == 0);
}
TEST_CASE("overflow goes to world inventory when local is full") {
WorldConfig config{};
uint16_t stoneID = config.RegisterItem("Stone");
WorldInstance world{ config };
auto entity = world.GetEcsWorld().entity();
Resource_Ore_Helper(entity, stoneID, 1);
Inventory_Helper(entity, config, 2);
// fill local inventory (max 2)
world.ProcessFrame();
world.ProcessFrame();
auto localInv = entity.get<Inventory>();
CHECK(localInv.GetItemsAmount(stoneID) == 2);
auto& worldInv = world.GetEcsWorld().ensure<WorldInventory>();
CHECK(worldInv.GetItemsAmount(stoneID) == 0);
// next item should overflow to world inventory
world.ProcessFrame();
localInv = entity.get<Inventory>();
CHECK(localInv.GetItemsAmount(stoneID) == 2);
CHECK(worldInv.GetItemsAmount(stoneID) == 1);
}
TEST_CASE("inventory tracks multiple item types independently") {
WorldConfig config{};
uint16_t stoneID = config.RegisterItem("Stone");
uint16_t ironID = config.RegisterItem("Iron");
WorldInstance world{ config };
auto stoneEntity = world.GetEcsWorld().entity();
Resource_Ore_Helper(stoneEntity, stoneID, 1);
Inventory_Helper(stoneEntity, config, 5);
auto ironEntity = world.GetEcsWorld().entity();
Resource_Ore_Helper(ironEntity, ironID, 1);
Inventory_Helper(ironEntity, config, 5);
world.ProcessFrame();
auto stoneInv = stoneEntity.get<Inventory>();
CHECK(stoneInv.GetItemsAmount(stoneID) == 1);
CHECK(stoneInv.GetItemsAmount(ironID) == 0);
auto ironInv = ironEntity.get<Inventory>();
CHECK(ironInv.GetItemsAmount(ironID) == 1);
CHECK(ironInv.GetItemsAmount(stoneID) == 0);
}
TEST_CASE("two producers share a separate inventory entity") {
WorldConfig config{};
uint16_t stoneID = config.RegisterItem("Stone");
uint16_t ironID = config.RegisterItem("Iron");
WorldInstance world{ config };
// inventory entity (chest/barrel)
auto chest = world.GetEcsWorld().entity();
Inventory_Helper(chest, config, 10);
// copy the shared inventory to both producers
auto chestInv = chest.get<Inventory>();
auto stoneProducer = world.GetEcsWorld().entity();
Resource_Ore_Helper(stoneProducer, stoneID, 1);
stoneProducer.set<Inventory>(chestInv);
auto ironProducer = world.GetEcsWorld().entity();
Resource_Ore_Helper(ironProducer, ironID, 1);
ironProducer.set<Inventory>(chestInv);
world.ProcessFrame();
world.ProcessFrame();
world.ProcessFrame();
// all items should be in the shared inventory
auto resultInv = chest.get<Inventory>();
CHECK(resultInv.GetItemsAmount(stoneID) == 3);
CHECK(resultInv.GetItemsAmount(ironID) == 3);
// world inventory should be empty
auto& worldInv = world.GetEcsWorld().ensure<WorldInventory>();
CHECK(worldInv.GetItemsAmount(stoneID) == 0);
CHECK(worldInv.GetItemsAmount(ironID) == 0);
}
}