|
1 | 1 | using MatthiWare.CommandLine.Abstractions.Command;
|
| 2 | +using MatthiWare.CommandLine.Abstractions.Usage; |
2 | 3 | using MatthiWare.CommandLine.Core.Attributes;
|
3 | 4 | using MatthiWare.CommandLine.Core.Exceptions;
|
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Moq; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
4 | 9 | using System.Linq;
|
5 | 10 | using System.Threading.Tasks;
|
6 | 11 | using Xunit;
|
@@ -130,6 +135,67 @@ public async Task CommandParseExceptionTestAsync()
|
130 | 135 | Assert.Same(parser.Commands.First(), result.Errors.Cast<CommandParseException>().First().Command);
|
131 | 136 | }
|
132 | 137 |
|
| 138 | + [Fact] |
| 139 | + public async Task CommandParseException_Prints_Errors() |
| 140 | + { |
| 141 | + var printerMock = new Mock<IUsagePrinter>(); |
| 142 | + |
| 143 | + Services.AddSingleton(printerMock.Object); |
| 144 | + |
| 145 | + var parser = new CommandLineParser<OtherOptions>(Services); |
| 146 | + |
| 147 | + parser.AddCommand<Options>() |
| 148 | + .Name("missing") |
| 149 | + .Required() |
| 150 | + .Configure(opt => opt.MissingOption) |
| 151 | + .Name("o") |
| 152 | + .Required(); |
| 153 | + |
| 154 | + var result = await parser.ParseAsync(new string[] { "-a", "1", "-b", "2", "-a", "10" ,"20" ,"30", "missing" }); |
| 155 | + |
| 156 | + printerMock.Verify(_ => _.PrintErrors(It.IsAny<IReadOnlyCollection<Exception>>())); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void CommandParseException_Should_Contain_Correct_Message_Single() |
| 161 | + { |
| 162 | + var cmdMock = new Mock<ICommandLineCommand>(); |
| 163 | + cmdMock.SetupGet(_ => _.Name).Returns("test"); |
| 164 | + |
| 165 | + var exceptionList = new List<Exception> |
| 166 | + { |
| 167 | + new Exception("msg1") |
| 168 | + }; |
| 169 | + |
| 170 | + var parseException = new CommandParseException(cmdMock.Object, exceptionList.AsReadOnly()); |
| 171 | + var msg = parseException.Message; |
| 172 | + var expected = @"Unable to parse command 'test' reason: msg1"; |
| 173 | + |
| 174 | + Assert.Equal(expected, msg); |
| 175 | + } |
| 176 | + |
| 177 | + [Fact] |
| 178 | + public void CommandParseException_Should_Contain_Correct_Message_Multiple() |
| 179 | + { |
| 180 | + var cmdMock = new Mock<ICommandLineCommand>(); |
| 181 | + cmdMock.SetupGet(_ => _.Name).Returns("test"); |
| 182 | + |
| 183 | + var exceptionList = new List<Exception> |
| 184 | + { |
| 185 | + new Exception("msg1"), |
| 186 | + new Exception("msg2") |
| 187 | + }; |
| 188 | + |
| 189 | + var parseException = new CommandParseException(cmdMock.Object, exceptionList.AsReadOnly()); |
| 190 | + var msg = parseException.Message; |
| 191 | + var expected = @"Unable to parse command 'test' because 2 errors occured |
| 192 | + - msg1 |
| 193 | + - msg2 |
| 194 | +"; |
| 195 | + |
| 196 | + Assert.Equal(expected, msg); |
| 197 | + } |
| 198 | + |
133 | 199 | [Fact]
|
134 | 200 | public void OptionParseExceptionTest()
|
135 | 201 | {
|
@@ -158,6 +224,15 @@ public async Task OptionParseExceptionTestAsync()
|
158 | 224 | Assert.Same(parser.Options.First(), result.Errors.Cast<OptionParseException>().First().Option);
|
159 | 225 | }
|
160 | 226 |
|
| 227 | + private class OtherOptions |
| 228 | + { |
| 229 | + [Required, Name("a")] |
| 230 | + public int A { get; set; } |
| 231 | + |
| 232 | + [Required, Name("b")] |
| 233 | + public int B { get; set; } |
| 234 | + } |
| 235 | + |
161 | 236 | private class Options
|
162 | 237 | {
|
163 | 238 | [Required, Name("m", "missing")]
|
|
0 commit comments