Mercurial > repos > IBBoard.WarFoundry.API.Tests
view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 153:e84149f37478
Re #351: Add extensible requirement handling method
* Test exception messages
* Test invalid numbers
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 02 Jul 2011 15:09:48 +0000 |
parents | 71b9364f0276 |
children | d0b8c43f705f |
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; using IBBoard.WarFoundry.API.Objects.Requirement; 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() { TestFileValidationFailure("testdata/xml-race-factory/non-existant-ability.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithDuplicateAbilityIdErrors() { TestFileValidationFailure("testdata/xml-race-factory/duplicate-ability.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithAbilityLoadsData() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-ability.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitType unitType = race.GetUnitType("Empire1"); Ability ability = race.GetAbility("leaderOfMen"); Assert.That(ability.Name, Is.EqualTo("Leader of Men")); Assert.That(ability.Description, Is.EqualTo("All men will follow the character and can use his leadership")); Assert.That(unitType.GetRequiredAbilities(), Has.Member(ability)); } [Test()] public void TestCompleteLoadingOnRaceWithIncorrectMemberTypeIDErrors() { TestFileValidationFailure("testdata/xml-race-factory/non-existant-member-type.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithDuplicateMemberTypeIDErrors() { TestFileValidationFailure("testdata/xml-race-factory/duplicate-member-type.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithMemberTypeLoadsData() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-member-type.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitMemberType memberType = race.GetUnitMemberType("General"); Assert.That(memberType.Name, Is.EqualTo("General")); } [Test()] public void TestCompleteLoadingOnRaceWithIncorrectEquipmentSlotErrors() { TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-slot-reference.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithDuplicateEquipmentSlotErrors() { TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-duplicate-equipment-slot.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithCorrectEquipmentSlotLoads() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-slot-reference.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitType unitType = race.GetUnitType("Empire1"); UnitEquipmentItem unitEquipmentItem = unitType.GetEquipmentItem("Empire1equip1"); Assert.That(unitEquipmentItem.SlotName, Is.EqualTo("slot1")); } [Test()] public void TestCompleteLoadingOnRaceWithNoEquipmentSlotLimitErrors() { TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-no-equipment-slot-limit.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithIncorrectEquipmentIDError() { TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-reference.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithDuplicateEquipmentIDError() { TestFileValidationFailure("testdata/xml-race-factory/duplicate-equipment-id.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithCorrectEquipmentIDLoads() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-reference.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitType unitType = race.GetUnitType("Empire1"); Assert.That(unitType.GetEquipmentItem("Empire1equip1"), Is.Not.Null); } [Test()] public void TestCompleteLoadingOnRaceWithIncorrectContainedUnitIDError() { TestFileValidationFailure("testdata/xml-race-factory/invalid-contained-unit-reference.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithCorrectContainedUnitIDLoadsData() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/valid-contained-unit-reference.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitType unitType = race.GetUnitType("Empire1"); Assert.That(unitType.ContainedUnitTypes, Has.Length(1)); } [Test()] public void TestCompleteLoadingOnRaceWithDuplicateExtraDataKeyErrors() { TestFileValidationFailure("testdata/xml-race-factory/duplicate-extra-data-key.racex"); } [Test()] public void TestCompleteLoadingOnRaceWithDifferentExtraDataKeysLoadsData() { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo("testdata/xml-race-factory/different-extra-data-key.racex"); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); UnitType unitType = race.GetUnitType("Empire1"); Assert.That(unitType.GetExtraData("data1"), Is.EqualTo("SomeStringHere")); } [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)); } [Test()] public void TestValidCategoriesLoadsCorrectly() { SetDefaultGameSystem(); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/valid-categories.racex")); Assert.That(race.Categories, Has.Length(5)); } [Test()] public void TestDuplicateCategoryIDErrors() { TestFileValidationFailure("testdata/xml-race-factory/duplicate-category-ids.racex"); } [Test()] public void TestExtendedRequirementsPassValidation() { SetDefaultGameSystem(); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/extended-requirements.racex")); Assert.That(race.Categories, Has.Length(5)); } [Test()] public void TestLoadingSimpleRequirement() { SetDefaultGameSystem(); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex")); UnitType unitType = race.GetUnitType("Empire1"); Assert.That(unitType.GetRequirements(), Has.Length(1)); } [Test()] public void TestLoadingRequirementType() { SetDefaultGameSystem(); Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex")); UnitType unitType = race.GetUnitType("Empire1"); Assert.That(unitType.GetRequirements()[0], Is.InstanceOfType(typeof(UnitRequiresAtLeastNUnitsRequirement))); } private void TestFileValidationFailure(string filePath) { SetDefaultGameSystem(); FileInfo raceFile = new FileInfo(filePath); try { SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile); Assert.Fail("Excepted exception not thrown"); } catch (InvalidFileException ex) { Console.WriteLine(ex.Message); Exception innerException = ex.InnerException; Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException))); } } 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); } } }