view API/WarFoundryLoaderTest.cs @ 157:17c5030485dc

Re #351: Add extensible requirement handling method * Swap to using IRequirement, as per interface * Test registration of factories
author IBBoard <dev@ibboard.co.uk>
date Sat, 23 Jul 2011 19:54:37 +0000
parents d1ec7618f812
children 7c21ca1482cb
line source
1 // This file (WarFoundryLoaderTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2008, 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.IO;
7 using NUnit.Framework;
8 using IBBoard.WarFoundry.API.Factories.Mock;
9 using IBBoard.WarFoundry.API.Objects;
10 using NUnit.Framework.SyntaxHelpers;
11 using System.Collections.Generic;
12 using IBBoard.WarFoundry.API.Factories.Xml;
13 using IBBoard.WarFoundry.API.Factories.Requirement;
14 using IBBoard.WarFoundry.API.Objects.Requirement;
16 namespace IBBoard.WarFoundry.API.Factories
17 {
18 [TestFixture()]
19 public class WarFoundryLoaderTest
20 {
21 private AbstractNativeWarFoundryFactory sysFactory;
23 [Test()]
24 public void TestLoadWithoutFactoriesCompletesWithoutError()
25 {
26 WarFoundryLoader.GetDefault().LoadFiles();
27 }
29 [Test()]
30 public void TestLoadingSystemCompletesWithoutError()
31 {
32 AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault();
33 DirectoryInfo dir = new DirectoryInfo("testdata");
34 loader.RegisterFactory(GetSystemFactory());
35 loader.AddLoadDirectory(dir);
36 loader.LoadFiles();
37 Assert.Greater(loader.GetGameSystems().Length, 0);
38 loader.RemoveLoadDirectory(dir);
39 loader.UnregisterFactory(GetSystemFactory());
40 }
42 [Test()]
43 public void TestBug318LoadingSingleFileShouldNotNullRef()
44 {
45 DefaultWarFoundryLoader loader = new DefaultWarFoundryLoader();
46 loader.RegisterFactory(WarFoundryXmlFactory.GetFactory());
47 ICollection<IWarFoundryObject> objs = loader.LoadFile(new FileInfo("testdata/Test.system"));
48 Assert.That(objs, Has.Count(1));
49 }
51 [Test()]
52 public void TestRegisterRequirementFactoryAddsFactory()
53 {
54 string factoryID = "SomeID";
55 MockRequirementFactory mockFactory = new MockRequirementFactory(factoryID);
56 WarFoundryLoader.RegisterRequirementFactory(mockFactory);
57 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory));
58 Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
59 }
61 [Test()]
62 public void TestRegisterRequirementFactoryAddsFactories()
63 {
64 string factoryID1 = "SomeID";
65 MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID1);
66 WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
67 string factoryID2 = "SomeOtherID";
68 MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID2);
69 WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
70 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID1), Is.EqualTo(mockFactory1));
71 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID2), Is.EqualTo(mockFactory2));
72 Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
73 }
75 [Test()]
76 public void TestRegisterRequirementFactoryOverridesWithSecondFactory()
77 {
78 string factoryID = "SomeID";
79 MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID);
80 WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
81 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory1));
82 MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID);
83 WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
84 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1));
85 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2));
86 }
88 private AbstractNativeWarFoundryFactory GetSystemFactory()
89 {
90 if (sysFactory == null)
91 {
92 sysFactory = new MockSystemFactory();
93 }
95 return sysFactory;
96 }
97 }
98 }