changeset 231:5e03b68dd214

Re #410: Create "N units per M models in parent unit" requirement * Add required usage of context objects * Add tests for new requirement * Add some extras to test unit type setup that the new requirement needs
author IBBoard <dev@ibboard.co.uk>
date Fri, 27 Jul 2012 20:33:40 +0100
parents d6883a522c70
children fdebdeb52a40
files API/Factories/Requirement/Mock/MockRequirement.cs API/Objects/Requirement/AbstractUnitRequirementTest.cs API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs API/Objects/Requirement/Mock/AbstractFixedRequirement.cs API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 6 files changed, 112 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/API/Factories/Requirement/Mock/MockRequirement.cs	Wed Jul 11 20:54:12 2012 +0100
+++ b/API/Factories/Requirement/Mock/MockRequirement.cs	Fri Jul 27 20:33:40 2012 +0100
@@ -4,6 +4,7 @@
 using System;
 using IBBoard.WarFoundry.API.Objects.Requirement;
 using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Objects.Requirement.Context;
 
 namespace IBBoard.WarFoundry.API.Factories.Requirement.Mock
 {
@@ -36,7 +37,7 @@
 			throw new NotImplementedException();
 		}
 
-		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy)
+		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context)
 		{
 			throw new NotImplementedException();
 		}
--- a/API/Objects/Requirement/AbstractUnitRequirementTest.cs	Wed Jul 11 20:54:12 2012 +0100
+++ b/API/Objects/Requirement/AbstractUnitRequirementTest.cs	Fri Jul 27 20:33:40 2012 +0100
@@ -5,6 +5,7 @@
 using NUnit.Framework;
 using NUnit.Framework.SyntaxHelpers;
 using IBBoard.NUnit;
+using System.Collections.Generic;
 
 namespace IBBoard.WarFoundry.API.Objects.Requirement
 {
@@ -58,17 +59,23 @@
 			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
 		}
 
-		protected static void AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army)
+		protected static ICollection<Unit> AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army)
 		{
+			ICollection<Unit> units = new List<Unit>();
+
 			for (int i = 0; i < count; i++)
 			{
-				AddUnitOfTypeToArmy(unitType, army);
+				units.Add(AddUnitOfTypeToArmy(unitType, army));
 			}
+
+			return units;
 		}
 
-		protected static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
+		protected static Unit AddUnitOfTypeToArmy(UnitType unitType, Army army)
 		{
-			army.AddUnit(CreateUnitOfType(unitType, army));
+			Unit unit = CreateUnitOfType(unitType, army);
+			army.AddUnit(unit);
+			return unit;
 		}
 
 		protected static Unit CreateUnitOfType(UnitType unitType, Army army)
--- a/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs	Wed Jul 11 20:54:12 2012 +0100
+++ b/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs	Fri Jul 27 20:33:40 2012 +0100
@@ -19,10 +19,15 @@
 		{
 			mockRace = new MockRace();
 			unitType1 = new MockUnitType("type1", "Unit Type 1");
+			unitType1.MinSize = 1;
 			mockRace.AddUnitType(unitType1);
 			unitType2 = new MockUnitType("type2", "Unit Type 2");
+			unitType2.MinSize = 5;
+			unitType2.AddContainedUnitType(unitType1);
 			mockRace.AddUnitType(unitType2);
 			unitType3 = new MockUnitType("type3", "Unit Type 3");
+			unitType3.MinSize = 1;
+			unitType3.AddContainedUnitType(unitType1);
 			mockRace.AddUnitType(unitType3);
 		}
 
--- a/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs	Wed Jul 11 20:54:12 2012 +0100
+++ b/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs	Fri Jul 27 20:33:40 2012 +0100
@@ -2,6 +2,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.WarFoundry.API.Objects.Requirement.Context;
 
 namespace IBBoard.WarFoundry.API.Objects.Requirement.Mock
 {
@@ -34,7 +35,7 @@
 			return (result.Equals(Validation.Failed)) ? "Validation failed" : "";
 		}
 
-		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy)
+		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context)
 		{
 			return (result.Equals(Validation.Failed)) ? "Validation failed" : "";
 		}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Fri Jul 27 20:33:40 2012 +0100
@@ -0,0 +1,91 @@
+// This file (UnitRequiresNoMoreThanNUnitsPerMModelsInParentUnitRequirementTests.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2012 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 NUnit.Framework.SyntaxHelpers;
+using IBBoard.WarFoundry.API.Objects.Requirement.Context;
+
+namespace IBBoard.WarFoundry.API.Objects.Requirement
+{
+	[TestFixture()]
+	public class UnitRequiresNParentModelsForMUnitsRequirementTests: AbstractUnitTypeUnitRequirementTest<UnitType, UnitRequiresNParentModelsForMUnitsRequirement>
+	{
+		[Test()]
+		public void TestAddingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__AddingNotApplicable(req, unitType1, army);
+		}
+		
+		[Test()]
+		public void TestAddingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType1, parent, army);
+		}
+		
+		[Test()]
+		public void TestAddingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__AddingNotApplicable(req, unitType1, parent, army);
+		}
+		
+		[Test()]
+		public void TestAddingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit parent = AddUnitOfTypeToArmy(unitType3, army);
+			Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
+			parent.AddContainedUnit(firstChild);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType3);
+			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as a sub-unit of each " + unitType3.Name + ", would have 2");
+		}
+
+		protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
+		{
+			return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn);
+		}
+		
+		protected static void Assert_That__PassesAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
+		{
+			AddingToParentContext ctx = new AddingToParentContext(parent);
+			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Passed));
+			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
+			Unit unit = CreateUnitOfType(unitType, army);
+			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Passed));
+			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
+		}
+		
+		protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
+		{
+			AddingToParentContext ctx = new AddingToParentContext(parent);
+			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.NotApplicable));
+			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
+			Unit unit = CreateUnitOfType(unitType, army);
+			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.NotApplicable));
+			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
+		}
+		
+		protected static void Assert_That__FailsAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army, string message)
+		{
+			AddingToParentContext ctx = new AddingToParentContext(parent);
+			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Failed));
+			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.EqualTo(message));
+			Unit unit = CreateUnitOfType(unitType, army);
+			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
+			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
+		}
+	}
+}
+
--- a/IBBoard.WarFoundry.API.Tests.csproj	Wed Jul 11 20:54:12 2012 +0100
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Fri Jul 27 20:33:40 2012 +0100
@@ -127,6 +127,7 @@
     <Compile Include="API\Loading\LoadableObjectSourceResourceSetTests.cs" />
     <Compile Include="API\Loading\LoadableObjectSourceDirectoryTests.cs" />
     <Compile Include="API\Factories\Mock\MockRaceAndSystemFactory.cs" />
+    <Compile Include="API\Objects\Requirement\UnitRequiresNParentModelsForMUnitsRequirementTests.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />