forked from PrismLibrary/Prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PrismLibrary#2180 from PrismLibrary/module-manager
add ModuleInfo collection to IModuleManager
- Loading branch information
Showing
8 changed files
with
296 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Linq; | ||
|
||
namespace Prism.Modularity | ||
{ | ||
/// <summary> | ||
/// Common extensions for the <see cref="IModuleManager"/> | ||
/// </summary> | ||
public static class IModuleManagerExtensions | ||
{ | ||
/// <summary> | ||
/// Checks to see if the <see cref="IModule"/> exists in the <see cref="IModuleCatalog.Modules"/> | ||
/// </summary> | ||
/// <returns><c>true</c> if the Module exists.</returns> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
/// <typeparam name="T">The <see cref="IModule"/> to check for.</typeparam> | ||
public static bool ModuleExists<T>(this IModuleManager manager) | ||
where T : IModule => | ||
manager.Modules.Any(mi => mi.ModuleType == typeof(T).AssemblyQualifiedName); | ||
|
||
/// <summary> | ||
/// Exists the specified catalog and name. | ||
/// </summary> | ||
/// <returns><c>true</c> if the Module exists.</returns> | ||
/// <param name="catalog">Catalog.</param> | ||
/// <param name="name">Name.</param> | ||
public static bool ModuleExists(this IModuleManager catalog, string name) => | ||
catalog.Modules.Any(module => module.ModuleName == name); | ||
|
||
/// <summary> | ||
/// Gets the current <see cref="ModuleState"/> of the <see cref="IModule"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The <see cref="IModule"/> to check.</typeparam> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
/// <returns></returns> | ||
public static ModuleState GetModuleState<T>(this IModuleManager manager) | ||
where T : IModule => | ||
manager.Modules.FirstOrDefault(mi => mi.ModuleType == typeof(T).AssemblyQualifiedName).State; | ||
|
||
/// <summary> | ||
/// Gets the current <see cref="ModuleState"/> of the <see cref="IModule"/>. | ||
/// </summary> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
/// <param name="name">Name.</param> | ||
/// <returns></returns> | ||
public static ModuleState GetModuleState(this IModuleManager manager, string name) => | ||
manager.Modules.FirstOrDefault(module => module.ModuleName == name).State; | ||
|
||
/// <summary> | ||
/// Checks to see if the <see cref="IModule"/> is already initialized. | ||
/// </summary> | ||
/// <returns><c>true</c>, if initialized, <c>false</c> otherwise.</returns> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
/// <typeparam name="T">The <see cref="IModule"/> to check.</typeparam> | ||
public static bool IsModuleInitialized<T>(this IModuleManager manager) | ||
where T : IModule => | ||
manager.Modules.FirstOrDefault(mi => mi.ModuleType == typeof(T).AssemblyQualifiedName)?.State == ModuleState.Initialized; | ||
|
||
/// <summary> | ||
/// Checks to see if the <see cref="IModule"/> is already initialized. | ||
/// </summary> | ||
/// <returns><c>true</c>, if initialized, <c>false</c> otherwise.</returns> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
/// <param name="name">Name.</param> | ||
public static bool IsModuleInitialized(this IModuleManager manager, string name) => | ||
manager.Modules.FirstOrDefault(module => module.ModuleName == name)?.State == ModuleState.Initialized; | ||
|
||
/// <summary> | ||
/// Loads and initializes the module in the <see cref="IModuleCatalog"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The <see cref="IModule"/> to load.</typeparam> | ||
/// <param name="manager">The <see cref="IModuleManager"/>.</param> | ||
public static void LoadModule<T>(this IModuleManager manager) | ||
where T : IModule => | ||
manager.LoadModule(typeof(T).Name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/Forms/Prism.Forms.Tests/Modularity/ModuleManagerExtensionsFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Moq; | ||
using Prism.Forms.Tests.Mocks.Modules; | ||
using Prism.Modularity; | ||
using Xunit; | ||
|
||
namespace Prism.Forms.Tests.Modularity | ||
{ | ||
public class ModuleManagerExtensionsFixture | ||
{ | ||
[Fact] | ||
public void ModuleManagerExposesIModuleCatalogModules() | ||
{ | ||
var modules = new[] | ||
{ | ||
new ModuleInfo(typeof(ModuleA)) | ||
}; | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(modules); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Same(modules, manager.Modules); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectModuleStateWithGeneric() | ||
{ | ||
IModuleInfo moduleInfo = new ModuleInfo(typeof(ModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Equal(moduleInfo.State, manager.GetModuleState<ModuleA>()); | ||
moduleInfo.State = ModuleState.LoadingTypes; | ||
Assert.Equal(moduleInfo.State, manager.GetModuleState<ModuleA>()); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectModuleStateWithName() | ||
{ | ||
IModuleInfo moduleInfo = new ModuleInfo(typeof(ModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Equal(moduleInfo.State, manager.GetModuleState(nameof(ModuleA))); | ||
moduleInfo.State = ModuleState.LoadingTypes; | ||
Assert.Equal(moduleInfo.State, manager.GetModuleState(nameof(ModuleA))); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectInitializationStateWithGeneric() | ||
{ | ||
IModuleInfo moduleInfo = new ModuleInfo(typeof(ModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.False(manager.IsModuleInitialized<ModuleA>()); | ||
moduleInfo.State = ModuleState.Initializing; | ||
Assert.False(manager.IsModuleInitialized<ModuleA>()); | ||
moduleInfo.State = ModuleState.Initialized; | ||
Assert.True(manager.IsModuleInitialized<ModuleA>()); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectInitializationStateWithName() | ||
{ | ||
IModuleInfo moduleInfo = new ModuleInfo(typeof(ModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.False(manager.IsModuleInitialized(nameof(ModuleA))); | ||
moduleInfo.State = ModuleState.Initializing; | ||
Assert.False(manager.IsModuleInitialized(nameof(ModuleA))); | ||
moduleInfo.State = ModuleState.Initialized; | ||
Assert.True(manager.IsModuleInitialized(nameof(ModuleA))); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerLoadModuleGeneric_CallsLoadModuleWithName() | ||
{ | ||
var managerMock = new Mock<IModuleManager>(); | ||
managerMock.Object.LoadModule<ModuleA>(); | ||
managerMock.Verify(m => m.LoadModule(nameof(ModuleA))); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/Wpf/Prism.Wpf.Tests/Modularity/ModuleManagerExtensionsFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Moq; | ||
using Prism.Modularity; | ||
using Prism.Wpf.Tests.Mocks.Modules; | ||
using Xunit; | ||
|
||
namespace Prism.Wpf.Tests.Modularity | ||
{ | ||
public class ModuleManagerExtensionsFixture | ||
{ | ||
[Fact] | ||
public void ModuleManagerExposesIModuleCatalogModules() | ||
{ | ||
var modules = new[] | ||
{ | ||
new ModuleInfo(typeof(MockModuleA)) | ||
}; | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(modules); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Same(modules, manager.Modules); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectModuleStateWithGeneric() | ||
{ | ||
var moduleInfo = new ModuleInfo(typeof(MockModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Equal(moduleInfo.State, manager.GetModuleState<MockModuleA>()); | ||
moduleInfo.State = ModuleState.LoadingTypes; | ||
Assert.Equal(moduleInfo.State, manager.GetModuleState<MockModuleA>()); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectModuleStateWithName() | ||
{ | ||
var moduleInfo = new ModuleInfo(typeof(MockModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.Equal(moduleInfo.State, manager.GetModuleState(nameof(MockModuleA))); | ||
moduleInfo.State = ModuleState.LoadingTypes; | ||
Assert.Equal(moduleInfo.State, manager.GetModuleState(nameof(MockModuleA))); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectInitializationStateWithGeneric() | ||
{ | ||
var moduleInfo = new ModuleInfo(typeof(MockModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.False(manager.IsModuleInitialized<MockModuleA>()); | ||
moduleInfo.State = ModuleState.Initializing; | ||
Assert.False(manager.IsModuleInitialized<MockModuleA>()); | ||
moduleInfo.State = ModuleState.Initialized; | ||
Assert.True(manager.IsModuleInitialized<MockModuleA>()); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerReturnsCorrectInitializationStateWithName() | ||
{ | ||
var moduleInfo = new ModuleInfo(typeof(MockModuleA)); | ||
var moduleCatalogMock = new Mock<IModuleCatalog>(); | ||
moduleCatalogMock.Setup(c => c.Modules).Returns(new[] { moduleInfo }); | ||
IModuleManager manager = new ModuleManager(Mock.Of<IModuleInitializer>(), moduleCatalogMock.Object); | ||
|
||
Assert.False(manager.IsModuleInitialized(nameof(MockModuleA))); | ||
moduleInfo.State = ModuleState.Initializing; | ||
Assert.False(manager.IsModuleInitialized(nameof(MockModuleA))); | ||
moduleInfo.State = ModuleState.Initialized; | ||
Assert.True(manager.IsModuleInitialized(nameof(MockModuleA))); | ||
} | ||
|
||
[Fact] | ||
public void ModuleManagerLoadModuleGeneric_CallsLoadModuleWithName() | ||
{ | ||
var managerMock = new Mock<IModuleManager>(); | ||
managerMock.Object.LoadModule<MockModuleA>(); | ||
managerMock.Verify(m => m.LoadModule(nameof(MockModuleA))); | ||
} | ||
} | ||
} |