Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 0a28113

Browse files
committed
(maint) added some unit tests
1 parent 58f969f commit 0a28113

File tree

5 files changed

+201
-24
lines changed

5 files changed

+201
-24
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using SimpleInjector;
5+
6+
using Spectre.Console.Cli;
7+
8+
namespace Spectre.Console.Registrars.SimpleInjector.Tests
9+
{
10+
internal class RegistrarFixture
11+
{
12+
private readonly List<Action<Container>> containerActions = new List<Action<Container>>();
13+
private readonly List<Action<ITypeRegistrar>> registrarActions = new List<Action<ITypeRegistrar>>();
14+
private Lifestyle lifestyle;
15+
private Type[] multiRegistrationTypes;
16+
17+
internal void GivenLifestyle(Lifestyle lifestyle)
18+
{
19+
this.lifestyle = lifestyle;
20+
}
21+
22+
internal void GivenMultiRegistrationTypes(params Type[] multiRegistrationTypes)
23+
{
24+
this.multiRegistrationTypes = multiRegistrationTypes;
25+
}
26+
27+
internal void GivenOnContainer(Action<Container> action)
28+
{
29+
containerActions.Add(action);
30+
}
31+
32+
internal void GivenOnRegistrar(Action<ITypeRegistrar> action)
33+
{
34+
registrarActions.Add(action);
35+
}
36+
37+
internal ITypeResolver GetResolver()
38+
{
39+
var container = new Container();
40+
foreach (var action in containerActions)
41+
{
42+
action(container);
43+
}
44+
45+
var registrar = new SimpleInjectorRegistrar(container, lifestyle, multiRegistrationTypes);
46+
foreach (var action in registrarActions)
47+
{
48+
action(registrar);
49+
}
50+
51+
return registrar.Build();
52+
}
53+
}
54+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Shouldly;
2+
3+
using Xunit;
4+
5+
namespace Spectre.Console.Registrars.SimpleInjector.Tests
6+
{
7+
public class RegistrarTests
8+
{
9+
[Fact]
10+
public void Resolver_Should_Return_Registration_From_Container()
11+
{
12+
var fixture = new RegistrarFixture();
13+
fixture.GivenOnContainer(c => c.Register<ISomeInterface, SomeDependency>());
14+
15+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
16+
17+
actual.ShouldNotBeNull();
18+
actual.ShouldBeOfType<SomeDependency>();
19+
}
20+
21+
[Fact]
22+
public void Resolver_Should_Return_Registration_From_Registrar()
23+
{
24+
var fixture = new RegistrarFixture();
25+
fixture.GivenOnRegistrar(r => r.Register(typeof(ISomeInterface), typeof(SomeDependency)));
26+
27+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
28+
29+
actual.ShouldNotBeNull();
30+
actual.ShouldBeOfType<SomeDependency>();
31+
}
32+
33+
[Fact]
34+
public void Resolver_Should_Return_Instance_From_Registrar()
35+
{
36+
var fixture = new RegistrarFixture();
37+
var expected = new SomeDependency();
38+
fixture.GivenOnRegistrar(r => r.RegisterInstance(typeof(ISomeInterface), expected));
39+
40+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
41+
42+
actual.ShouldNotBeNull();
43+
ReferenceEquals(expected, actual).ShouldBeTrue();
44+
}
45+
46+
[Fact]
47+
public void Resolver_Should_Return_Lazy_From_Registrar()
48+
{
49+
var fixture = new RegistrarFixture();
50+
var expected = new SomeDependency();
51+
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () => expected));
52+
53+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
54+
55+
actual.ShouldNotBeNull();
56+
ReferenceEquals(expected, actual).ShouldBeTrue();
57+
}
58+
59+
[Fact(Skip = "Doesn't work at the moment")]
60+
public void Resolver_Should_Not_Call_Lazy_Factory_If_Not_Needed()
61+
{
62+
var fixture = new RegistrarFixture();
63+
var factoryCalled = false;
64+
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () =>
65+
{
66+
factoryCalled = true;
67+
return new SomeDependency();
68+
}));
69+
70+
fixture.GetResolver();
71+
72+
factoryCalled.ShouldBeFalse();
73+
}
74+
75+
private interface ISomeInterface
76+
{
77+
}
78+
79+
private class SomeDependency : ISomeInterface
80+
{
81+
}
82+
83+
private class SomeOtherDependency : ISomeInterface
84+
{
85+
}
86+
}
87+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
9+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
10+
<PrivateAssets>all</PrivateAssets>
11+
</PackageReference>
12+
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
14+
<PackageReference Include="Moq" Version="4.16.1" />
15+
<PackageReference Include="Shouldly" Version="4.0.3" />
16+
<PackageReference Include="SimpleInjector" Version="5.3.2" />
17+
<PackageReference Include="Spectre.Console" Version="0.41.0" />
18+
<PackageReference Include="xunit" Version="2.4.1" />
19+
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
20+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
22+
<PrivateAssets>all</PrivateAssets>
23+
</PackageReference>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\Spectre.Console.Registrars.SimpleInjector\Spectre.Console.Registrars.SimpleInjector.csproj" />
28+
</ItemGroup>
29+
30+
</Project>

