changeset 19:9b23e72ba3cf

Re #198: Add equipment slots * Fix unit tests
author IBBoard <dev@ibboard.co.uk>
date Tue, 27 Oct 2009 20:00:54 +0000
parents e8e7554fb7f3
children beaf7a48a93f
files API/Objects/Mock/MockNumericAmountUnitEquipmentItem.cs API/Objects/Mock/MockPercentageAmountUnitEquipmentItem.cs API/Objects/Mock/MockUnit.cs API/Objects/UnitEquipmentItemTest.cs API/Objects/UnitTest.cs API/Util/UnitEquipmentUtilTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 7 files changed, 134 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Mock/MockNumericAmountUnitEquipmentItem.cs	Sat Oct 24 18:59:30 2009 +0000
+++ b/API/Objects/Mock/MockNumericAmountUnitEquipmentItem.cs	Tue Oct 27 20:00:54 2009 +0000
@@ -3,16 +3,21 @@
 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
 
 using System;
+using IBBoard.Limits;
 
 namespace IBBoard.WarFoundry.API.Objects.Mock
 {
 	public class MockNumericAmountUnitEquipmentItem : UnitEquipmentItem
 	{
-
-		public MockNumericAmountUnitEquipmentItem () : base(new MockEquipmentItem(), new MockUnitType())
+		
+		public MockNumericAmountUnitEquipmentItem () : this(5)
 		{
-			MaxPercentage = 100; //TODO: This seems odd - maybe IsRatio should be "!=0"
-			MaxNumber = WarFoundryCore.INFINITY;
+		}
+			
+
+		public MockNumericAmountUnitEquipmentItem (int limit) : base(new MockEquipmentItem(), new MockUnitType())
+		{
+			MaxLimit = new NumericSizeConstrainedLimit(limit);
 		}
 	}
 }
--- a/API/Objects/Mock/MockPercentageAmountUnitEquipmentItem.cs	Sat Oct 24 18:59:30 2009 +0000
+++ b/API/Objects/Mock/MockPercentageAmountUnitEquipmentItem.cs	Tue Oct 27 20:00:54 2009 +0000
@@ -3,6 +3,7 @@
 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
 
 using System;
+using IBBoard.Limits;
 using IBBoard.WarFoundry.API.Objects;
 
 namespace IBBoard.WarFoundry.API.Objects.Mock
@@ -15,7 +16,7 @@
 		
 		public MockPercentageAmountUnitEquipmentItem(EquipmentItem item, UnitType type) : base(item, type)
 		{
-			MaxPercentage = 50;
+			MaxLimit = new SimpleRoundedPercentageLimit(50, false);
 		}
 	}
 }
--- a/API/Objects/Mock/MockUnit.cs	Sat Oct 24 18:59:30 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-// This file (MockUnit.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 IBBoard
-// 
-// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
-
-using System;
-using IBBoard.WarFoundry.API.Objects;
-
-namespace IBBoard.WarFoundry.API.Objects.Mock
-{
-	public class MockUnit : Unit
-	{
-		public MockUnit (UnitType unitType, ArmyCategory parentCat) : base(unitType, parentCat)
-		{
-		}
-		
-		protected override string DefaultName ()
-		{
-			return "Default Name";
-		}
-	}
-}
--- a/API/Objects/UnitEquipmentItemTest.cs	Sat Oct 24 18:59:30 2009 +0000
+++ b/API/Objects/UnitEquipmentItemTest.cs	Tue Oct 27 20:00:54 2009 +0000
@@ -4,6 +4,7 @@
 
 using System;
 using NUnit.Framework;
+using IBBoard.Limits;
 using IBBoard.WarFoundry.API.Objects.Mock;
 
 namespace IBBoard.WarFoundry.API.Objects
