|
6 | 6 | using System;
|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Threading;
|
| 9 | +using System.Threading.Tasks; |
9 | 10 | using Xunit;
|
10 | 11 |
|
11 | 12 | namespace MatthiWare.CommandLine.Tests.Command
|
@@ -42,6 +43,36 @@ public void TestSubCommandWorksCorrectlyInModel(bool autoExecute, string bla, in
|
42 | 43 | Assert.All(result.CommandResults.Select(r => r.Executed), Assert.True);
|
43 | 44 | }
|
44 | 45 |
|
| 46 | + [Theory] |
| 47 | + [InlineData(true, "something", 15, -1)] |
| 48 | + [InlineData(false, "something", 15, -1)] |
| 49 | + [InlineData(true, "", 15, -1)] |
| 50 | + public async Task TestSubCommandWorksCorrectlyInModelAsync(bool autoExecute, string bla, int i, int n) |
| 51 | + { |
| 52 | + var lock1 = new ManualResetEventSlim(); |
| 53 | + var lock2 = new ManualResetEventSlim(); |
| 54 | + |
| 55 | + var containerResolver = new CustomInstantiator(lock1, lock2, autoExecute, bla, i, n); |
| 56 | + |
| 57 | + var parser = new CommandLineParser<MainModel>(containerResolver); |
| 58 | + |
| 59 | + var result = await parser.ParseAsync(new[] { "main", "-b", bla, "sub", "-i", i.ToString(), "-n", n.ToString() }); |
| 60 | + |
| 61 | + result.AssertNoErrors(); |
| 62 | + |
| 63 | + if (!autoExecute) |
| 64 | + { |
| 65 | + Assert.All(result.CommandResults.Select(r => r.Executed), Assert.False); |
| 66 | + |
| 67 | + result.ExecuteCommands(); |
| 68 | + } |
| 69 | + |
| 70 | + Assert.True(lock1.Wait(1000), "MainCommand didn't execute in time."); |
| 71 | + Assert.True(lock2.Wait(1000), "SubCommand didn't execute in time."); |
| 72 | + |
| 73 | + Assert.All(result.CommandResults.Select(r => r.Executed), Assert.True); |
| 74 | + } |
| 75 | + |
45 | 76 | private class CustomInstantiator : DefaultContainerResolver
|
46 | 77 | {
|
47 | 78 | private readonly ManualResetEventSlim lock1;
|
@@ -108,6 +139,18 @@ public override void OnExecute(MainModel options, SubModel commandOptions)
|
108 | 139 |
|
109 | 140 | locker.Set();
|
110 | 141 | }
|
| 142 | + |
| 143 | + public override Task OnExecuteAsync(MainModel options, SubModel commandOptions, CancellationToken cancellationToken) |
| 144 | + { |
| 145 | + base.OnExecuteAsync(options, commandOptions, cancellationToken); |
| 146 | + |
| 147 | + Assert.Equal(bla, options.Bla); |
| 148 | + Assert.Equal(i, commandOptions.Item); |
| 149 | + |
| 150 | + locker.Set(); |
| 151 | + |
| 152 | + return Task.CompletedTask; |
| 153 | + } |
111 | 154 | }
|
112 | 155 |
|
113 | 156 | public class SubCommand : Command<MainModel, SubSubModel>
|
@@ -144,6 +187,18 @@ public override void OnExecute(MainModel options, SubSubModel commandOptions)
|
144 | 187 |
|
145 | 188 | locker.Set();
|
146 | 189 | }
|
| 190 | + |
| 191 | + public override Task OnExecuteAsync(MainModel options, SubSubModel commandOptions, CancellationToken cancellationToken) |
| 192 | + { |
| 193 | + base.OnExecuteAsync(options, commandOptions, cancellationToken); |
| 194 | + |
| 195 | + Assert.Equal(bla, options.Bla); |
| 196 | + Assert.Equal(n, commandOptions.Nothing); |
| 197 | + |
| 198 | + locker.Set(); |
| 199 | + |
| 200 | + return Task.CompletedTask; |
| 201 | + } |
147 | 202 | }
|
148 | 203 |
|
149 | 204 | public class MainModel
|
|
0 commit comments