changeset 52:97ea355f9564

Re #270: Add multiple categories to API * Add tests for army loading to make sure that units go back in same category
author IBBoard <dev@ibboard.co.uk>
date Wed, 19 May 2010 20:05:52 +0000
parents 9d68b5dd70b3
children f6bbd77b5473
files API/Factories/Xml/SingleXmlObjectLoader.cs API/Factories/Xml/WarFoundryXmlArmyParserTest.cs API/FixedObjectWarFoundryLoader.cs IBBoard.WarFoundry.API.Tests.csproj testdata/unit-in-default-category.armyx testdata/unit-in-non-main-category.armyx testdata/unit-in-specified-main-category.armyx
diffstat 7 files changed, 152 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/API/Factories/Xml/SingleXmlObjectLoader.cs	Wed May 19 19:22:20 2010 +0000
     1.2 +++ b/API/Factories/Xml/SingleXmlObjectLoader.cs	Wed May 19 20:05:52 2010 +0000
     1.3 @@ -21,20 +21,25 @@
     1.4  			
     1.5  			try
     1.6  			{
     1.7 -				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream (stream));
     1.8 +				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream(stream));
     1.9  			}
    1.10 -			finally 
    1.11 +			finally
    1.12  			{
    1.13 -				if (stream !=null)
    1.14 +				if (stream != null)
    1.15  				{
    1.16  					stream.Close();
    1.17  				}
    1.18  			}
    1.19  		}
    1.20  		
    1.21 -		private static XmlElement CreateDocumentElementFromStream (Stream stream)
    1.22 +		public static XmlElement CreateDocumentElementFromFile(FileInfo file)
    1.23  		{
    1.24 -			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream (stream).DocumentElement;
    1.25 +			return CreateDocumentElementFromStream(file.OpenRead());
    1.26 +		}
    1.27 +		
    1.28 +		private static XmlElement CreateDocumentElementFromStream(Stream stream)
    1.29 +		{
    1.30 +			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream).DocumentElement;
    1.31  		}
    1.32  		
    1.33  		public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/API/Factories/Xml/WarFoundryXmlArmyParserTest.cs	Wed May 19 20:05:52 2010 +0000
     2.3 @@ -0,0 +1,74 @@
     2.4 +//  This file (WarFoundryXmlArmyParserTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2010 IBBoard
     2.5 +// 
     2.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.
     2.7 +using IBBoard.WarFoundry.API.Factories.Mock;
     2.8 +using IBBoard.WarFoundry.API.Objects;
     2.9 +using NUnit.Framework;
    2.10 +using System.IO;
    2.11 +using NUnit.Framework.SyntaxHelpers;
    2.12 +
    2.13 +namespace IBBoard.WarFoundry.API.Factories.Xml
    2.14 +{
    2.15 +	[TestFixture()]
    2.16 +	public class WarFoundryXmlArmyParserTest
    2.17 +	{
    2.18 +		[TearDown()]
    2.19 +		public void AfterTestCleanup()
    2.20 +		{
    2.21 +			WarFoundryLoader.SetDefault(null);
    2.22 +		}
    2.23 +		
    2.24 +		[Test()]
    2.25 +		public void TestUnitFromNonMainCategoryIsInCorrectCategory()
    2.26 +		{
    2.27 +			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
    2.28 +			WarFoundryLoader.SetDefault(loader);
    2.29 +			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
    2.30 +			loader.SetGameSystem(system);
    2.31 +			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
    2.32 +			loader.SetRace(race);
    2.33 +			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-non-main-category.armyx")));
    2.34 +			Army army = parser.GetArmy();
    2.35 +			Unit unit = army.GetUnits()[0];
    2.36 +			Category cat2 = army.Race.GetCategory("cat2");
    2.37 +			Assert.That(army.GetUnits(cat2), Has.Member(unit));
    2.38 +			Assert.That(unit.Category.Category, Is.EqualTo(cat2));
    2.39 +			Assert.That(army.GetUnits(army.Race.GetCategory("cat1")), Has.No.Member(unit));
    2.40 +		}
    2.41 +		
    2.42 +		[Test()]
    2.43 +		public void TestUnitFromMainCategoryIsInCorrectCategory()
    2.44 +		{
    2.45 +			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
    2.46 +			WarFoundryLoader.SetDefault(loader);
    2.47 +			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
    2.48 +			loader.SetGameSystem(system);
    2.49 +			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
    2.50 +			loader.SetRace(race);
    2.51 +			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-specified-main-category.armyx")));
    2.52 +			Army army = parser.GetArmy();
    2.53 +			Unit unit = army.GetUnits()[0];
    2.54 +			Category cat1 = army.Race.GetCategory("cat1");
    2.55 +			Assert.That(army.GetUnits(cat1), Has.Member(unit));
    2.56 +			Assert.That(unit.Category.Category, Is.EqualTo(cat1));
    2.57 +		}
    2.58 +
    2.59 +		[Test()]
    2.60 +		public void TestUnitFromDefaultCategoryIsInCorrectCategory()
    2.61 +		{
    2.62 +			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
    2.63 +			WarFoundryLoader.SetDefault(loader);
    2.64 +			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
    2.65 +			loader.SetGameSystem(system);
    2.66 +			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
    2.67 +			loader.SetRace(race);
    2.68 +			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-default-category.armyx")));
    2.69 +			Army army = parser.GetArmy();
    2.70 +			Unit unit = army.GetUnits()[0];
    2.71 +			Category cat1 = army.Race.GetCategory("cat1");
    2.72 +			Assert.That(army.GetUnits(cat1), Has.Member(unit));
    2.73 +			Assert.That(unit.Category.Category, Is.EqualTo(cat1));
    2.74 +		}
    2.75 +	}
    2.76 +}
    2.77 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/API/FixedObjectWarFoundryLoader.cs	Wed May 19 20:05:52 2010 +0000
     3.3 @@ -0,0 +1,41 @@
     3.4 +// This file (FixedGameSystemAndRaceWarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 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 +
     3.8 +using System;
     3.9 +using IBBoard.WarFoundry.API.Objects;
    3.10 +
    3.11 +namespace IBBoard.WarFoundry.API
    3.12 +{
    3.13 +	public class FixedObjectWarFoundryLoader : DefaultWarFoundryLoader
    3.14 +	{
    3.15 +		private Race race;
    3.16 +		private GameSystem system;
    3.17 +
    3.18 +		public FixedObjectWarFoundryLoader()
    3.19 +		{
    3.20 +			LoadFiles();
    3.21 +		}
    3.22 +		
    3.23 +		public void SetRace(Race fixedRace)
    3.24 +		{
    3.25 +			race = fixedRace;
    3.26 +		}
    3.27 +		
    3.28 +		public override Race GetRace(GameSystem system, string raceID)
    3.29 +		{
    3.30 +			return race;
    3.31 +		}
    3.32 +		
    3.33 +		public void SetGameSystem(GameSystem fixedGameSystem)
    3.34 +		{
    3.35 +			system = fixedGameSystem;
    3.36 +		}
    3.37 +		
    3.38 +		public override GameSystem GetGameSystem (string systemID)
    3.39 +		{
    3.40 +			return system;
    3.41 +		}
    3.42 +		
    3.43 +	}
    3.44 +}
     4.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Wed May 19 19:22:20 2010 +0000
     4.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Wed May 19 20:05:52 2010 +0000
     4.3 @@ -61,6 +61,8 @@
     4.4      <Compile Include="API\Factories\Xml\SingleXmlObjectLoader.cs" />
     4.5      <Compile Include="API\Factories\Xml\WarFoundryXmlFactoryUtilTest.cs" />
     4.6      <Compile Include="API\Objects\UnitTypeTest.cs" />
     4.7 +    <Compile Include="API\Factories\Xml\WarFoundryXmlArmyParserTest.cs" />
     4.8 +    <Compile Include="API\FixedObjectWarFoundryLoader.cs" />
     4.9    </ItemGroup>
    4.10    <ItemGroup>
    4.11      <None Include="testdata\Test.race">
    4.12 @@ -124,6 +126,13 @@
    4.13      <None Include="testdata\single-unit-two-categories.racex">
    4.14        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.15      </None>
    4.16 +    <None Include="testdata\unit-in-non-main-category.armyx" />
    4.17 +    <None Include="testdata\unit-in-default-category.armyx">
    4.18 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.19 +    </None>
    4.20 +    <None Include="testdata\unit-in-specified-main-category.armyx">
    4.21 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.22 +    </None>
    4.23    </ItemGroup>
    4.24    <ItemGroup>
    4.25      <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73">
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/testdata/unit-in-default-category.armyx	Wed May 19 20:05:52 2010 +0000
     5.3 @@ -0,0 +1,6 @@
     5.4 +<?xml version="1.0" encoding="UTF-8"?>
     5.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" id="army" name="" system="wh" race="empire" maxPoints="100">
     5.6 +	<units>
     5.7 +		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1"/>
     5.8 +	</units>
     5.9 +</army>
    5.10 \ No newline at end of file
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/testdata/unit-in-non-main-category.armyx	Wed May 19 20:05:52 2010 +0000
     6.3 @@ -0,0 +1,6 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" id="army" name="" system="wh" race="empire" maxPoints="100">
     6.6 +	<units>
     6.7 +		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1" category="cat2"/>
     6.8 +	</units>
     6.9 +</army>
    6.10 \ No newline at end of file
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/testdata/unit-in-specified-main-category.armyx	Wed May 19 20:05:52 2010 +0000
     7.3 @@ -0,0 +1,6 @@
     7.4 +<?xml version="1.0" encoding="UTF-8"?>
     7.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" id="army" name="" system="wh" race="empire" maxPoints="100">
     7.6 +	<units>
     7.7 +		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1" category="cat1"/>
     7.8 +	</units>
     7.9 +</army>
    7.10 \ No newline at end of file