view API/Factories/Xml/WarFoundryXmlRaceFactoryTest.cs @ 228:7c21ca1482cb

Re #419: Remove assumptions of a file-based install * Update tests to use new "loadable object" and "loadable object source" wrappers
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Jul 2012 21:02:53 +0100
parents c6313a2c08a2
children 833f72be715a
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;
15 using IBBoard.WarFoundry.API.Loading;
17 namespace IBBoard.WarFoundry.API.Factories.Xml
18 {
19 //It would be better to explicitly check that some of these errors are certain types of schema validation error, but Mono and .Net have
20 //different exception messages for the same text and just have one class for all schema exceptions of all types.
21 //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.
22 [TestFixture()]
23 public class WarFoundryXmlRaceFactoryTest
24 {
25 [SetUp()]
26 public void RegisterRequirementHandler()
27 {
28 WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresAtLeastNUnitsRequirementFactory());
29 }
31 [TearDown()]
32 public void AfterTestCleanup()
33 {
34 WarFoundryLoader.SetDefault(null);
35 }
37 [Test()]
38 public void TestCompleteLoadingOnRaceWithMissingAbilityIdErrors()
39 {
40 TestFileValidationFailure("testdata/xml-race-factory/non-existant-ability.racex");
41 }
43 [Test()]
44 public void TestCompleteLoadingOnRaceWithDuplicateAbilityIdErrors()
45 {
46 TestFileValidationFailure("testdata/xml-race-factory/duplicate-ability.racex");
47 }
49 [Test()]
50 public void TestCompleteLoadingOnRaceWithAbilityLoadsData()
51 {
52 SetDefaultGameSystem();
53 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-ability.racex");
54 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
55 UnitType unitType = race.GetUnitType("Empire1");
56 Ability ability = race.GetAbility("leaderOfMen");
57 Assert.That(ability.Name, Is.EqualTo("Leader of Men"));
58 Assert.That(ability.Description, Is.EqualTo("All men will follow the character and can use his leadership"));
59 Assert.That(unitType.GetRequiredAbilities(), Has.Member(ability));
60 }
62 [Test()]
63 public void TestCompleteLoadingOnRaceWithIncorrectMemberTypeIDErrors()
64 {
65 TestFileValidationFailure("testdata/xml-race-factory/non-existant-member-type.racex");
66 }
68 [Test()]
69 public void TestCompleteLoadingOnRaceWithDuplicateMemberTypeIDErrors()
70 {
71 TestFileValidationFailure("testdata/xml-race-factory/duplicate-member-type.racex");
72 }
74 [Test()]
75 public void TestCompleteLoadingOnRaceWithMemberTypeLoadsData()
76 {
77 SetDefaultGameSystem();
78 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/existing-member-type.racex");
79 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
80 UnitMemberType memberType = race.GetUnitMemberType("General");
81 Assert.That(memberType.Name, Is.EqualTo("General"));
82 }
84 [Test()]
85 public void TestCompleteLoadingOnRaceWithIncorrectEquipmentSlotErrors()
86 {
87 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-slot-reference.racex");
88 }
90 [Test()]
91 public void TestCompleteLoadingOnRaceWithDuplicateEquipmentSlotErrors()
92 {
93 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-duplicate-equipment-slot.racex");
94 }
96 [Test()]
97 public void TestCompleteLoadingOnRaceWithCorrectEquipmentSlotLoads()
98 {
99 SetDefaultGameSystem();
100 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-slot-reference.racex");
101 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
102 UnitType unitType = race.GetUnitType("Empire1");
103 UnitEquipmentItem unitEquipmentItem = unitType.GetEquipmentItem("Empire1equip1");
104 Assert.That(unitEquipmentItem.SlotName, Is.EqualTo("slot1"));
105 }
107 [Test()]
108 public void TestCompleteLoadingOnRaceWithNoEquipmentSlotLimitErrors()
109 {
110 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-no-equipment-slot-limit.racex");
111 }
113 [Test()]
114 public void TestCompleteLoadingOnRaceWithIncorrectEquipmentIDError()
115 {
116 TestFileValidationFailure("testdata/xml-race-factory/single-unit-with-invalid-equipment-reference.racex");
117 }
119 [Test()]
120 public void TestCompleteLoadingOnRaceWithDuplicateEquipmentIDError()
121 {
122 TestFileValidationFailure("testdata/xml-race-factory/duplicate-equipment-id.racex");
123 }
125 [Test()]
126 public void TestCompleteLoadingOnRaceWithCorrectEquipmentIDLoads()
127 {
128 SetDefaultGameSystem();
129 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/single-unit-with-valid-equipment-reference.racex");
130 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
131 UnitType unitType = race.GetUnitType("Empire1");
132 Assert.That(unitType.GetEquipmentItem("Empire1equip1"), Is.Not.Null);
133 }
135 [Test()]
136 public void TestCompleteLoadingOnRaceWithIncorrectContainedUnitIDError()
137 {
138 TestFileValidationFailure("testdata/xml-race-factory/invalid-contained-unit-reference.racex");
139 }
141 [Test()]
142 public void TestCompleteLoadingOnRaceWithCorrectContainedUnitIDLoadsData()
143 {
144 SetDefaultGameSystem();
145 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/valid-contained-unit-reference.racex");
146 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
147 UnitType unitType = race.GetUnitType("Empire1");
148 Assert.That(unitType.ContainedUnitTypes, Has.Length(1));
149 }
151 [Test()]
152 public void TestCompleteLoadingOnRaceWithDuplicateExtraDataKeyErrors()
153 {
154 TestFileValidationFailure("testdata/xml-race-factory/duplicate-extra-data-key.racex");
155 }
157 [Test()]
158 public void TestCompleteLoadingOnRaceWithDifferentExtraDataKeysLoadsData()
159 {
160 SetDefaultGameSystem();
161 FileInfo raceFile = new FileInfo("testdata/xml-race-factory/different-extra-data-key.racex");
162 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
163 UnitType unitType = race.GetUnitType("Empire1");
164 Assert.That(unitType.GetExtraData("data1"), Is.EqualTo("SomeStringHere"));
165 }
167 [Test()]
168 [ExpectedException(typeof(InvalidFileException), ExpectedMessage="Referenced game system, 'nonexistant-system', did not exist")]
169 public void TestCompleteLoadingOnRaceWithMissingGameSystemErrors()
170 {
171 ICollection<IWarFoundryObject> objs = WarFoundryXmlFactory.GetFactory().CreateObjectsFromFile(new LoadableFileObject(new FileInfo("testdata/race-with-non-existant-game-system.race")));
172 Assert.AreEqual(1, objs.Count);
173 IEnumerator<IWarFoundryObject> enumerator = objs.GetEnumerator();
174 enumerator.Reset();
175 enumerator.MoveNext();
176 Race race = (Race)enumerator.Current;
177 race.EnsureFullyLoaded();
178 }
180 [Test()]
181 public void TestSingleUnitArmyLoadsSuccessfully()
182 {
183 SetDefaultGameSystem();
184 FileInfo raceFile = new FileInfo("testdata/single-unit-race.racex");
185 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
186 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
187 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
188 Assert.AreEqual(1, unitTypes.Length);
189 UnitType unitType = unitTypes[0];
190 Stat[] stats = unitType.UnitStatsArrays[0];
191 Assert.AreEqual(9, stats.Length);
192 Assert.AreEqual("M", stats[0].ParentSlotName);
193 Assert.AreEqual("4", stats[0].SlotValueString);
194 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
195 }
197 [Test()]
198 public void TestSingleUnitArmyWithMemberTypeReferenceLoadsSuccessfully()
199 {
200 SetDefaultGameSystem();
201 FileInfo raceFile = new FileInfo("testdata/single-unit-type-referencing-race.racex");
202 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
203 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
204 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
205 Assert.AreEqual(1, unitTypes.Length);
206 UnitType unitType = unitTypes[0];
207 Stat[] stats = unitType.UnitStatsArrays[0];
208 Assert.AreEqual(9, stats.Length);
209 Assert.AreEqual("M", stats[0].ParentSlotName);
210 Assert.AreEqual("4", stats[0].SlotValueString);
211 Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
212 }
214 [Test()]
215 public void TestSingleUnitArmyWithMultipleMemberTypeReferencesLoadsSuccessfully()
216 {
217 SetDefaultGameSystem();
218 FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race.racex");
219 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
220 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
221 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
222 Assert.AreEqual(1, unitTypes.Length);
223 UnitType unitType = unitTypes[0];
224 Stat[][] stats = unitType.UnitStatsArrays;
225 Assert.AreEqual(2, stats.Length);
226 Assert.AreEqual("M", stats[0][0].ParentSlotName);
227 Assert.AreEqual("4", stats[0][0].SlotValueString);
228 Assert.AreEqual("General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
229 Assert.AreEqual("M", stats[1][0].ParentSlotName);
230 Assert.AreEqual("8", stats[1][0].SlotValueString);
231 Assert.AreEqual("Warhorse", unitType.UnitStatsArraysWithName[1][0].SlotValueString);
232 }
234 [Test()]
235 public void TestSingleUnitArmyWithMultipleMemberTypeReferencesAndOverrideLoadsSuccessfully()
236 {
237 SetDefaultGameSystem();
238 FileInfo raceFile = new FileInfo("testdata/single-unit-multi-type-referencing-race-with-override.racex");
239 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
240 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
241 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
242 Assert.AreEqual(1, unitTypes.Length);
243 UnitType unitType = unitTypes[0];
244 Stat[][] allStats = unitType.UnitStatsArrays;
245 Stat[] stats = allStats[0];
246 Assert.AreEqual(1, allStats.Length);
247 Assert.AreEqual(9, stats.Length);
248 Assert.AreEqual("M", stats[0].ParentSlotName);
249 Assert.AreEqual("4", stats[0].SlotValueString);
250 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
251 }
253 [Test()]
254 public void TestSingleUnitArmyWithNoStatsReturnsUnitWithBlankStats()
255 {
256 SetDefaultGameSystem();
257 FileInfo raceFile = new FileInfo("testdata/single-unit-no-stats-race.racex");
258 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
259 Assert.That(race.GetUnitType("Empire1"), Is.Not.Null);
260 UnitType[] unitTypes = race.GetUnitTypes(race.GetCategory("cat1"));
261 Assert.AreEqual(1, unitTypes.Length);
262 UnitType unitType = unitTypes[0];
263 Stat[] stats = unitType.UnitStatsArrays[0];
264 Assert.That(stats.Length == race.GameSystem.StandardSystemStats.SlotCount);
266 foreach (Stat stat in stats)
267 {
268 Assert.AreEqual("", stat.SlotValueString);
269 }
271 Assert.AreEqual("Empire General", unitType.UnitStatsArraysWithName[0][0].SlotValueString);
272 }
274 [Test()]
275 public void TestSingleUnitArmyWithNoCategoriesUsesMainCategory()
276 {
277 SetDefaultGameSystem();
278 FileInfo raceFile = new FileInfo("testdata/single-unit-no-category.racex");
279 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
280 Category category = race.GetCategory("cat1");
281 UnitType[] unitTypes = race.GetUnitTypes(category);
282 Assert.AreEqual(1, unitTypes.Length);
283 UnitType unitType = unitTypes[0];
284 Assert.That(unitType.MainCategory, Is.EqualTo(category));
285 Assert.That(unitType.Categories.Length, Is.EqualTo(1));
286 Assert.That(unitType.Categories, Has.Member(category));
287 }
289 [Test()]
290 public void TestSingleUnitArmyWithOneCategoryAndSameMainCategoryHasOneCategory()
291 {
292 SetDefaultGameSystem();
293 FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-same-main-category.racex");
294 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
295 Category category = race.GetCategory("cat1");
296 UnitType[] unitTypes = race.GetUnitTypes(category);
297 Assert.AreEqual(1, unitTypes.Length);
298 UnitType unitType = unitTypes[0];
299 Assert.That(unitType.MainCategory, Is.EqualTo(category));
300 Assert.That(unitType.Categories.Length, Is.EqualTo(1));
301 Assert.That(unitType.Categories, Has.Member(category));
302 }
304 [Test()]
305 public void TestSingleUnitArmyWithOneCategoryAndDifferentMainCategoryHasTwoCategories()
306 {
307 SetDefaultGameSystem();
308 FileInfo raceFile = new FileInfo("testdata/single-unit-one-category-different-main-category.racex");
309 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
310 Category category = race.GetCategory("cat1");
311 UnitType[] unitTypes = race.GetUnitTypes(category);
312 Assert.AreEqual(1, unitTypes.Length);
313 UnitType unitType = unitTypes[0];
314 Assert.That(unitType.MainCategory, Is.EqualTo(category));
315 Assert.That(unitType.Categories.Length, Is.EqualTo(2));
316 Assert.That(unitType.Categories, Has.Member(category));
317 Assert.That(unitType.Categories, Has.Member(race.GetCategory("cat2")));
318 }
320 [Test()]
321 public void TestSingleUnitArmyWithMultipleCategoriesIsInEachCategory()
322 {
323 SetDefaultGameSystem();
324 FileInfo raceFile = new FileInfo("testdata/single-unit-two-categories.racex");
325 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
326 UnitType unitType = race.GetUnitType("Empire1");
327 Assert.That(unitType.Categories.Length, Is.EqualTo(2));
328 Category cat1 = race.GetCategory("cat1");
329 Category cat2 = race.GetCategory("cat2");
330 Assert.That(unitType.MainCategory, Is.EqualTo(cat1));
331 Assert.That(unitType.Categories, Has.Member(cat1));
332 Assert.That(unitType.Categories, Has.Member(cat2));
333 Assert.That(race.GetUnitTypes(cat1), Is.All.EqualTo(unitType));
334 UnitType[] unitTypesCat2 = race.GetUnitTypes(cat2);
335 Assert.That(unitTypesCat2, Has.Length(1));
336 Assert.That(unitTypesCat2, Is.All.EqualTo(unitType));
337 }
339 [Test()]
340 public void TestValidCategoriesLoadsCorrectly()
341 {
342 SetDefaultGameSystem();
343 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/valid-categories.racex"));
344 Assert.That(race.Categories, Has.Length(5));
345 }
347 [Test()]
348 public void TestDuplicateCategoryIDErrors()
349 {
350 TestFileValidationFailure("testdata/xml-race-factory/duplicate-category-ids.racex");
351 }
353 [Test()]
354 public void TestExtendedRequirementsPassValidation()
355 {
356 SetDefaultGameSystem();
357 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/extended-requirements.racex"));
358 Assert.That(race.Categories, Has.Length(5));
359 }
361 [Test()]
362 public void TestLoadingSimpleRequirement()
363 {
364 SetDefaultGameSystem();
365 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex"));
366 UnitType unitType = race.GetUnitType("Empire1");
367 Assert.That(unitType.GetRequirements(), Has.Length(1));
368 }
370 [Test()]
371 public void TestLoadingRequirementType()
372 {
373 SetDefaultGameSystem();
374 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/simple-requirement.racex"));
375 UnitType unitType = race.GetUnitType("Empire1");
376 Assert.That(unitType.GetRequirements()[0], Is.InstanceOfType(typeof(UnitRequiresAtLeastNUnitsRequirement)));
377 }
379 [Test]
380 public void TestCyclicReferenceDoesNotCrash()
381 {
382 SetDefaultGameSystem();
383 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/cyclic.racex"));
384 UnitType unitType = race.GetUnitType("Empire1");
385 Assert.That(unitType.GetRequirements()[0], Is.InstanceOfType(typeof(UnitRequiresNoMoreThanNOfUnitTypeRequirement)));
386 }
388 [Test]
389 public void TestContainedUnitsAreLoaded()
390 {
391 SetDefaultGameSystem();
392 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/with-contained-only-unit.racex"));
393 Assert.That(race.GetUnitType("Empire2"), Is.Not.Null);
394 UnitType unitType1 = race.GetUnitType("Empire1");
395 Assert.That(race.GetUnitTypes(race.GetCategory("cat1")), Has.Length(1));
396 Assert.That(race.GetUnitTypes(race.GetCategory("cat1")), Has.Member(unitType1));
397 }
399 [Test]
400 public void TestMixedContainedUnitsAreLoaded()
401 {
402 SetDefaultGameSystem();
403 Race race = SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), new FileInfo("testdata/xml-race-factory/with-mixed-contained-units.racex"));
404 Assert.That(race.GetUnitType("Empire2"), Is.Not.Null);
405 UnitType unitType1 = race.GetUnitType("Empire1");
406 UnitType unitType3 = race.GetUnitType("Empire3");
407 Assert.That(race.GetUnitTypes(race.GetCategory("cat1")), Has.Length(2));
408 Assert.That(race.GetUnitTypes(race.GetCategory("cat1")), Has.Member(unitType1));
409 Assert.That(race.GetUnitTypes(race.GetCategory("cat1")), Has.Member(unitType3));
410 }
412 private void TestFileValidationFailure(string filePath)
413 {
414 SetDefaultGameSystem();
415 FileInfo raceFile = new FileInfo(filePath);
417 try
418 {
419 SingleXmlObjectLoader.LoadRaceFromXML(WarFoundryXmlFactory.GetFactory(), raceFile);
420 Assert.Fail("Excepted exception not thrown");
421 }
422 catch (InvalidFileException ex)
423 {
424 Console.WriteLine(ex.Message);
425 Exception innerException = ex.InnerException;
426 Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
427 }
428 }
430 private static void SetDefaultGameSystem()
431 {
432 SetDefaultGameSystem(new FileInfo("testdata/default.systemx"));
433 }
435 public static void SetDefaultGameSystem(FileInfo systemFile)
436 {
437 GameSystem system = SingleXmlObjectLoader.LoadGameSystemFromXML(WarFoundryXmlFactory.GetFactory(), systemFile);
438 FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system);
439 WarFoundryLoader.SetDefault(fixedLoader);
440 }
441 }
442 }