changeset 215:7e7296c0ae1f

Re #379: Fix validation of requirements to check for unit * Add tests for requiring minimum number of units in army
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Mar 2012 20:35:21 +0000
parents 1674a499168e
children 442bcf7306b9
files API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirementTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 2 files changed, 297 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirementTest.cs	Sun Mar 04 20:35:21 2012 +0000
@@ -0,0 +1,296 @@
+// This file (RaceRequiresAtLeastNUnitsRequirementTest.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 RaceRequiresAtLeastNUnitsRequirementTest : 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);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequiredFails()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType3.Name + " (would have 0).");
+			Assert_That__FailsAdding(req, unitType3, army, "Army must contain: 1 × " + unitType2.Name + " (would have 0).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType3.Name + " (would have 0).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingOtherUnitTypeWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__PassesAdding(req, unitType3, army);
+		}
+
+		[Test()]
+		public void TestAddingOtherUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType3, army, "Army must contain: 1 × " + unitType2.Name + " (would have 0).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__PassesAdding(req, unitType2, army);
+			Assert_That__PassesAdding(req, unitType3, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicable()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__AddingNotApplicable(req, unitType1, army);
+		}
+
+		[Test()]
+		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicableWhenRequirementsNotMet()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(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);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingOne()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeMakesRequirementPass()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 2 × " + unitType2.Name + " (would have 1).");
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__PassesAdding(req, unitType2, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithNoUnitsAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0).");
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithNoUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0); 1 × " + unitType3.Name + " (have 0).");
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType3.Name + " (have 0).");
+		}
+
+		[Test()]
+		public void TestValidatingWithOtherOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0).");
+		}
+
+		[Test()]
+		public void TestValidatingWithBothUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithOneUnitAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " (have 1).");
+		}
+
+		[Test()]
+		public void TestValidatingWithUnrelatedUnitTypeValidatesAsFailed()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " (have 0).");
+		}
+
+		[Test()]
+		public void TestValidatingWithTwoUnitsAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingRequirementWithNoNumberDefaultsToHavingOne()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeMakesRequirementPassValidating()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(mockRace);
+			req.AddUnitTypeRequirement(unitType2, 2);
+			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " (have 1).");
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__ValidationPasses(req, army);
+		}
+	}
+}
+
--- a/IBBoard.WarFoundry.API.Tests.csproj	Sun Feb 26 20:18:45 2012 +0000
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Sun Mar 04 20:35:21 2012 +0000
@@ -117,6 +117,7 @@
     <Compile Include="API\Objects\Requirement\UnitRequiresNUnitsForMUnitsRequirementTests.cs" />
     <Compile Include="API\Objects\Requirement\Mock\MockRequiresNUnitsForMObjectsRequirement.cs" />
     <Compile Include="API\Objects\Requirement\AbstractUnitRequirementTest.cs" />
+    <Compile Include="API\Objects\Requirement\RaceRequiresAtLeastNUnitsRequirementTest.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />