view API/WarFoundryLoaderTest.cs @ 185:ba9af0ce916a

Fixes #366: Rewrite equipment handling and limits * Add on more test to make sure that things behave as expected
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Nov 2011 20:14:36 +0000
parents 17c5030485dc
children 7c21ca1482cb
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;

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());
			loader.AddLoadDirectory(dir);
			loader.LoadFiles();
			Assert.Greater(loader.GetGameSystems().Length, 0);
			loader.RemoveLoadDirectory(dir);
			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));
		}
				
		private AbstractNativeWarFoundryFactory GetSystemFactory()
		{
			if (sysFactory == null)
			{
				sysFactory = new MockSystemFactory();
			}
			
			return sysFactory;
		}
	}
}