@@ -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
362461namespace Injectio.Sample;
363462
364- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Alpha"")]
463+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Alpha"")]
365464public class ServiceAlphaKeyed : IServiceKeyed
366465{ }
367466
368- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Beta"")]
467+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Beta"")]
369468public class ServiceBetaKeyed : IServiceKeyed
370469{ }
371470
372- [RegisterSingleton<IServiceKeyed>( ServiceKey = ServiceType.Alpha)]
471+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Alpha)]
373472public class ServiceAlphaTypeKeyed : IServiceKeyed
374473{ }
375474
376- [RegisterSingleton<IServiceKeyed>( ServiceKey = ServiceType.Beta)]
475+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Beta)]
377476public 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))]
382481public 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 {
0 commit comments