Mercurial > repos > IBBoard.WarFoundry.API.Tests
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 wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/Mock/MockRaceAndSystemFactory.cs Wed Jul 11 20:33:11 2012 +0100 @@ -0,0 +1,92 @@ +// This file (MockSystemFactory.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2008, 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 IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Objects; +using ICSharpCode.SharpZipLib.Zip; +using System.Collections.Generic; + +namespace IBBoard.WarFoundry.API.Factories.Mock +{ + public class MockRaceAndSystemFactory : AbstractNativeWarFoundryFactory + { + private static MockRaceAndSystemFactory mock; + + private GameSystem gameSystem; + + public static MockRaceAndSystemFactory GetMockFactory() + { + if (mock == null) + { + mock = new MockRaceAndSystemFactory(); + } + + return mock; + } + + public MockRaceAndSystemFactory() + { + gameSystem = new GameSystem("test", "Test System", this); + } + + protected override bool CheckCanFindArmyFileContent(ZipFile file) + { + return false; + } + + protected override bool CheckCanFindRaceFileContent(ZipFile file) + { + return true; + } + + protected override bool CheckCanFindSystemFileContent(ZipFile file) + { + return true; + } + + protected override Army CreateArmyFromStream(ZipFile file, Stream dataStream) + { + throw new NotImplementedException(); + } + + protected override GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream) + { + return gameSystem; + } + + protected override Race CreateRaceFromStream(ZipFile file, Stream dataStream) + { + return new Race("test", "Test Race", gameSystem, this); + } + + protected override ICollection<ZipEntry> GetArmyZipEntries(ZipFile file) + { + throw new NotImplementedException(); + } + + protected override ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file) + { + List<ZipEntry> entries = new List<ZipEntry>(); + ZipEntry zipEntry = file.GetEntry("data.systemx"); + if (zipEntry != null) + { + entries.Add(zipEntry); + } + return entries; + } + + protected override ICollection<ZipEntry> GetRaceZipEntries(ZipFile file) + { + List<ZipEntry> entries = new List<ZipEntry>(); + ZipEntry zipEntry = file.GetEntry("data.racex"); + if (zipEntry != null) + { + entries.Add(zipEntry); + } + return entries; + } + } +}
--- a/API/Factories/Xml/WarFoundryXmlFactoryTests.cs Sat Jul 07 21:02:53 2012 +0100 +++ b/API/Factories/Xml/WarFoundryXmlFactoryTests.cs Wed Jul 11 20:33:11 2012 +0100 @@ -31,8 +31,7 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } @@ -67,8 +66,7 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } @@ -103,13 +101,12 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } } - + Assert.That(raceCount, Is.EqualTo(2)); Assert.That(systemCount, Is.EqualTo(2)); } @@ -118,5 +115,30 @@ loader.UnregisterFactory(factory); } } + + [Test()] + public void TestLoadingRaceFromResource() + { + WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory(); + GameSystem system = new GameSystem("sample", "Sample System", factory); + FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system); + WarFoundryLoader.SetDefault(fixedLoader); + ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race")); + Assert.That(objs, Has.Count(1)); + IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator(); + enumerator.MoveNext(); + Assert.That(enumerator.Current, Is.TypeOf(typeof(Race))); + } + + [Test()] + public void TestLoadingSystemFromResource() + { + WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory(); + ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.SampleSystem.system")); + Assert.That(objs, Has.Count(1)); + IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator(); + enumerator.MoveNext(); + Assert.That(enumerator.Current, Is.TypeOf(typeof(GameSystem))); + } } }
--- a/API/WarFoundryLoaderTest.cs Sat Jul 07 21:02:53 2012 +0100 +++ b/API/WarFoundryLoaderTest.cs Wed Jul 11 20:33:11 2012 +0100 @@ -86,6 +86,28 @@ Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1)); Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2)); } + + [Test()] + public void TestRegisteringResourceFiles() + { + AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault(); + loader.RegisterFactory(new MockRaceAndSystemFactory()); + ILoadableObjectSource loadSource = new LoadableObjectSourceResourceSet(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race", "IBBoard.WarFoundry.data.SampleSystem.system"); + loader.AddLoadSource(loadSource); + loader.LoadFiles(); + + try + { + Assert.That(loader.GetGameSystems(), Has.Length(1)); + GameSystem gameSystem = loader.GetGameSystems()[0]; + Assert.That(loader.GetRaces(gameSystem), Has.Length(1)); + } + finally + { + loader.RemoveLoadSource(loadSource); + loader.UnregisterFactory(GetSystemFactory()); + } + } private AbstractNativeWarFoundryFactory GetSystemFactory() {
--- a/IBBoard.WarFoundry.API.Tests.csproj Sat Jul 07 21:02:53 2012 +0100 +++ b/IBBoard.WarFoundry.API.Tests.csproj Wed Jul 11 20:33:11 2012 +0100 @@ -126,6 +126,7 @@ <Compile Include="API\Commands\RemoveUnitCommandTest.cs" /> <Compile Include="API\Loading\LoadableObjectSourceResourceSetTests.cs" /> <Compile Include="API\Loading\LoadableObjectSourceDirectoryTests.cs" /> + <Compile Include="API\Factories\Mock\MockRaceAndSystemFactory.cs" /> </ItemGroup> <ItemGroup> <None Include="app.config" />