Skip to content

Commit 84d47b0

Browse files
committed
more test and parse argument stability
1 parent fcda518 commit 84d47b0

File tree

5 files changed

+166
-4
lines changed

5 files changed

+166
-4
lines changed

src/MicroBatchFramework/BatchEngine.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal async Task RunAsync(Type type, string[] args)
7676
goto RUN;
7777
}
7878

79-
FAIL:
79+
FAIL:
8080
await SetFailAsync(ctx, "Method can not select. T of Run/UseBatchEngine<T> have to be contain single method or command. Type:" + type.FullName);
8181
return;
8282
}
@@ -86,7 +86,7 @@ internal async Task RunAsync(Type type, string[] args)
8686
return;
8787
}
8888

89-
RUN:
89+
RUN:
9090
await RunCore(ctx, type, method, args, argsOffset);
9191
}
9292

@@ -234,6 +234,12 @@ static ReadOnlyDictionary<string, OptionParameter> ParseArgument(string[] args,
234234
var dict = new Dictionary<string, OptionParameter>(args.Length, StringComparer.OrdinalIgnoreCase);
235235
for (int i = argsOffset; i < args.Length;)
236236
{
237+
if (!args[i].StartsWith("-"))
238+
{
239+
i++;
240+
continue; // not key
241+
}
242+
237243
var key = args[i++].TrimStart('-');
238244
if (i < args.Length && !args[i].StartsWith("-"))
239245
{

src/MicroBatchFramework/CommandAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public CommandAttribute(string[] commandNames)
2424

2525
public CommandAttribute(string[] commandNames, string description)
2626
{
27-
foreach (var item in CommandNames)
27+
foreach (var item in commandNames)
2828
{
2929
if (item.Equals("list", StringComparison.OrdinalIgnoreCase)
3030
|| item.Equals("help", StringComparison.OrdinalIgnoreCase))
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using FluentAssertions;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Xunit.Abstractions;
6+
using Microsoft.Extensions.Logging;
7+
using Xunit;
8+
using System.Threading.Tasks;
9+
using Microsoft.Extensions.Hosting;
10+
11+
namespace MicroBatchFramework.Tests
12+
{
13+
public class Multi1 : BatchBase
14+
{
15+
public void Hello1()
16+
{
17+
Context.Logger.LogInformation("ok");
18+
}
19+
20+
public void Hello2(string input)
21+
{
22+
Context.Logger.LogInformation(input);
23+
}
24+
}
25+
26+
public class Multi2 : BatchBase
27+
{
28+
public void Hello1([Option("x")]int xxx, [Option("y")]int yyy)
29+
{
30+
Context.Logger.LogInformation($"{xxx}:{yyy}");
31+
}
32+
33+
public void Hello2(bool x, bool y, string foo, int nano = 999)
34+
{
35+
Context.Logger.LogInformation($"{x}:{y}:{foo}:{nano}");
36+
}
37+
}
38+
39+
public class MultiContainedTest
40+
{
41+
ITestOutputHelper testOutput;
42+
43+
public MultiContainedTest(ITestOutputHelper testOutput)
44+
{
45+
this.testOutput = testOutput;
46+
}
47+
48+
[Fact]
49+
public async Task MultiContained()
50+
{
51+
{
52+
var args = "Multi1.Hello1".Split(' ');
53+
var log = new LogStack();
54+
await new HostBuilder()
55+
.ConfigureTestLogging(testOutput, log, true)
56+
.RunBatchEngineAsync(args);
57+
log.InfoLogShouldBe(0, "ok");
58+
}
59+
{
60+
var args = "Multi1.Hello2 -input yeah".Split(' ');
61+
var log = new LogStack();
62+
await new HostBuilder()
63+
.ConfigureTestLogging(testOutput, log, true)
64+
.RunBatchEngineAsync(args);
65+
log.InfoLogShouldBe(0, "yeah");
66+
}
67+
{
68+
var args = "Multi2.Hello1 -x 20 -y 30".Split(' ');
69+
var log = new LogStack();
70+
await new HostBuilder()
71+
.ConfigureTestLogging(testOutput, log, true)
72+
.RunBatchEngineAsync(args);
73+
log.InfoLogShouldBe(0, "20:30");
74+
}
75+
{
76+
var args = "Multi2.Hello2 -x -y -foo yeah".Split(' ');
77+
var log = new LogStack();
78+
await new HostBuilder()
79+
.ConfigureTestLogging(testOutput, log, true)
80+
.RunBatchEngineAsync(args);
81+
log.InfoLogShouldBe(0, "True:True:yeah:999");
82+
}
83+
}
84+
}
85+
}

tests/MicroBatchFramework.Tests/SingleContainedTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public async Task TwoArgsWithOptionTest()
143143
var args = new string[0];
144144
await new HostBuilder().RunBatchEngineAsync<TwoArgsWithOption>(args);
145145
var strAssertion = log.ToStringInfo().Should();
146-
strAssertion.StartWith("argument list:"); // ok to show help
146+
strAssertion.Contain("argument list:"); // ok to show help
147147
strAssertion.Contain("-n");
148148
strAssertion.Contain("name of this");
149149
strAssertion.Contain("-r");

tests/MicroBatchFramework.Tests/SubCommandTest.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,77 @@ public SubCommandTest(ITestOutputHelper testOutput)
1818
this.testOutput = testOutput;
1919
}
2020

21+
public class TwoSubCommand : BatchBase
22+
{
23+
public void Main(double d)
24+
{
25+
Context.Logger.LogInformation($"d:{d}");
26+
}
27+
28+
[Command("run")]
29+
public void Run(string path, string pfx)
30+
{
31+
Context.Logger.LogInformation($"path:{path}");
32+
Context.Logger.LogInformation($"pfx:{pfx}");
33+
}
34+
35+
[Command("sum")]
36+
public void Sum([Option(0)]int x, [Option(1)]int y)
37+
{
38+
Context.Logger.LogInformation($"x:{x}");
39+
Context.Logger.LogInformation($"y:{y}");
40+
}
41+
42+
[Command("opt")]
43+
public void Option([Option(0)]string input, [Option("x")]int xxx, [Option("y")]int yyy)
44+
{
45+
Context.Logger.LogInformation($"input:{input}");
46+
Context.Logger.LogInformation($"x:{xxx}");
47+
Context.Logger.LogInformation($"y:{yyy}");
48+
}
49+
}
50+
51+
[Fact]
52+
public async Task TwoSubCommandTest()
53+
{
54+
{
55+
var args = "-d 12345.12345".Split(' ');
56+
var log = new LogStack();
57+
await new HostBuilder()
58+
.ConfigureTestLogging(testOutput, log, true)
59+
.RunBatchEngineAsync<TwoSubCommand>(args);
60+
log.InfoLogShouldBe(0, "d:12345.12345");
61+
}
62+
{
63+
var args = "run -path foo -pfx bar".Split(' ');
64+
var log = new LogStack();
65+
await new HostBuilder()
66+
.ConfigureTestLogging(testOutput, log, true)
67+
.RunBatchEngineAsync<TwoSubCommand>(args);
68+
log.InfoLogShouldBe(0, "path:foo");
69+
log.InfoLogShouldBe(1, "pfx:bar");
70+
}
71+
{
72+
var args = "sum 10 20".Split(' ');
73+
var log = new LogStack();
74+
await new HostBuilder()
75+
.ConfigureTestLogging(testOutput, log, true)
76+
.RunBatchEngineAsync<TwoSubCommand>(args);
77+
log.InfoLogShouldBe(0, "x:10");
78+
log.InfoLogShouldBe(1, "y:20");
79+
}
80+
{
81+
var args = "opt foobarbaz -x 10 -y 20".Split(' ');
82+
var log = new LogStack();
83+
await new HostBuilder()
84+
.ConfigureTestLogging(testOutput, log, true)
85+
.RunBatchEngineAsync<TwoSubCommand>(args);
86+
log.InfoLogShouldBe(0, "input:foobarbaz");
87+
log.InfoLogShouldBe(1, "x:10");
88+
log.InfoLogShouldBe(2, "y:20");
89+
}
90+
}
91+
2192
public class NotFoundPath : BatchBase
2293
{
2394
[Command("run")]

0 commit comments

Comments
 (0)