From fcdc14882f75cb2b8be6e2370b2372c3ee81cb6b Mon Sep 17 00:00:00 2001 From: Rodney Littles II <6969701+RLittlesII@users.noreply.github.com> Date: Wed, 27 Mar 2019 05:42:49 -0500 Subject: [PATCH] housekeeping: Added Splat adapter tests (#309) - Added additional Autofac tests - Added additional DryIoc tests - Made DryIocDependencyResolver methods virtual --- .../DependencyResolverTests.cs | 46 ++++++++++ .../DependencyResolverTests.cs | 91 ++++++++++++++++++- src/Splat.DryIoc/DryIocDependencyResolver.cs | 16 ++-- .../DependencyResolverTests.cs | 2 +- 4 files changed, 142 insertions(+), 13 deletions(-) diff --git a/src/Splat.Autofac.Tests/DependencyResolverTests.cs b/src/Splat.Autofac.Tests/DependencyResolverTests.cs index d4a2fd00a..e94975471 100644 --- a/src/Splat.Autofac.Tests/DependencyResolverTests.cs +++ b/src/Splat.Autofac.Tests/DependencyResolverTests.cs @@ -3,6 +3,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -87,5 +88,50 @@ public void AutofacDependencyResolver_Should_Resolve_Screen() screen.ShouldNotBeNull(); screen.ShouldBeOfType(); } + + /// + /// Should throw an exception if service registration call back called. + /// + [Fact] + public void AutofacDependencyResolver_Should_Throw_If_UnregisterCurrent_Called() + { + var container = new ContainerBuilder(); + container.UseAutofacDependencyResolver(); + + var result = Record.Exception(() => + Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen))); + + result.ShouldBeOfType(); + } + + /// + /// Should unregister all. + /// + [Fact] + public void AutofacDependencyResolver_Should_UnregisterAll_Called() + { + var container = new ContainerBuilder(); + container.UseAutofacDependencyResolver(); + + var result = Record.Exception(() => + Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen))); + + result.ShouldBeOfType(); + } + + /// + /// Should throw an exception if service registration call back called. + /// + [Fact] + public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called() + { + var container = new ContainerBuilder(); + container.UseAutofacDependencyResolver(); + + var result = Record.Exception(() => + Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { })); + + result.ShouldBeOfType(); + } } } diff --git a/src/Splat.DryIoc.Tests/DependencyResolverTests.cs b/src/Splat.DryIoc.Tests/DependencyResolverTests.cs index 52078f3a0..10a72d380 100644 --- a/src/Splat.DryIoc.Tests/DependencyResolverTests.cs +++ b/src/Splat.DryIoc.Tests/DependencyResolverTests.cs @@ -19,7 +19,7 @@ namespace Splat.DryIoc.Tests public class DependencyResolverTests { /// - /// Shoulds the resolve views. + /// Should resolve the views. /// [Fact] public void DryIocDependencyResolver_Should_Resolve_Views() @@ -39,7 +39,7 @@ public void DryIocDependencyResolver_Should_Resolve_Views() } /// - /// Shoulds the resolve views. + /// Should resolve the views. /// [Fact] public void DryIocDependencyResolver_Should_Resolve_Named_View() @@ -55,7 +55,7 @@ public void DryIocDependencyResolver_Should_Resolve_Named_View() } /// - /// Shoulds the resolve view models. + /// Should resolve the view models. /// [Fact] public void DryIocDependencyResolver_Should_Resolve_View_Models() @@ -73,7 +73,7 @@ public void DryIocDependencyResolver_Should_Resolve_View_Models() } /// - /// Shoulds the resolve screen. + /// Should resolve the screen. /// [Fact] public void DryIocDependencyResolver_Should_Resolve_Screen() @@ -87,5 +87,88 @@ public void DryIocDependencyResolver_Should_Resolve_Screen() screen.ShouldNotBeNull(); screen.ShouldBeOfType(); } + + /// + /// Should unregister the screen. + /// + [Fact] + public void DryIocDependencyResolver_Should_UnregisterCurrent_Screen() + { + var builder = new Container(); + builder.Register(Reuse.Singleton); + builder.UseDryIocDependencyResolver(); + + Locator.Current.GetService().ShouldNotBeNull(); + + Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen)); + + Locator.Current.GetService().ShouldBeNull(); + } + + /// + /// Should unregister the screen. + /// + [Fact] + public void DryIocDependencyResolver_Should_UnregisterCurrent_Screen_With_Contract() + { + var builder = new Container(); + builder.Register(Reuse.Singleton, serviceKey: nameof(MockScreen)); + builder.UseDryIocDependencyResolver(); + + Locator.Current.GetService(nameof(MockScreen)).ShouldNotBeNull(); + + Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen), nameof(MockScreen)); + + Locator.Current.GetService(nameof(MockScreen)).ShouldBeNull(); + } + + /// + /// Should unregister the screen. + /// + [Fact] + public void DryIocDependencyResolver_Should_UnregisterAll_Screen() + { + var builder = new Container(); + builder.Register(Reuse.Singleton); + builder.UseDryIocDependencyResolver(); + + Locator.Current.GetService().ShouldNotBeNull(); + + Locator.CurrentMutable.UnregisterAll(typeof(IScreen)); + + Locator.Current.GetService().ShouldBeNull(); + } + + /// + /// Should unregister the screen. + /// + [Fact] + public void DryIocDependencyResolver_Should_UnregisterAll_Screen_With_Contract() + { + var builder = new Container(); + builder.Register(Reuse.Singleton, serviceKey: nameof(MockScreen)); + builder.UseDryIocDependencyResolver(); + + Locator.Current.GetService(nameof(MockScreen)).ShouldNotBeNull(); + + Locator.CurrentMutable.UnregisterAll(typeof(IScreen), nameof(MockScreen)); + + Locator.Current.GetService(nameof(MockScreen)).ShouldBeNull(); + } + + /// + /// Should throw an exception if service registration call back called. + /// + [Fact] + public void DryIocDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called() + { + var container = new Container(); + container.UseDryIocDependencyResolver(); + + var result = Record.Exception(() => + Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { })); + + result.ShouldBeOfType(); + } } } diff --git a/src/Splat.DryIoc/DryIocDependencyResolver.cs b/src/Splat.DryIoc/DryIocDependencyResolver.cs index 77da6b5a5..0d9c99f08 100644 --- a/src/Splat.DryIoc/DryIocDependencyResolver.cs +++ b/src/Splat.DryIoc/DryIocDependencyResolver.cs @@ -28,19 +28,19 @@ public DryIocDependencyResolver(Container container = null) } /// - public object GetService(Type serviceType, string contract = null) => + public virtual object GetService(Type serviceType, string contract = null) => string.IsNullOrEmpty(contract) ? _container.Resolve(serviceType, IfUnresolved.ReturnDefault) : _container.Resolve(serviceType, contract, IfUnresolved.ReturnDefault); /// - public IEnumerable GetServices(Type serviceType, string contract = null) => + public virtual IEnumerable GetServices(Type serviceType, string contract = null) => string.IsNullOrEmpty(contract) ? _container.ResolveMany(serviceType) : _container.ResolveMany(serviceType, serviceKey: contract); /// - public void Register(Func factory, Type serviceType, string contract = null) + public virtual void Register(Func factory, Type serviceType, string contract = null) { if (string.IsNullOrEmpty(contract)) { @@ -53,7 +53,7 @@ public void Register(Func factory, Type serviceType, string contract = n } /// - public void UnregisterCurrent(Type serviceType, string contract = null) + public virtual void UnregisterCurrent(Type serviceType, string contract = null) { if (string.IsNullOrEmpty(contract)) { @@ -66,20 +66,20 @@ public void UnregisterCurrent(Type serviceType, string contract = null) } /// - public void UnregisterAll(Type serviceType, string contract = null) + public virtual void UnregisterAll(Type serviceType, string contract = null) { if (string.IsNullOrEmpty(contract)) { - _container.Unregister(serviceType, condition: x => x.ImplementationType == serviceType); + _container.Unregister(serviceType); } else { - _container.Unregister(serviceType, contract, condition: x => x.ImplementationType == serviceType); + _container.Unregister(serviceType, contract); } } /// - public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action callback) + public virtual IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action callback) { throw new NotImplementedException(); } diff --git a/src/Splat.Ninject.Tests/DependencyResolverTests.cs b/src/Splat.Ninject.Tests/DependencyResolverTests.cs index dd420d3af..16b515a86 100644 --- a/src/Splat.Ninject.Tests/DependencyResolverTests.cs +++ b/src/Splat.Ninject.Tests/DependencyResolverTests.cs @@ -107,7 +107,7 @@ public void NinjectDependencyResolver_Should_Throw_If_UnregisterCurrent_Called() /// Should unregister all. /// [Fact] - public void NinjectDependencyResolver_Should_Unregister_All() + public void NinjectDependencyResolver_Should_UnregisterAll() { var container = new StandardKernel(); container.Bind().ToConstant(new MockScreen());