changeset 157:17c5030485dc

Re #351: Add extensible requirement handling method * Swap to using IRequirement, as per interface * Test registration of factories
author IBBoard <dev@ibboard.co.uk>
date Sat, 23 Jul 2011 19:54:37 +0000
parents ae2f1db8290c
children d0b8c43f705f
files API/Factories/Requirement/Mock/MockRequirement.cs API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs API/MockRequirementFactory.cs API/WarFoundryLoaderTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 5 files changed, 117 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Requirement/Mock/MockRequirement.cs	Sat Jul 23 19:54:37 2011 +0000
     1.3 @@ -0,0 +1,37 @@
     1.4 +// This file (MockRequirement.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
     1.5 +// 
     1.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.
     1.7 +using System;
     1.8 +using IBBoard.WarFoundry.API.Objects.Requirement;
     1.9 +using IBBoard.WarFoundry.API.Objects;
    1.10 +
    1.11 +namespace IBBoard.WarFoundry.API.Factories.Requirement.Mock
    1.12 +{
    1.13 +	public class MockRequirement : IRequirement
    1.14 +	{
    1.15 +		public MockRequirement()
    1.16 +		{
    1.17 +		}
    1.18 +
    1.19 +		public Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    1.20 +		{
    1.21 +			throw new NotImplementedException();
    1.22 +		}
    1.23 +
    1.24 +		public Validation ValidatesArmy(Army army)
    1.25 +		{
    1.26 +			throw new NotImplementedException();
    1.27 +		}
    1.28 +
    1.29 +		public string GetValidationMessage(Army army)
    1.30 +		{
    1.31 +			throw new NotImplementedException();
    1.32 +		}
    1.33 +
    1.34 +		public string GetAllowsAddingMessage(UnitType toAdd, Army toArmy)
    1.35 +		{
    1.36 +			throw new NotImplementedException();
    1.37 +		}
    1.38 +	}
    1.39 +}
    1.40 +
     2.1 --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs	Sat Jul 16 19:59:33 2011 +0000
     2.2 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs	Sat Jul 23 19:54:37 2011 +0000
     2.3 @@ -45,7 +45,7 @@
     2.4  			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
     2.5  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
     2.6  			expectedReq.AddUnitTypeRequirement(unitType1);
     2.7 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1, raceFactory);
     2.8 +			IRequirement req = factory.CreateRequirement(unitType, typeID1, raceFactory);
     2.9  			Assert.That(req, Is.EqualTo(expectedReq));
    2.10  		}
    2.11  
    2.12 @@ -57,7 +57,7 @@
    2.13  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
    2.14  			expectedReq.AddUnitTypeRequirement(unitType1);
    2.15  			expectedReq.AddUnitTypeRequirement(unitType2);
    2.16 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2, raceFactory);
    2.17 +			IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2, raceFactory);
    2.18  			Assert.That(req, Is.EqualTo(expectedReq));
    2.19  		}
    2.20  
    2.21 @@ -68,7 +68,7 @@
    2.22  			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
    2.23  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
    2.24  			expectedReq.AddUnitTypeRequirement(unitType1, 2);
    2.25 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2", raceFactory);
    2.26 +			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2", raceFactory);
    2.27  			Assert.That(req, Is.EqualTo(expectedReq));
    2.28  		}
    2.29  
    2.30 @@ -80,7 +80,7 @@
    2.31  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
    2.32  			expectedReq.AddUnitTypeRequirement(unitType1, 2);
    2.33  			expectedReq.AddUnitTypeRequirement(unitType2, 3);
    2.34 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2 + ":3", raceFactory);
    2.35 +			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2 + ":3", raceFactory);
    2.36  			Assert.That(req, Is.EqualTo(expectedReq));
    2.37  		}
    2.38  
    2.39 @@ -92,7 +92,7 @@
    2.40  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
    2.41  			expectedReq.AddUnitTypeRequirement(unitType1, 2);
    2.42  			expectedReq.AddUnitTypeRequirement(unitType2);
    2.43 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2, raceFactory);
    2.44 +			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2, raceFactory);
    2.45  			Assert.That(req, Is.EqualTo(expectedReq));
    2.46  		}
    2.47  
    2.48 @@ -104,7 +104,7 @@
    2.49  			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
    2.50  			expectedReq.AddUnitTypeRequirement(unitType1);
    2.51  			expectedReq.AddUnitTypeRequirement(unitType2, 3);
    2.52 -			UnitRequiresAtLeastNUnitsRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2 + ":3", raceFactory);
    2.53 +			IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2 + ":3", raceFactory);
    2.54  			Assert.That(req, Is.EqualTo(expectedReq));
    2.55  		}
    2.56  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/API/MockRequirementFactory.cs	Sat Jul 23 19:54:37 2011 +0000
     3.3 @@ -0,0 +1,32 @@
     3.4 +// This file (MockRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
     3.5 +// 
     3.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.
     3.7 +using System;
     3.8 +using IBBoard.WarFoundry.API.Factories.Requirement;
     3.9 +using IBBoard.WarFoundry.API.Factories.Requirement.Mock;
    3.10 +using IBBoard.WarFoundry.API.Objects.Requirement;
    3.11 +
    3.12 +namespace IBBoard.WarFoundry.API
    3.13 +{
    3.14 +	public class MockRequirementFactory : IRequirementFactory
    3.15 +	{
    3.16 +		private string factoryID;
    3.17 +
    3.18 +		public MockRequirementFactory(string factoryID)
    3.19 +		{
    3.20 +			this.factoryID = factoryID;
    3.21 +		}
    3.22 +
    3.23 +		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE> (IBBoard.WarFoundry.API.Objects.UnitType type, string data, IBBoard.WarFoundry.API.Factories.IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    3.24 +		{
    3.25 +			throw new NotImplementedException ();
    3.26 +		}
    3.27 +
    3.28 +		public string AppliesToID {
    3.29 +			get {
    3.30 +				return factoryID;
    3.31 +			}
    3.32 +		}
    3.33 +	}
    3.34 +}
    3.35 +
     4.1 --- a/API/WarFoundryLoaderTest.cs	Sat Jul 16 19:59:33 2011 +0000
     4.2 +++ b/API/WarFoundryLoaderTest.cs	Sat Jul 23 19:54:37 2011 +0000
     4.3 @@ -10,6 +10,8 @@
     4.4  using NUnit.Framework.SyntaxHelpers;
     4.5  using System.Collections.Generic;
     4.6  using IBBoard.WarFoundry.API.Factories.Xml;
     4.7 +using IBBoard.WarFoundry.API.Factories.Requirement;
     4.8 +using IBBoard.WarFoundry.API.Objects.Requirement;
     4.9  
    4.10  namespace IBBoard.WarFoundry.API.Factories
    4.11  {
    4.12 @@ -45,6 +47,43 @@
    4.13  			ICollection<IWarFoundryObject> objs = loader.LoadFile(new FileInfo("testdata/Test.system"));
    4.14  			Assert.That(objs, Has.Count(1));
    4.15  		}
    4.16 +
    4.17 +		[Test()]
    4.18 +		public void TestRegisterRequirementFactoryAddsFactory()
    4.19 +		{
    4.20 +			string factoryID = "SomeID";
    4.21 +			MockRequirementFactory mockFactory = new MockRequirementFactory(factoryID);
    4.22 +			WarFoundryLoader.RegisterRequirementFactory(mockFactory);
    4.23 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory));
    4.24 +			Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
    4.25 +		}
    4.26 +
    4.27 +		[Test()]
    4.28 +		public void TestRegisterRequirementFactoryAddsFactories()
    4.29 +		{
    4.30 +			string factoryID1 = "SomeID";
    4.31 +			MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID1);
    4.32 +			WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
    4.33 +			string factoryID2 = "SomeOtherID";
    4.34 +			MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID2);
    4.35 +			WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
    4.36 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID1), Is.EqualTo(mockFactory1));
    4.37 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID2), Is.EqualTo(mockFactory2));
    4.38 +			Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
    4.39 +		}
    4.40 +
    4.41 +		[Test()]
    4.42 +		public void TestRegisterRequirementFactoryOverridesWithSecondFactory()
    4.43 +		{
    4.44 +			string factoryID = "SomeID";
    4.45 +			MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID);
    4.46 +			WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
    4.47 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory1));
    4.48 +			MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID);
    4.49 +			WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
    4.50 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1));
    4.51 +			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2));
    4.52 +		}
    4.53  				
    4.54  		private AbstractNativeWarFoundryFactory GetSystemFactory()
    4.55  		{
     5.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Sat Jul 16 19:59:33 2011 +0000
     5.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Sat Jul 23 19:54:37 2011 +0000
     5.3 @@ -105,6 +105,8 @@
     5.4      <Compile Include="API\Objects\Requirement\UnitCountRequirementDataTest.cs" />
     5.5      <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs" />
     5.6      <Compile Include="API\Factories\Mock\MockRaceFactory.cs" />
     5.7 +    <Compile Include="API\MockRequirementFactory.cs" />
     5.8 +    <Compile Include="API\Factories\Requirement\Mock\MockRequirement.cs" />
     5.9    </ItemGroup>
    5.10    <ItemGroup>
    5.11      <None Include="app.config" />
    5.12 @@ -365,5 +367,6 @@
    5.13      <Folder Include="API\Savers\Xml\" />
    5.14      <Folder Include="API\Objects\Requirement\Mock\" />
    5.15      <Folder Include="API\Factories\Requirement\" />
    5.16 +    <Folder Include="API\Factories\Requirement\Mock\" />
    5.17    </ItemGroup>
    5.18  </Project>
    5.19 \ No newline at end of file