Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Immediate.Injections.Analyzers/RegisterTypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,34 @@ public bool AnalyzeRegisterType0Attribute()
valid = false;
}
}
else if (containerSymbol.IsGenericType)
{
if (useProxy)
{
context.ReportDiagnostic(
Diagnostic.Create(
RegisterTypeAnalyzer.CannotUseProxyFactoryForOpenGeneric,
location,
containerSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)
)
);

valid = false;
}

if (factory is { })
{
context.ReportDiagnostic(
Diagnostic.Create(
RegisterTypeAnalyzer.CannotUseFactoryMethodWithOpenGeneric,
location,
containerSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)
)
);

valid = false;
}
}

switch (registrationStrategy)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,84 @@ public interface IService<T>;
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task UseProxyFactoryWithInferredOpenGenericSelfStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

[{|INJ0007:{|INJ0008:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.Self, UseProxyFactory = true)|}|}|}]
public class Class<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task UseProxyFactoryWithInferredOpenGenericImplementedInterfacesStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

public interface IService<T>;

[{|INJ0008:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces, UseProxyFactory = true)|}|}]
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);
Comment on lines +20 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Cover the remaining inferred registration strategies.

Both new tests use RegistrationStrategy.ImplementedInterfaces; add inferred Self and SelfAndImplementedInterfaces cases, including assembly-default variants, so regressions in strategy resolution cannot silently lose INJ0008.

Also applies to: 33-46

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs`
around lines 20 - 31, Extend RegisterTypeAnalyzerTests with coverage for
inferred Self and SelfAndImplementedInterfaces strategies, mirroring the
existing open-generic proxy-factory test. Add cases using explicit strategies
and assembly-default configuration variants, and assert each still reports
INJ0008 alongside the existing registration diagnostic.


[Fact]
public async Task UseProxyFactoryWithInferredOpenGenericSelfAndImplementedInterfacesStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

public interface IService<T>;

[{|INJ0008:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.SelfAndImplementedInterfaces, UseProxyFactory = true)|}|}]
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task UseProxyFactoryWithAssemblyDefaultInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces)]

public interface IService<T>;

[{|INJ0008:{|INJ0002:RegisterSingleton(UseProxyFactory = true)|}|}]
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

[Fact]
public async Task UseProxyFactoryWithAssemblyDefaultSelfInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.Self)]

[{|INJ0007:{|INJ0008:{|INJ0002:RegisterSingleton(UseProxyFactory = true)|}|}|}]
public class Class<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task UseProxyFactoryWithAssemblyDefaultSelfAndImplementedInterfacesInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.SelfAndImplementedInterfaces)]

public interface IService<T>;

[{|INJ0008:{|INJ0002:RegisterSingleton(UseProxyFactory = true)|}|}]
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,114 @@ public class Class<T> : IService<T>
}
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task FactoryMethodWithInferredOpenGenericSelfStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

[{|INJ0012:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.Self, Factory = nameof(Factory))|}|}]
public class Class<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task FactoryMethodWithInferredOpenGenericImplementedInterfacesStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

public interface IService<T>;

[{|INJ0012:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces, Factory = nameof(Factory))|}|}]
public class Class<T> : IService<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

[Fact]
public async Task FactoryMethodWithInferredOpenGenericSelfAndImplementedInterfacesStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

public interface IService<T>;

[{|INJ0012:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.SelfAndImplementedInterfaces, Factory = nameof(Factory))|}|}]
public class Class<T> : IService<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task FactoryMethodWithAssemblyDefaultInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces)]

public interface IService<T>;

[{|INJ0012:{|INJ0002:RegisterSingleton(Factory = nameof(Factory))|}|}]
public class Class<T> : IService<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task FactoryMethodWithAssemblyDefaultSelfInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.Self)]

[{|INJ0012:{|INJ0002:RegisterSingleton(Factory = nameof(Factory))|}|}]
public class Class<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);

[Fact]
public async Task FactoryMethodWithAssemblyDefaultSelfAndImplementedInterfacesInferredOpenGenericTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using System;

using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.SelfAndImplementedInterfaces)]

public interface IService<T>;

[{|INJ0012:{|INJ0002:RegisterSingleton(Factory = nameof(Factory))|}|}]
public class Class<T> : IService<T>
{
public static Class<T> Factory(IServiceProvider provider) => new();
}
"""
).RunAsync(TestContext.Current.CancellationToken);
}