feat: wip...migrating to proper app
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
[*.cs]
|
||||
dotnet_diagnostic.CA1707.severity = none
|
||||
@@ -1,13 +1,13 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageVersion Include="xunit.v3" Version="3.0.0" />
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="AwesomeAssertions" Version="9.1.0" />
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageVersion Include="Moq" Version="4.20.72" />
|
||||
<PackageVersion Include="Spectre.Console.Testing" Version="0.50.0" />
|
||||
<PackageVersion Include="xunit.v3" Version="3.0.0" />
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,7 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AwesomeAssertions" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="Moq" />
|
||||
<PackageReference Include="Spectre.Console.Testing" />
|
||||
<PackageReference Include="xunit.v3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
namespace StreamShorts.Console.Tests.Unit.Hosting;
|
||||
|
||||
public class TypeRegistrarTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldNotThrowShould()
|
||||
{
|
||||
var mockBuilder = new Mock<IHostBuilder>();
|
||||
|
||||
var action = () => new TypeRegistrar(mockBuilder.Object);
|
||||
|
||||
action.Should().NotThrow<Exception>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_WhenCalled_ItShouldReturnResolverAndBuildHost()
|
||||
{
|
||||
var mockHost = new Mock<IHost>();
|
||||
var mockBuilder = new Mock<IHostBuilder>();
|
||||
|
||||
mockBuilder
|
||||
.Setup(static b => b.Build())
|
||||
.Returns(mockHost.Object);
|
||||
|
||||
var registrar = new TypeRegistrar(mockBuilder.Object);
|
||||
|
||||
var resolver = registrar.Build();
|
||||
|
||||
resolver.Should().BeOfType<TypeResolver>();
|
||||
mockBuilder.Verify(static b => b.Build(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenCalledWithType_ItShouldAddToContainer()
|
||||
{
|
||||
var builder = Host.CreateDefaultBuilder();
|
||||
var registrar = new TypeRegistrar(builder);
|
||||
registrar.Register(typeof(IService), typeof(ServiceImplementation));
|
||||
|
||||
using var host = builder.Build();
|
||||
var service = host.Services.GetService<IService>();
|
||||
|
||||
service.Should().NotBeNull();
|
||||
service.Should().BeOfType<ServiceImplementation>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterInstance_WhenCalledWithInstance_ItShouldAddToContainer()
|
||||
{
|
||||
var builder = Host.CreateDefaultBuilder();
|
||||
var registrar = new TypeRegistrar(builder);
|
||||
var instance = new ServiceImplementation();
|
||||
registrar.RegisterInstance(typeof(IService), instance);
|
||||
|
||||
using var host = builder.Build();
|
||||
var service = host.Services.GetService<IService>();
|
||||
|
||||
service.Should().BeSameAs(instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterLazy_WhenCalledWithFunc_ItShouldAddToContainer()
|
||||
{
|
||||
var builder = Host.CreateDefaultBuilder();
|
||||
var registrar = new TypeRegistrar(builder);
|
||||
registrar.RegisterLazy(typeof(IService), static () => new ServiceImplementation());
|
||||
|
||||
using var host = builder.Build();
|
||||
var service = host.Services.GetService<IService>();
|
||||
|
||||
service.Should().NotBeNull();
|
||||
service.Should().BeOfType<ServiceImplementation>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterLazy_WhenFuncIsNull_ItShouldThrow()
|
||||
{
|
||||
var builder = Host.CreateDefaultBuilder();
|
||||
var registrar = new TypeRegistrar(builder);
|
||||
|
||||
var action = () => registrar.RegisterLazy(typeof(IService), null!);
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private interface IService { }
|
||||
private sealed class ServiceImplementation : IService { }
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
namespace StreamShorts.Console.Tests.Unit.Hosting;
|
||||
|
||||
public class TypeResolverTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithNullHost_ItShouldThrowArgumentNullException()
|
||||
{
|
||||
var action = static () => new TypeResolver(null!);
|
||||
|
||||
action.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_WhenTypeIsNull_ItShouldReturnNull()
|
||||
{
|
||||
var mockHost = new Mock<IHost>();
|
||||
using var resolver = new TypeResolver(mockHost.Object);
|
||||
|
||||
var result = resolver.Resolve(null);
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_WhenCalledWithRegisteredType_ItShouldReturnAnInstance()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton(new TestService());
|
||||
|
||||
var mockHost = new Mock<IHost>();
|
||||
|
||||
mockHost
|
||||
.Setup(static h => h.Services)
|
||||
.Returns(services.BuildServiceProvider());
|
||||
|
||||
using var resolver = new TypeResolver(mockHost.Object);
|
||||
|
||||
var result = resolver.Resolve(typeof(TestService));
|
||||
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeOfType<TestService>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_WhenCalledWithUnregisteredType_ItShouldReturnNull()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var mockHost = new Mock<IHost>();
|
||||
|
||||
mockHost
|
||||
.Setup(static h => h.Services)
|
||||
.Returns(services.BuildServiceProvider());
|
||||
|
||||
using var resolver = new TypeResolver(mockHost.Object);
|
||||
|
||||
var result = resolver.Resolve(typeof(TestService));
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenCalled_ItShouldAlsoDisposeHost()
|
||||
{
|
||||
var mockHost = new Mock<IHost>();
|
||||
var resolver = new TypeResolver(mockHost.Object);
|
||||
|
||||
resolver.Dispose();
|
||||
|
||||
mockHost.Verify(static h => h.Dispose(), Times.Once);
|
||||
}
|
||||
|
||||
private sealed class TestService { }
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace StreamShorts.Console.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
Assert.True(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
global using AwesomeAssertions;
|
||||
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Hosting;
|
||||
|
||||
global using Moq;
|
||||
|
||||
global using StreamShorts.Console.Hosting;
|
||||
Reference in New Issue
Block a user