Esempio n. 1
0
        [Test] public void LoadPlugins()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                // Set up some mock data for available assemblies
                MockAssembly[] mockPlugins = new MockAssembly[]
                {
                    new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir/MockPluginB.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir2/MockPluginC.dll", typeof(MockPlugin))
                };
                MockAssembly[] mockNoise = new MockAssembly[]
                {
                    new MockAssembly("MockDir/MockAuxillaryA.dll"),
                    MockAssembly.CreateInvalid("MockDir2/MockPluginF.dll"),
                    MockAssembly.CreateInvalid("MockDir2/MockAuxillaryC.dll")
                };
                string[] mockLoadedPaths = new string[] {
                    mockPlugins[0].Location, mockPlugins[1].Location, mockPlugins[2].Location,
                    mockNoise[0].Location, mockNoise[1].Location, mockNoise[2].Location,
                    "MockDir2/MockAuxillaryD.dll"
                };

                pluginLoader.AddBaseDir("MockDir");
                pluginLoader.AddBaseDir("MockDir2");
                for (int i = 0; i < mockPlugins.Length; i++)
                {
                    pluginLoader.AddPlugin(mockPlugins[i]);
                }
                for (int i = 0; i < mockNoise.Length; i++)
                {
                    pluginLoader.AddPlugin(mockNoise[i]);
                }
                pluginLoader.AddIncompatibleDll("MockDir2/MockAuxillaryD.dll");

                // Set up a plugin manager using the mock loader
                MockPluginManager pluginManager = new MockPluginManager();
                pluginManager.Init(pluginLoader);

                // Load all plugins
                pluginManager.LoadPlugins();
                MockPlugin[] loadedPlugins = pluginManager.LoadedPlugins.ToArray();

                // Assert that we loaded all expected plugins, but nothing more
                Assert.AreEqual(3, loadedPlugins.Length);
                CollectionAssert.AreEquivalent(mockPlugins, loadedPlugins.Select(plugin => plugin.PluginAssembly));

                // Assert that we properly assigned all plugin properties
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.FilePath == plugin.PluginAssembly.Location));
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.FileHash == pluginLoader.GetAssemblyHash(plugin.FilePath)));
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.AssemblyName == plugin.PluginAssembly.GetShortAssemblyName()));

                // Assert that we loaded core plugin and auxilliary libraries, but not editor plugins
                CollectionAssert.AreEquivalent(
                    mockLoadedPaths,
                    pluginLoader.LoadedAssemblies);

                // Assert that we can access all assemblies and types from plugins
                foreach (MockAssembly mockAssembly in mockPlugins)
                {
                    CollectionAssert.Contains(pluginManager.GetAssemblies(), mockAssembly);
                }
                CollectionAssert.Contains(pluginManager.GetTypes(typeof(object)), typeof(MockPlugin));
                Assert.AreEqual(3, pluginManager.GetTypes(typeof(MockPlugin)).Count());

                pluginManager.Terminate();
            }
        }