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 wrap: on
line diff
--- a/API/Factories/Xml/SingleXmlObjectLoader.cs	Wed May 19 19:22:20 2010 +0000
+++ b/API/Factories/Xml/SingleXmlObjectLoader.cs	Wed May 19 20:05:52 2010 +0000
@@ -21,20 +21,25 @@
 			
 			try
 			{
-				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream (stream));
+				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream(stream));
 			}
-			finally 
+			finally
 			{
-				if (stream !=null)
+				if (stream != null)
 				{
 					stream.Close();
 				}
 			}
 		}
 		
-		private static XmlElement CreateDocumentElementFromStream (Stream stream)
+		public static XmlElement CreateDocumentElementFromFile(FileInfo file)
 		{
-			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream (stream).DocumentElement;
+			return CreateDocumentElementFromStream(file.OpenRead());
+		}
+		
+		private static XmlElement CreateDocumentElementFromStream(Stream stream)
+		{
+			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream).DocumentElement;
 		}
 		
 		public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Factories/Xml/WarFoundryXmlArmyParserTest.cs	Wed May 19 20:05:52 2010 +0000
@@ -0,0 +1,74 @@
+//  This file (WarFoundryXmlArmyParserTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2010 IBBoard
+// 
+//  // 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.
+using IBBoard.WarFoundry.API.Factories.Mock;
+using IBBoard.WarFoundry.API.Objects;
+using NUnit.Framework;
+using System.IO;
+using NUnit.Framework.SyntaxHelpers;
+
+namespace IBBoard.WarFoundry.API.Factories.Xml
+{
+	[TestFixture()]
+	public class WarFoundryXmlArmyParserTest
+	{
+		[TearDown()]
+		public void AfterTestCleanup()
+		{
+			WarFoundryLoader.SetDefault(null);
+		}
+		
+		[Test()]
+		public void TestUnitFromNonMainCategoryIsInCorrectCategory()
+		{
+			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
+			WarFoundryLoader.SetDefault(loader);
+			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
+			loader.SetGameSystem(system);
+			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
+			loader.SetRace(race);
+			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-non-main-category.armyx")));
+			Army army = parser.GetArmy();
+			Unit unit = army.GetUnits()[0];
+			Category cat2 = army.Race.GetCategory("cat2");
+			Assert.That(army.GetUnits(cat2), Has.Member(unit));
+			Assert.That(unit.Category.Category, Is.EqualTo(cat2));
+			Assert.That(army.GetUnits(army.Race.GetCategory("cat1")), Has.No.Member(unit));
+		}
+		
+		[Test()]
+		public void TestUnitFromMainCategoryIsInCorrectCategory()
+		{
+			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
+			WarFoundryLoader.SetDefault(loader);
+			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
+			loader.SetGameSystem(system);
+			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
+			loader.SetRace(race);
+			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-specified-main-category.armyx")));
+			Army army = parser.GetArmy();
+			Unit unit = army.GetUnits()[0];
+			Category cat1 = army.Race.GetCategory("cat1");
+			Assert.That(army.GetUnits(cat1), Has.Member(unit));
+			Assert.That(unit.Category.Category, Is.EqualTo(cat1));
+		}
+
+		[Test()]
+		public void TestUnitFromDefaultCategoryIsInCorrectCategory()
+		{
+			FixedObjectWarFoundryLoader loader = new FixedObjectWarFoundryLoader();
+			WarFoundryLoader.SetDefault(loader);
+			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/default.systemx"));
+			loader.SetGameSystem(system);
+			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/single-unit-two-categories.racex"));
+			loader.SetRace(race);
+			WarFoundryXmlArmyParser parser = new WarFoundryXmlArmyParser(null, SingleXmlObjectLoader.CreateDocumentElementFromFile(new FileInfo("testdata/unit-in-default-category.armyx")));
+			Army army = parser.GetArmy();
+			Unit unit = army.GetUnits()[0];
+			Category cat1 = army.Race.GetCategory("cat1");
+			Assert.That(army.GetUnits(cat1), Has.Member(unit));
+			Assert.That(unit.Category.Category, Is.EqualTo(cat1));
+		}
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/FixedObjectWarFoundryLoader.cs	Wed May 19 20:05:52 2010 +0000
@@ -0,0 +1,41 @@
+// This file (FixedGameSystemAndRaceWarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 IBBoard
+// 
+// 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.
+
+using System;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API
+{
+	public class FixedObjectWarFoundryLoader : DefaultWarFoundryLoader
+	{
+		private Race race;
+		private GameSystem system;
+
+		public FixedObjectWarFoundryLoader()
+		{
+			LoadFiles();
+		}
+		
+		public void SetRace(Race fixedRace)
+		{
+			race = fixedRace;
+		}
+		
+		public override Race GetRace(GameSystem system, string raceID)
+		{
+			return race;
+		}
+		
+		public void SetGameSystem(GameSystem fixedGameSystem)
+		{
+			system = fixedGameSystem;
+		}
+		
+		public override GameSystem GetGameSystem (string systemID)
+		{
+			return system;
+		}
+		
+	}
+}
--- a/IBBoard.WarFoundry.API.Tests.csproj	Wed May 19 19:22:20 2010 +0000
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Wed May 19 20:05:52 2010 +0000
@@ -61,6 +61,8 @@
     <Compile Include="API\Factories\Xml\SingleXmlObjectLoader.cs" />
     <Compile Include="API\Factories\Xml\WarFoundryXmlFactoryUtilTest.cs" />
     <Compile Include="API\Objects\UnitTypeTest.cs" />
+    <Compile Include="API\Factories\Xml\WarFoundryXmlArmyParserTest.cs" />
+    <Compile Include="API\FixedObjectWarFoundryLoader.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="testdata\Test.race">
@@ -124,6 +126,13 @@
     <None Include="testdata\single-unit-two-categories.racex">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Include="testdata\unit-in-non-main-category.armyx" />
+    <None Include="testdata\unit-in-default-category.armyx">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Include="testdata\unit-in-specified-main-category.armyx">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/unit-in-default-category.armyx	Wed May 19 20:05:52 2010 +0000
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+	<units>
+		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1"/>
+	</units>
+</army>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/unit-in-non-main-category.armyx	Wed May 19 20:05:52 2010 +0000
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+	<units>
+		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1" category="cat2"/>
+	</units>
+</army>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/unit-in-specified-main-category.armyx	Wed May 19 20:05:52 2010 +0000
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+	<units>
+		<unit id="unit1" unitType="Empire1" unitName="General Bob" size="1" category="cat1"/>
+	</units>
+</army>
\ No newline at end of file