Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Update Splat.Autofac to work with Autofac 5.0 (#465) #585

Merged
merged 8 commits into from
Sep 21, 2020
Merged
145 changes: 117 additions & 28 deletions src/Splat.Autofac.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public class DependencyResolverTests : BaseDependencyResolverTests<AutofacDepend
[Fact]
public void AutofacDependencyResolver_Should_Resolve_Views()
{
var builder = new ContainerBuilder();
builder.RegisterType<ViewOne>().As<IViewFor<ViewModelOne>>();
builder.RegisterType<ViewTwo>().As<IViewFor<ViewModelTwo>>();
builder.Build().UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<ViewOne>().As<IViewFor<ViewModelOne>>();
containerBuilder.RegisterType<ViewTwo>().As<IViewFor<ViewModelTwo>>();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var viewOne = Locator.Current.GetService(typeof(IViewFor<ViewModelOne>));
var viewTwo = Locator.Current.GetService(typeof(IViewFor<ViewModelTwo>));
Expand All @@ -43,9 +45,11 @@ public void AutofacDependencyResolver_Should_Resolve_Views()
[Fact]
public void AutofacDependencyResolver_Should_Resolve_Named_View()
{
var builder = new ContainerBuilder();
builder.RegisterType<ViewTwo>().Named<IViewFor<ViewModelTwo>>("Other");
builder.Build().UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<ViewTwo>().Named<IViewFor<ViewModelTwo>>("Other");

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var viewTwo = Locator.Current.GetService(typeof(IViewFor<ViewModelTwo>), "Other");

Expand All @@ -59,10 +63,12 @@ public void AutofacDependencyResolver_Should_Resolve_Named_View()
[Fact]
public void AutofacDependencyResolver_Should_Resolve_View_Models()
{
var builder = new ContainerBuilder();
builder.RegisterType<ViewModelOne>().AsSelf();
builder.RegisterType<ViewModelTwo>().AsSelf();
builder.Build().UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<ViewModelOne>().AsSelf();
containerBuilder.RegisterType<ViewModelTwo>().AsSelf();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var vmOne = Locator.Current.GetService<ViewModelOne>();
var vmTwo = Locator.Current.GetService<ViewModelTwo>();
Expand All @@ -77,9 +83,11 @@ public void AutofacDependencyResolver_Should_Resolve_View_Models()
[Fact]
public void AutofacDependencyResolver_Should_Resolve_Screen()
{
var builder = new ContainerBuilder();
builder.RegisterType<MockScreen>().As<IScreen>().SingleInstance();
builder.Build().UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<MockScreen>().As<IScreen>().SingleInstance();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var screen = Locator.Current.GetService<IScreen>();

Expand All @@ -91,10 +99,12 @@ public void AutofacDependencyResolver_Should_Resolve_Screen()
/// Should throw an exception if service registration call back called.
/// </summary>
[Fact]
public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called()
public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistrationCallback_Called()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var result = Record.Exception(() =>
Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { }));
Expand All @@ -111,15 +121,18 @@ public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistionCallback_C
[Fact]
public void AutofacDependencyResolver_Should_ReturnRegisteredLogger()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();
var containerBuilder = new ContainerBuilder();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();

Locator.CurrentMutable.RegisterConstant(
new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger())),
typeof(ILogManager));

var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var logManager = Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(logManager);
}

/// <summary>
Expand All @@ -131,21 +144,97 @@ public void AutofacDependencyResolver_Should_ReturnRegisteredLogger()
[Fact]
public void AutofacDependencyResolver_PreInit_Should_ReturnRegisteredLogger()
{
var builder = new ContainerBuilder();
builder.Register(_ => new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger()))).As(typeof(ILogManager))
var containerBuilder = new ContainerBuilder();

var autofacResolver = containerBuilder.UseAutofacDependencyResolver();

containerBuilder.Register(_ => new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger()))).As(typeof(ILogManager))
.AsImplementedInterfaces();

builder.UseAutofacDependencyResolver();
autofacResolver.SetLifetimeScope(containerBuilder.Build());

var logManager = Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(logManager);
}

/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterCurrent"/>
/// </summary>
[Fact]
public override void UnregisterCurrent_Doesnt_Throw_When_List_Empty()
{
}

/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterCurrent"/>
/// </summary>
[Fact]
public override void UnregisterCurrent_Remove_Last()
{
}

/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterCurrent"/>
/// </summary>
[Fact]
public override void UnregisterCurrentByName_Doesnt_Throw_When_List_Empty()
{
}

var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterAll"/>
/// </summary>
[Fact]
public override void UnregisterAll_UnregisterCurrent_Doesnt_Throw_When_List_Empty()
{
}

/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterAll"/>
/// </summary>
[Fact]
public override void UnregisterAllByContract_UnregisterCurrent_Doesnt_Throw_When_List_Empty()
{
}

/// <summary>
/// <inheritdoc cref="AutofacDependencyResolver.UnregisterAll"/>
/// <inheritdoc cref="BaseDependencyResolverTests{T}.HasRegistration"/>
/// </summary>
[Fact]
public override void HasRegistration()
{
var type = typeof(string);
const string contractOne = "ContractOne";
const string contractTwo = "ContractTwo";
var resolver = GetDependencyResolver();

Assert.False(resolver.HasRegistration(type));
Assert.False(resolver.HasRegistration(type, contractOne));
Assert.False(resolver.HasRegistration(type, contractTwo));

resolver.Register(() => "unnamed", type);
Assert.True(resolver.HasRegistration(type));
Assert.False(resolver.HasRegistration(type, contractOne));
Assert.False(resolver.HasRegistration(type, contractTwo));

resolver.Register(() => contractOne, type, contractOne);
Assert.True(resolver.HasRegistration(type));
Assert.True(resolver.HasRegistration(type, contractOne));
Assert.False(resolver.HasRegistration(type, contractTwo));

resolver.Register(() => contractTwo, type, contractTwo);
Assert.True(resolver.HasRegistration(type));
Assert.True(resolver.HasRegistration(type, contractOne));
Assert.True(resolver.HasRegistration(type, contractTwo));
}

/// <inheritdoc />
protected override AutofacDependencyResolver GetDependencyResolver()
{
var container = new ContainerBuilder();
return new AutofacDependencyResolver(container.Build());
var containerBuilder = new ContainerBuilder();

return containerBuilder.UseAutofacDependencyResolver();
}
}
}
4 changes: 0 additions & 4 deletions src/Splat.Autofac.Tests/Splat.Autofac.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<NoWarn>$(NoWarn);1591;CA1707;SA1633;CA2000</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Splat.Autofac\Splat.Autofac.csproj" />
<ProjectReference Include="..\Splat.Common.Test\Splat.Common.Test.csproj" />
Expand Down
Loading