changeset 232:fdebdeb52a40

Re #410: Create "N units per M models in parent unit" requirement * Finish "adding" tests * Add common methods for handling adding units
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jul 2012 14:17:21 +0100
parents 5e03b68dd214
children 99248d05ba9c
files API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs
diffstat 1 files changed, 46 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Fri Jul 27 20:33:40 2012 +0100
+++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Sun Jul 29 14:17:21 2012 +0100
@@ -5,6 +5,7 @@
 using NUnit.Framework;
 using NUnit.Framework.SyntaxHelpers;
 using IBBoard.WarFoundry.API.Objects.Requirement.Context;
+using System.Collections.Generic;
 
 namespace IBBoard.WarFoundry.API.Objects.Requirement
 {
@@ -45,11 +46,34 @@
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			Unit parent = AddUnitOfTypeToArmy(unitType3, army);
-			Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
-			parent.AddContainedUnit(firstChild);
+			AddUnitOfTypeToArmy(unitType1, army, parent);
 			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");
+			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as sub-units of each " + unitType3.Name + ", would have 2");
+		}
+		
+		[Test()]
+		public void TestAddingTooManyUnitTypesWithParentAndFiveUnitTypeRequiredFails()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitsOfTypeToArmy(5, unitType1, army, parent);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
+		}
+		
+		[Test()]
+		public void TestAddingTooManyUnitsAtThreshold()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitsOfTypeToArmy(4, unitType1, army, parent);
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType1, parent, army);
+			AddUnitOfTypeToArmy(unitType1, army, parent);
+			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
 		}
 
 		protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
@@ -86,6 +110,25 @@
 			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
 			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
 		}
+
+		private Unit AddUnitOfTypeToArmy(UnitType unitType, Army army, Unit parent)
+		{
+			Unit child = AddUnitOfTypeToArmy(unitType, army);
+			parent.AddContainedUnit(child);
+			return child;
+		}
+		
+		private ICollection<Unit> AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army, Unit parent)
+		{
+			ICollection<Unit> units = new List<Unit>();
+
+			for (int i = 0; i < count; i++)
+			{
+				units.Add(AddUnitOfTypeToArmy(unitType, army, parent));
+			}
+
+			return units;
+		}
 	}
 }