Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Nov 22, 2023
1 parent 4d802b5 commit 802540d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Injectio.Generators/ServiceRegistrationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static (IReadOnlyCollection<Diagnostic> diagnostics, bool hasServiceColl
if (!hasServiceCollection)
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptors.InvalidModuleParameter,
DiagnosticDescriptors.InvalidServiceCollectionParameter,
methodDeclaration.GetLocation(),
parameterSymbol.Name,
methodName
Expand Down Expand Up @@ -243,12 +243,12 @@ private static (IReadOnlyCollection<Diagnostic> diagnostics, bool hasServiceColl
var typeParameter = attributeClass.TypeParameters[index];
var typeArgument = attributeClass.TypeArguments[index];

if (typeParameter.Name == "TService")
if (typeParameter.Name == "TService" || index == 0)
{
var service = typeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
serviceTypes.Add(service);
}
else if (typeParameter.Name == "TImplementation")
else if (typeParameter.Name == "TImplementation" || index == 1)
{
implementationType = typeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Injectio.Tests/Injectio.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
Expand Down
112 changes: 105 additions & 7 deletions tests/Injectio.Tests/ServiceRegistrationGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,78 @@ public static void Register(IServiceCollection services)
.ScrubLinesContaining("GeneratedCodeAttribute");
}

[Fact]
public Task GenerateRegisterServicesInvalidMethod()
{
var source = @"
using Injectio.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Injectio.Sample;
public interface IModuleService
{
}
public class ModuleService : IModuleService
{
}
public static class RegistrationModule
{
[RegisterServices]
public static void Register(IServiceCollection services, string test)
{
services.TryAddTransient<IModuleService, ModuleService>();
}
}
";

var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);

diagnostics.Should().NotBeEmpty();
diagnostics[0].Id.Should().Be("SD0010");

return Task.CompletedTask;
}

[Fact]
public Task GenerateRegisterServicesInvalidService()
{
var source = @"
using Injectio.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Injectio.Sample;
public interface IModuleService
{
}
public class ModuleService : IModuleService
{
}
public static class RegistrationModule
{
[RegisterServices]
public static void Register(string test)
{
services.TryAddTransient<IModuleService, ModuleService>();
}
}
";

var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);

diagnostics.Should().NotBeEmpty();
diagnostics[0].Id.Should().Be("SD0011");

return Task.CompletedTask;
}

[Fact]
public Task GenerateRegisterSingletonFactory()
{
Expand Down Expand Up @@ -351,6 +423,33 @@ public class ServiceTag : IServiceTag
.ScrubLinesContaining("GeneratedCodeAttribute");
}

#if NET7_0_OR_GREATER
[Fact]
public Task GenerateRegisterSingletonGeneric()
{
var source = @"
using Injectio.Attributes;
namespace Injectio.Sample;
public interface IService { }
[RegisterSingleton<IService, SingletonService>(Duplicate = DuplicateStrategy.Replace)]
public class SingletonService : IService
{ }
";

var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);

diagnostics.Should().BeEmpty();

return Verifier
.Verify(output)
.UseDirectory("Snapshots")
.ScrubLinesContaining("GeneratedCodeAttribute");
}
#endif

[Fact]
public Task GenerateRegisterSingletonServiceKeys()
{
Expand All @@ -361,24 +460,24 @@ public Task GenerateRegisterSingletonServiceKeys()
namespace Injectio.Sample;
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Alpha"")]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Alpha"")]
public class ServiceAlphaKeyed : IServiceKeyed
{ }
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Beta"")]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Beta"")]
public class ServiceBetaKeyed : IServiceKeyed
{ }
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Alpha)]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Alpha)]
public class ServiceAlphaTypeKeyed : IServiceKeyed
{ }
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Beta)]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Beta)]
public class ServiceBetaTypeKeyed : IServiceKeyed
{ }
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
public class ServiceFactoryKeyed : IServiceKeyed
{
public ServiceFactoryKeyed(object? serviceKey)
Expand Down Expand Up @@ -415,7 +514,6 @@ public enum ServiceType
}



private static (ImmutableArray<Diagnostic> Diagnostics, string Output) GetGeneratedOutput<T>(string source)
where T : IIncrementalGenerator, new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <auto-generated />
#nullable enable

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for discovered service registrations
/// </summary>
public static class DiscoveredServicesExtensions
{
/// <summary>
/// Adds discovered services from Test.Generator to the specified service collection
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="tags">The service registration tags to include.</param>
/// <returns>The service collection</returns>
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddTestGenerator(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, params string[]? tags)
{
var tagSet = new global::System.Collections.Generic.HashSet<string>(tags ?? global::System.Linq.Enumerable.Empty<string>());

global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.Replace(
serviceCollection,
global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
typeof(global::Injectio.Sample.IService),
typeof(global::Injectio.Sample.SingletonService),
global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
)
);

return serviceCollection;
}
}
}

0 comments on commit 802540d

Please sign in to comment.