view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 58:c642d217036e

Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type * Simplify "non-existant ability" unit test by removing SystemX file * Remove line number checks, since Mono exception line numbers are end of file when the exception is a missing key ** * Re-implement equipment slot check test ** previous line numbers were taken from errors saying that keyref's key definition couldn't be found, not an error saying that the keyref value didn't exist
author IBBoard <dev@ibboard.co.uk>
date Sat, 12 Jun 2010 20:40:18 +0000
parents 745820ad7656
children 6efa080b500f
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 System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Schema;
using IBBoard.IO;
using IBBoard.WarFoundry.API.Objects;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace IBBoard.WarFoundry.API.Factories.Xml
{
	//It would be better to explicitly check that some of these errors are certain types of schema validation error, but Mono and .Net have
	//different exception messages for the same text and just have one class for all schema exceptions of all types.
	//We can't even check line numbers because Mono embeds them in the text and the actual line number is the end of the file.
	[TestFixture()]
	public class WarFoundryXmlRaceFactoryTest
	{
		[TearDown()]
		public void AfterTestCleanup()
		{
			WarFoundryLoader.SetDefault(null);
		}
		
		[Test()]
		public void TestCompleteLoadingOnRaceWithMissingAbilityIdErrors()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/race-with-non-existant-ability.racex");
			
			try
			{
				SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			}
			catch (InvalidFileException ex)
			{
				Console.WriteLine(ex.Message);
				Exception innerException = ex.InnerException;
				Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
				//XmlSchemaValidationException schemaEx = (XmlSchemaValidationException) innerException;
				//Assert.That(schemaEx.LineNumber, Is.EqualTo(17));
				//Assert.That(schemaEx.LinePosition, Is.EqualTo(6));
			}
		}
		
		[Test()]
		public void TestCompleteLoadingOnRaceWithIncorrectMemberTypeIDError()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-non-existant-type-referencing-race.racex");
			try
			{
				SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);			
				Assert.Fail();
			}
			catch (InvalidFileException ex)
			{
				Console.WriteLine(ex.Message);
				Exception innerException = ex.InnerException;
				Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
				//XmlSchemaValidationException schemaEx = (XmlSchemaValidationException) innerException;
				//Assert.That(schemaEx.LineNumber, Is.EqualTo(6));
				//Assert.That(schemaEx.LinePosition, Is.EqualTo(6));
			}
			
		}

		[Test()]
		public void TestCompleteLoadingOnRaceWithIncorrectEquipmentSlotErrors()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-with-invalid-equipment-reference.racex");
			
			try
			{
				SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			}
			catch (InvalidFileException ex)
			{
				Console.WriteLine(ex.Message);
				Exception innerException = ex.InnerException;
				Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
				//XmlSchemaValidationException schemaEx = (XmlSchemaValidationException) innerException;
				//Assert.That(schemaEx.LineNumber, Is.EqualTo(6));
				//Assert.That(schemaEx.LinePosition, Is.EqualTo(6));
			}
		}
		
		[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);
			Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
			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);
			Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
			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);
			Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
			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);
			Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
			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);
			Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
			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")));
		}

		[Test()]
		public void TestSingleUnitArmyWithMultipleCategoriesIsInEachCategory()
		{
			SetDefaultGameSystem();
			FileInfo raceFile = new FileInfo("testdata/single-unit-two-categories.racex");
			Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
			UnitType unitType = race.GetUnitType("Empire1");
			Assert.That(unitType.Categories.Length, Is.EqualTo(2));
			Category cat1 = race.GetCategory("cat1");
			Category cat2 = race.GetCategory("cat2");
			Assert.That(unitType.MainCategory, Is.EqualTo(cat1));
			Assert.That(unitType.Categories, Has.Member(cat1));
			Assert.That(unitType.Categories, Has.Member(cat2));
			Assert.That(race.GetUnitTypes(cat1), Is.All.EqualTo(unitType));
			UnitType[] unitTypesCat2 = race.GetUnitTypes(cat2);
			Assert.That(unitTypesCat2, Has.Length(1));
			Assert.That(unitTypesCat2, Is.All.EqualTo(unitType));
		}
		
		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);
		}
	}
}