Skip to content

Commit

Permalink
ShouldRegisterServiceDescriptor -> FAIL
Browse files Browse the repository at this point in the history
  • Loading branch information
LBoullosa committed Jun 3, 2024
1 parent ad64773 commit 34ae0f1
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ private static string[] CreateRandomPathArray()
private static Assembly CreateRandomAssembly()
{
string randomAssemblyName = GetRandomString();

var assemblyName =
new AssemblyName("randomAssemblyName");
new AssemblyName(randomAssemblyName);

AssemblyBuilder assemblyBuilder =
AssemblyBuilder.DefineDynamicAssembly(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections
{
public partial class ServiceCollectionServiceTests
{
[Fact]
private void ShouldRegisterServiceDescriptor()
{
// given
dynamic randomProperties = CreateRandomProperties();
dynamic inputProperties = randomProperties;

ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptor;
ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor;

IServiceCollection randomServiceCollection = randomProperties.ServiceCollection;
IServiceCollection expectedServiceCollection = randomServiceCollection;
IServiceCollection returnedServiceCollection = randomServiceCollection;

this.dependencyInjectionBroker
.Setup(broker =>
broker.RegisterServiceDescriptor(
It.Is<ServiceDescriptor>(actualServiceDescriptor =>
actualServiceDescriptor == inputServiceDescriptor)))
.Returns(returnedServiceCollection);

// when
IServiceCollection actualServiceCollection =
this.serviceCollectionService.RegisterServiceDescriptor(
inputProperties.SpalInterfaceType,
inputProperties.ImplementationType,
inputProperties.ServiceLifeTime);

//then
actualServiceCollection.Should().BeSameAs(expectedServiceCollection);

this.dependencyInjectionBroker.Verify(
broker =>
broker.RegisterServiceDescriptor(
It.Is<ServiceDescriptor>(actualServiceDescriptor =>
actualServiceDescriptor == inputServiceDescriptor)),
Times.Once);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using STX.SPAL.Abstractions;
using STX.SPAL.Core.Brokers.DependenciesInjection;
using STX.SPAL.Core.Models.Services.Foundations.ServicesCollections;
using Tynamix.ObjectFiller;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections
{
public partial class ServiceCollectionServiceTests
{
private readonly Mock<IDependencyInjectionBroker> dependencyInjectionBroker;
private readonly IServiceCollectionService serviceCollectionService;

public ServiceCollectionServiceTests()
{
this.dependencyInjectionBroker = new Mock<IDependencyInjectionBroker>();

this.serviceCollectionService =
new ServiceCollectionService(dependencyInjectionBroker.Object);
}

private static string GetRandomString() =>
new MnemonicString().GetValue();

private static AssemblyBuilder CreateRandomAssembly()
{
string randomAssemblyName = GetRandomString();

var assemblyName =
new AssemblyName(randomAssemblyName);

AssemblyBuilder assemblyBuilder =
AssemblyBuilder.DefineDynamicAssembly(
assemblyName,
AssemblyBuilderAccess.Run);

return assemblyBuilder;
}

private static Type CreateRandomSpalInterfaceType()
{
AssemblyBuilder spalAssembly = CreateRandomAssembly();
string assemblyName = spalAssembly.GetName().Name;
ModuleBuilder moduleBuilder = spalAssembly.DefineDynamicModule(assemblyName);

TypeBuilder typeBuilder = moduleBuilder.DefineType(
name: GetRandomString(),
attr: TypeAttributes.Public,
parent: null,
interfaces: new Type[]
{
typeof(ISPALBase)
});

return typeBuilder;
}

private static Type CreateRandomImplementationType()
{
AssemblyBuilder spalAssembly = CreateRandomAssembly();
string assemblyName = spalAssembly.GetName().Name;
ModuleBuilder moduleBuilder = spalAssembly.DefineDynamicModule(assemblyName);

TypeBuilder typeBuilder = moduleBuilder.DefineType(
name: GetRandomString(),
attr: TypeAttributes.Public,
parent: null,
interfaces: new Type[]
{
typeof(ISPALBase)
});

return typeBuilder;
}

private static ServiceLifetime CreateRandomServiceLifeTime()
{
int randomNumber = new IntRange(min: 0, max: 2).GetValue();

return randomNumber switch
{
0 => ServiceLifetime.Singleton,
1 => ServiceLifetime.Scoped,
_ => ServiceLifetime.Transient
};
}

private static dynamic CreateRandomProperties()
{
Type randomSpalInterfaceType = CreateRandomSpalInterfaceType();
string randomSpalId = GetRandomString();
Type randomImplementationType = CreateRandomImplementationType();
ServiceLifetime randomServiceLifeTime = CreateRandomServiceLifeTime();

return new
{
SpalInterfaceType = randomSpalInterfaceType,
SpalId = randomSpalId,
ImplementationType = randomImplementationType,
ServiceLifeTime = randomServiceLifeTime,

ServiceDescriptor =
new ServiceDescriptor(
randomSpalInterfaceType,
randomSpalId,
randomImplementationType,
randomServiceLifeTime),

ServiceCollection = new ServiceCollection()
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection
{
internal partial class DependencyInjectionBroker
{
public void RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor) =>
serviceColllection.Add(serviceDescriptor);
public IServiceCollection RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor) =>
serviceCollection.Add(serviceDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection
internal partial class DependencyInjectionBroker
{
public IServiceProvider BuildServiceProvider() =>
serviceColllection.BuildServiceProvider();
serviceCollection.BuildServiceProvider();

public T ResolveImplementation<T>(ServiceProvider serviceProvider) =>
serviceProvider.GetRequiredService<T>();

T ResolveImplementation<T>(ServiceProvider serviceProvider, string spalId) =>
public T ResolveImplementation<T>(ServiceProvider serviceProvider, string spalId) =>
serviceProvider.GetRequiredKeyedService<T>(spalId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection
{
internal partial class DependencyInjectionBroker : IDependencyInjectionBroker
{
private readonly ServiceCollection serviceColllection;
private readonly ServiceCollection serviceCollection;

public DependencyInjectionBroker()
{
serviceColllection = new ServiceCollection();
serviceCollection = new ServiceCollection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace STX.SPAL.Core.Brokers.DependenciesInjection
{
internal partial interface IDependencyInjection
internal partial interface IDependencyInjectionBroker
{
void RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor);
IServiceCollection RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace STX.SPAL.Core.Brokers.DependenciesInjection
{
internal partial interface IDependencyInjection
internal partial interface IDependencyInjectionBroker
{
IServiceProvider BuildServiceProvider();
T ResolveImplementation<T>(ServiceProvider serviceProvider);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Extensions.DependencyInjection;

namespace STX.SPAL.Core.Models.Services.Foundations.ServicesCollections
{
internal partial interface IServiceCollectionService
{
IServiceCollection RegisterServiceDescriptor(
Type spalInterfaceType,
Type implementationType,
ServiceLifetime serviceLifetime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Extensions.DependencyInjection;
using STX.SPAL.Core.Brokers.DependenciesInjection;

namespace STX.SPAL.Core.Models.Services.Foundations.ServicesCollections
{
internal partial class ServiceCollectionService : IServiceCollectionService
{
private readonly IDependencyInjectionBroker dependencyInjectionBroker;

public ServiceCollectionService(IDependencyInjectionBroker dependencyInjectionBroker)
{
this.dependencyInjectionBroker = dependencyInjectionBroker;
}

public IServiceCollection RegisterServiceDescriptor(
Type spalInterfaceType,
Type implementationType,
ServiceLifetime serviceLifetime)
{
throw new System.NotImplementedException();
}
}
}

0 comments on commit 34ae0f1

Please sign in to comment.