@@ -11,6 +12,7 @@
 	[TestFixture()]
 	public class UnitEquipmentItemTest
 	{
+		/*
 		[Test()]
 		public void DefaultMinMaxNumberLimitsAreZero()
 		{
@@ -136,5 +138,21 @@
 			item.MinPercentage = 50;
 			item.MaxPercentage = 50;
 		}
+		*/
+		[Test()]
+		public void TestSettingOnlyMinLimitSetsMatchingMaxLimit()
+		{
+			UnitEquipmentItem item = new UnitEquipmentItem(new MockEquipmentItem(), new MockUnitType());
+			item.MinLimit = new UnlimitedLimit();
+			Assert.AreEqual(item.MinLimit, item.MaxLimit);
+		}
+		
+		[Test()]
+		public void TestSettingOnlyMaxLimitSetsMatchingMinLimit()
+		{
+			UnitEquipmentItem item = new UnitEquipmentItem(new MockEquipmentItem(), new MockUnitType());
+			item.MaxLimit = new UnlimitedLimit();
+			Assert.AreEqual(item.MaxLimit, item.MinLimit);
+		}
 	}
 }
--- a/API/Objects/UnitTest.cs	Sat Oct 24 18:59:30 2009 +0000
+++ b/API/Objects/UnitTest.cs	Tue Oct 27 20:00:54 2009 +0000
@@ -5,6 +5,7 @@
 using System;
 using NUnit.Framework;
 using IBBoard.WarFoundry.API.Objects.Mock;
