changeset 229:6ec46f3db2d4

Re #419: Remove assumptions of a file-based install * Test loading from resources
author IBBoard <dev@ibboard.co.uk>
date Wed, 11 Jul 2012 20:33:11 +0100
parents 7c21ca1482cb
children d6883a522c70
files API/Factories/Mock/MockRaceAndSystemFactory.cs API/Factories/Xml/WarFoundryXmlFactoryTests.cs API/WarFoundryLoaderTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 4 files changed, 144 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Mock/MockRaceAndSystemFactory.cs	Wed Jul 11 20:33:11 2012 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +// This file (MockSystemFactory.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2008, 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 IBBoard.WarFoundry.API.Factories;
    1.11 +using IBBoard.WarFoundry.API.Objects;
    1.12 +using ICSharpCode.SharpZipLib.Zip;
    1.13 +using System.Collections.Generic;
    1.14 +
    1.15 +namespace IBBoard.WarFoundry.API.Factories.Mock
    1.16 +{
    1.17 +	public class MockRaceAndSystemFactory : AbstractNativeWarFoundryFactory
    1.18 +	{
    1.19 +		private static MockRaceAndSystemFactory mock;
    1.20 +
    1.21 +		private GameSystem gameSystem;
    1.22 +		
    1.23 +		public static MockRaceAndSystemFactory GetMockFactory()
    1.24 +		{
    1.25 +			if (mock == null)
    1.26 +			{
    1.27 +				mock = new MockRaceAndSystemFactory();
    1.28 +			}
    1.29 +			
    1.30 +			return mock;
    1.31 +		}
    1.32 +		
    1.33 +		public MockRaceAndSystemFactory()
    1.34 +		{
    1.35 +			gameSystem = new GameSystem("test", "Test System", this);
    1.36 +		}
    1.37 +		
    1.38 +		protected override bool CheckCanFindArmyFileContent(ZipFile file)
    1.39 +		{
    1.40 +			return false;
    1.41 +		}
    1.42 +		
    1.43 +		protected override bool CheckCanFindRaceFileContent(ZipFile file)
    1.44 +		{
    1.45 +			return true;
    1.46 +		}
    1.47 +		
    1.48 +		protected override bool CheckCanFindSystemFileContent(ZipFile file)
    1.49 +		{
    1.50 +			return true;
    1.51 +		}
    1.52 +		
    1.53 +		protected override Army CreateArmyFromStream(ZipFile file, Stream dataStream)
    1.54 +		{
    1.55 +			throw new NotImplementedException();
    1.56 +		}
    1.57 +		
    1.58 +		protected override GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream)
    1.59 +		{
    1.60 +			return gameSystem;
    1.61 +		}
    1.62 +		
    1.63 +		protected override Race CreateRaceFromStream(ZipFile file, Stream dataStream)
    1.64 +		{
    1.65 +			return new Race("test", "Test Race", gameSystem, this);
    1.66 +		}
    1.67 +		
    1.68 +		protected override ICollection<ZipEntry> GetArmyZipEntries(ZipFile file)
    1.69 +		{
    1.70 +			throw new NotImplementedException();
    1.71 +		}
    1.72 +		
    1.73 +		protected override ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file)
    1.74 +		{
    1.75 +			List<ZipEntry> entries = new List<ZipEntry>();
    1.76 +			ZipEntry zipEntry = file.GetEntry("data.systemx");
    1.77 +			if (zipEntry != null)
    1.78 +			{
    1.79 +				entries.Add(zipEntry);
    1.80 +			}
    1.81 +			return entries;
    1.82 +		}
    1.83 +
    1.84 +		protected override ICollection<ZipEntry> GetRaceZipEntries(ZipFile file)
    1.85 +		{
    1.86 +			List<ZipEntry> entries = new List<ZipEntry>();
    1.87 +			ZipEntry zipEntry = file.GetEntry("data.racex");
    1.88 +			if (zipEntry != null)
    1.89 +			{
    1.90 +				entries.Add(zipEntry);
    1.91 +			}
    1.92 +			return entries;
    1.93 +		}
    1.94 +	}
    1.95 +}
     2.1 --- a/API/Factories/Xml/WarFoundryXmlFactoryTests.cs	Sat Jul 07 21:02:53 2012 +0100
     2.2 +++ b/API/Factories/Xml/WarFoundryXmlFactoryTests.cs	Wed Jul 11 20:33:11 2012 +0100
     2.3 @@ -31,8 +31,7 @@
     2.4  					{
     2.5  						raceCount++;
     2.6  					}
     2.7 -					else
     2.8 -					if (obj is GameSystem)
     2.9 +					else if (obj is GameSystem)
    2.10  					{
    2.11  						systemCount++;
    2.12  					}
    2.13 @@ -67,8 +66,7 @@
    2.14  					{
    2.15  						raceCount++;
    2.16  					}
    2.17 -					else
    2.18 -					if (obj is GameSystem)
    2.19 +					else if (obj is GameSystem)
    2.20  					{
    2.21  						systemCount++;
    2.22  					}
    2.23 @@ -103,13 +101,12 @@
    2.24  					{
    2.25  						raceCount++;
    2.26  					}
    2.27 -					else
    2.28 -					if (obj is GameSystem)
    2.29 +					else if (obj is GameSystem)
    2.30  					{
    2.31  						systemCount++;
    2.32  					}
    2.33  				}
    2.34 -				
    2.35 +
    2.36  				Assert.That(raceCount, Is.EqualTo(2));
    2.37  				Assert.That(systemCount, Is.EqualTo(2));
    2.38  			}
    2.39 @@ -118,5 +115,30 @@
    2.40  				loader.UnregisterFactory(factory);
    2.41  			}
    2.42  		}
    2.43 +		
    2.44 +		[Test()]
    2.45 +		public void TestLoadingRaceFromResource()
    2.46 +		{
    2.47 +			WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory();
    2.48 +			GameSystem system = new GameSystem("sample", "Sample System", factory);
    2.49 +			FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
    2.50 +			WarFoundryLoader.SetDefault(fixedLoader);
    2.51 +			ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race"));
    2.52 +			Assert.That(objs, Has.Count(1));
    2.53 +			IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
    2.54 +			enumerator.MoveNext();
    2.55 +			Assert.That(enumerator.Current, Is.TypeOf(typeof(Race)));
    2.56 +		}
    2.57 +		
    2.58 +		[Test()]
    2.59 +		public void TestLoadingSystemFromResource()
    2.60 +		{
    2.61 +			WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory();
    2.62 +			ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.SampleSystem.system"));
    2.63 +			Assert.That(objs, Has.Count(1));
    2.64 +			IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
    2.65 +			enumerator.MoveNext();
    2.66 +			Assert.That(enumerator.Current, Is.TypeOf(typeof(GameSystem)));
    2.67 +		}
    2.68  	}
    2.69  }
     3.1 --- a/API/WarFoundryLoaderTest.cs	Sat Jul 07 21:02:53 2012 +0100
     3.2 +++ b/API/WarFoundryLoaderTest.cs	Wed Jul 11 20:33:11 2012 +0100
     3.3 @@ -86,6 +86,28 @@
     3.4  			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1));
     3.5  			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2));
     3.6  		}
     3.7 +
     3.8 +		[Test()]
     3.9 +		public void TestRegisteringResourceFiles()
    3.10 +		{
    3.11 +			AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault();
    3.12 +			loader.RegisterFactory(new MockRaceAndSystemFactory());
    3.13 +			ILoadableObjectSource loadSource = new LoadableObjectSourceResourceSet(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race", "IBBoard.WarFoundry.data.SampleSystem.system");
    3.14 +			loader.AddLoadSource(loadSource);
    3.15 +			loader.LoadFiles();
    3.16 +
    3.17 +			try
    3.18 +			{
    3.19 +				Assert.That(loader.GetGameSystems(), Has.Length(1));
    3.20 +				GameSystem gameSystem = loader.GetGameSystems()[0];
    3.21 +				Assert.That(loader.GetRaces(gameSystem), Has.Length(1));
    3.22 +			}
    3.23 +			finally
    3.24 +			{
    3.25 +				loader.RemoveLoadSource(loadSource);
    3.26 +				loader.UnregisterFactory(GetSystemFactory());
    3.27 +			}
    3.28 +		}
    3.29  				
    3.30  		private AbstractNativeWarFoundryFactory GetSystemFactory()
    3.31  		{
     4.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Sat Jul 07 21:02:53 2012 +0100
     4.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Wed Jul 11 20:33:11 2012 +0100
     4.3 @@ -126,6 +126,7 @@
     4.4      <Compile Include="API\Commands\RemoveUnitCommandTest.cs" />
     4.5      <Compile Include="API\Loading\LoadableObjectSourceResourceSetTests.cs" />
     4.6      <Compile Include="API\Loading\LoadableObjectSourceDirectoryTests.cs" />
     4.7 +    <Compile Include="API\Factories\Mock\MockRaceAndSystemFactory.cs" />
     4.8    </ItemGroup>
     4.9    <ItemGroup>
    4.10      <None Include="app.config" />