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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Xml/SingleXmlObjectLoader.cs	Thu Dec 24 20:14:28 2009 +0000
     1.3 @@ -0,0 +1,57 @@
     1.4 +// This file (SingleXmlObjectLoader.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 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 +
     1.8 +using System;
     1.9 +using System.IO;
    1.10 +using System.Xml;
    1.11 +using IBBoard.WarFoundry.API.Objects;
    1.12 +
    1.13 +namespace IBBoard.WarFoundry.API.Factories.Xml
    1.14 +{
    1.15 +
    1.16 +	/// <summary>
    1.17 +	/// A helper class that loads a known XML file to the known type to centralise common testing code in one method
    1.18 +	/// </summary>
    1.19 +	public class SingleXmlObjectLoader
    1.20 +	{
    1.21 +		public static Race LoadRaceFromXML(WarFoundryXmlFactory factory, FileInfo file)
    1.22 +		{
    1.23 +			Stream stream = file.OpenRead();
    1.24 +			
    1.25 +			try
    1.26 +			{
    1.27 +				return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream (stream));
    1.28 +			}
    1.29 +			finally 
    1.30 +			{
    1.31 +				if (stream !=null)
    1.32 +				{
    1.33 +					stream.Close();
    1.34 +				}
    1.35 +			}
    1.36 +		}
    1.37 +		
    1.38 +		private static XmlElement CreateDocumentElementFromStream (Stream stream)
    1.39 +		{
    1.40 +			return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream (stream).DocumentElement;
    1.41 +		}
    1.42 +		
    1.43 +		public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
    1.44 +		{
    1.45 +			Stream stream = file.OpenRead();
    1.46 +			
    1.47 +			try
    1.48 +			{
    1.49 +				return factory.GetSystemFactory().CreateSystemFromElement(null, CreateDocumentElementFromStream(stream));
    1.50 +			}
    1.51 +			finally 
    1.52 +			{
    1.53 +				if (stream !=null)
    1.54 +				{
    1.55 +					stream.Close();
    1.56 +				}
    1.57 +			}
    1.58 +		}
    1.59 +	}
    1.60 +}
     2.1 --- a/API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs	Thu Dec 24 19:46:30 2009 +0000
     2.2 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs	Thu Dec 24 20:14:28 2009 +0000
     2.3 @@ -24,14 +24,11 @@
     2.4  		{
     2.5  			try
     2.6  			{
     2.7 -				WarFoundryLoader.SetDefault(new FixedGameSystemWarFoundryLoader(new MockGameSystem()));
     2.8 -				WarFoundryLoader.GetDefault().LoadFiles();
     2.9 -				ICollection<IWarFoundryObject> objs = WarFoundryXmlFactory.GetFactory().CreateObjectsFromFile(new FileInfo("testdata/race-with-non-existant-ability.race"));
    2.10 -				Assert.AreEqual(1, objs.Count);
    2.11 -				IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
    2.12 -				enumerator.Reset();
    2.13 -				enumerator.MoveNext();
    2.14 -				Race race = (Race)enumerator.Current;
    2.15 +				GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/race-with-non-existant-ability.systemx"));
    2.16 +				FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
    2.17 +				WarFoundryLoader.SetDefault(fixedLoader);
    2.18 +				FileInfo raceFile = new FileInfo ("testdata/race-with-non-existant-ability.racex");
    2.19 +				Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
    2.20  				Category[] cats = race.Categories;
    2.21  			}
    2.22  			finally
     3.1 --- a/API/FixedGameSystemWarFoundryLoader.cs	Thu Dec 24 19:46:30 2009 +0000
     3.2 +++ b/API/FixedGameSystemWarFoundryLoader.cs	Thu Dec 24 20:14:28 2009 +0000
     3.3 @@ -13,6 +13,7 @@
     3.4  
     3.5  		public FixedGameSystemWarFoundryLoader (GameSystem fixedSystem)
     3.6  		{
     3.7 +			LoadFiles();
     3.8  			system = fixedSystem;
     3.9  		}
    3.10  		
     4.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Thu Dec 24 19:46:30 2009 +0000
     4.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Thu Dec 24 20:14:28 2009 +0000
     4.3 @@ -58,6 +58,7 @@
     4.4      <Compile Include="API\Util\UnitEquipmentUtilTest.cs" />
     4.5      <Compile Include="API\Factories\Xml\WarFoundryXmlRaceFactoryTest.cs" />
     4.6      <Compile Include="API\FixedGameSystemWarFoundryLoader.cs" />
     4.7 +    <Compile Include="API\Factories\Xml\SingleXmlObjectLoader.cs" />
     4.8    </ItemGroup>
     4.9    <ItemGroup>
    4.10      <None Include="testdata\Test.race">
    4.11 @@ -67,11 +68,13 @@
    4.12        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.13      </None>
    4.14      <None Include="COPYING" />
    4.15 -    <None Include="testdata\race-with-non-existant-ability.race">
    4.16 +    <None Include="testdata\race-with-non-existant-game-system.race">
    4.17        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.18      </None>
    4.19 -    <None Include="testdata\race-with-non-existant-ability.system" />
    4.20 -    <None Include="testdata\race-with-non-existant-game-system.race">
    4.21 +    <None Include="testdata\race-with-non-existant-ability.racex">
    4.22 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.23 +    </None>
    4.24 +    <None Include="testdata\race-with-non-existant-ability.systemx">
    4.25        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    4.26      </None>
    4.27    </ItemGroup>
     5.1 Binary file testdata/race-with-non-existant-ability.race has changed
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/testdata/race-with-non-existant-ability.racex	Thu Dec 24 20:14:28 2009 +0000
     6.3 @@ -0,0 +1,31 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<race xmlns="http://ibboard.co.uk/warfoundry/race" xmlns:core="http://ibboard.co.uk/warfoundry/core" id="Empire" name="Empire" system="nonexistant">
     6.6 +	<units>
     6.7 +		<unit id="Empire1" typeName="Empire General" cat="cat1" points="100" maxNum="1" maxSize="1">
     6.8 +			<stats>
     6.9 +				<stat name="M">4</stat>
    6.10 +				<stat name="WS">6</stat>
    6.11 +				<stat name="BS">6</stat>
    6.12 +				<stat name="S">4</stat>
    6.13 +				<stat name="T">4</stat>
    6.14 +				<stat name="W">3</stat>
    6.15 +				<stat name="I">6</stat>
    6.16 +				<stat name="A">4</stat>
    6.17 +				<stat name="Ld">9</stat>
    6.18 +			</stats>
    6.19 +			<unitAbilities>
    6.20 +				<unitAbility abilityID="leaderOfMen"/>
    6.21 +			</unitAbilities>
    6.22 +		</unit>
    6.23 +	</units>
    6.24 +	<equipment>
    6.25 +		<equipmentItem id="equip1" name="sword" cost="1"/>
    6.26 +		<equipmentItem id="equip2" name="shield" cost="1"/>
    6.27 +		<equipmentItem id="equip3" name="broadsword" cost="2" />
    6.28 +		<equipmentItem id="equip4" name="Standard Guns" cost="3" />
    6.29 +		<equipmentItem id="equip5" name="Special Guns" cost="5" />
    6.30 +		<equipmentItem id="equip6" name="Long Gun" cost="4" />
    6.31 +		<equipmentItem id="equip7" name="Big Gun" cost="8" />
    6.32 +		<equipmentItem id="equip8" name="Quick Gun" cost="6" />
    6.33 +	</equipment>
    6.34 +</race>
    6.35 \ No newline at end of file
     7.1 Binary file testdata/race-with-non-existant-ability.system has changed
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/testdata/race-with-non-existant-ability.systemx	Thu Dec 24 20:14:28 2009 +0000
     8.3 @@ -0,0 +1,23 @@
     8.4 +<?xml version="1.0" encoding="UTF-8"?>
     8.5 +<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">
     8.6 +	<categories>
     8.7 +		<cats:cat id="cat1" name="Characters" minPercentage="0" maxPercentage="50" />
     8.8 +		<cats:cat id="cat2" name="Regiments" minPercentage="25" maxPercentage="100" />
     8.9 +		<cats:cat id="cat3" name="War Machines" minPercentage="0" maxPercentage="25" />
    8.10 +		<cats:cat id="cat4" name="Monsters" minPercentage="0" maxPercentage="25" />
    8.11 +		<cats:cat id="cat5" name="Allies" minPercentage="0" maxPercentage="25" />
    8.12 +	</categories>
    8.13 +	<sysStatsList defaultStats="whDefault">
    8.14 +		<sysStats id="whDefault">
    8.15 +			<sysStat name="M"/>
    8.16 +			<sysStat name="WS"/>
    8.17 +			<sysStat name="BS"/>
    8.18 +			<sysStat name="S"/>
    8.19 +			<sysStat name="T"/>
    8.20 +			<sysStat name="W"/>
    8.21 +			<sysStat name="I"/>
    8.22 +			<sysStat name="A"/>
    8.23 +			<sysStat name="Ld"/>
    8.24 +		</sysStats>
    8.25 +	</sysStatsList>
    8.26 +</system>