Inventory refuses non-positive counts; unknown saved items kept; factories follow reloads

This commit is contained in:
cthangover committed 2026-10-21 12:31:55 +02:00
1 parent d29df6dab6
commit 6bb47971a3
11 files changed
+334 -215

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
namespace Cthangover.Mods.Items.Tests
{
using System.Linq;
using Cthangover.Core.Items;
using Cthangover.Core.Mods.Resolvers;
using Cthangover.Core.Mods.Testing;
/// <summary>
/// Stack arithmetic of <see cref="Inventory"/>: a count that is not
/// positive is no operation at all — a negative removal must not grow a
/// stack and an addition must not create an empty or negative one.
/// </summary>
public class InventoryTests
{
[ModTest("inventory/non_positive_counts_change_nothing")]
static void NonPositiveCountsChangeNothing(ModTestContext ctx)
{
var id = Entities.Collect<ItemInfo>().Keys.First();
var inventory = new Inventory();
inventory.Add(id, 2);
var before = inventory.CheckCount(id);
ModAssert.False(inventory.Remove(id, -5), "negative removal is refused");
ModAssert.False(inventory.Remove(id, 0), "zero removal is refused");
inventory.Add(id, -3);
inventory.Add(id, 0);
ModAssert.Equal(before, inventory.CheckCount(id), "stack count after non-positive operations");
var other = Entities.Collect<ItemInfo>().Keys.FirstOrDefault(k => !inventory.HasItem(k));
if (other == null)
return;
inventory.Add(other, 0);
ModAssert.False(inventory.HasItem(other), "zero addition creates no stack");
}
}
}