|
| 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