|
1 | | -using System; |
2 | | -using System.Threading; |
3 | | -using MatthiWare.CommandLine; |
| 1 | +using MatthiWare.CommandLine; |
4 | 2 | using MatthiWare.CommandLine.Abstractions; |
5 | 3 | using MatthiWare.CommandLine.Abstractions.Command; |
6 | 4 | using MatthiWare.CommandLine.Core.Attributes; |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
7 | 8 | using Xunit; |
8 | 9 |
|
9 | 10 | namespace MatthiWare.CommandLine.Tests.Command |
10 | 11 | { |
11 | 12 | public class SubCommandTests |
12 | 13 | { |
13 | 14 | [Theory] |
14 | | - [InlineData(true)] |
15 | | - [InlineData(false)] |
16 | | - public void TestSubCommandWorksCorrectlyInModel(bool autoExecute) |
| 15 | + [InlineData(true, "something", 15, -1)] |
| 16 | + [InlineData(false, "something", 15, -1)] |
| 17 | + [InlineData(true, "", 15, -1)] |
| 18 | + public void TestSubCommandWorksCorrectlyInModel(bool autoExecute, string bla, int i, int n) |
17 | 19 | { |
18 | 20 | var lock1 = new ManualResetEventSlim(); |
19 | 21 | var lock2 = new ManualResetEventSlim(); |
20 | 22 |
|
21 | | - var containerResolver = new CustomInstantiator(lock1, lock2, autoExecute); |
| 23 | + var containerResolver = new CustomInstantiator(lock1, lock2, autoExecute, bla, i, n); |
22 | 24 |
|
23 | 25 | var parser = new CommandLineParser<MainModel>(containerResolver); |
24 | 26 |
|
25 | | - var result = parser.Parse(new[] { "main", "-b", "something", "sub", "-i", "15", "-n", "-1" }); |
| 27 | + var result = parser.Parse(new[] { "main", "-b", bla, "sub", "-i", i.ToString(), "-n", n.ToString() }); |
26 | 28 |
|
27 | 29 | result.AssertNoErrors(); |
28 | 30 |
|
29 | 31 | if (!autoExecute) |
| 32 | + { |
| 33 | + Assert.All(result.CommandResults.Select(r => r.Executed), Assert.False); |
| 34 | + |
30 | 35 | result.ExecuteCommands(); |
| 36 | + } |
31 | 37 |
|
32 | 38 | Assert.True(lock1.Wait(1000), "MainCommand didn't execute in time."); |
33 | 39 | Assert.True(lock2.Wait(1000), "SubCommand didn't execute in time."); |
| 40 | + |
| 41 | + Assert.All(result.CommandResults.Select(r => r.Executed), Assert.True); |
34 | 42 | } |
35 | 43 |
|
36 | 44 | private class CustomInstantiator : IContainerResolver |
37 | 45 | { |
38 | 46 | private readonly ManualResetEventSlim lock1; |
39 | 47 | private readonly ManualResetEventSlim lock2; |
40 | 48 | private readonly bool autoExecute; |
| 49 | + private readonly string bla; |
| 50 | + private readonly int i; |
| 51 | + private readonly int n; |
41 | 52 |
|
42 | | - public CustomInstantiator(ManualResetEventSlim lock1, ManualResetEventSlim lock2, bool autoExecute) |
| 53 | + public CustomInstantiator(ManualResetEventSlim lock1, ManualResetEventSlim lock2, bool autoExecute, string bla, int i, int n) |
43 | 54 | { |
44 | 55 | this.lock1 = lock1; |
45 | 56 | this.lock2 = lock2; |
46 | 57 | this.autoExecute = autoExecute; |
| 58 | + this.bla = bla; |
| 59 | + this.i = i; |
| 60 | + this.n = n; |
47 | 61 | } |
48 | 62 |
|
49 | 63 | public T Resolve<T>() |
50 | 64 | { |
51 | 65 | if (typeof(T) == typeof(MainCommand)) |
52 | | - return (T)Activator.CreateInstance(typeof(T), lock1, autoExecute); |
| 66 | + return (T)Activator.CreateInstance(typeof(T), lock1, autoExecute, bla, i, n); |
53 | 67 | else if (typeof(T) == typeof(SubCommand)) |
54 | | - return (T)Activator.CreateInstance(typeof(T), lock2, autoExecute); |
| 68 | + return (T)Activator.CreateInstance(typeof(T), lock2, autoExecute, bla, i, n); |
55 | 69 | else |
56 | | - return default; |
| 70 | + throw new InvalidCastException($"Unable to resolve {(typeof(T)).Name}"); |
57 | 71 | } |
58 | 72 | } |
59 | | - } |
60 | | - |
61 | | - public class MainCommand : Command<MainModel, SubModel> |
62 | | - { |
63 | | - private readonly ManualResetEventSlim locker; |
64 | | - private readonly bool autoExecute; |
65 | 73 |
|
66 | | - public MainCommand(ManualResetEventSlim locker, bool autoExecute) |
| 74 | + public class MainCommand : Command<MainModel, SubModel> |
67 | 75 | { |
68 | | - this.locker = locker; |
69 | | - this.autoExecute = autoExecute; |
70 | | - } |
| 76 | + private readonly ManualResetEventSlim locker; |
| 77 | + private readonly bool autoExecute; |
| 78 | + private readonly string bla; |
| 79 | + private readonly int i; |
| 80 | + private readonly int n; |
71 | 81 |
|
72 | | - public override void OnConfigure(ICommandConfigurationBuilder<SubModel> builder) |
73 | | - { |
74 | | - builder |
75 | | - .Name("main") |
76 | | - .AutoExecute(autoExecute) |
77 | | - .Required(); |
| 82 | + public MainCommand(ManualResetEventSlim locker, bool autoExecute, string bla, int i, int n) |
| 83 | + { |
| 84 | + this.locker = locker; |
| 85 | + this.autoExecute = autoExecute; |
| 86 | + this.bla = bla; |
| 87 | + this.i = i; |
| 88 | + this.n = n; |
| 89 | + } |
| 90 | + |
| 91 | + public override void OnConfigure(ICommandConfigurationBuilder<SubModel> builder) |
| 92 | + { |
| 93 | + builder |
| 94 | + .Name("main") |
| 95 | + .AutoExecute(autoExecute) |
| 96 | + .Required(); |
| 97 | + } |
| 98 | + |
| 99 | + public override void OnExecute(MainModel options, SubModel commandOptions) |
| 100 | + { |
| 101 | + base.OnExecute(options, commandOptions); |
| 102 | + |
| 103 | + Assert.Equal(bla, options.Bla); |
| 104 | + Assert.Equal(i, commandOptions.Item); |
| 105 | + |
| 106 | + locker.Set(); |
| 107 | + } |
78 | 108 | } |
79 | 109 |
|
80 | | - public override void OnExecute(MainModel options, SubModel commandOptions) |
| 110 | + public class SubCommand : Command<MainModel, SubSubModel> |
81 | 111 | { |
82 | | - base.OnExecute(options, commandOptions); |
| 112 | + private readonly ManualResetEventSlim locker; |
| 113 | + private readonly bool autoExecute; |
| 114 | + private readonly string bla; |
| 115 | + private readonly int i; |
| 116 | + private readonly int n; |
83 | 117 |
|
84 | | - locker.Set(); |
85 | | - } |
86 | | - } |
| 118 | + public SubCommand(ManualResetEventSlim locker, bool autoExecute, string bla, int i, int n) |
| 119 | + { |
| 120 | + this.locker = locker; |
| 121 | + this.autoExecute = autoExecute; |
| 122 | + this.bla = bla; |
| 123 | + this.i = i; |
| 124 | + this.n = n; |
| 125 | + } |
87 | 126 |
|
88 | | - public class SubCommand : Command<MainModel, SubSubModel> |
89 | | - { |
90 | | - private readonly ManualResetEventSlim locker; |
91 | | - private readonly bool autoExecute; |
| 127 | + public override void OnConfigure(ICommandConfigurationBuilder<SubSubModel> builder) |
| 128 | + { |
| 129 | + builder |
| 130 | + .Name("sub") |
| 131 | + .AutoExecute(autoExecute) |
| 132 | + .Required(); |
| 133 | + } |
92 | 134 |
|
93 | | - public SubCommand(ManualResetEventSlim locker, bool autoExecute) |
94 | | - { |
95 | | - this.locker = locker; |
96 | | - this.autoExecute = autoExecute; |
| 135 | + public override void OnExecute(MainModel options, SubSubModel commandOptions) |
| 136 | + { |
| 137 | + base.OnExecute(options, commandOptions); |
| 138 | + |
| 139 | + Assert.Equal(bla, options.Bla); |
| 140 | + Assert.Equal(n, commandOptions.Nothing); |
| 141 | + |
| 142 | + locker.Set(); |
| 143 | + } |
97 | 144 | } |
98 | 145 |
|
99 | | - public override void OnConfigure(ICommandConfigurationBuilder<SubSubModel> builder) |
| 146 | + public class MainModel |
100 | 147 | { |
101 | | - builder |
102 | | - .Name("sub") |
103 | | - .AutoExecute(autoExecute) |
104 | | - .Required(); |
| 148 | + [Required, Name("b")] |
| 149 | + public string Bla { get; set; } |
| 150 | + public MainCommand MainCommand { get; set; } |
105 | 151 | } |
106 | 152 |
|
107 | | - public override void OnExecute(MainModel options, SubSubModel commandOptions) |
| 153 | + public class SubModel |
108 | 154 | { |
109 | | - base.OnExecute(options, commandOptions); |
110 | | - |
111 | | - locker.Set(); |
| 155 | + [Required, Name("i")] |
| 156 | + public int Item { get; set; } |
| 157 | + public SubCommand SubCommand { get; set; } |
112 | 158 | } |
113 | | - } |
114 | | - |
115 | | - public class MainModel |
116 | | - { |
117 | | - [Required, Name("b")] |
118 | | - public string Bla { get; set; } |
119 | | - public MainCommand MainCommand { get; set; } |
120 | | - } |
121 | 159 |
|
122 | | - public class SubModel |
123 | | - { |
124 | | - [Required, Name("i")] |
125 | | - public int Item { get; set; } |
126 | | - public SubCommand SubCommand { get; set; } |
127 | | - } |
128 | | - |
129 | | - public class SubSubModel |
130 | | - { |
131 | | - [Required, Name("n")] |
132 | | - public int Nothing { get; set; } |
| 160 | + public class SubSubModel |
| 161 | + { |
| 162 | + [Required, Name("n")] |
| 163 | + public int Nothing { get; set; } |
| 164 | + } |
133 | 165 | } |
134 | 166 | } |
0 commit comments