changeset 216:442bcf7306b9

Re #379: Fix validation of requirements to check for unit * Add tests for max limit on units in army * Re-add code for unit type requirement checking with min/max limits
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Mar 2012 21:12:44 +0000
parents 7e7296c0ae1f
children 9bf34e88da89
files API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirementTest.cs API/Objects/UnitTypeTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 3 files changed, 317 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirementTest.cs	Sun Mar 04 21:12:44 2012 +0000
@@ -0,0 +1,307 @@
+// This file (RaceRequiresNoMoreThanNUnitsRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 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 NUnit.Framework;
+using IBBoard.WarFoundry.API.Objects.Mock;
+using NUnit.Framework.SyntaxHelpers;
+
+namespace IBBoard.WarFoundry.API.Objects.Requirement
+{
+	[TestFixture()]
+	public class RaceRequiresNoMoreThanNUnitsRequirementTest : AbstractUnitRequirementTest<Race>
+	{
+		private MockRace mockRace;
+		private UnitType unitType1;
+		private UnitType unitType2;
+		private UnitType unitType3;
+
+		[TestFixtureSetUp()]
+		public void SetupRace()
+		{
+			mockRace = new MockRace();
+			unitType1 = new MockUnitType("type1", "Unit Type 1");
+			mockRace.AddUnitType(unitType1);
+			unitType2 = new MockUnitType("type2", "Unit Type 2");
+			mockRace.AddUnitType(unitType2);
+			unitType3 = new MockUnitType("type3", "Unit Type 3");
+			mockRace.AddUnitType(unitType3);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithNoUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 2).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequiredFails()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1).");
+			Assert_That__FailsAdding(req, unitType3, army, "Army cannot contain more than: 0 × " + unitType3.Name + " (would have 1).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 2).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1); 0 × " + unitType3.Name + " (would have 1).");
+		}
+
+		[Test()]
+		public void TestAddingOtherUnitTypeWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType3, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1); 0 × " + unitType3.Name + " (would have 1).");
+		}
+
+		[Test()]
+		public void TestAddingOtherUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType3, army, "Army cannot contain more than: 0 × " + unitType3.Name + " (would have 2).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 2); 0 × " + unitType3.Name + " (would have 1).");
+			Assert_That__FailsAdding(req, unitType3, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1); 0 × " + unitType3.Name + " (would have 2).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicable()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__AddingNotApplicable(req, unitType1, army);
+		}
+
+		[Test()]
+		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicableWhenRequirementsNotMet()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__AddingNotApplicable(req, unitType1, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithTwoUnitsAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 2 × " + unitType2.Name + " (would have 3).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingZero()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (would have 1).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeMakesRequirementFail()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 1);
+			Assert_That__PassesAdding(req, unitType2, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__FailsAdding(req, unitType2, army, "Army cannot contain more than: 1 × " + unitType2.Name + " (would have 2).");
+		}
+
+		[Test()]
+		public void TestValidatingWithNoUnitsAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestValidatingWithNoUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestValidatingWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 0 × " + unitType3.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestValidatingWithBothUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (have 1); 0 × " + unitType3.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestValidatingWithBothUnitsUnderLimitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 1);
+			req.AddUnitTypeRequirement(unitType3, 1);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithUnrelatedUnitTypeValidatesAsPass()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithTwoUnitsAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingRequirementWithNoNumberDefaultsToHavingZero()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 0 × " + unitType2.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeMakesRequirementFailValidating()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 1);
+			Assert_That__ValidationPasses(req, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__ValidationFails(req, army, "Army cannot contain more than: 1 × " + unitType2.Name + " (have 2).");
+		}
+	}
+}
+
--- a/API/Objects/UnitTypeTest.cs	Sun Mar 04 20:35:21 2012 +0000
+++ b/API/Objects/UnitTypeTest.cs	Sun Mar 04 21:12:44 2012 +0000
@@ -62,17 +62,17 @@
 		[Test]
 		public void TestGetRequirementsGetsUnitCountRequirements()
 		{
-			UnitType unitType = new UnitType("unittype1", "Swordsmen", new MockRace());
+			MockRace mockRace = new MockRace();
+			UnitType unitType = new UnitType("unittype1", "Swordsmen", mockRace);
 			unitType.MinNumber = 1;
 			unitType.MaxNumber = 2;
-			Assert.Fail("Needs fixing after changes");
-			//Assert.That(unitType.GetRequirements(), Has.Length(2));
-			//RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> maxLimit = new RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE>();
-			//maxLimit.AddUnitTypeRequirement(unitType, 2);
-			//Assert.That(unitType.GetRequirements(), Has.Member(maxLimit));
-			//RequiresAtLeastNUnitsRequirement minLimit = new RequiresAtLeastNUnitsRequirement();
-			//minLimit.AddUnitTypeRequirement(unitType, 1);
-			//Assert.That(unitType.GetRequirements(), Has.Member(minLimit));
+			Assert.That(unitType.GetRequirements(), Has.Length(2));
+			RaceRequiresNoMoreThanNUnitsRequirement maxLimit = new RaceRequiresNoMoreThanNUnitsRequirement(mockRace, unitType);
+			maxLimit.AddUnitTypeRequirement(unitType, 2);
+			Assert.That(unitType.GetRequirements(), Has.Member(maxLimit));
+			RaceRequiresAtLeastNUnitsRequirement minLimit = new RaceRequiresAtLeastNUnitsRequirement(mockRace, unitType);
+			minLimit.AddUnitTypeRequirement(unitType, 1);
+			Assert.That(unitType.GetRequirements(), Has.Member(minLimit));
 		}
 
 		public override UnitType GetObject()
--- a/IBBoard.WarFoundry.API.Tests.csproj	Sun Mar 04 20:35:21 2012 +0000
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Sun Mar 04 21:12:44 2012 +0000
@@ -118,6 +118,7 @@
     <Compile Include="API\Objects\Requirement\Mock\MockRequiresNUnitsForMObjectsRequirement.cs" />
     <Compile Include="API\Objects\Requirement\AbstractUnitRequirementTest.cs" />
     <Compile Include="API\Objects\Requirement\RaceRequiresAtLeastNUnitsRequirementTest.cs" />
+    <Compile Include="API\Objects\Requirement\RaceRequiresNoMoreThanNUnitsRequirementTest.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />