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 diff
     1.1 --- a/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Fri Jul 27 20:33:40 2012 +0100
     1.2 +++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Sun Jul 29 14:17:21 2012 +0100
     1.3 @@ -5,6 +5,7 @@
     1.4  using NUnit.Framework;
     1.5  using NUnit.Framework.SyntaxHelpers;
     1.6  using IBBoard.WarFoundry.API.Objects.Requirement.Context;
     1.7 +using System.Collections.Generic;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.10  {
    1.11 @@ -45,11 +46,34 @@
    1.12  		{
    1.13  			Army army = new Army(mockRace, "Test", 1000);
    1.14  			Unit parent = AddUnitOfTypeToArmy(unitType3, army);
    1.15 -			Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
    1.16 -			parent.AddContainedUnit(firstChild);
    1.17 +			AddUnitOfTypeToArmy(unitType1, army, parent);
    1.18  			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    1.19  			req.AddUnitTypeRequirement(unitType3);
    1.20 -			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");
    1.21 +			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as sub-units of each " + unitType3.Name + ", would have 2");
    1.22 +		}
    1.23 +		
    1.24 +		[Test()]
    1.25 +		public void TestAddingTooManyUnitTypesWithParentAndFiveUnitTypeRequiredFails()
    1.26 +		{
    1.27 +			Army army = new Army(mockRace, "Test", 1000);
    1.28 +			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
    1.29 +			AddUnitsOfTypeToArmy(5, unitType1, army, parent);
    1.30 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    1.31 +			req.AddUnitTypeRequirement(unitType2);
    1.32 +			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
    1.33 +		}
    1.34 +		
    1.35 +		[Test()]
    1.36 +		public void TestAddingTooManyUnitsAtThreshold()
    1.37 +		{
    1.38 +			Army army = new Army(mockRace, "Test", 1000);
    1.39 +			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
    1.40 +			AddUnitsOfTypeToArmy(4, unitType1, army, parent);
    1.41 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    1.42 +			req.AddUnitTypeRequirement(unitType2);
    1.43 +			Assert_That__PassesAdding(req, unitType1, parent, army);
    1.44 +			AddUnitOfTypeToArmy(unitType1, army, parent);
    1.45 +			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
    1.46  		}
    1.47  
    1.48  		protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
    1.49 @@ -86,6 +110,25 @@
    1.50  			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
    1.51  			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
    1.52  		}
    1.53 +
    1.54 +		private Unit AddUnitOfTypeToArmy(UnitType unitType, Army army, Unit parent)
    1.55 +		{
    1.56 +			Unit child = AddUnitOfTypeToArmy(unitType, army);
    1.57 +			parent.AddContainedUnit(child);
    1.58 +			return child;
    1.59 +		}
    1.60 +		
    1.61 +		private ICollection<Unit> AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army, Unit parent)
    1.62 +		{
    1.63 +			ICollection<Unit> units = new List<Unit>();
    1.64 +
    1.65 +			for (int i = 0; i < count; i++)
    1.66 +			{
    1.67 +				units.Add(AddUnitOfTypeToArmy(unitType, army, parent));
    1.68 +			}
    1.69 +
    1.70 +			return units;
    1.71 +		}
    1.72  	}
    1.73  }
    1.74