view API/WarFoundryLoaderTest.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 17c5030485dc
children 6ec46f3db2d4
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;
15 using IBBoard.WarFoundry.API.Loading;
17 namespace IBBoard.WarFoundry.API.Factories
18 {
19 [TestFixture()]
20 public class WarFoundryLoaderTest
21 {
22 private AbstractNativeWarFoundryFactory sysFactory;
24 [Test()]
25 public void TestLoadWithoutFactoriesCompletesWithoutError()
26 {
27 WarFoundryLoader.GetDefault().LoadFiles();
28 }
30 [Test()]
31 public void TestLoadingSystemCompletesWithoutError()
32 {
33 AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault();
34 DirectoryInfo dir = new DirectoryInfo("testdata");
35 loader.RegisterFactory(GetSystemFactory());
36 ILoadableObjectSource loadSource = new LoadableObjectSourceDirectory(dir);
37 loader.AddLoadSource(loadSource);
38 loader.LoadFiles();
39 Assert.Greater(loader.GetGameSystems().Length, 0);
40 loader.RemoveLoadSource(loadSource);
41 loader.UnregisterFactory(GetSystemFactory());
42 }
44 [Test()]
45 public void TestBug318LoadingSingleFileShouldNotNullRef()
46 {
47 DefaultWarFoundryLoader loader = new DefaultWarFoundryLoader();
48 loader.RegisterFactory(WarFoundryXmlFactory.GetFactory());
49 ICollection<IWarFoundryObject> objs = loader.LoadFile(new FileInfo("testdata/Test.system"));
50 Assert.That(objs, Has.Count(1));
51 }
53 [Test()]
54 public void TestRegisterRequirementFactoryAddsFactory()
55 {
56 string factoryID = "SomeID";
57 MockRequirementFactory mockFactory = new MockRequirementFactory(factoryID);
58 WarFoundryLoader.RegisterRequirementFactory(mockFactory);
59 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory));
60 Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
61 }
63 [Test()]
64 public void TestRegisterRequirementFactoryAddsFactories()
65 {
66 string factoryID1 = "SomeID";
67 MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID1);
68 WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
69 string factoryID2 = "SomeOtherID";
70 MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID2);
71 WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
72 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID1), Is.EqualTo(mockFactory1));
73 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID2), Is.EqualTo(mockFactory2));
74 Assert.That(WarFoundryLoader.GetRequirementFactory("fibble"), Is.Null);
75 }
77 [Test()]
78 public void TestRegisterRequirementFactoryOverridesWithSecondFactory()
79 {
80 string factoryID = "SomeID";
81 MockRequirementFactory mockFactory1 = new MockRequirementFactory(factoryID);
82 WarFoundryLoader.RegisterRequirementFactory(mockFactory1);
83 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory1));
84 MockRequirementFactory mockFactory2 = new MockRequirementFactory(factoryID);
85 WarFoundryLoader.RegisterRequirementFactory(mockFactory2);
86 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1));
87 Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2));
88 }
90 private AbstractNativeWarFoundryFactory GetSystemFactory()
91 {
92 if (sysFactory == null)
93 {
94 sysFactory = new MockSystemFactory();
95 }
97 return sysFactory;
98 }
99 }
100 }