Skip to content

Commit 802540d

Browse files
committed
improve tests
1 parent 4d802b5 commit 802540d

5 files changed

+143
-11
lines changed

src/Injectio.Generators/ServiceRegistrationGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private static (IReadOnlyCollection<Diagnostic> diagnostics, bool hasServiceColl
179179
if (!hasServiceCollection)
180180
{
181181
var diagnostic = Diagnostic.Create(
182-
DiagnosticDescriptors.InvalidModuleParameter,
182+
DiagnosticDescriptors.InvalidServiceCollectionParameter,
183183
methodDeclaration.GetLocation(),
184184
parameterSymbol.Name,
185185
methodName
@@ -243,12 +243,12 @@ private static (IReadOnlyCollection<Diagnostic> diagnostics, bool hasServiceColl
243243
var typeParameter = attributeClass.TypeParameters[index];
244244
var typeArgument = attributeClass.TypeArguments[index];
245245

246-
if (typeParameter.Name == "TService")
246+
if (typeParameter.Name == "TService" || index == 0)
247247
{
248248
var service = typeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
249249
serviceTypes.Add(service);
250250
}
251-
else if (typeParameter.Name == "TImplementation")
251+
else if (typeParameter.Name == "TImplementation" || index == 1)
252252
{
253253
implementationType = typeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
254254
}

tests/Injectio.Tests/Injectio.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<IsPackable>false</IsPackable>
77
<LangVersion>latest</LangVersion>

tests/Injectio.Tests/ServiceRegistrationGeneratorTests.cs

+105-7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,78 @@ public static void Register(IServiceCollection services)
257257
.ScrubLinesContaining("GeneratedCodeAttribute");
258258
}
259259

260+
[Fact]
261+
public Task GenerateRegisterServicesInvalidMethod()
262+
{
263+
var source = @"
264+
using Injectio.Attributes;
265+
using Microsoft.Extensions.DependencyInjection;
266+
using Microsoft.Extensions.DependencyInjection.Extensions;
267+
268+
namespace Injectio.Sample;
269+
270+
public interface IModuleService
271+
{
272+
}
273+
274+
public class ModuleService : IModuleService
275+
{
276+
}
277+
278+
public static class RegistrationModule
279+
{
280+
[RegisterServices]
281+
public static void Register(IServiceCollection services, string test)
282+
{
283+
services.TryAddTransient<IModuleService, ModuleService>();
284+
}
285+
}
286+
";
287+
288+
var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);
289+
290+
diagnostics.Should().NotBeEmpty();
291+
diagnostics[0].Id.Should().Be("SD0010");
292+
293+
return Task.CompletedTask;
294+
}
295+
296+
[Fact]
297+
public Task GenerateRegisterServicesInvalidService()
298+
{
299+
var source = @"
300+
using Injectio.Attributes;
301+
using Microsoft.Extensions.DependencyInjection;
302+
using Microsoft.Extensions.DependencyInjection.Extensions;
303+
304+
namespace Injectio.Sample;
305+
306+
public interface IModuleService
307+
{
308+
}
309+
310+
public class ModuleService : IModuleService
311+
{
312+
}
313+
314+
public static class RegistrationModule
315+
{
316+
[RegisterServices]
317+
public static void Register(string test)
318+
{
319+
services.TryAddTransient<IModuleService, ModuleService>();
320+
}
321+
}
322+
";
323+
324+
var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);
325+
326+
diagnostics.Should().NotBeEmpty();
327+
diagnostics[0].Id.Should().Be("SD0011");
328+
329+
return Task.CompletedTask;
330+
}
331+
260332
[Fact]
261333
public Task GenerateRegisterSingletonFactory()
262334
{
@@ -351,6 +423,33 @@ public class ServiceTag : IServiceTag
351423
.ScrubLinesContaining("GeneratedCodeAttribute");
352424
}
353425

426+
#if NET7_0_OR_GREATER
427+
[Fact]
428+
public Task GenerateRegisterSingletonGeneric()
429+
{
430+
var source = @"
431+
using Injectio.Attributes;
432+
433+
namespace Injectio.Sample;
434+
435+
public interface IService { }
436+
437+
[RegisterSingleton<IService, SingletonService>(Duplicate = DuplicateStrategy.Replace)]
438+
public class SingletonService : IService
439+
{ }
440+
";
441+
442+
var (diagnostics, output) = GetGeneratedOutput<ServiceRegistrationGenerator>(source);
443+
444+
diagnostics.Should().BeEmpty();
445+
446+
return Verifier
447+
.Verify(output)
448+
.UseDirectory("Snapshots")
449+
.ScrubLinesContaining("GeneratedCodeAttribute");
450+
}
451+
#endif
452+
354453
[Fact]
355454
public Task GenerateRegisterSingletonServiceKeys()
356455
{
@@ -361,24 +460,24 @@ public Task GenerateRegisterSingletonServiceKeys()
361460
362461
namespace Injectio.Sample;
363462
364-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Alpha"")]
463+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Alpha"")]
365464
public class ServiceAlphaKeyed : IServiceKeyed
366465
{ }
367466
368-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Beta"")]
467+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Beta"")]
369468
public class ServiceBetaKeyed : IServiceKeyed
370469
{ }
371470
372-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Alpha)]
471+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Alpha)]
373472
public class ServiceAlphaTypeKeyed : IServiceKeyed
374473
{ }
375474
376-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Beta)]
475+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Beta)]
377476
public class ServiceBetaTypeKeyed : IServiceKeyed
378477
{ }
379478
380-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
381-
[RegisterSingleton<IServiceKeyed>(ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
479+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
480+
[RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
382481
public class ServiceFactoryKeyed : IServiceKeyed
383482
{
384483
public ServiceFactoryKeyed(object? serviceKey)
@@ -415,7 +514,6 @@ public enum ServiceType
415514
}
416515

417516

418-
419517
private static (ImmutableArray<Diagnostic> Diagnostics, string Output) GetGeneratedOutput<T>(string source)
420518
where T : IIncrementalGenerator, new()
421519
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// <auto-generated />
2+
#nullable enable
3+
4+
namespace Microsoft.Extensions.DependencyInjection
5+
{
6+
/// <summary>
7+
/// Extension methods for discovered service registrations
8+
/// </summary>
9+
public static class DiscoveredServicesExtensions
10+
{
11+
/// <summary>
12+
/// Adds discovered services from Test.Generator to the specified service collection
13+
/// </summary>
14+
/// <param name="serviceCollection">The service collection.</param>
15+
/// <param name="tags">The service registration tags to include.</param>
16+
/// <returns>The service collection</returns>
17+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddTestGenerator(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, params string[]? tags)
18+
{
19+
var tagSet = new global::System.Collections.Generic.HashSet<string>(tags ?? global::System.Linq.Enumerable.Empty<string>());
20+
21+
global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.Replace(
22+
serviceCollection,
23+
global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
24+
typeof(global::Injectio.Sample.IService),
25+
typeof(global::Injectio.Sample.SingletonService),
26+
global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
27+
)
28+
);
29+
30+
return serviceCollection;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)