+using IBBoard.Limits;
 
 namespace IBBoard.WarFoundry.API.Objects
 {
@@ -14,21 +15,22 @@
 		[Test()]
 		public void TestAddNumericAmountEquipmentAddsEquipmentSelection()
 		{
-			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
+			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem(5);
+			Assert.IsTrue(equip.MaxLimit is Limits.NumericSizeConstrainedLimit);
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new Unit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.SetEquipmentAmount(equip, WarFoundryCore.INFINITY);
-			Assert.AreEqual(WarFoundryCore.INFINITY, unit.GetEquipmentAmount(equip));
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
+			unit.SetEquipmentAmount(equip, 5);
+			Assert.AreEqual(5, unit.GetEquipmentAmount(equip));
 			Assert.IsFalse(unit.GetEquipmentAmountIsRatio(equip));
 		}
 		
 		[Test()]
 		public void TestRemoveNumericAmountEquipmentRemovesEquipmentSelection()
 		{
-			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
+			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem(5);
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new Unit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.SetEquipmentAmount(equip, WarFoundryCore.INFINITY);
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
+			unit.SetEquipmentAmount(equip, 5);
 			unit.SetEquipmentAmount(equip, 0);
 			Assert.AreEqual(0, unit.GetEquipmentAmount(equip));
 		}
@@ -38,7 +40,7 @@
 		{
 			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new Unit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentRatio(equip, 50);
 			Assert.AreEqual(50, unit.GetEquipmentAmount(equip));
 			Assert.IsTrue(unit.GetEquipmentAmountIsRatio(equip));
@@ -49,7 +51,7 @@
 		{
 			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new Unit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentRatio(equip, 50);
 			unit.SetEquipmentRatio(equip, 0);
 			Assert.AreEqual(0, unit.GetEquipmentAmount(equip));
@@ -60,7 +62,7 @@
 		{
 			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = new Unit("unit", "Test Unit", 5, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.Size = 5;
 			unit.SetEquipmentAmount(equip, 2);
 			Assert.AreEqual(2, unit.GetEquipmentAmount(equip));
@@ -72,8 +74,7 @@
 		{
 			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.Size = 5;
+			Unit unit = new Unit("unit", "Test Unit", 5, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentAmount(equip, 2);
 			unit.SetEquipmentAmount(equip, 0);
 			Assert.AreEqual(0, unit.GetEquipmentAmount(equip));
@@ -83,7 +84,7 @@
 		public void TestAmountInSlotWithNoItems()
 		{
 			UnitType unitType = new MockUnitType();
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
 			Assert.AreEqual(0, unit.GetEquipmentAmountInSlot(""));
 		}
 		
@@ -94,7 +95,7 @@
 			string slotName = "TestSlot";
 			equip.SlotName = slotName;
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = new Unit("unit", "Test Unit", 5, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.Size = 5;
 			unit.SetEquipmentRatio(equip, 50);
 			Assert.AreEqual(2, unit.GetEquipmentAmountInSlot(slotName));
@@ -107,15 +108,14 @@
 			string slotName = "TestSlot";
 			equip1.SlotName = slotName;
 			UnitType unitType = equip1.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.Size = 10;
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentRatio(equip1, 50);
 			UnitEquipmentItem equip2 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip2"), unitType);
 			equip2.SlotName = slotName;
-			equip2.MinPercentage = 0;
+			equip2.MinLimit = new SimpleRoundedPercentageLimit(0);
 			UnitEquipmentItem equip3 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip3"), unitType);
 			equip3.SlotName = slotName;
-			equip3.MinPercentage = 0;
+			equip3.MinLimit = new SimpleRoundedPercentageLimit(0);
 			unit.SetEquipmentRatio(equip2, 25);
 			unit.SetEquipmentRatio(equip3, 10);
 			Assert.AreEqual(8, unit.GetEquipmentAmountInSlot(slotName));
@@ -128,8 +128,7 @@
 			string slotName = "TestSlot";
 			equip.SlotName = slotName;
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.Size = 5;
+			Unit unit = new Unit("unit", "Test Unit", 5, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentRatio(equip, 50);
 			unit.SetEquipmentRatio(equip, 0);
 			Assert.AreEqual(0, unit.GetEquipmentAmountInSlot(slotName));
@@ -142,15 +141,14 @@
 			string slotName = "TestSlot";
 			equip1.SlotName = slotName;
 			UnitType unitType = equip1.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
-			unit.Size = 10;
+			Unit unit = new Unit("unit", "Test Unit", 10, unitType, new MockArmyCategory(unitType.MainCategory));
 			unit.SetEquipmentRatio(equip1, 50);
 			UnitEquipmentItem equip2 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip2"), unitType);
 			equip2.SlotName = slotName;
-			equip2.MinPercentage = 0;
+			equip2.MinLimit = new SimpleRoundedPercentageLimit(0);
 			UnitEquipmentItem equip3 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip3"), unitType);
 			equip3.SlotName = slotName;
-			equip3.MinPercentage = 0;
+			equip3.MinLimit = new SimpleRoundedPercentageLimit(0);
 			unit.SetEquipmentRatio(equip2, 25);
 			unit.SetEquipmentRatio(equip3, 10);
 			unit.SetEquipmentAmount(equip1, 0);
--- a/API/Util/UnitEquipmentUtilTest.cs	Sat Oct 24 18:59:30 2009 +0000
+++ b/API/Util/UnitEquipmentUtilTest.cs	Tue Oct 27 20:00:54 2009 +0000
@@ -16,22 +16,100 @@
 		[Test()]
 		public void TestGetMaxEquipmentCountForFixedLimitItemOnDefaultSlot()
 		{
-			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
-			equip.MaxNumber = 3;
+			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem(3);
 			UnitType unitType = equip.EquipmentForUnit;
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = GetUnit (unitType);
 			Assert.AreEqual(3, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
 		}
+
 		
 		[Test()]
 		public void TestGetMaxEquipmentCountForFixedLimitItemOnNumberLimitedSlot()
 		{
 			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
-			equip.MaxNumber = 3;
+			equip.MaxLimit = new NumericSizeConstrainedLimit(3);
+			equip.SlotName = "slot";
 			UnitType unitType = equip.EquipmentForUnit;
 			unitType.AddEquipmentSlot("slot", new AbsoluteNumericLimit(2));
-			Unit unit = new MockUnit(unitType, new MockArmyCategory(unitType.MainCategory));
+			Unit unit = GetUnit(unitType);
 			Assert.AreEqual(2, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
 		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForFixedLimitItemOnPercentageLimitedSlot()
+		{
+			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
+			equip.MaxLimit = new NumericSizeConstrainedLimit(7);
+			equip.SlotName = "slot";
+			UnitType unitType = equip.EquipmentForUnit;
+			unitType.AddEquipmentSlot("slot", new SimpleRoundedPercentageLimit(50, false));
+			Unit unit = GetUnit(unitType);
+			Assert.AreEqual(5, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForFixedLimitItemOnNumberLimitedSlotWithExistingEquipment()
+		{
+			UnitEquipmentItem equip = new MockNumericAmountUnitEquipmentItem();
+			equip.MaxLimit = new NumericSizeConstrainedLimit(3);
+			equip.SlotName = "slot";
+			UnitType unitType = equip.EquipmentForUnit;
+			unitType.AddEquipmentSlot("slot", new AbsoluteNumericLimit(6));
+			Unit unit =GetUnit(unitType);
+			UnitEquipmentItem equip2 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip2"), unitType);
+			equip2.SlotName = "slot";
+			unit.SetEquipmentAmount(equip2, 5);
+			Assert.AreEqual(1, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForPercentageLimitItemOnDefaultSlot()
+		{
+			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
+			UnitType unitType = equip.EquipmentForUnit;
+			Unit unit = GetUnit(unitType);
+			Assert.AreEqual(5, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForPercentageLimitItemOnNumberLimitedSlot()
+		{
+			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
+			equip.SlotName = "slot";
+			UnitType unitType = equip.EquipmentForUnit;
+			unitType.AddEquipmentSlot("slot", new AbsoluteNumericLimit(2));
+			Unit unit = GetUnit(unitType);
+			Assert.AreEqual(2, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForPercentageLimitItemOnPercentageLimitedSlot()
+		{
+			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
+			equip.SlotName = "slot";
+			UnitType unitType = equip.EquipmentForUnit;
+			unitType.AddEquipmentSlot("slot", new SimpleRoundedPercentageLimit(25, false));
+			Unit unit = GetUnit(unitType);
+			Assert.AreEqual(2, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+		
+		[Test()]
+		public void TestGetMaxEquipmentCountForPercentageLimitItemOnNumberLimitedSlotWithExistingEquipment()
+		{
+			UnitEquipmentItem equip = new MockPercentageAmountUnitEquipmentItem();
+			equip.SlotName = "slot";
+			UnitType unitType = equip.EquipmentForUnit;
+			unitType.AddEquipmentSlot("slot", new AbsoluteNumericLimit(6));
+			Unit unit = GetUnit(unitType);
+			UnitEquipmentItem equip2 = new MockPercentageAmountUnitEquipmentItem(new MockEquipmentItem("equip2"), unitType);
+			equip2.SlotName = "slot";
+			unit.SetEquipmentAmount(equip2, 5);
+			Assert.AreEqual(1, UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip));
+		}
+
+		private static Unit GetUnit (UnitType unitType)
+		{
+			return new Unit ("unit", "Test Unit", 10, unitType, new MockArmyCategory (unitType.MainCategory));
+		}
 	}
 }
--- a/IBBoard.WarFoundry.API.Tests.csproj	Sat Oct 24 18:59:30 2009 +0000
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Tue Oct 27 20:00:54 2009 +0000
@@ -55,7 +55,6 @@
     <Compile Include="API\Objects\Mock\MockCategory.cs" />
     <Compile Include="API\Objects\Mock\MockNumericAmountUnitEquipmentItem.cs" />
     <Compile Include="API\Objects\Mock\MockPercentageAmountUnitEquipmentItem.cs" />
-    <Compile Include="API\Objects\Mock\MockUnit.cs" />
     <Compile Include="API\Util\UnitEquipmentUtilTest.cs" />
   </ItemGroup>
   <ItemGroup>