Inventory refuses non-positive counts; unknown saved items kept; factories follow reloads
This commit is contained in:
1 parent
d29df6dab6
commit
6bb47971a3
11 files changed
+334
-215
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
namespace Cthangover.Mods.Items.Tests
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cthangover.Core.Items;
|
||||
using Cthangover.Core.Mods.Resolvers;
|
||||
using Cthangover.Core.Mods.Testing;
|
||||
using Cthangover.Core.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// The items slot across a change of the game's content: a save may name
|
||||
/// an item no loaded mod defines any more (its mod was removed or the id
|
||||
/// renamed). Loading it must keep the rest of the inventory usable, and
|
||||
/// the next save must still be written — with the unknown stack kept, so
|
||||
/// it comes back with its mod.
|
||||
/// </summary>
|
||||
public class ItemsSaveableTests
|
||||
{
|
||||
private const string UnknownId = "__test/no_such_item";
|
||||
|
||||
[ModTest("save/unknown_item_survives_load_and_save")]
|
||||
static void UnknownItemSurvivesLoadAndSave(ModTestContext ctx)
|
||||
{
|
||||
var known = Entities.Collect<ItemInfo>().Keys.First();
|
||||
var runtime = GameData.Instance.Runtime;
|
||||
var original = runtime.Inventory;
|
||||
try
|
||||
{
|
||||
runtime.Inventory = new Inventory();
|
||||
var slot = new ModSlot();
|
||||
slot.Data["Inventory"] = new DataBlob(1, new List<CItem>
|
||||
{
|
||||
new() { ID = known, Count = 4 },
|
||||
new() { ID = UnknownId, Count = 3 },
|
||||
});
|
||||
var saveable = new ItemsSaveable();
|
||||
|
||||
ModAssert.True(saveable.OnLoad(slot, out _), "slot with an unknown item loads");
|
||||
ModAssert.Equal(4, runtime.Inventory.CheckCount(known), "known stack restored");
|
||||
ModAssert.False(runtime.Inventory.HasItem(UnknownId), "unknown item is not a usable stack");
|
||||
|
||||
var saved = new ModSlot();
|
||||
saveable.OnSave(saved);
|
||||
var items = saved.Data["Inventory"].ReadJson<List<CItem>>();
|
||||
ModAssert.Equal(4, items.Single(i => i.ID == known).Count, "known stack saved");
|
||||
ModAssert.Equal(3, items.Single(i => i.ID == UnknownId).Count, "unknown stack kept in the save");
|
||||
}
|
||||
finally
|
||||
{
|
||||
runtime.Inventory = original;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user