diff --git a/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs b/src/Common/ITypeSymbolExtensions.cs similarity index 69% rename from src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs rename to src/Common/ITypeSymbolExtensions.cs index be1b908a..ac56cb59 100644 --- a/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs +++ b/src/Common/ITypeSymbolExtensions.cs @@ -1,6 +1,7 @@ +using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; -namespace Immediate.Handlers.Generators; +namespace Immediate.Handlers; internal static class ITypeSymbolExtensions { @@ -90,4 +91,42 @@ typeSymbol is INamedTypeSymbol } } }; + + public static bool IsBehaviorsAttribute(this ITypeSymbol? typeSymbol) => + typeSymbol is + { + Name: "BehaviorsAttribute", + ContainingNamespace: + { + Name: "Shared", + ContainingNamespace: + { + Name: "Handlers", + ContainingNamespace: + { + Name: "Immediate", + ContainingNamespace.IsGlobalNamespace: true, + }, + }, + }, + }; + + public static bool IsIHandler([NotNullWhen(true)] this ITypeSymbol? typeSymbol) => + typeSymbol is INamedTypeSymbol + { + MetadataName: "IHandler`2", + ContainingNamespace: + { + Name: "Shared", + ContainingNamespace: + { + Name: "Handlers", + ContainingNamespace: + { + Name: "Immediate", + ContainingNamespace.IsGlobalNamespace: true, + }, + }, + }, + }; } diff --git a/src/Immediate.Handlers.Analyzers/ITypeSymbolExtensions.cs b/src/Immediate.Handlers.Analyzers/ITypeSymbolExtensions.cs deleted file mode 100644 index da66ee7c..00000000 --- a/src/Immediate.Handlers.Analyzers/ITypeSymbolExtensions.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Microsoft.CodeAnalysis; - -namespace Immediate.Handlers.Analyzers; - -internal static class ITypeSymbolExtensions -{ - public static bool IsBehaviorsAttribute(this ITypeSymbol? typeSymbol) => - typeSymbol is - { - Name: "BehaviorsAttribute", - ContainingNamespace: - { - Name: "Shared", - ContainingNamespace: - { - Name: "Handlers", - ContainingNamespace: - { - Name: "Immediate", - ContainingNamespace.IsGlobalNamespace: true, - }, - }, - }, - }; - - public static bool IsBehavior2(this ITypeSymbol typeSymbol) => - typeSymbol is - { - MetadataName: "Behavior`2", - ContainingNamespace: - { - Name: "Shared", - ContainingNamespace: - { - Name: "Handlers", - ContainingNamespace: - { - Name: "Immediate", - ContainingNamespace.IsGlobalNamespace: true, - }, - }, - }, - }; - - public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) => - typeSymbol.IsBehavior2() - || (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition)); - - public static bool IsCancellationToken(this ITypeSymbol typeSymbol) => - typeSymbol is INamedTypeSymbol - { - Name: "CancellationToken", - ContainingNamespace: - { - Name: "Threading", - ContainingNamespace: - { - Name: "System", - ContainingNamespace.IsGlobalNamespace: true - } - } - }; - - public static bool IsIHandler([NotNullWhen(true)] this ITypeSymbol? typeSymbol) => - typeSymbol is INamedTypeSymbol - { - MetadataName: "IHandler`2", - ContainingNamespace: - { - Name: "Shared", - ContainingNamespace: - { - Name: "Handlers", - ContainingNamespace: - { - Name: "Immediate", - ContainingNamespace.IsGlobalNamespace: true, - }, - }, - }, - }; -} diff --git a/src/Immediate.Handlers.Analyzers/Immediate.Handlers.Analyzers.csproj b/src/Immediate.Handlers.Analyzers/Immediate.Handlers.Analyzers.csproj index f4604932..f16865f6 100644 --- a/src/Immediate.Handlers.Analyzers/Immediate.Handlers.Analyzers.csproj +++ b/src/Immediate.Handlers.Analyzers/Immediate.Handlers.Analyzers.csproj @@ -1,21 +1,25 @@ - - netstandard2.0 - true - true - + + netstandard2.0 + true + true + - - - - - + + + - - minor - preview.0 - v - + + + + + + + + minor + preview.0 + v + diff --git a/src/Immediate.Handlers.CodeFixes/Immediate.Handlers.CodeFixes.csproj b/src/Immediate.Handlers.CodeFixes/Immediate.Handlers.CodeFixes.csproj index de9f7342..14ad5818 100644 --- a/src/Immediate.Handlers.CodeFixes/Immediate.Handlers.CodeFixes.csproj +++ b/src/Immediate.Handlers.CodeFixes/Immediate.Handlers.CodeFixes.csproj @@ -1,26 +1,30 @@ - - netstandard2.0 - true - true - + + netstandard2.0 + true + true + - - - + + + - - - - - - + + + - - minor - preview.0 - v - + + + + + + + + + minor + preview.0 + v + diff --git a/src/Immediate.Handlers.Generators/Immediate.Handlers.Generators.csproj b/src/Immediate.Handlers.Generators/Immediate.Handlers.Generators.csproj index 5fcf75dd..b27ccc73 100644 --- a/src/Immediate.Handlers.Generators/Immediate.Handlers.Generators.csproj +++ b/src/Immediate.Handlers.Generators/Immediate.Handlers.Generators.csproj @@ -1,39 +1,43 @@ - - netstandard2.0 - true - true - $(NoWarn);CA1716 - + + netstandard2.0 + true + true + $(NoWarn);CA1716 + - - - - + + + + + + + + + + + + + + + + + + + $(GetTargetPathDependsOn);GetDependencyTargetPaths + + - - - - - + + - - $(GetTargetPathDependsOn);GetDependencyTargetPaths - - - - - - - - - - minor - preview.0 - v - + + minor + preview.0 + v + diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.Models.cs similarity index 100% rename from src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs rename to src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.Models.cs diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformBehaviors.cs b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.TransformBehaviors.cs similarity index 100% rename from src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformBehaviors.cs rename to src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.TransformBehaviors.cs diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.TransformHandler.cs similarity index 93% rename from src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs rename to src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.TransformHandler.cs index de140e25..07167ad7 100644 --- a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs +++ b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.TransformHandler.cs @@ -90,7 +90,7 @@ CancellationToken cancellationToken private static EquatableReadOnlyList? GetOverrideBehaviors( INamedTypeSymbol symbol, CancellationToken cancellationToken) => - symbol.GetAttribute("Immediate.Handlers.Shared.BehaviorsAttribute") + symbol.GetBehaviorsAttribute() is { } ba ? ParseBehaviors(ba, cancellationToken) : null; @@ -129,3 +129,11 @@ private static void AddBaseTypes(ITypeSymbol type, List implements) AddBaseTypes(i, implements); } } + +file static class Extensions +{ + public static AttributeData? GetBehaviorsAttribute(this INamedTypeSymbol symbol) => + symbol + .GetAttributes() + .FirstOrDefault(a => a.AttributeClass.IsBehaviorsAttribute()); +} diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs similarity index 100% rename from src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs rename to src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs diff --git a/src/Immediate.Handlers.Generators/Utility.cs b/src/Immediate.Handlers.Generators/Utility.cs index 70a3aea9..e7cc0609 100644 --- a/src/Immediate.Handlers.Generators/Utility.cs +++ b/src/Immediate.Handlers.Generators/Utility.cs @@ -9,13 +9,6 @@ internal static class Utility ? ((INamedTypeSymbol)method.ReturnType).TypeArguments.FirstOrDefault() : null; - public static AttributeData? GetAttribute(this INamedTypeSymbol symbol, string attribute) => - symbol - .GetAttributes() - .FirstOrDefault(a => - a.AttributeClass?.ToString() == attribute - ); - public static string? NullIf(this string value, string check) => value.Equals(check, StringComparison.Ordinal) ? null : value; } diff --git a/tests/Immediate.Handlers.Tests/CodeFixTests/CodeFixTestHelper.cs b/tests/Immediate.Handlers.Tests/CodeFixTests/CodeFixTestHelper.cs index 3ac5052c..8ef35609 100644 --- a/tests/Immediate.Handlers.Tests/CodeFixTests/CodeFixTestHelper.cs +++ b/tests/Immediate.Handlers.Tests/CodeFixTests/CodeFixTestHelper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Immediate.Handlers.Tests.Helpers; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Testing; @@ -20,8 +21,8 @@ public static class CodeFixTestHelper """; public static CSharpCodeFixTest CreateCodeFixTest( - string inputSource, - string fixedSource, + [StringSyntax("c#-test")] string inputSource, + [StringSyntax("c#-test")] string fixedSource, DriverReferenceAssemblies assemblies, int codeActionIndex = 0 ) diff --git a/tests/Immediate.Handlers.Tests/CodeFixTests/HandlerClassCodeFixTests/Tests.HandleMethodDoesNotExist.cs b/tests/Immediate.Handlers.Tests/CodeFixTests/Tests.HandleMethodDoesNotExist.cs similarity index 94% rename from tests/Immediate.Handlers.Tests/CodeFixTests/HandlerClassCodeFixTests/Tests.HandleMethodDoesNotExist.cs rename to tests/Immediate.Handlers.Tests/CodeFixTests/Tests.HandleMethodDoesNotExist.cs index 37b1e9aa..114aa849 100644 --- a/tests/Immediate.Handlers.Tests/CodeFixTests/HandlerClassCodeFixTests/Tests.HandleMethodDoesNotExist.cs +++ b/tests/Immediate.Handlers.Tests/CodeFixTests/Tests.HandleMethodDoesNotExist.cs @@ -2,7 +2,7 @@ using Immediate.Handlers.CodeFixes; using Immediate.Handlers.Tests.Helpers; -namespace Immediate.Handlers.Tests.CodeFixTests.HandlerClassCodeFixTests; +namespace Immediate.Handlers.Tests.CodeFixTests; [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1724:Type names should not match namespaces", Justification = "Not being consumed by other code")] public partial class Tests diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#IH..ConstraintHandler.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Msdi#IH..ConstraintHandler.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#IH..ConstraintHandler.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Msdi#IH..ConstraintHandler.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#IH..ConstraintHandler.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Normal#IH..ConstraintHandler.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#IH..ConstraintHandler.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Normal#IH..ConstraintHandler.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Msdi#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.cs new file mode 100644 index 00000000..41d341de --- /dev/null +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.cs @@ -0,0 +1,309 @@ +using Immediate.Handlers.Tests.Helpers; +using System.Diagnostics; + +namespace Immediate.Handlers.Tests.GeneratorTests; + +public class BehaviorTests +{ + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task SingleBehavior(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + [assembly: Behaviors( + typeof(LoggingBehavior<,>) + )] + + namespace Dummy; + + public class GetUsersEndpoint(GetUsersQuery.Handler handler) + { + public ValueTask> GetUsers() => + handler.HandleAsync(new GetUsersQuery.Query()); + } + + [Handler] + public static partial class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [ + "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.Dummy.GetUsersQuery.g.cs", + .. assemblies switch + { + DriverReferenceAssemblies.Normal => Enumerable.Empty(), + DriverReferenceAssemblies.Msdi => + ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], + + _ => throw new UnreachableException(), + }, + ], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task MultipleBehaviors(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + [assembly: Behaviors( + typeof(LoggingBehavior<,>), + typeof(YetAnotherDummy.OtherBehavior<,>), + typeof(SecondLoggingBehavior<,>), + typeof(YetAnotherDummy.LoggingBehavior<,>), + typeof(YetAnotherDummy.SecondLoggingBehavior<,>) + )] + + namespace YetAnotherDummy + { + public class User { } + public class UsersService + { + public Task> GetUsers() => + Task.FromResult(Enumerable.Empty()); + } + + public class OtherService + { + public Task> GetUsers() => + Task.FromResult(Enumerable.Empty()); + } + + public class OtherBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class SecondLoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + } + + namespace Dummy + { + public class GetUsersEndpoint(GetUsersQuery.Handler handler) + { + public ValueTask> GetUsers() => + handler.HandleAsync(new GetUsersQuery.Query()); + } + + [Handler] + public static partial class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class SecondLoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + } + """, + assemblies + ); + + Assert.Equal( + [ + "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.Dummy.GetUsersQuery.g.cs", + .. assemblies switch + { + DriverReferenceAssemblies.Normal => Enumerable.Empty(), + DriverReferenceAssemblies.Msdi => + ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], + + _ => throw new UnreachableException(), + }, + ], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task CrtpBehavior(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System; + using System.Threading; + using System.Threading.Tasks; + using Immediate.Handlers.Shared; + + public interface IConstraint where T : IConstraint + { + static abstract bool IsValid(T instance); + } + + public sealed class ConstraintBehavior + : Behavior + where TRequest : IConstraint + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + if (!TRequest.IsValid(request)) + throw new InvalidOperationException(); + + return await Next(request, cancellationToken); + } + } + + [Handler] + [Behaviors(typeof(ConstraintBehavior<,>))] + public static partial class ConstraintHandler + { + public sealed record Command : IConstraint + { + public static bool IsValid(Command instance) => true; + + public int Id { get; init; } + } + + private static ValueTask HandleAsync( + Command _, + CancellationToken __ + ) => ValueTask.CompletedTask; + } + """, + assemblies + ); + + Assert.Equal( + [ + "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH..ConstraintHandler.g.cs", + .. assemblies switch + { + DriverReferenceAssemblies.Normal => Enumerable.Empty(), + DriverReferenceAssemblies.Msdi => + ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], + + _ => throw new UnreachableException(), + }, + ], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } +} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs deleted file mode 100644 index 6b7f5536..00000000 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Diagnostics; -using Immediate.Handlers.Tests.Helpers; - -namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors; - -public sealed class CrtpBehaviorTest -{ - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task CrtpBehavior(DriverReferenceAssemblies assemblies) - { - var result = GeneratorTestHelper.RunGenerator( - """ - using System; - using System.Threading; - using System.Threading.Tasks; - using Immediate.Handlers.Shared; - - public interface IConstraint where T : IConstraint - { - static abstract bool IsValid(T instance); - } - - public sealed class ConstraintBehavior - : Behavior - where TRequest : IConstraint - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - if (!TRequest.IsValid(request)) - throw new InvalidOperationException(); - - return await Next(request, cancellationToken); - } - } - - [Handler] - [Behaviors(typeof(ConstraintBehavior<,>))] - public static partial class ConstraintHandler - { - public sealed record Command : IConstraint - { - public static bool IsValid(Command instance) => true; - - public int Id { get; init; } - } - - private static ValueTask HandleAsync( - Command _, - CancellationToken __ - ) => ValueTask.CompletedTask; - } - """, - assemblies - ); - - Assert.Equal( - [ - "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH..ConstraintHandler.g.cs", - .. assemblies switch - { - DriverReferenceAssemblies.Normal => Enumerable.Empty(), - DriverReferenceAssemblies.Msdi => - ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], - - _ => throw new UnreachableException(), - }, - ], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } -} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs deleted file mode 100644 index d3874dac..00000000 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System.Diagnostics; -using Immediate.Handlers.Tests.Helpers; - -namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors; - -public class MultipleBehaviorTest -{ - private const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -[assembly: Behaviors( - typeof(LoggingBehavior<,>), - typeof(YetAnotherDummy.OtherBehavior<,>), - typeof(SecondLoggingBehavior<,>), - typeof(YetAnotherDummy.LoggingBehavior<,>), - typeof(YetAnotherDummy.SecondLoggingBehavior<,>) -)] - -namespace YetAnotherDummy -{ - public class User { } - public class UsersService - { - public Task> GetUsers() => - Task.FromResult(Enumerable.Empty()); - } - - public class OtherService - { - public Task> GetUsers() => - Task.FromResult(Enumerable.Empty()); - } - - public class OtherBehavior(ILogger> logger) - : Behavior - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } - } - - public class LoggingBehavior(ILogger> logger) - : Behavior - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } - } - - public class SecondLoggingBehavior(ILogger> logger) - : Behavior - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } - } -} - -namespace Dummy -{ - public class GetUsersEndpoint(GetUsersQuery.Handler handler) - { - public ValueTask> GetUsers() => - handler.HandleAsync(new GetUsersQuery.Query()); - } - - [Handler] - public static partial class GetUsersQuery - { - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } - } - - public class LoggingBehavior(ILogger> logger) - : Behavior - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } - } - - public class SecondLoggingBehavior(ILogger> logger) - : Behavior - { - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } - } - - public class User { } - public class UsersService - { - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); - } - - public interface ILogger; -} -"""; - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task MultipleBehaviors(DriverReferenceAssemblies assemblies) - { - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [ - "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.Dummy.GetUsersQuery.g.cs", - .. assemblies switch - { - DriverReferenceAssemblies.Normal => Enumerable.Empty(), - DriverReferenceAssemblies.Msdi => - ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], - - _ => throw new UnreachableException(), - }, - ], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } -} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs deleted file mode 100644 index b6696dc9..00000000 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System.Diagnostics; -using Immediate.Handlers.Tests.Helpers; - -namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors; - -public class SingleBehaviorTest -{ - private const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -[assembly: Behaviors( - typeof(LoggingBehavior<,>) -)] - -namespace Dummy; - -public class GetUsersEndpoint(GetUsersQuery.Handler handler) -{ - public ValueTask> GetUsers() => - handler.HandleAsync(new GetUsersQuery.Query()); -} - -[Handler] -public static partial class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) - : Behavior -{ - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task SingleBehavior(DriverReferenceAssemblies assemblies) - { - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [ - "Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.Dummy.GetUsersQuery.g.cs", - .. assemblies switch - { - DriverReferenceAssemblies.Normal => Enumerable.Empty(), - DriverReferenceAssemblies.Msdi => - ["Immediate.Handlers.Generators/Immediate.Handlers.Generators.ImmediateHandlers.ImmediateHandlersGenerator/IH.ServiceCollectionExtensions.g.cs"], - - _ => throw new UnreachableException(), - }, - ], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } -} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/GeneratorTestHelper.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/GeneratorTestHelper.cs index 822181a1..8de33d0a 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/GeneratorTestHelper.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/GeneratorTestHelper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Immediate.Handlers.Generators.ImmediateHandlers; using Immediate.Handlers.Tests.Helpers; using Microsoft.CodeAnalysis; @@ -7,7 +8,10 @@ namespace Immediate.Handlers.Tests.GeneratorTests; public static class GeneratorTestHelper { - public static GeneratorDriverRunResult RunGenerator(string source, DriverReferenceAssemblies assemblies) + public static GeneratorDriverRunResult RunGenerator( + [StringSyntax("c#-test")] string source, + DriverReferenceAssemblies assemblies + ) { var syntaxTree = CSharpSyntaxTree.ParseText(source); diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.IntReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.IntReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.MissingCancellationToken_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.MissingCancellationToken_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.VoidReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs similarity index 100% rename from tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.VoidReturnType_assemblies=Normal#IH.Dummy.GetUsersQuery.g.verified.cs diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.cs similarity index 96% rename from tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs rename to tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.cs index 89726c6b..33ba9dbb 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.cs @@ -1,8 +1,8 @@ using Immediate.Handlers.Tests.Helpers; -namespace Immediate.Handlers.Tests.GeneratorTests.SimpleHandler; +namespace Immediate.Handlers.Tests.GeneratorTests; -public class SimpleHandlerTests +public class HandlerTests { [Theory] [InlineData(DriverReferenceAssemblies.Normal)] diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidBehaviorsTests.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidBehaviorsTests.cs new file mode 100644 index 00000000..9ec7c41d --- /dev/null +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidBehaviorsTests.cs @@ -0,0 +1,470 @@ +using Immediate.Handlers.Tests.Helpers; + +namespace Immediate.Handlers.Tests.GeneratorTests; + +public class InvalidBehaviorsTests +{ + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task NonBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior<,>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + { + public async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + await Task.Delay(1000, cancellationToken); + + return default!; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task BoundGenericShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task NonGenericBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger logger) + : Behavior> + { + public override async ValueTask> HandleAsync(GetUsersQuery.Query request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task AbstractBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior<,>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public abstract class LoggingBehavior(ILogger> logger) + : Behavior; + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task BehaviorHasTooManyTRequestConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior<,>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior where TRequest : IEnumerable, IEquatable + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task BehaviorHasTooManyTResponseConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior<,>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior where TResponse : IEnumerable, IEquatable + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task BehaviorHasTooManyTypeParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + #pragma warning disable CS9113 + + namespace Dummy; + + [Handler] + [Behaviors( + typeof(LoggingBehavior<,,>) + )] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class LoggingBehavior(ILogger> logger) + : Behavior + { + public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await Next(request, cancellationToken); + + return response; + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } +} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs deleted file mode 100644 index 4c3aa762..00000000 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs +++ /dev/null @@ -1,463 +0,0 @@ -using Immediate.Handlers.Tests.Helpers; - -namespace Immediate.Handlers.Tests.GeneratorTests.InvalidCode; - -public class InvalidBehaviorsTest -{ - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task NonBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior<,>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) -{ - public async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - await Task.Delay(1000, cancellationToken); - - return default!; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task BoundGenericShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) - : Behavior -{ - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task NonGenericBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger logger) - : Behavior> -{ - public override async ValueTask> HandleAsync(GetUsersQuery.Query request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task AbstractBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior<,>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public abstract class LoggingBehavior(ILogger> logger) - : Behavior; - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task BehaviorHasTooManyTRequestConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior<,>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) - : Behavior where TRequest : IEnumerable, IEquatable -{ - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task BehaviorHasTooManyTResponseConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior<,>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) - : Behavior where TResponse : IEnumerable, IEquatable -{ - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task BehaviorHasTooManyTypeParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -#pragma warning disable CS9113 - -namespace Dummy; - -[Handler] -[Behaviors( - typeof(LoggingBehavior<,,>) -)] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class LoggingBehavior(ILogger> logger) - : Behavior -{ - public override async ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) - { - var response = await Next(request, cancellationToken); - - return response; - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } -} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs deleted file mode 100644 index 06105985..00000000 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs +++ /dev/null @@ -1,296 +0,0 @@ -using Immediate.Handlers.Tests.Helpers; - -namespace Immediate.Handlers.Tests.GeneratorTests.InvalidCode; - -public class InvalidHandlerTest -{ - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithoutHandlerMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -[Handler] -public static class GetUsersQuery -{ - public record Query; -} -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithTwoHandlersMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -[Handler] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> Handle( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithNoParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -[Handler] -public static class GetUsersQuery -{ - public record Query; - - private static ValueTask> HandleAsync() - { - return ValueTask.FromResult(Enumerable.Empty()); - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithVoidResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -[Handler] -public static class GetUsersQuery -{ - public record Query; - - private static void HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithTaskResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -[Handler] -public static class GetUsersQuery -{ - public record Query; - - private static async Task> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return await usersService.GetUsers(); - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } - - [Theory] - [InlineData(DriverReferenceAssemblies.Normal)] - [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task NestedHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies) - { - const string Input = """ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Dummy; -using Immediate.Handlers.Shared; - -namespace Dummy; - -public class Wrapper -{ - [Handler] - public static class GetUsersQuery - { - public record Query; - - private static ValueTask> HandleAsync( - Query _, - UsersService usersService, - CancellationToken token) - { - return usersService.GetUsers(); - } - } -} - -public class User { } -public class UsersService -{ - public ValueTask> GetUsers() => - ValueTask.FromResult(Enumerable.Empty()); -} - -public interface ILogger; -"""; - - var result = GeneratorTestHelper.RunGenerator(Input, assemblies); - - Assert.Equal( - [], - result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) - ); - - _ = await Verify(result) - .UseParameters(string.Join("_", assemblies)); - } -} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidHandlerTests.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidHandlerTests.cs new file mode 100644 index 00000000..89a3c654 --- /dev/null +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidHandlerTests.cs @@ -0,0 +1,302 @@ +using Immediate.Handlers.Tests.Helpers; + +namespace Immediate.Handlers.Tests.GeneratorTests; + +public class InvalidHandlerTests +{ + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task HandlerWithoutHandlerMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + } + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task HandlerWithTwoHandlersMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> Handle( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task HandlerWithNoParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync() + { + return ValueTask.FromResult(Enumerable.Empty()); + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task HandlerWithVoidResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static void HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task HandlerWithTaskResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static async Task> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return await usersService.GetUsers(); + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + [InlineData(DriverReferenceAssemblies.Msdi)] + public async Task NestedHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies) + { + var result = GeneratorTestHelper.RunGenerator( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Dummy; + using Immediate.Handlers.Shared; + + namespace Dummy; + + public class Wrapper + { + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } + } + } + + public class User { } + public class UsersService + { + public ValueTask> GetUsers() => + ValueTask.FromResult(Enumerable.Empty()); + } + + public interface ILogger; + """, + assemblies + ); + + Assert.Equal( + [], + result.GeneratedTrees.Select(t => t.FilePath.Replace("\\", "/", StringComparison.Ordinal)) + ); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } +} diff --git a/tests/Immediate.Handlers.Tests/Immediate.Handlers.Tests.csproj b/tests/Immediate.Handlers.Tests/Immediate.Handlers.Tests.csproj index 55c1f972..fc84046e 100644 --- a/tests/Immediate.Handlers.Tests/Immediate.Handlers.Tests.csproj +++ b/tests/Immediate.Handlers.Tests/Immediate.Handlers.Tests.csproj @@ -1,4 +1,4 @@ - + $(NoWarn);NU1903