changeset 36:82cd08385bfe

Fixes #228: Crash on invalid ability ID * Replace zipped Race and System files with unpackaged XML * Add helper class to create WarFoundry objects from XML files for tests * Use new helper classes to make test pass
author IBBoard <dev@ibboard.co.uk>
date Thu, 24 Dec 2009 20:14:28 +0000
parents 4302e6b2c5c1
children bf46c113d85c
files API/Factories/Xml/SingleXmlObjectLoader.cs API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs API/FixedGameSystemWarFoundryLoader.cs IBBoard.WarFoundry.API.Tests.csproj testdata/race-with-non-existant-ability.race testdata/race-with-non-existant-ability.racex testdata/race-with-non-existant-ability.system testdata/race-with-non-existant-ability.systemx
diffstat 8 files changed, 123 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Factories/Xml/SingleXmlObjectLoader.cs	Thu Dec 24 20:14:28 2009 +0000
@@ -0,0 +1,57 @@
+// This file (SingleXmlObjectLoader.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 System.IO;
+using System.Xml;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API.Factories.Xml
+{
+
+	/// <summary>
+	/// A helper class that loads a known XML file to the known type to centralise common testing code in one method
+	/// </summary>
+	public class SingleXmlObjectLoader
+	{
+		public static Race LoadRaceFromXML(WarFoundryXmlFactory factory, FileInfo file)
+		{
+			Stream stream = file.OpenRead();
+			
+			try
+			{
+				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream (stream));
+			}
+			finally 
+			{
+				if (stream !=null)
+				{
+					stream.Close();
+				}
+			}
+		}
+		
+		private static XmlElement CreateDocumentElementFromStream (Stream stream)
+		{
+			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream (stream).DocumentElement;
+		}
+		
+		public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
+		{
+			Stream stream = file.OpenRead();
+			
+			try
+			{
+				return factory.GetSystemFactory().CreateSystemFromElement(null, CreateDocumentElementFromStream(stream));
+			}
+			finally 
+			{
+				if (stream !=null)
+				{
+					stream.Close();
+				}
+			}
+		}
+	}
+}
--- a/API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs	Thu Dec 24 19:46:30 2009 +0000
+++ b/API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs	Thu Dec 24 20:14:28 2009 +0000
@@ -24,14 +24,11 @@
 		{
 			try
 			{
-				WarFoundryLoader.SetDefault(new FixedGameSystemWarFoundryLoader(new MockGameSystem()));
-				WarFoundryLoader.GetDefault().LoadFiles();
-				ICollection<IWarFoundryObject> objs = WarFoundryXmlFactory.GetFactory().CreateObjectsFromFile(new FileInfo("testdata/race-with-non-existant-ability.race"));
-				Assert.AreEqual(1, objs.Count);
-				IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
-				enumerator.Reset();
-				enumerator.MoveNext();
-				Race race = (Race)enumerator.Current;
+				GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/race-with-non-existant-ability.systemx"));
+				FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
+				WarFoundryLoader.SetDefault(fixedLoader);
+				FileInfo raceFile = new FileInfo ("testdata/race-with-non-existant-ability.racex");
+				Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
 				Category[] cats = race.Categories;
 			}
 			finally
--- a/API/FixedGameSystemWarFoundryLoader.cs	Thu Dec 24 19:46:30 2009 +0000
+++ b/API/FixedGameSystemWarFoundryLoader.cs	Thu Dec 24 20:14:28 2009 +0000
@@ -13,6 +13,7 @@
 
 		public FixedGameSystemWarFoundryLoader (GameSystem fixedSystem)
 		{
+			LoadFiles();
 			system = fixedSystem;
 		}
 		
--- a/IBBoard.WarFoundry.API.Tests.csproj	Thu Dec 24 19:46:30 2009 +0000
+++ b/IBBoard.WarFoundry.API.Tests.csproj	Thu Dec 24 20:14:28 2009 +0000
@@ -58,6 +58,7 @@
     <Compile Include="API\Util\UnitEquipmentUtilTest.cs" />
     <Compile Include="API\Factories\Xml\WarFoundryXmlRaceFactoryTest.cs" />
     <Compile Include="API\FixedGameSystemWarFoundryLoader.cs" />
+    <Compile Include="API\Factories\Xml\SingleXmlObjectLoader.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="testdata\Test.race">
@@ -67,11 +68,13 @@
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
     <None Include="COPYING" />
-    <None Include="testdata\race-with-non-existant-ability.race">
+    <None Include="testdata\race-with-non-existant-game-system.race">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
-    <None Include="testdata\race-with-non-existant-ability.system" />
-    <None Include="testdata\race-with-non-existant-game-system.race">
+    <None Include="testdata\race-with-non-existant-ability.racex">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Include="testdata\race-with-non-existant-ability.systemx">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
   </ItemGroup>
Binary file testdata/race-with-non-existant-ability.race has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/race-with-non-existant-ability.racex	Thu Dec 24 20:14:28 2009 +0000
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<race xmlns="http://ibboard.co.uk/warfoundry/race" xmlns:core="http://ibboard.co.uk/warfoundry/core" id="Empire" name="Empire" system="nonexistant">
+	<units>
+		<unit id="Empire1" typeName="Empire General" cat="cat1" points="100" maxNum="1" maxSize="1">
+			<stats>
+				<stat name="M">4</stat>
+				<stat name="WS">6</stat>
+				<stat name="BS">6</stat>
+				<stat name="S">4</stat>
+				<stat name="T">4</stat>
+				<stat name="W">3</stat>
+				<stat name="I">6</stat>
+				<stat name="A">4</stat>
+				<stat name="Ld">9</stat>
+			</stats>
+			<unitAbilities>
+				<unitAbility abilityID="leaderOfMen"/>
+			</unitAbilities>
+		</unit>
+	</units>
+	<equipment>
+		<equipmentItem id="equip1" name="sword" cost="1"/>
+		<equipmentItem id="equip2" name="shield" cost="1"/>
+		<equipmentItem id="equip3" name="broadsword" cost="2" />
+		<equipmentItem id="equip4" name="Standard Guns" cost="3" />
+		<equipmentItem id="equip5" name="Special Guns" cost="5" />
+		<equipmentItem id="equip6" name="Long Gun" cost="4" />
+		<equipmentItem id="equip7" name="Big Gun" cost="8" />
+		<equipmentItem id="equip8" name="Quick Gun" cost="6" />
+	</equipment>
+</race>
\ No newline at end of file
Binary file testdata/race-with-non-existant-ability.system has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testdata/race-with-non-existant-ability.systemx	Thu Dec 24 20:14:28 2009 +0000
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<system xmlns="http://ibboard.co.uk/warfoundry/system" xmlns:cats="http://ibboard.co.uk/warfoundry/cats" id="nonexistant" name="NonExistant Race Game System" warn="false">
+	<categories>
+		<cats:cat id="cat1" name="Characters" minPercentage="0" maxPercentage="50" />
+		<cats:cat id="cat2" name="Regiments" minPercentage="25" maxPercentage="100" />
+		<cats:cat id="cat3" name="War Machines" minPercentage="0" maxPercentage="25" />
+		<cats:cat id="cat4" name="Monsters" minPercentage="0" maxPercentage="25" />
+		<cats:cat id="cat5" name="Allies" minPercentage="0" maxPercentage="25" />
+	</categories>
+	<sysStatsList defaultStats="whDefault">
+		<sysStats id="whDefault">
+			<sysStat name="M"/>
+			<sysStat name="WS"/>
+			<sysStat name="BS"/>
+			<sysStat name="S"/>
+			<sysStat name="T"/>
+			<sysStat name="W"/>
+			<sysStat name="I"/>
+			<sysStat name="A"/>
+			<sysStat name="Ld"/>
+		</sysStats>
+	</sysStatsList>
+</system>