From 1f5b765f256faad5cb2673eff154729d63497848 Mon Sep 17 00:00:00 2001 From: Hirotada Kobayashi Date: Wed, 24 Jul 2024 19:20:48 +0900 Subject: [PATCH] fix IndexOutOfRangeException when ConsoleAppFilterAttribute is defined above CommandAttribute --- src/ConsoleAppFramework/Parser.cs | 2 +- .../ConsoleAppBuilderTest.cs | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/ConsoleAppFramework/Parser.cs b/src/ConsoleAppFramework/Parser.cs index 019bf15..4baaf67 100644 --- a/src/ConsoleAppFramework/Parser.cs +++ b/src/ConsoleAppFramework/Parser.cs @@ -144,7 +144,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod var commandAttribute = x.GetAttributes().FirstOrDefault(x => x.AttributeClass?.Name == "CommandAttribute"); if (commandAttribute != null) { - commandName = (x.GetAttributes()[0].ConstructorArguments[0].Value as string)!; + commandName = (commandAttribute.ConstructorArguments[0].Value as string)!; } else { diff --git a/tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs b/tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs index a6a9bf5..9f42e68 100644 --- a/tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs +++ b/tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs @@ -241,7 +241,46 @@ public void Do() verifier.Execute(code, "nomunomu", "yeah"); } -} + [Fact] + public void CommandAttrWithFilter() + { + var code = """ +var builder = ConsoleApp.Create(); +builder.Add(); +builder.Run(args); + +public class MyClass() +{ + [ConsoleAppFilter] + [Command("nomunomu")] + [ConsoleAppFilter] + public void Do() + { + Console.Write("command"); + } +} +internal class NopFilter1(ConsoleAppFilter next) + : ConsoleAppFilter(next) +{ + public override Task InvokeAsync(ConsoleAppContext context,CancellationToken cancellationToken) + { + Console.Write("filter1-"); + return Next.InvokeAsync(context, cancellationToken); + } +} +internal class NopFilter2(ConsoleAppFilter next) + : ConsoleAppFilter(next) +{ + public override Task InvokeAsync(ConsoleAppContext context,CancellationToken cancellationToken) + { + Console.Write("filter2-"); + return Next.InvokeAsync(context, cancellationToken); + } +} +"""; + verifier.Execute(code, "nomunomu", "filter1-filter2-command"); + } +} \ No newline at end of file