src/Spectre.Console.Registrars.SimpleInjector.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30114.105
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectre.Console.Registrars.SimpleInjector", "Spectre.Console.Registrars.SimpleInjector\Spectre.Console.Registrars.SimpleInjector.csproj", "{3C1439A7-B1E2-4321-9878-68376D7048FC}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectre.Console.Registrars.SimpleInjector.Tests", "Spectre.Console.Registrars.SimpleInjector.Tests\Spectre.Console.Registrars.SimpleInjector.Tests.csproj", "{A9545423-59A6-43C7-BC0B-A2F775E5AD0F}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
1820
{3C1439A7-B1E2-4321-9878-68376D7048FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
1921
{3C1439A7-B1E2-4321-9878-68376D7048FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
2022
{3C1439A7-B1E2-4321-9878-68376D7048FC}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{A9545423-59A6-43C7-BC0B-A2F775E5AD0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{A9545423-59A6-43C7-BC0B-A2F775E5AD0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{A9545423-59A6-43C7-BC0B-A2F775E5AD0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{A9545423-59A6-43C7-BC0B-A2F775E5AD0F}.Release|Any CPU.Build.0 = Release|Any CPU
2127
EndGlobalSection
2228
EndGlobal

src/Spectre.Console.Registrars.SimpleInjector/SimlpeInjectorRegistrar.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace Spectre.Console.Registrars.SimpleInjector
1010
/// </summary>
1111
public class SimpleInjectorRegistrar : ITypeRegistrar
1212
{
13-
private readonly Container _container;
14-
private readonly Lifestyle _lifestyle;
15-
private readonly Type[] _multiRegistrationTypes;
13+
private readonly Container container;
14+
private readonly Lifestyle lifestyle;
15+
private readonly Type[] multiRegistrationTypes;
1616

1717
/// <summary>
1818
/// Constructs a new instance using the the given <see cref="Container" />.
@@ -29,13 +29,13 @@ public class SimpleInjectorRegistrar : ITypeRegistrar
2929
/// <param name="multiRegistrationTypes">List of types that are to be registered multiple times.
3030
/// Default is [ <see cref="ICommand{TSettings}"/>, <see cref="ICommand"/> ].</param>
3131
/// <exception cref="ArgumentNullException"></exception>
32-
public SimpleInjectorRegistrar(Container container,
32+
public SimpleInjectorRegistrar(Container container,
3333
Lifestyle lifestyle = null,
3434
Type[] multiRegistrationTypes = null)
3535
{
36-
_container = container ?? throw new ArgumentNullException(nameof(container));
37-
_lifestyle = lifestyle ?? Lifestyle.Singleton;
38-
_multiRegistrationTypes = multiRegistrationTypes ?? new[]
36+
this.container = container ?? throw new ArgumentNullException(nameof(container));
37+
this.lifestyle = lifestyle ?? Lifestyle.Singleton;
38+
this.multiRegistrationTypes = multiRegistrationTypes ?? new[]
3939
{
4040
typeof(ICommand),
4141
typeof(ICommand<>)
@@ -45,55 +45,55 @@ public SimpleInjectorRegistrar(Container container,
4545
/// <inheritdoc cref="ITypeRegistrar.Register"/>
4646
public void Register(Type service, Type implementation)
4747
{
48-
if (_multiRegistrationTypes.Contains(service))
48+
if (multiRegistrationTypes.Contains(service))
4949
{
50-
_container.Collection.Append(service, implementation, _lifestyle);
50+
container.Collection.Append(service, implementation, lifestyle);
5151
return;
5252
}
5353

54-
_container.Register(service, implementation, _lifestyle);
54+
container.Register(service, implementation, lifestyle);
5555
}
5656

5757
/// <inheritdoc cref="ITypeRegistrar.RegisterInstance"/>
5858
public void RegisterInstance(Type service, object implementation)
5959
{
60-
if (_multiRegistrationTypes.Contains(service))
60+
if (multiRegistrationTypes.Contains(service))
6161
{
62-
_container.Collection.AppendInstance(service, implementation);
62+
container.Collection.AppendInstance(service, implementation);
6363
return;
6464
}
65-
66-
_container.RegisterInstance(service, implementation);
65+
66+
container.RegisterInstance(service, implementation);
6767
}
6868

6969
/// <inheritdoc cref="ITypeRegistrar.RegisterLazy"/>
7070
public void RegisterLazy(Type service, Func<object> factory)
7171
{
7272
// todo: non of these code-paths are lazy!!
73-
if (_multiRegistrationTypes.Contains(service))
73+
if (multiRegistrationTypes.Contains(service))
7474
{
7575
// todo: this ignores the lifestyle, but seemingly Func<object> can not be used on collections.
76-
_container.Collection.AppendInstance(service, factory());
76+
container.Collection.AppendInstance(service, factory());
7777
return;
7878
}
79-
80-
_container.Register(service, factory, _lifestyle);
79+
80+
container.Register(service, factory, lifestyle);
8181
}
8282

8383
/// <inheritdoc cref="ITypeRegistrar.Build"/>
8484
public ITypeResolver Build()
8585
{
86-
_container.Verify();
87-
return new SimpleInjectorTypeResolver(_container);
86+
container.Verify();
87+
return new SimpleInjectorTypeResolver(container);
8888
}
8989

9090
private class SimpleInjectorTypeResolver : ITypeResolver
9191
{
92-
private readonly Container _container;
92+
private readonly Container container;
9393

9494
public SimpleInjectorTypeResolver(Container container)
9595
{
96-
_container = container;
96+
this.container = container;
9797
}
9898

9999
public object Resolve(Type type)
@@ -103,8 +103,8 @@ public object Resolve(Type type)
103103
throw new ArgumentNullException(nameof(type));
104104
}
105105

106-
return _container.GetInstance(type);
106+
return container.GetInstance(type);
107107
}
108108
}
109109
}
110-
}
110+
}

0 commit comments

Comments
 (0)