geometry-tower-defense/Assets/Tests/EditMode/PlayerInventoryTowerRosterS...

99 lines
4.9 KiB
C#

using GeometryTD.CustomComponent;
using GeometryTD.CustomUtility;
using GeometryTD.Definition;
using NUnit.Framework;
namespace GeometryTD.Tests.EditMode
{
public sealed class PlayerInventoryTowerRosterServiceTests
{
[Test]
public void ReduceTowerEndurance_Reduces_Only_Target_Towers_And_Deduplicates_Ids()
{
BackpackInventoryData inventory = CreateInventory(100f, 100f, 100f);
PlayerInventoryTowerRosterService service = CreateService(inventory, out PlayerInventoryQueryModel queryModel);
int affectedTowerCount = service.ReduceTowerEndurance(new long[] { 90001, 90001, 90002 }, 1f);
Assert.That(affectedTowerCount, Is.EqualTo(2));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Muzzle.Endurance, Is.EqualTo(99f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Bearing.Endurance, Is.EqualTo(99f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Base.Endurance, Is.EqualTo(99f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90002).Muzzle.Endurance, Is.EqualTo(99f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90003).Muzzle.Endurance, Is.EqualTo(100f));
}
[Test]
public void ReduceTowerEndurance_Clamps_Component_Endurance_To_Zero()
{
BackpackInventoryData inventory = CreateInventory(1f, 1f, 1f);
PlayerInventoryTowerRosterService service = CreateService(inventory, out PlayerInventoryQueryModel queryModel);
int affectedTowerCount = service.ReduceTowerEndurance(new long[] { 90001 }, 5f);
Assert.That(affectedTowerCount, Is.EqualTo(1));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Muzzle.Endurance, Is.EqualTo(0f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Bearing.Endurance, Is.EqualTo(0f));
Assert.That(GetTowerComponents(queryModel.Inventory, 90001).Base.Endurance, Is.EqualTo(0f));
}
private static PlayerInventoryTowerRosterService CreateService(
BackpackInventoryData inventory,
out PlayerInventoryQueryModel queryModel)
{
PlayerInventoryState state = new PlayerInventoryState();
queryModel = new PlayerInventoryQueryModel(state);
PlayerInventoryCommandModel commandModel = new PlayerInventoryCommandModel(state);
commandModel.Initialize(inventory, 4);
return new PlayerInventoryTowerRosterService(queryModel, 4);
}
private static BackpackInventoryData CreateInventory(
float firstTowerEndurance,
float secondTowerEndurance,
float thirdTowerEndurance)
{
BackpackInventoryData inventory = new BackpackInventoryData();
AddTower(inventory, 90001, 10001, 20001, 30001, firstTowerEndurance);
AddTower(inventory, 90002, 10002, 20002, 30002, secondTowerEndurance);
AddTower(inventory, 90003, 10003, 20003, 30003, thirdTowerEndurance);
return inventory;
}
private static void AddTower(
BackpackInventoryData inventory,
long towerId,
long muzzleId,
long bearingId,
long baseId,
float endurance)
{
inventory.MuzzleComponents.Add(new MuzzleCompItemData { InstanceId = muzzleId, Endurance = endurance });
inventory.BearingComponents.Add(new BearingCompItemData { InstanceId = bearingId, Endurance = endurance });
inventory.BaseComponents.Add(new BaseCompItemData { InstanceId = baseId, Endurance = endurance });
inventory.Towers.Add(new TowerItemData
{
InstanceId = towerId,
MuzzleComponentInstanceId = muzzleId,
BearingComponentInstanceId = bearingId,
BaseComponentInstanceId = baseId,
Stats = new TowerStatsData()
});
}
private static (MuzzleCompItemData Muzzle, BearingCompItemData Bearing, BaseCompItemData Base) GetTowerComponents(
BackpackInventoryData inventory,
long towerInstanceId)
{
Assert.That(InventoryParticipantUtility.TryGetTowerById(inventory, towerInstanceId, out TowerItemData tower), Is.True);
MuzzleCompItemData muzzle = inventory.MuzzleComponents.Find(item => item.InstanceId == tower.MuzzleComponentInstanceId);
BearingCompItemData bearing = inventory.BearingComponents.Find(item => item.InstanceId == tower.BearingComponentInstanceId);
BaseCompItemData baseComp = inventory.BaseComponents.Find(item => item.InstanceId == tower.BaseComponentInstanceId);
Assert.That(muzzle, Is.Not.Null);
Assert.That(bearing, Is.Not.Null);
Assert.That(baseComp, Is.Not.Null);
return (muzzle, bearing, baseComp);
}
}
}