From 22b5dd34ee4eb86448064434b2fb01642b9dc982 Mon Sep 17 00:00:00 2001 From: Stuart Turner Date: Wed, 29 May 2024 08:44:58 -0500 Subject: [PATCH] Add support for `Handler()` without Token (#83) --- .../ITypeSymbolExtensions.cs | 15 ++++++ .../ImmediateHandlersGenerator_Entrypoint.cs | 1 + ...diateHandlersGenerator_TransformHandler.cs | 12 +++-- .../ImmediateHandlersGenerator_Types.cs | 1 + .../Templates/Handler.sbntxt | 16 +++--- .../NoBehaviors/ParameterlessTests.cs | 27 ++++++++++ ...lies=Msdi#.ConstraintHandler.g.verified.cs | 4 +- ...es=Normal#.ConstraintHandler.g.verified.cs | 4 +- .../Behaviors/CrtpBehaviorTest.cs | 1 + ...ies=Msdi#Dummy.GetUsersQuery.g.verified.cs | 6 +-- ...s=Normal#Dummy.GetUsersQuery.g.verified.cs | 6 +-- .../Behaviors/MultipleBehaviorTest.cs | 1 + ...ies=Msdi#Dummy.GetUsersQuery.g.verified.cs | 6 +-- ...s=Normal#Dummy.GetUsersQuery.g.verified.cs | 6 +-- .../Behaviors/SingleBehaviorTest.cs | 1 + .../InvalidCode/InvalidBehaviorsTest.cs | 7 +++ .../InvalidCode/InvalidHandlerTest.cs | 11 ++-- .../InvalidCode/InvalidRenderModeTest.cs | 2 + ...s=Normal#Dummy.GetUsersQuery.g.verified.cs | 4 +- ...s=Normal#Dummy.GetUsersQuery.g.verified.cs | 54 +++++++++++++++++++ ...s=Normal#Dummy.GetUsersQuery.g.verified.cs | 4 +- .../SimpleHandler/SimpleHandlerTests.cs | 37 ++++++++++++- 22 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs diff --git a/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs b/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs index 2801a7a0..be1b908a 100644 --- a/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs +++ b/src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs @@ -75,4 +75,19 @@ typeSymbol is INamedTypeSymbol ContainingNamespace.IsGlobalNamespace: true }, }; + + public static bool IsCancellationToken(this ITypeSymbol typeSymbol) => + typeSymbol is INamedTypeSymbol + { + Name: "CancellationToken", + ContainingNamespace: + { + Name: "Threading", + ContainingNamespace: + { + Name: "System", + ContainingNamespace.IsGlobalNamespace: true + } + } + }; } diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs index 76079e5f..74447d49 100644 --- a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs +++ b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs @@ -169,6 +169,7 @@ Template template handler.MethodName, HandlerParameters = handler.Parameters, + handler.UseToken, RequestType = handler.RequestType.Name, ResponseType = responseType.Name, diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs index 2189f63a..cdda03e0 100644 --- a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs +++ b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_TransformHandler.cs @@ -33,14 +33,17 @@ CancellationToken cancellationToken } // must have request type and cancellation token - if (handleMethod.Parameters.Length < 2) + if (handleMethod.Parameters is []) return null; if (handleMethod.ReturnsVoid) return null; cancellationToken.ThrowIfCancellationRequested(); + var requestType = BuildGenericType(handleMethod.Parameters[0].Type); + var useToken = handleMethod.Parameters[^1] + .Type.IsCancellationToken(); cancellationToken.ThrowIfCancellationRequested(); var responseTypeSymbol = handleMethod.GetTaskReturnType(); @@ -55,7 +58,8 @@ CancellationToken cancellationToken cancellationToken.ThrowIfCancellationRequested(); var parameters = handleMethod.Parameters - .Skip(1).Take(handleMethod.Parameters.Length - 2) + .Skip(1) + .Take(handleMethod.Parameters.Length - (useToken ? 2 : 1)) .Select(p => new Parameter { Type = p.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), @@ -77,12 +81,12 @@ CancellationToken cancellationToken DisplayName = displayName, MethodName = handleMethod.Name, + Parameters = parameters, + UseToken = useToken, RequestType = requestType, ResponseType = responseType, - Parameters = parameters, - OverrideRenderMode = renderMode, OverrideBehaviors = behaviors, }; diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs index 52538936..2071b943 100644 --- a/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs +++ b/src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Types.cs @@ -40,6 +40,7 @@ private sealed record Handler public required string MethodName { get; init; } public required EquatableReadOnlyList Parameters { get; init; } + public required bool UseToken { get; init; } public required GenericType RequestType { get; init; } public required GenericType? ResponseType { get; init; } diff --git a/src/Immediate.Handlers.Generators/Templates/Handler.sbntxt b/src/Immediate.Handlers.Generators/Templates/Handler.sbntxt index aaff0876..9da95c0d 100644 --- a/src/Immediate.Handlers.Generators/Templates/Handler.sbntxt +++ b/src/Immediate.Handlers.Generators/Templates/Handler.sbntxt @@ -85,21 +85,25 @@ partial class {{ class_name }} {{~ if !is_implicit_value_tuple ~}} return await {{ class_fully_qualified_name }} .{{ method_name }}( - request, + request {{~ for parameter in handler_parameters ~}} - _{{ parameter.name }}, + , _{{ parameter.name }} + {{~ end ~}} + {{~ if use_token ~}} + , cancellationToken {{~ end ~}} - cancellationToken ) .ConfigureAwait(false); {{~ else ~}} await {{ class_fully_qualified_name }} .{{ method_name }}( - request, + request {{~ for parameter in handler_parameters ~}} - _{{ parameter.name }}, + , _{{ parameter.name }} + {{~ end ~}} + {{~ if use_token ~}} + , cancellationToken {{~ end ~}} - cancellationToken ) .ConfigureAwait(false); diff --git a/tests/Immediate.Handlers.FunctionalTests/NoBehaviors/ParameterlessTests.cs b/tests/Immediate.Handlers.FunctionalTests/NoBehaviors/ParameterlessTests.cs index bee1ee65..2cc043d4 100644 --- a/tests/Immediate.Handlers.FunctionalTests/NoBehaviors/ParameterlessTests.cs +++ b/tests/Immediate.Handlers.FunctionalTests/NoBehaviors/ParameterlessTests.cs @@ -16,6 +16,19 @@ private static ValueTask HandleAsync( } } +[Handler] +public static partial class NoBehaviorNoTokenOneAdder +{ + public sealed record Query(int Input); + + private static ValueTask HandleAsync( + Query query + ) + { + return ValueTask.FromResult(query.Input + 1); + } +} + public class ParameterlessTests { [Fact] @@ -31,4 +44,18 @@ public async Task NoBehaviorShouldReturnExpectedResponse() Assert.Equal(Input + 1, result); } + + [Fact] + public async Task NoTokenShouldReturnExpectedResponse() + { + const int Input = 1; + + var handler = HandlerResolver.Resolve(); + + var query = new NoBehaviorNoTokenOneAdder.Query(Input); + + var result = await handler.HandleAsync(query); + + Assert.Equal(Input + 1, result); + } } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#.ConstraintHandler.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#.ConstraintHandler.g.verified.cs index 026c0c26..11be04da 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#.ConstraintHandler.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Msdi#.ConstraintHandler.g.verified.cs @@ -52,8 +52,8 @@ public HandleBehavior( { await global::ConstraintHandler .HandleAsync( - request, - cancellationToken + request + , cancellationToken ) .ConfigureAwait(false); diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#.ConstraintHandler.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#.ConstraintHandler.g.verified.cs index 3f842b7d..f00e08cc 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#.ConstraintHandler.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.CrtpBehavior_assemblies=Normal#.ConstraintHandler.g.verified.cs @@ -50,8 +50,8 @@ public HandleBehavior( { await global::ConstraintHandler .HandleAsync( - request, - cancellationToken + request + , cancellationToken ) .ConfigureAwait(false); diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs index fd26ba55..6ca5d5f5 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/CrtpBehaviorTest.cs @@ -11,6 +11,7 @@ public async Task CrtpBehavior(DriverReferenceAssemblies assemblies) { var driver = GeneratorTestHelper.GetDriver( """ + using System.Threading; using System.Threading.Tasks; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs index 9a08f3b3..de50de73 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs @@ -81,9 +81,9 @@ public HandleBehavior( { return await global::Dummy.GetUsersQuery .HandleAsync( - request, - _usersService, - cancellationToken + request + , _usersService + , cancellationToken ) .ConfigureAwait(false); } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs index a3e786e7..473742a5 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.MultipleBehaviors_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs @@ -79,9 +79,9 @@ public HandleBehavior( { return await global::Dummy.GetUsersQuery .HandleAsync( - request, - _usersService, - cancellationToken + request + , _usersService + , cancellationToken ) .ConfigureAwait(false); } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs index 06b198dd..ec156ca6 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/MultipleBehaviorTest.cs @@ -6,6 +6,7 @@ namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors; public class MultipleBehaviorTest { private const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs index 314625d8..8cdc73f7 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Msdi#Dummy.GetUsersQuery.g.verified.cs @@ -57,9 +57,9 @@ public HandleBehavior( { return await global::Dummy.GetUsersQuery .HandleAsync( - request, - _usersService, - cancellationToken + request + , _usersService + , cancellationToken ) .ConfigureAwait(false); } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs index 1875f1dc..c50a1d26 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.SingleBehavior_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs @@ -55,9 +55,9 @@ public HandleBehavior( { return await global::Dummy.GetUsersQuery .HandleAsync( - request, - _usersService, - cancellationToken + request + , _usersService + , cancellationToken ) .ConfigureAwait(false); } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs index 0d890526..ea73a061 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/Behaviors/SingleBehaviorTest.cs @@ -6,6 +6,7 @@ namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors; public class SingleBehaviorTest { private const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs index 6ccb8e87..18dc9f2f 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidBehaviorsTest.cs @@ -11,6 +11,7 @@ public class InvalidBehaviorsTest public async Task NonBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -73,6 +74,7 @@ public interface ILogger; public async Task BoundGenericShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -136,6 +138,7 @@ public interface ILogger; public async Task NonGenericBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -199,6 +202,7 @@ public interface ILogger; public async Task AbstractBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -254,6 +258,7 @@ public interface ILogger; public async Task BehaviorHasTooManyTRequestConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -317,6 +322,7 @@ public interface ILogger; public async Task BehaviorHasTooManyTResponseConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -380,6 +386,7 @@ public interface ILogger; public async Task BehaviorHasTooManyTypeParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs index 3f9e7bea..77bf5309 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidHandlerTest.cs @@ -11,6 +11,7 @@ public class InvalidHandlerTest public async Task HandlerWithoutHandlerMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -52,6 +53,7 @@ public interface ILogger; public async Task HandlerWithTwoHandlersMethodShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -106,9 +108,10 @@ public interface ILogger; [Theory] [InlineData(DriverReferenceAssemblies.Normal)] [InlineData(DriverReferenceAssemblies.Msdi)] - public async Task HandlerWithOneParameterShouldProduceNothing(DriverReferenceAssemblies assemblies) + public async Task HandlerWithNoParametersShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -126,8 +129,7 @@ public static class GetUsersQuery { public record Query; - private static ValueTask> HandleAsync( - Query _) + private static ValueTask> HandleAsync() { return usersService.GetUsers(); } @@ -156,6 +158,7 @@ public interface ILogger; public async Task HandlerWithVoidResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -204,6 +207,7 @@ public interface ILogger; public async Task HandlerWithTaskResponseShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -253,6 +257,7 @@ public interface ILogger; public async Task NestedHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidRenderModeTest.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidRenderModeTest.cs index 5c6c28f8..f5877b73 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidRenderModeTest.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/InvalidCode/InvalidRenderModeTest.cs @@ -10,6 +10,7 @@ public class InvalidRenderModeTest public async Task InvalidRenderModeOnAssemblyShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Dummy; using Immediate.Handlers.Shared; @@ -54,6 +55,7 @@ public interface ILogger; public async Task InvalidRenderModeOnHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies) { const string Input = """ +using System.Threading; using System.Threading.Tasks; using Immediate.Handlers.Shared; diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs index 3f5e9167..34d2b7aa 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.IntReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs @@ -46,8 +46,8 @@ public HandleBehavior( { return await global::Dummy.GetUsersQuery .HandleAsync( - request, - cancellationToken + request + , cancellationToken ) .ConfigureAwait(false); } diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs new file mode 100644 index 00000000..a351e3f9 --- /dev/null +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.MissingCancellationToken_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs @@ -0,0 +1,54 @@ +//HintName: Dummy.GetUsersQuery.g.cs +#pragma warning disable CS1591 + +namespace Dummy; + +partial class GetUsersQuery +{ + public sealed partial class Handler : global::Immediate.Handlers.Shared.IHandler + { + private readonly global::Dummy.GetUsersQuery.HandleBehavior _handleBehavior; + + public Handler( + global::Dummy.GetUsersQuery.HandleBehavior handleBehavior + ) + { + var handlerType = typeof(GetUsersQuery); + + _handleBehavior = handleBehavior; + + } + + public async global::System.Threading.Tasks.ValueTask HandleAsync( + global::Dummy.GetUsersQuery.Query request, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + return await _handleBehavior + .HandleAsync(request, cancellationToken) + .ConfigureAwait(false); + } + } + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public sealed class HandleBehavior : global::Immediate.Handlers.Shared.Behavior + { + + public HandleBehavior( + ) + { + } + + public override async global::System.Threading.Tasks.ValueTask HandleAsync( + global::Dummy.GetUsersQuery.Query request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return await global::Dummy.GetUsersQuery + .HandleAsync( + request + ) + .ConfigureAwait(false); + } + } +} diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs index 0f49f7a8..b698accb 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.VoidReturnType_assemblies=Normal#Dummy.GetUsersQuery.g.verified.cs @@ -46,8 +46,8 @@ public HandleBehavior( { await global::Dummy.GetUsersQuery .HandleAsync( - request, - cancellationToken + request + , cancellationToken ) .ConfigureAwait(false); diff --git a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs index a560c813..a843098a 100644 --- a/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs +++ b/tests/Immediate.Handlers.Tests/GeneratorTests/SimpleHandler/SimpleHandlerTests.cs @@ -2,7 +2,6 @@ namespace Immediate.Handlers.Tests.GeneratorTests.SimpleHandler; - public class SimpleHandlerTests { [Theory] @@ -11,6 +10,7 @@ public async Task IntReturnType(DriverReferenceAssemblies assemblies) { var driver = GeneratorTestHelper.GetDriver( """ + using System.Threading; using System.Threading.Tasks; using Immediate.Handlers.Shared; @@ -44,6 +44,7 @@ public async Task VoidReturnType(DriverReferenceAssemblies assemblies) { var driver = GeneratorTestHelper.GetDriver( """ + using System.Threading; using System.Threading.Tasks; using Immediate.Handlers.Shared; @@ -69,4 +70,38 @@ private static ValueTask HandleAsync( _ = await Verify(result) .UseParameters(string.Join("_", assemblies)); } + + [Theory] + [InlineData(DriverReferenceAssemblies.Normal)] + public async Task MissingCancellationToken(DriverReferenceAssemblies assemblies) + { + var driver = GeneratorTestHelper.GetDriver( + """ + using System.Threading; + using System.Threading.Tasks; + using Immediate.Handlers.Shared; + + namespace Dummy; + + [Handler] + public static class GetUsersQuery + { + public record Query; + + private static ValueTask HandleAsync( + Query _ + ) + { + return 0; + } + } + """, + assemblies); + + var result = driver.GetRunResult(); + Assert.Empty(result.Diagnostics); + + _ = await Verify(result) + .UseParameters(string.Join("_", assemblies)); + } }