view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 47:b4b4bde843d7

Re #274: crash when missing unitMember id * Tests and test data for unit member crash. Schema should be able to validate using key/keyref, but can't get it working at the moment.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 May 2010 18:40:27 +0000
parents 04d7cd276b1d
children d3519f38a0f4
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;

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);
		}
		
		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);
		}
	}
}