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 diff
     1.1 --- a/API/Factories/Requirement/Mock/MockRequirement.cs	Wed Jul 11 20:54:12 2012 +0100
     1.2 +++ b/API/Factories/Requirement/Mock/MockRequirement.cs	Fri Jul 27 20:33:40 2012 +0100
     1.3 @@ -4,6 +4,7 @@
     1.4  using System;
     1.5  using IBBoard.WarFoundry.API.Objects.Requirement;
     1.6  using IBBoard.WarFoundry.API.Objects;
     1.7 +using IBBoard.WarFoundry.API.Objects.Requirement.Context;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Factories.Requirement.Mock
    1.10  {
    1.11 @@ -36,7 +37,7 @@
    1.12  			throw new NotImplementedException();
    1.13  		}
    1.14  
    1.15 -		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy)
    1.16 +		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context)
    1.17  		{
    1.18  			throw new NotImplementedException();
    1.19  		}
     2.1 --- a/API/Objects/Requirement/AbstractUnitRequirementTest.cs	Wed Jul 11 20:54:12 2012 +0100
     2.2 +++ b/API/Objects/Requirement/AbstractUnitRequirementTest.cs	Fri Jul 27 20:33:40 2012 +0100
     2.3 @@ -5,6 +5,7 @@
     2.4  using NUnit.Framework;
     2.5  using NUnit.Framework.SyntaxHelpers;
     2.6  using IBBoard.NUnit;
     2.7 +using System.Collections.Generic;
     2.8  
     2.9  namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.10  {
    2.11 @@ -58,17 +59,23 @@
    2.12  			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
    2.13  		}
    2.14  
    2.15 -		protected static void AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army)
    2.16 +		protected static ICollection<Unit> AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army)
    2.17  		{
    2.18 +			ICollection<Unit> units = new List<Unit>();
    2.19 +
    2.20  			for (int i = 0; i < count; i++)
    2.21  			{
    2.22 -				AddUnitOfTypeToArmy(unitType, army);
    2.23 +				units.Add(AddUnitOfTypeToArmy(unitType, army));
    2.24  			}
    2.25 +
    2.26 +			return units;
    2.27  		}
    2.28  
    2.29 -		protected static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
    2.30 +		protected static Unit AddUnitOfTypeToArmy(UnitType unitType, Army army)
    2.31  		{
    2.32 -			army.AddUnit(CreateUnitOfType(unitType, army));
    2.33 +			Unit unit = CreateUnitOfType(unitType, army);
    2.34 +			army.AddUnit(unit);
    2.35 +			return unit;
    2.36  		}
    2.37  
    2.38  		protected static Unit CreateUnitOfType(UnitType unitType, Army army)
     3.1 --- a/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs	Wed Jul 11 20:54:12 2012 +0100
     3.2 +++ b/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs	Fri Jul 27 20:33:40 2012 +0100
     3.3 @@ -19,10 +19,15 @@
     3.4  		{
     3.5  			mockRace = new MockRace();
     3.6  			unitType1 = new MockUnitType("type1", "Unit Type 1");
     3.7 +			unitType1.MinSize = 1;
     3.8  			mockRace.AddUnitType(unitType1);
     3.9  			unitType2 = new MockUnitType("type2", "Unit Type 2");
    3.10 +			unitType2.MinSize = 5;
    3.11 +			unitType2.AddContainedUnitType(unitType1);
    3.12  			mockRace.AddUnitType(unitType2);
    3.13  			unitType3 = new MockUnitType("type3", "Unit Type 3");
    3.14 +			unitType3.MinSize = 1;
    3.15 +			unitType3.AddContainedUnitType(unitType1);
    3.16  			mockRace.AddUnitType(unitType3);
    3.17  		}
    3.18  
     4.1 --- a/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs	Wed Jul 11 20:54:12 2012 +0100
     4.2 +++ b/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs	Fri Jul 27 20:33:40 2012 +0100
     4.3 @@ -2,6 +2,7 @@
     4.4  // 
     4.5  // 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.
     4.6  using System;
     4.7 +using IBBoard.WarFoundry.API.Objects.Requirement.Context;
     4.8  
     4.9  namespace IBBoard.WarFoundry.API.Objects.Requirement.Mock
    4.10  {
    4.11 @@ -34,7 +35,7 @@
    4.12  			return (result.Equals(Validation.Failed)) ? "Validation failed" : "";
    4.13  		}
    4.14  
    4.15 -		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy)
    4.16 +		public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context)
    4.17  		{
    4.18  			return (result.Equals(Validation.Failed)) ? "Validation failed" : "";
    4.19  		}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs	Fri Jul 27 20:33:40 2012 +0100
     5.3 @@ -0,0 +1,91 @@
     5.4 +// This file (UnitRequiresNoMoreThanNUnitsPerMModelsInParentUnitRequirementTests.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2012 IBBoard
     5.5 +//
     5.6 +// 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.
     5.7 +using System;
     5.8 +using NUnit.Framework;
     5.9 +using NUnit.Framework.SyntaxHelpers;
    5.10 +using IBBoard.WarFoundry.API.Objects.Requirement.Context;
    5.11 +
    5.12 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    5.13 +{
    5.14 +	[TestFixture()]
    5.15 +	public class UnitRequiresNParentModelsForMUnitsRequirementTests: AbstractUnitTypeUnitRequirementTest<UnitType, UnitRequiresNParentModelsForMUnitsRequirement>
    5.16 +	{
    5.17 +		[Test()]
    5.18 +		public void TestAddingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed()
    5.19 +		{
    5.20 +			Army army = new Army(mockRace, "Test", 1000);
    5.21 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    5.22 +			req.AddUnitTypeRequirement(unitType2);
    5.23 +			Assert_That__AddingNotApplicable(req, unitType1, army);
    5.24 +		}
    5.25 +		
    5.26 +		[Test()]
    5.27 +		public void TestAddingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed()
    5.28 +		{
    5.29 +			Army army = new Army(mockRace, "Test", 1000);
    5.30 +			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
    5.31 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    5.32 +			req.AddUnitTypeRequirement(unitType2);
    5.33 +			Assert_That__PassesAdding(req, unitType1, parent, army);
    5.34 +		}
    5.35 +		
    5.36 +		[Test()]
    5.37 +		public void TestAddingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable()
    5.38 +		{
    5.39 +			Army army = new Army(mockRace, "Test", 1000);
    5.40 +			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
    5.41 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    5.42 +			req.AddUnitTypeRequirement(unitType3);
    5.43 +			Assert_That__AddingNotApplicable(req, unitType1, parent, army);
    5.44 +		}
    5.45 +		
    5.46 +		[Test()]
    5.47 +		public void TestAddingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails()
    5.48 +		{
    5.49 +			Army army = new Army(mockRace, "Test", 1000);
    5.50 +			Unit parent = AddUnitOfTypeToArmy(unitType3, army);
    5.51 +			Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
    5.52 +			parent.AddContainedUnit(firstChild);
    5.53 +			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
    5.54 +			req.AddUnitTypeRequirement(unitType3);
    5.55 +			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");
    5.56 +		}
    5.57 +
    5.58 +		protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
    5.59 +		{
    5.60 +			return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn);
    5.61 +		}
    5.62 +		
    5.63 +		protected static void Assert_That__PassesAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
    5.64 +		{
    5.65 +			AddingToParentContext ctx = new AddingToParentContext(parent);
    5.66 +			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Passed));
    5.67 +			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
    5.68 +			Unit unit = CreateUnitOfType(unitType, army);
    5.69 +			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Passed));
    5.70 +			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
    5.71 +		}
    5.72 +		
    5.73 +		protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
    5.74 +		{
    5.75 +			AddingToParentContext ctx = new AddingToParentContext(parent);
    5.76 +			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.NotApplicable));
    5.77 +			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
    5.78 +			Unit unit = CreateUnitOfType(unitType, army);
    5.79 +			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.NotApplicable));
    5.80 +			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
    5.81 +		}
    5.82 +		
    5.83 +		protected static void Assert_That__FailsAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army, string message)
    5.84 +		{
    5.85 +			AddingToParentContext ctx = new AddingToParentContext(parent);
    5.86 +			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Failed));
    5.87 +			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.EqualTo(message));
    5.88 +			Unit unit = CreateUnitOfType(unitType, army);
    5.89 +			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
    5.90 +			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
    5.91 +		}
    5.92 +	}
    5.93 +}
    5.94 +
     6.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Wed Jul 11 20:54:12 2012 +0100
     6.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Fri Jul 27 20:33:40 2012 +0100
     6.3 @@ -127,6 +127,7 @@
     6.4      <Compile Include="API\Loading\LoadableObjectSourceResourceSetTests.cs" />
     6.5      <Compile Include="API\Loading\LoadableObjectSourceDirectoryTests.cs" />
     6.6      <Compile Include="API\Factories\Mock\MockRaceAndSystemFactory.cs" />
     6.7 +    <Compile Include="API\Objects\Requirement\UnitRequiresNParentModelsForMUnitsRequirementTests.cs" />
     6.8    </ItemGroup>
     6.9    <ItemGroup>
    6.10      <None Include="app.config" />