Skip to content

Commit

Permalink
Merge pull request #139 from naratteu/master
Browse files Browse the repository at this point in the history
Bug Fix - IArgumentParser Attribute doesn't work in Method
  • Loading branch information
neuecc authored Sep 9, 2024
2 parents 8e5c14c + ea45660 commit bf311f5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/ConsoleAppFramework/EquatableTypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ public bool Equals(EquatableTypeSymbol other)
return this.TypeSymbol.EqualsNamespaceAndName(other.TypeSymbol);
}
}

static class EquatableTypeSymbolExtensions
{
public static EquatableTypeSymbol ToEquatable(this ITypeSymbol typeSymbol) => new(typeSymbol);
}
4 changes: 2 additions & 2 deletions src/ConsoleAppFramework/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
Location = x.GetLocation(),
HasDefaultValue = hasDefault,
DefaultValue = defaultValue,
CustomParserType = customParserType == null ? null : new EquatableTypeSymbol(customParserType),
CustomParserType = customParserType?.ToEquatable(),
HasValidation = hasValidation,
IsCancellationToken = isCancellationToken,
IsFromServices = isFromServices,
Expand Down Expand Up @@ -520,7 +520,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
Type = new EquatableTypeSymbol(x.Type),
HasDefaultValue = x.HasExplicitDefaultValue,
DefaultValue = x.HasExplicitDefaultValue ? x.ExplicitDefaultValue : null,
CustomParserType = null,
CustomParserType = customParserType?.AttributeClass?.ToEquatable(),
IsCancellationToken = isCancellationToken,
IsFromServices = hasFromServices,
HasValidation = hasValidation,
Expand Down
91 changes: 91 additions & 0 deletions tests/ConsoleAppFramework.GeneratorTests/ArgumentParserTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Diagnostics.CodeAnalysis;

namespace ConsoleAppFramework.GeneratorTests;

public class ArgumentParserTest(ITestOutputHelper output)
{
readonly VerifyHelper verifier = new(output, "CAF");

[Fact]
public void Lamda()
{
verifier.Execute(HEAD + Body("""
ConsoleApp.Run(args, ([Vector3Parser] Vector3 v) => Console.Write(v));
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
verifier.Execute(HEAD + Body("""
var app = ConsoleApp.Create();
app.Add("", ([Vector3Parser] Vector3 v) => Console.Write(v));
app.Run(args);
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
}

[Fact]
public void Method()
{
verifier.Execute(HEAD + Body("""
ConsoleApp.Run(args, MyCommands.Static);
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
verifier.Execute(HEAD + Body("""
var app = ConsoleApp.Create();
app.Add("", MyCommands.Static);
app.Run(args);
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
}

[Fact]
public void Class()
{
verifier.Execute(HEAD + Body("""
var app = ConsoleApp.Create();
app.Add<MyCommands>();
app.Run(args);
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
}

static string Body([StringSyntax("C#-test")] string code) => code;
/// <summary>
/// <see href="https://github.com/Cysharp/ConsoleAppFramework/blob/master/ReadMe.md#custom-value-converter"/>
/// </summary>
[StringSyntax("C#-test")]
const string
HEAD = """
using System.Numerics;
""",
TAIL = """
public class MyCommands
{
[Command("")]
public void Root([Vector3Parser] Vector3 v) => Console.Write(v);
public static void Static([Vector3Parser] Vector3 v) => Console.Write(v);
}
[AttributeUsage(AttributeTargets.Parameter)]
public class Vector3ParserAttribute : Attribute, IArgumentParser<Vector3>
{
public static bool TryParse(ReadOnlySpan<char> s, out Vector3 result)
{
Span<Range> ranges = stackalloc Range[3];
var splitCount = s.Split(ranges, ',');
if (splitCount != 3)
{
result = default;
return false;
}
float x;
float y;
float z;
if (float.TryParse(s[ranges[0]], out x) && float.TryParse(s[ranges[1]], out y) && float.TryParse(s[ranges[2]], out z))
{
result = new Vector3(x, y, z);
return true;
}
result = default;
return false;
}
}
""";
}

0 comments on commit bf311f5

Please sign in to comment.