view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 158:d0b8c43f705f

Fixes #351: Add extensible requirement handling method * Register requirement factory for the tests
author IBBoard <dev@ibboard.co.uk>
date Sat, 23 Jul 2011 20:01:54 +0000
parents 71b9364f0276
children 2b27447ae74e
line source
1 // This file (WarFoundryXmlRaceFactoryTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 IBBoard
2 //
3 // 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.
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml.Schema;
9 using IBBoard.IO;
10 using IBBoard.WarFoundry.API.Objects;
11 using NUnit.Framework;
12 using NUnit.Framework.SyntaxHelpers;
13 using IBBoard.WarFoundry.API.Objects.Requirement;
14 using IBBoard.WarFoundry.API.Factories.Requirement;
16 namespace IBBoard.WarFoundry.API.Factories.Xml
17 {
18 //It would be better to explicitly check that some of these errors are certain types of schema validation error, but Mono and .Net have
19 //different exception messages for the same text and just have one class for all schema exceptions of all types.
20 //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.
21 [TestFixture()]
22 public class WarFoundryXmlRaceFactoryTest
23 {
24 [SetUp()]
25 public void RegisterRequirementHandler()
26 {
27 WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresAtLeastNUnitsRequirementFactory());
28 }
30 [TearDown()]
31 public void AfterTestCleanup()
32 {
33 WarFoundryLoader.SetDefault(null);
34 }
36 [Test()]
37 public void TestCompleteLoadingOnRaceWithMissingAbilityIdErrors()
38 {
39 TestFileValidationFailure("testdata/xml-race-factory/non-existant-ability.racex");
40 }
42 [Test()]
43 public void TestCompleteLoadingOnRaceWithDuplicateAbilityIdErrors()
44 {
45 TestFileValidationFailure("testdata/xml-race-factory/duplicate-ability.racex");
46 }
48 [Test()]
49 public void TestCompleteLoadingOnRaceWithAbilityLoadsData()
50 {
51 SetDefaultGameSystem();
52 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-ability.racex");
53 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
54 UnitType unitType = race.GetUnitType("Empire1");
55 Ability ability = race.GetAbility("leaderOfMen");
56 Assert.That(ability.Name, Is.EqualTo("Leader of Men"));
57 Assert.That(ability.Description, Is.EqualTo("All men will follow the character and can use his leadership"));
58 Assert.That(unitType.GetRequiredAbilities(), Has.Member(ability));
59 }
61 [Test()]
62 public void TestCompleteLoadingOnRaceWithIncorrectMemberTypeIDErrors()
63 {
64 TestFileValidationFailure("testdata/xml-race-factory/non-existant-member-type.racex");
65 }
67 [Test()]
68 public void TestCompleteLoadingOnRaceWithDuplicateMemberTypeIDErrors()
69 {
70 TestFileValidationFailure("testdata/xml-race-factory/duplicate-member-type.racex");
71 }
73 [Test()]
74 public void TestCompleteLoadingOnRaceWithMemberTypeLoadsData()
75 {
76 SetDefaultGameSystem();
77 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-member-type.racex");
78 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
79 UnitMemberType memberType = race.GetUnitMemberType("General");
80 Assert.That(memberType.Name, Is.EqualTo("General"));
81 }
83 [Test()]
84 public void TestCompleteLoadingOnRaceWithIncorrectEquipmentSlotErrors()
85 {
86 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-slot-reference.racex");
87 }
89 [Test()]
90 public void TestCompleteLoadingOnRaceWithDuplicateEquipmentSlotErrors()
91 {
92 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-duplicate-equipment-slot.racex");
93 }
95 [Test()]
96 public void TestCompleteLoadingOnRaceWithCorrectEquipmentSlotLoads()
97 {
98 SetDefaultGameSystem();
99 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-slot-reference.racex");
100 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
101 UnitType unitType = race.GetUnitType("Empire1");
102 UnitEquipmentItem unitEquipmentItem = unitType.GetEquipmentItem("Empire1equip1");
103 Assert.That(unitEquipmentItem.SlotName, Is.EqualTo("slot1"));
104 }
106 [Test()]
107 public void TestCompleteLoadingOnRaceWithNoEquipmentSlotLimitErrors()
108 {
109 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-no-equipment-slot-limit.racex");
110 }
112 [Test()]
113 public void TestCompleteLoadingOnRaceWithIncorrectEquipmentIDError()
114 {
115 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-reference.racex");
116 }
118 [Test()]
119 public void TestCompleteLoadingOnRaceWithDuplicateEquipmentIDError()
120 {
121 TestFileValidationFailure("testdata/xml-race-factory/duplicate-equipment-id.racex");
122 }
124 [Test()]
125 public void TestCompleteLoadingOnRaceWithCorrectEquipmentIDLoads()
126 {
127 SetDefaultGameSystem();
128 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-reference.racex");
129 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
130 UnitType unitType = race.GetUnitType("Empire1");
131 Assert.That(unitType.GetEquipmentItem("Empire1equip1"), Is.Not.Null);
132 }
134 [Test()]
135 public void TestCompleteLoadingOnRaceWithIncorrectContainedUnitIDError()
136 {
137 TestFileValidationFailure("testdata/xml-race-factory/invalid-contained-unit-reference.racex");
138 }
140 [Test()]
141 public void TestCompleteLoadingOnRaceWithCorrectContainedUnitIDLoadsData()
142 {
143 SetDefaultGameSystem();
144 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/valid-contained-unit-reference.racex");
145 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
146 UnitType unitType = race.GetUnitType("Empire1");
147 Assert.That(unitType.ContainedUnitTypes, Has.Length(1));
148 }
150 [Test()]
151 public void TestCompleteLoadingOnRaceWithDuplicateExtraDataKeyErrors()
152 {
153 TestFileValidationFailure("testdata/xml-race-factory/duplicate-extra-data-key.racex");
154 }
156 [Test()]
157 public void TestCompleteLoadingOnRaceWithDifferentExtraDataKeysLoadsData()
158 {
159 SetDefaultGameSystem();
160 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/different-extra-data-key.racex");
161 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
162 UnitType unitType = race.GetUnitType("Empire1");
163 Assert.That(unitType.GetExtraData("data1"), Is.EqualTo("SomeStringHere"));
164 }
166 [Test()]
167 [ExpectedException(typeof(InvalidFileException), ExpectedMessage="Referenced game system, 'nonexistant-system', did not exist")]
168 public void TestCompleteLoadingOnRaceWithMissingGameSystemErrors()
169 {
170 ICollection<IWarFoundryObject> objs = WarFoundryXmlFactory.GetFactory().CreateObjectsFromFile(new FileInfo("testdata/race-with-non-existant-game-system.race"));
171 Assert.AreEqual(1, objs.Count);
172 IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
173 enumerator.Reset();
174 enumerator.MoveNext();
175 Race race = (Race)enumerator.Current;
176 race.EnsureFullyLoaded();
177 }
179 [Test()]
180 public void TestSingleUnitArmyLoadsSuccessfully()
181 {
182 SetDefaultGameSystem();
183 FileInfo raceFile = new FileInfo("testdata/single-unit-race.racex");
184 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
185 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
186 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
187 Assert.AreEqual(1, unitTypes.Length);
188 UnitType unitType = unitTypes[0];
189 Stat[] stats = unitType.UnitStatsArrays[0];
190 Assert.AreEqual(9, stats.Length);
191 Assert.AreEqual("M", stats[0].ParentSlotName);
192 Assert.AreEqual("4", stats[0].SlotValueString);
193 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
194 }
196 [Test()]
197 public void TestSingleUnitArmyWithMemberTypeReferenceLoadsSuccessfully()
198 {
199 SetDefaultGameSystem();
200 FileInfo raceFile = new FileInfo("testdata/single-unit-type-referencing-race.racex");
201 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
202 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
203 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
204 Assert.AreEqual(1, unitTypes.Length);
205 UnitType unitType = unitTypes[0];
206 Stat[] stats = unitType.UnitStatsArrays[0];
207 Assert.AreEqual(9, stats.Length);
208 Assert.AreEqual("M", stats[0].ParentSlotName);
209 Assert.AreEqual("4", stats[0].SlotValueString);
210 Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
211 }
213 [Test()]
214 public void TestSingleUnitArmyWithMultipleMemberTypeReferencesLoadsSuccessfully()
215 {
216 SetDefaultGameSystem();
217 FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race.racex");
218 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
219 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
220 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
221 Assert.AreEqual(1, unitTypes.Length);
222 UnitType unitType = unitTypes[0];
223 Stat[][] stats = unitType.UnitStatsArrays;
224 Assert.AreEqual(2, stats.Length);
225 Assert.AreEqual("M", stats[0][0].ParentSlotName);
226 Assert.AreEqual("4", stats[0][0].SlotValueString);
227 Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
228 Assert.AreEqual("M", stats[1][0].ParentSlotName);
229 Assert.AreEqual("8", stats[1][0].SlotValueString);
230 Assert.AreEqual("Warhorse", unitType.UnitStatsArraysWithName[1][0].SlotValueString);
231 }
233 [Test()]
234 public void TestSingleUnitArmyWithMultipleMemberTypeReferencesAndOverrideLoadsSuccessfully()
235 {
236 SetDefaultGameSystem();
237 FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race-with-override.racex");
238 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
239 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
240 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
241 Assert.AreEqual(1, unitTypes.Length);
242 UnitType unitType = unitTypes[0];
243 Stat[][] allStats = unitType.UnitStatsArrays;
244 Stat[] stats = allStats[0];
245 Assert.AreEqual(1, allStats.Length);
246 Assert.AreEqual(9, stats.Length);
247 Assert.AreEqual("M", stats[0].ParentSlotName);
248 Assert.AreEqual("4", stats[0].SlotValueString);
249 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
250 }
252 [Test()]
253 public void TestSingleUnitArmyWithNoStatsReturnsUnitWithBlankStats()
254 {
255 SetDefaultGameSystem();
256 FileInfo raceFile = new FileInfo("testdata/single-unit-no-stats-race.racex");
257 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
258 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
259 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
260 Assert.AreEqual(1, unitTypes.Length);
261 UnitType unitType = unitTypes[0];
262 Stat[] stats = unitType.UnitStatsArrays[0];
263 Assert.That(stats.Length == race.GameSystem.StandardSystemStats.SlotCount);
265 foreach (Stat stat in stats)
266 {
267 Assert.AreEqual("", stat.SlotValueString);
268 }
270 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
271 }
273 [Test()]
274 public void TestSingleUnitArmyWithNoCategoriesUsesMainCategory()
275 {
276 SetDefaultGameSystem();
277 FileInfo raceFile = new FileInfo("testdata/single-unit-no-category.racex");
278 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
279 Category category = race.GetCategory("cat1");
280 UnitType[] unitTypes = race.GetUnitTypes(category);
281 Assert.AreEqual(1, unitTypes.Length);
282 UnitType unitType = unitTypes[0];
283 Assert.That(unitType.MainCategory, Is.EqualTo(category));
284 Assert.That(unitType.Categories.Length, Is.EqualTo(1));
285 Assert.That(unitType.Categories, Has.Member(category));
286 }
288 [Test()]
289 public void TestSingleUnitArmyWithOneCategoryAndSameMainCategoryHasOneCategory()
290 {
291 SetDefaultGameSystem();
292 FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-same-main-category.racex");
293 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
294 Category category = race.GetCategory("cat1");
295 UnitType[] unitTypes = race.GetUnitTypes(category);
296 Assert.AreEqual(1, unitTypes.Length);
297 UnitType unitType = unitTypes[0];
298 Assert.That(unitType.MainCategory, Is.EqualTo(category));
299 Assert.That(unitType.Categories.Length, Is.EqualTo(1));
300 Assert.That(unitType.Categories, Has.Member(category));
301 }
303 [Test()]
304 public void TestSingleUnitArmyWithOneCategoryAndDifferentMainCategoryHasTwoCategories()
305 {
306 SetDefaultGameSystem();
307 FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-different-main-category.racex");
308 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
309 Category category = race.GetCategory("cat1");
310 UnitType[] unitTypes = race.GetUnitTypes(category);
311 Assert.AreEqual(1, unitTypes.Length);
312 UnitType unitType = unitTypes[0];
313 Assert.That(unitType.MainCategory, Is.EqualTo(category));
314 Assert.That(unitType.Categories.Length, Is.EqualTo(2));
315 Assert.That(unitType.Categories, Has.Member(category));
316 Assert.That(unitType.Categories, Has.Member(race.GetCategory("cat2")));
317 }
319 [Test()]
320 public void TestSingleUnitArmyWithMultipleCategoriesIsInEachCategory()
321 {
322 SetDefaultGameSystem();
323 FileInfo raceFile = new FileInfo("testdata/single-unit-two-categories.racex");
324 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
325 UnitType unitType = race.GetUnitType("Empire1");
326 Assert.That(unitType.Categories.Length, Is.EqualTo(2));
327 Category cat1 = race.GetCategory("cat1");
328 Category cat2 = race.GetCategory("cat2");
329 Assert.That(unitType.MainCategory, Is.EqualTo(cat1));
330 Assert.That(unitType.Categories, Has.Member(cat1));
331 Assert.That(unitType.Categories, Has.Member(cat2));
332 Assert.That(race.GetUnitTypes(cat1), Is.All.EqualTo(unitType));
333 UnitType[] unitTypesCat2 = race.GetUnitTypes(cat2);
334 Assert.That(unitTypesCat2, Has.Length(1));
335 Assert.That(unitTypesCat2, Is.All.EqualTo(unitType));
336 }
338 [Test()]
339 public void TestValidCategoriesLoadsCorrectly()
340 {
341 SetDefaultGameSystem();
342 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/valid-categories.racex"));
343 Assert.That(race.Categories, Has.Length(5));
344 }
346 [Test()]
347 public void TestDuplicateCategoryIDErrors()
348 {
349 TestFileValidationFailure("testdata/xml-race-factory/duplicate-category-ids.racex");
350 }
352 [Test()]
353 public void TestExtendedRequirementsPassValidation()
354 {
355 SetDefaultGameSystem();
356 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/extended-requirements.racex"));
357 Assert.That(race.Categories, Has.Length(5));
358 }
360 [Test()]
361 public void TestLoadingSimpleRequirement()
362 {
363 SetDefaultGameSystem();
364 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex"));
365 UnitType unitType = race.GetUnitType("Empire1");
366 Assert.That(unitType.GetRequirements(), Has.Length(1));
367 }
369 [Test()]
370 public void TestLoadingRequirementType()
371 {
372 SetDefaultGameSystem();
373 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex"));
374 UnitType unitType = race.GetUnitType("Empire1");
375 Assert.That(unitType.GetRequirements()[0], Is.InstanceOfType(typeof(UnitRequiresAtLeastNUnitsRequirement)));
376 }
378 private void TestFileValidationFailure(string filePath)
379 {
380 SetDefaultGameSystem();
381 FileInfo raceFile = new FileInfo(filePath);
383 try
384 {
385 SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
386 Assert.Fail("Excepted exception not thrown");
387 }
388 catch (InvalidFileException ex)
389 {
390 Console.WriteLine(ex.Message);
391 Exception innerException = ex.InnerException;
392 Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
393 }
394 }
396 private static void SetDefaultGameSystem()
397 {
398 SetDefaultGameSystem(new FileInfo("testdata/default.systemx"));
399 }
401 public static void SetDefaultGameSystem(FileInfo systemFile)
402 {
403 GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), systemFile);
404 FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
405 WarFoundryLoader.SetDefault(fixedLoader);
406 }
407 }
408 }