view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 50:d3519f38a0f4

Re #270: Add multiple categories to API * Add unit tests and test data for handling multiple categories and existing method
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 May 2010 19:57:44 +0000
parents b4b4bde843d7
children 9d68b5dd70b3
line wrap: on
line source

// This file (WarFoundryXmlRaceFactoryTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 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 IBBoard.IO;
using IBBoard.WarFoundry.API.Objects;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework.SyntaxHelpers;

namespace IBBoard.WarFoundry.API.Factories.Xml
{


	[TestFixture()]
	public class WarFoundryXmlRaceFactoryTest
	{
		[TearDown()]
		public void AfterTestCleanup()
		{
			WarFoundryLoader.SetDefault(null);
		}
		
		[Test()]
		[ExpectedException(typeof(InvalidFileException), ExpectedMessage="Ability for Empire General with ID leaderOfMen did not exist in race definition")]
		public void TestCompleteLoadingOnRaceWithMissingAbilityIdErrors()
		{
			SetDefaultGameSystem(new FileInfo("testdata/race-with-non-existant-ability.systemx"));
			FileInfo raceFile = new FileInfo("testdata/race-with-non-existant-ability.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			race.EnsureFullyLoaded();
		}
		
		[Test()]
		[ExpectedException(typeof(InvalidFileException), ExpectedMessage="TBD")]
		public void TestCompleteLoadingOnRaceWithIncorrectMemberTypeIDError()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-non-existant-type-referencing-race.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			race.EnsureFullyLoaded();
		}

		[Test()]
		[ExpectedException(typeof(InvalidFileException), ExpectedMessage = "TBD")]
		public void TestCompleteLoadingOnRaceWithIncorrectEquipmentSlotErrors()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-with-invalid-equipment-reference.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			race.EnsureFullyLoaded();
		}
		
		[Test()]
		[ExpectedException(typeof(InvalidFileException), ExpectedMessage="Referenced game system, 'nonexistant-system', did not exist")]
		public void TestCompleteLoadingOnRaceWithMissingGameSystemErrors()
		{
			ICollection<IWarFoundryObject> objs = WarFoundryXmlFactory.GetFactory().CreateObjectsFromFile(new FileInfo("testdata/race-with-non-existant-game-system.race"));
			Assert.AreEqual(1, objs.Count);
			IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
			enumerator.Reset();
			enumerator.MoveNext();
			Race race = (Race)enumerator.Current;
			race.EnsureFullyLoaded();
		}
		
		[Test()]
		public void TestSingleUnitArmyLoadsSuccessfully()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-race.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Stat[] stats = unitType.UnitStatsArrays[0];
			Assert.AreEqual(9, stats.Length);
			Assert.AreEqual("M", stats[0].ParentSlotName);
			Assert.AreEqual("4", stats[0].SlotValueString);
			Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
		}
		
		[Test()]
		public void TestSingleUnitArmyWithMemberTypeReferenceLoadsSuccessfully()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-type-referencing-race.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Stat[] stats = unitType.UnitStatsArrays[0];
			Assert.AreEqual(9, stats.Length);
			Assert.AreEqual("M", stats[0].ParentSlotName);
			Assert.AreEqual("4", stats[0].SlotValueString);
			Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
		}
		
		[Test()]
		public void TestSingleUnitArmyWithMultipleMemberTypeReferencesLoadsSuccessfully()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Stat[][] stats = unitType.UnitStatsArrays;
			Assert.AreEqual(2, stats.Length);
			Assert.AreEqual("M", stats[0][0].ParentSlotName);
			Assert.AreEqual("4", stats[0][0].SlotValueString);
			Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
			Assert.AreEqual("M", stats[1][0].ParentSlotName);
			Assert.AreEqual("8", stats[1][0].SlotValueString);
			Assert.AreEqual("Warhorse", unitType.UnitStatsArraysWithName[1][0].SlotValueString);
		}
		
		[Test()]
		public void TestSingleUnitArmyWithMultipleMemberTypeReferencesAndOverrideLoadsSuccessfully()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race-with-override.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Stat[][] allStats = unitType.UnitStatsArrays;
			Stat[] stats = allStats[0];
			Assert.AreEqual(1, allStats.Length);
			Assert.AreEqual(9, stats.Length);
			Assert.AreEqual("M", stats[0].ParentSlotName);
			Assert.AreEqual("4", stats[0].SlotValueString);
			Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
		}
		
		[Test()]
		public void TestSingleUnitArmyWithNoStatsReturnsUnitWithBlankStats()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-no-stats-race.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Stat[] stats = unitType.UnitStatsArrays[0];
			Assert.That(stats.Length == race.GameSystem.StandardSystemStats.SlotCount);
			
			foreach (Stat stat in stats)
			{
				Assert.AreEqual("", stat.SlotValueString);
			}
			
			Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
		}

		[Test()]
		public void TestSingleUnitArmyWithNoCategoriesUsesMainCategory()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-no-category.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			Category category = race.GetCategory("cat1");
			UnitType[] unitTypes = race.GetUnitTypes(category);
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Assert.That(unitType.MainCategory, Is.EqualTo(category));
			Assert.That(unitType.Categories.Length, Is.EqualTo(1));
			Assert.That(unitType.Categories, Has.Member(category));
		}

		[Test()]
		public void TestSingleUnitArmyWithOneCategoryAndSameMainCategoryHasOneCategory()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-same-main-category.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			Category category = race.GetCategory("cat1");
			UnitType[] unitTypes = race.GetUnitTypes(category);
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Assert.That(unitType.MainCategory, Is.EqualTo(category));
			Assert.That(unitType.Categories.Length, Is.EqualTo(1));
			Assert.That(unitType.Categories, Has.Member(category));
		}

		[Test()]
		public void TestSingleUnitArmyWithOneCategoryAndDifferentMainCategoryHasTwoCategories()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-different-main-category.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			Category category = race.GetCategory("cat1");
			UnitType[] unitTypes = race.GetUnitTypes(category);
			Assert.AreEqual(1, unitTypes.Length);
			UnitType unitType = unitTypes[0];
			Assert.That(unitType.MainCategory, Is.EqualTo(category));
			Assert.That(unitType.Categories.Length, Is.EqualTo(2));
			Assert.That(unitType.Categories, Has.Member(category));
			Assert.That(unitType.Categories, Has.Member(race.GetCategory("cat2")));
		}
		
		private static void SetDefaultGameSystem()
		{
			SetDefaultGameSystem(new FileInfo("testdata/default.systemx"));
		}
		
		public static void SetDefaultGameSystem(FileInfo systemFile)
		{
			GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), systemFile);
			FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
			WarFoundryLoader.SetDefault(fixedLoader);
		}
	}
}