Skip to content

Commit

Permalink
housekeeping: Added Splat adapter tests (#309)
Browse files Browse the repository at this point in the history
- Added additional Autofac tests
- Added additional DryIoc tests
- Made DryIocDependencyResolver methods virtual
  • Loading branch information
RLittlesII authored and glennawatson committed Mar 27, 2019
1 parent 3c416dc commit fcdc148
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 13 deletions.
46 changes: 46 additions & 0 deletions src/Splat.Autofac.Tests/DependencyResolverTests.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -87,5 +88,50 @@ public void AutofacDependencyResolver_Should_Resolve_Screen()
screen.ShouldNotBeNull();
screen.ShouldBeOfType<MockScreen>();
}

/// <summary>
/// Should throw an exception if service registration call back called.
/// </summary>
[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<NotImplementedException>();
}

/// <summary>
/// Should unregister all.
/// </summary>
[Fact]
public void AutofacDependencyResolver_Should_UnregisterAll_Called()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();

var result = Record.Exception(() =>
Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen)));

result.ShouldBeOfType<NotImplementedException>();
}

/// <summary>
/// Should throw an exception if service registration call back called.
/// </summary>
[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<NotImplementedException>();
}
}
}
91 changes: 87 additions & 4 deletions src/Splat.DryIoc.Tests/DependencyResolverTests.cs
Expand Up @@ -19,7 +19,7 @@ namespace Splat.DryIoc.Tests
public class DependencyResolverTests
{
/// <summary>
/// Shoulds the resolve views.
/// Should resolve the views.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_Resolve_Views()
Expand All @@ -39,7 +39,7 @@ public void DryIocDependencyResolver_Should_Resolve_Views()
}

/// <summary>
/// Shoulds the resolve views.
/// Should resolve the views.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_Resolve_Named_View()
Expand All @@ -55,7 +55,7 @@ public void DryIocDependencyResolver_Should_Resolve_Named_View()
}

/// <summary>
/// Shoulds the resolve view models.
/// Should resolve the view models.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_Resolve_View_Models()
Expand All @@ -73,7 +73,7 @@ public void DryIocDependencyResolver_Should_Resolve_View_Models()
}

/// <summary>
/// Shoulds the resolve screen.
/// Should resolve the screen.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_Resolve_Screen()
Expand All @@ -87,5 +87,88 @@ public void DryIocDependencyResolver_Should_Resolve_Screen()
screen.ShouldNotBeNull();
screen.ShouldBeOfType<MockScreen>();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_UnregisterCurrent_Screen()
{
var builder = new Container();
builder.Register<IScreen, MockScreen>(Reuse.Singleton);
builder.UseDryIocDependencyResolver();

Locator.Current.GetService<IScreen>().ShouldNotBeNull();

Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen));

Locator.Current.GetService<IScreen>().ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_UnregisterCurrent_Screen_With_Contract()
{
var builder = new Container();
builder.Register<IScreen, MockScreen>(Reuse.Singleton, serviceKey: nameof(MockScreen));
builder.UseDryIocDependencyResolver();

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldNotBeNull();

Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_UnregisterAll_Screen()
{
var builder = new Container();
builder.Register<IScreen, MockScreen>(Reuse.Singleton);
builder.UseDryIocDependencyResolver();

Locator.Current.GetService<IScreen>().ShouldNotBeNull();

Locator.CurrentMutable.UnregisterAll(typeof(IScreen));

Locator.Current.GetService<IScreen>().ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_UnregisterAll_Screen_With_Contract()
{
var builder = new Container();
builder.Register<IScreen, MockScreen>(Reuse.Singleton, serviceKey: nameof(MockScreen));
builder.UseDryIocDependencyResolver();

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldNotBeNull();

Locator.CurrentMutable.UnregisterAll(typeof(IScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldBeNull();
}

/// <summary>
/// Should throw an exception if service registration call back called.
/// </summary>
[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<NotImplementedException>();
}
}
}
16 changes: 8 additions & 8 deletions src/Splat.DryIoc/DryIocDependencyResolver.cs
Expand Up @@ -28,19 +28,19 @@ public DryIocDependencyResolver(Container container = null)
}

/// <inheritdoc />
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);

/// <inheritdoc />
public IEnumerable<object> GetServices(Type serviceType, string contract = null) =>
public virtual IEnumerable<object> GetServices(Type serviceType, string contract = null) =>
string.IsNullOrEmpty(contract)
? _container.ResolveMany(serviceType)
: _container.ResolveMany(serviceType, serviceKey: contract);

/// <inheritdoc />
public void Register(Func<object> factory, Type serviceType, string contract = null)
public virtual void Register(Func<object> factory, Type serviceType, string contract = null)
{
if (string.IsNullOrEmpty(contract))
{
Expand All @@ -53,7 +53,7 @@ public void Register(Func<object> factory, Type serviceType, string contract = n
}

/// <inheritdoc />
public void UnregisterCurrent(Type serviceType, string contract = null)
public virtual void UnregisterCurrent(Type serviceType, string contract = null)
{
if (string.IsNullOrEmpty(contract))
{
Expand All @@ -66,20 +66,20 @@ public void UnregisterCurrent(Type serviceType, string contract = null)
}

/// <inheritdoc />
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);
}
}

/// <inheritdoc />
public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action<IDisposable> callback)
public virtual IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action<IDisposable> callback)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Splat.Ninject.Tests/DependencyResolverTests.cs
Expand Up @@ -107,7 +107,7 @@ public void NinjectDependencyResolver_Should_Throw_If_UnregisterCurrent_Called()
/// Should unregister all.
/// </summary>
[Fact]
public void NinjectDependencyResolver_Should_Unregister_All()
public void NinjectDependencyResolver_Should_UnregisterAll()
{
var container = new StandardKernel();
container.Bind<IScreen>().ToConstant(new MockScreen());
Expand Down

0 comments on commit fcdc148

Please sign in to comment.