view API/WarFoundryLoaderTest.cs @ 235:d122354eeebc

Re #410: N per M models in parent requirement * Add tests for adding * Change text on adding to make more sense (tell users N, M, and how many there are now)
author IBBoard <dev@ibboard.co.uk>
date Wed, 01 Aug 2012 21:01:43 +0100
parents 6ec46f3db2d4
children
line wrap: on
line source

// This file (WarFoundryLoaderTest.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 NUnit.Framework;
using IBBoard.WarFoundry.API.Factories.Mock;
using IBBoard.WarFoundry.API.Objects;
using NUnit.Framework.SyntaxHelpers;
using System.Collections.Generic;
using IBBoard.WarFoundry.API.Factories.Xml;
using IBBoard.WarFoundry.API.Factories.Requirement;
using IBBoard.WarFoundry.API.Objects.Requirement;
using IBBoard.WarFoundry.API.Loading;

namespace IBBoard.WarFoundry.API.Factories
{
	[TestFixture()]
	public class WarFoundryLoaderTest
	{
		private AbstractNativeWarFoundryFactory sysFactory;
				
		[Test()]
		public void TestLoadWithoutFactoriesCompletesWithoutError()
		{
			WarFoundryLoader.GetDefault().LoadFiles();
		}
		
		[Test()]
		public void TestLoadingSystemCompletesWithoutError()
		{
			AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault();
			DirectoryInfo dir = new DirectoryInfo("testdata");
			loader.RegisterFactory(GetSystemFactory());
			ILoadableObjectSource loadSource = new LoadableObjectSourceDirectory(dir);
			loader.AddLoadSource(loadSource);
			loader.LoadFiles();
			Assert.Greater(loader.GetGameSystems().Length, 0);
			loader.RemoveLoadSource(loadSource);
			loader.UnregisterFactory(GetSystemFactory());
		}
		
		[Test()]
		public void TestBug318LoadingSingleFileShouldNotNullRef()
		{
			DefaultWarFoundryLoader loader = new DefaultWarFoundryLoader();
			loader.RegisterFactory(WarFoundryXmlFactory.GetFactory());
			ICollection<IWarFoundryObject> objs = loader.LoadFile(new FileInfo("testdata/Test.system"));
			Assert.That(objs, Has.Count(1));
		}

		[Test()]
		public void TestRegisterRequirementFactoryAddsFactory()
		{
			string factoryID = "SomeID";
			MockRequirementFactory mockFactory = new MockRequirementFactory(factoryID);
			WarFoundryLoader.RegisterRequirementFactory(mockFactory);
			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory));
			Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
		}

		[Test()]
		public void TestRegisterRequirementFactoryAddsFactories()
		{
			string factoryID1 = "SomeID";
			MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID1);
			WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
			string factoryID2 = "SomeOtherID";
			MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID2);
			WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID1), Is.EqualTo(mockFactory1));
			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID2), Is.EqualTo(mockFactory2));
			Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
		}

		[Test()]
		public void TestRegisterRequirementFactoryOverridesWithSecondFactory()
		{
			string factoryID = "SomeID";
			MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID);
			WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
			Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory1));
			MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID);
			WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
			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()
		{
			if (sysFactory == null)
			{
				sysFactory = new MockSystemFactory();
			}
			
			return sysFactory;
		}
	}
}