|
7 | 7 | using System;
|
8 | 8 | using System.Linq;
|
9 | 9 | using System.Threading;
|
| 10 | +using System.Threading.Tasks; |
10 | 11 | using Xunit;
|
11 | 12 |
|
12 | 13 | namespace MatthiWare.CommandLine.Tests
|
@@ -84,6 +85,36 @@ public void CommandLineParserUsesContainerCorrectly(bool generic)
|
84 | 85 | containerMock.VerifyAll();
|
85 | 86 | }
|
86 | 87 |
|
| 88 | + [Theory] |
| 89 | + [InlineData(true)] |
| 90 | + [InlineData(false)] |
| 91 | + public async Task CommandLineParserUsesContainerCorrectlyAsync(bool generic) |
| 92 | + { |
| 93 | + var commandMock = new Mock<MyCommand>(); |
| 94 | + commandMock.Setup( |
| 95 | + c => c.OnConfigure(It.IsAny<ICommandConfigurationBuilder<object>>())) |
| 96 | + .CallBase().Verifiable("OnConfigure not called"); |
| 97 | + |
| 98 | + commandMock.Setup(c => c.OnExecuteAsync(It.IsAny<object>(), It.IsAny<object>(), It.IsAny<CancellationToken>())).Verifiable("OnExecute not called"); |
| 99 | + |
| 100 | + var containerMock = new Mock<IContainerResolver>(); |
| 101 | + containerMock.Setup(c => c.Resolve<MyCommand>()).Returns(commandMock.Object).Verifiable(); |
| 102 | + |
| 103 | + var parser = new CommandLineParser<object>(containerMock.Object); |
| 104 | + |
| 105 | + if (generic) |
| 106 | + parser.RegisterCommand<MyCommand, object>(); |
| 107 | + else |
| 108 | + parser.RegisterCommand(typeof(MyCommand), typeof(object)); |
| 109 | + |
| 110 | + var result = await parser.ParseAsync(new[] { "app.exe", "my" }); |
| 111 | + |
| 112 | + result.AssertNoErrors(); |
| 113 | + |
| 114 | + commandMock.VerifyAll(); |
| 115 | + containerMock.VerifyAll(); |
| 116 | + } |
| 117 | + |
87 | 118 | [Fact]
|
88 | 119 | public void CommandLinerParserPassesContainerCorreclty()
|
89 | 120 | {
|
@@ -150,6 +181,30 @@ public void AutoExecuteCommandsWithExceptionDoesntCrashTheParser()
|
150 | 181 | Assert.Equal(ex, result.Errors.First());
|
151 | 182 | }
|
152 | 183 |
|
| 184 | + [Fact] |
| 185 | + public async Task AutoExecuteCommandsWithExceptionDoesntCrashTheParserAsync() |
| 186 | + { |
| 187 | + var parser = new CommandLineParser(); |
| 188 | + |
| 189 | + var ex = new Exception("uh-oh"); |
| 190 | + |
| 191 | + parser.AddCommand() |
| 192 | + .Name("test") |
| 193 | + .InvokeCommand(true) |
| 194 | + .Required(true) |
| 195 | + .OnExecutingAsync(async (_, __) => |
| 196 | + { |
| 197 | + await Task.Delay(1); |
| 198 | + throw ex; |
| 199 | + }); |
| 200 | + |
| 201 | + var result = await parser.ParseAsync(new[] { "test" }); |
| 202 | + |
| 203 | + Assert.True(result.HasErrors); |
| 204 | + |
| 205 | + Assert.Equal(ex, result.Errors.First()); |
| 206 | + } |
| 207 | + |
153 | 208 | [Fact]
|
154 | 209 | public void CommandLineParserUsesArgumentFactoryCorrectly()
|
155 | 210 | {
|
@@ -347,6 +402,49 @@ public void ParseWithCommandTests()
|
347 | 402 | Assert.True(wait.WaitOne(2000));
|
348 | 403 | }
|
349 | 404 |
|
| 405 | + [Fact] |
| 406 | + public async Task ParseWithCommandTestsAsync() |
| 407 | + { |
| 408 | + var wait = new ManualResetEvent(false); |
| 409 | + |
| 410 | + var parser = new CommandLineParser<Options>(); |
| 411 | + |
| 412 | + parser.Configure(opt => opt.Option1) |
| 413 | + .Name("o") |
| 414 | + .Default("Default message") |
| 415 | + .Required(); |
| 416 | + |
| 417 | + var addCmd = parser.AddCommand<AddOption>() |
| 418 | + .Name("add") |
| 419 | + .OnExecutingAsync(async (opt, cmdOpt, ctx) => |
| 420 | + { |
| 421 | + await Task.Delay(100); |
| 422 | + |
| 423 | + Assert.Equal("test", opt.Option1); |
| 424 | + Assert.Equal("my message", cmdOpt.Message); |
| 425 | + |
| 426 | + await Task.Delay(100); |
| 427 | + |
| 428 | + wait.Set(); |
| 429 | + |
| 430 | + await Task.Delay(100); |
| 431 | + }); |
| 432 | + |
| 433 | + addCmd.Configure(opt => opt.Message) |
| 434 | + .Name("m", "message") |
| 435 | + .Required(); |
| 436 | + |
| 437 | + var parsed = await parser.ParseAsync(new string[] { "app.exe", "-o", "test", "add", "-m=my message" }); |
| 438 | + |
| 439 | + parsed.AssertNoErrors(); |
| 440 | + |
| 441 | + Assert.Equal("test", parsed.Result.Option1); |
| 442 | + |
| 443 | + parsed.ExecuteCommands(); |
| 444 | + |
| 445 | + Assert.True(wait.WaitOne(2000)); |
| 446 | + } |
| 447 | + |
350 | 448 | [Theory]
|
351 | 449 | [InlineData(new[] { "app.exe", "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
|
352 | 450 | [InlineData(new[] { "app.exe", "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
|
@@ -383,6 +481,44 @@ public void ParseCommandTests(string[] args, string result1, string result2)
|
383 | 481 | Assert.True(wait.WaitOne(2000));
|
384 | 482 | }
|
385 | 483 |
|
| 484 | + [Theory] |
| 485 | + [InlineData(new[] { "app.exe", "add", "-m", "message2", "-m", "message1" }, "message1", "message2")] |
| 486 | + [InlineData(new[] { "app.exe", "-m", "message1", "add", "-m", "message2" }, "message1", "message2")] |
| 487 | + [InlineData(new[] { "add", "-m", "message2", "-m", "message1" }, "message1", "message2")] |
| 488 | + [InlineData(new[] { "-m", "message1", "add", "-m", "message2" }, "message1", "message2")] |
| 489 | + public async Task ParseCommandTestsAsync(string[] args, string result1, string result2) |
| 490 | + { |
| 491 | + var parser = new CommandLineParser<AddOption>(); |
| 492 | + var wait = new ManualResetEvent(false); |
| 493 | + |
| 494 | + parser.AddCommand<AddOption>() |
| 495 | + .Name("add") |
| 496 | + .Required() |
| 497 | + .OnExecutingAsync(async (opt1, opt2, ctx) => |
| 498 | + { |
| 499 | + await Task.Delay(100); |
| 500 | + |
| 501 | + wait.Set(); |
| 502 | + |
| 503 | + Assert.Equal(result2, opt2.Message); |
| 504 | + }) |
| 505 | + .Configure(c => c.Message) |
| 506 | + .Name("m", "message") |
| 507 | + .Required(); |
| 508 | + |
| 509 | + parser.Configure(opt => opt.Message) |
| 510 | + .Name("m", "message") |
| 511 | + .Required(); |
| 512 | + |
| 513 | + var result = await parser.ParseAsync(args); |
| 514 | + |
| 515 | + result.AssertNoErrors(); |
| 516 | + |
| 517 | + Assert.Equal(result1, result.Result.Message); |
| 518 | + |
| 519 | + Assert.True(wait.WaitOne(2000)); |
| 520 | + } |
| 521 | + |
386 | 522 | [Theory]
|
387 | 523 | [InlineData(new string[] { "-x", "" }, true)]
|
388 | 524 | [InlineData(new string[] { "-x" }, true)]
|
|
0 commit comments