Skip to content

Commit 5ca8acd

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents bce7ef0 + bf311f5 commit 5ca8acd

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

src/ConsoleAppFramework/EquatableTypeSymbol.cs

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ public bool Equals(EquatableTypeSymbol other)
2626
return this.TypeSymbol.EqualsNamespaceAndName(other.TypeSymbol);
2727
}
2828
}
29+
30+
static class EquatableTypeSymbolExtensions
31+
{
32+
public static EquatableTypeSymbol ToEquatable(this ITypeSymbol typeSymbol) => new(typeSymbol);
33+
}

src/ConsoleAppFramework/Parser.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
358358
Location = x.GetLocation(),
359359
HasDefaultValue = hasDefault,
360360
DefaultValue = defaultValue,
361-
CustomParserType = customParserType == null ? null : new EquatableTypeSymbol(customParserType),
361+
CustomParserType = customParserType?.ToEquatable(),
362362
HasValidation = hasValidation,
363363
IsCancellationToken = isCancellationToken,
364364
IsFromServices = isFromServices,
@@ -520,7 +520,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
520520
Type = new EquatableTypeSymbol(x.Type),
521521
HasDefaultValue = x.HasExplicitDefaultValue,
522522
DefaultValue = x.HasExplicitDefaultValue ? x.ExplicitDefaultValue : null,
523-
CustomParserType = null,
523+
CustomParserType = customParserType?.AttributeClass?.ToEquatable(),
524524
IsCancellationToken = isCancellationToken,
525525
IsFromServices = hasFromServices,
526526
HasValidation = hasValidation,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ConsoleAppFramework.GeneratorTests;
4+
5+
public class ArgumentParserTest(ITestOutputHelper output)
6+
{
7+
readonly VerifyHelper verifier = new(output, "CAF");
8+
9+
[Fact]
10+
public void Lamda()
11+
{
12+
verifier.Execute(HEAD + Body("""
13+
ConsoleApp.Run(args, ([Vector3Parser] Vector3 v) => Console.Write(v));
14+
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
15+
verifier.Execute(HEAD + Body("""
16+
var app = ConsoleApp.Create();
17+
app.Add("", ([Vector3Parser] Vector3 v) => Console.Write(v));
18+
app.Run(args);
19+
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
20+
}
21+
22+
[Fact]
23+
public void Method()
24+
{
25+
verifier.Execute(HEAD + Body("""
26+
ConsoleApp.Run(args, MyCommands.Static);
27+
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
28+
verifier.Execute(HEAD + Body("""
29+
var app = ConsoleApp.Create();
30+
app.Add("", MyCommands.Static);
31+
app.Run(args);
32+
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
33+
}
34+
35+
[Fact]
36+
public void Class()
37+
{
38+
verifier.Execute(HEAD + Body("""
39+
var app = ConsoleApp.Create();
40+
app.Add<MyCommands>();
41+
app.Run(args);
42+
""") + TAIL, args: "--v 1,2,3", expected: "<1, 2, 3>");
43+
}
44+
45+
static string Body([StringSyntax("C#-test")] string code) => code;
46+
/// <summary>
47+
/// <see href="https://github.com/Cysharp/ConsoleAppFramework/blob/master/ReadMe.md#custom-value-converter"/>
48+
/// </summary>
49+
[StringSyntax("C#-test")]
50+
const string
51+
HEAD = """
52+
using System.Numerics;
53+
54+
""",
55+
TAIL = """
56+
57+
public class MyCommands
58+
{
59+
[Command("")]
60+
public void Root([Vector3Parser] Vector3 v) => Console.Write(v);
61+
public static void Static([Vector3Parser] Vector3 v) => Console.Write(v);
62+
}
63+
64+
[AttributeUsage(AttributeTargets.Parameter)]
65+
public class Vector3ParserAttribute : Attribute, IArgumentParser<Vector3>
66+
{
67+
public static bool TryParse(ReadOnlySpan<char> s, out Vector3 result)
68+
{
69+
Span<Range> ranges = stackalloc Range[3];
70+
var splitCount = s.Split(ranges, ',');
71+
if (splitCount != 3)
72+
{
73+
result = default;
74+
return false;
75+
}
76+
77+
float x;
78+
float y;
79+
float z;
80+
if (float.TryParse(s[ranges[0]], out x) && float.TryParse(s[ranges[1]], out y) && float.TryParse(s[ranges[2]], out z))
81+
{
82+
result = new Vector3(x, y, z);
83+
return true;
84+
}
85+
86+
result = default;
87+
return false;
88+
}
89+
}
90+
""";
91+
}

0 commit comments

Comments
 (0)