BadScript 2
Loading...
Searching...
No Matches
ParserTests.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.IO;
6using System.Linq;
7using Xunit;
8using FluentAssertions;
11
13{
14 public class ParserTests
15 {
16 [Fact]
18 {
19 // Fixture setup
20 var writer = new StringWriter();
21 var sut = new Parser(with => with.HelpWriter = writer);
22
23 // Exercize system
24 sut.ParseArguments<Options_With_Required_Set_To_True>(new string[] { });
25
26 // Verify outcome
27 var text = writer.ToString();
28 Assert.True(text.Length > 0);
29 // Teardown
30 }
31
32 [Fact]
34 {
35 // Fixture setup
36 var writer = new StringWriter();
37 var sut = new Parser(with => with.HelpWriter = writer);
38
39 // Exercize system
40 sut.ParseArguments(new string[] { }, typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb));
41
42 // Verify outcome
43 var text = writer.ToString();
44 text.Should().NotBeEmpty();
45 // Teardown
46 }
47
48 [Fact]
50 {
51 // Fixture setup
52 var writer = new StringWriter();
53 var sut = new Parser(with => with.HelpWriter = writer);
54
55 // Exercize system
56 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new string[] { });
57
58 // Verify outcome
59 var text = writer.ToString();
60 text.Should().NotBeEmpty();
61 // Teardown
62 }
63
64 [Fact]
65 public void Parse_options()
66 {
67 // Fixture setup
68 var expectedOptions = new Simple_Options { StringValue = "strvalue", IntSequence = new[] { 1, 2, 3 } };
69 var sut = new Parser();
70
71 // Exercize system
72 var result = sut.ParseArguments<Simple_Options>(new[] { "--stringvalue=strvalue", "-i1", "2", "3" });
73
74 // Verify outcome
75 ((Parsed<Simple_Options>)result).Value.Should().BeEquivalentTo(expectedOptions);
76 // Teardown
77 }
78
79 [Theory]
80 [InlineData("file", new[] { "-o", "file" })]
81 [InlineData("file", new[] { "-ofile" })]
82 [InlineData("hile", new[] { "-o", "hile" })]
83 [InlineData("hile", new[] { "-ohile" })]
84 public void Parse_options_with_short_name(string outputFile, string[] args)
85 {
86 // Fixture setup
87 var expectedOptions = new Options_With_Switches { OutputFile = outputFile };
88 var sut = new Parser();
89
90 // Exercize system
91 var result = sut.ParseArguments<Options_With_Switches>(args);
92
93 // Verify outcome
94 ((Parsed<Options_With_Switches>)result).Value.Should().BeEquivalentTo(expectedOptions);
95 // Teardown
96 }
97
98 [Theory]
99 [InlineData(new string[0], 0, 0)]
100 [InlineData(new[] { "-v" }, 1, 0)]
101 [InlineData(new[] { "-vv" }, 2, 0)]
102 [InlineData(new[] { "-v", "-v" }, 2, 0)]
103 [InlineData(new[] { "-v", "-v", "-v" }, 3, 0)]
104 [InlineData(new[] { "-v", "-vv" }, 3, 0)]
105 [InlineData(new[] { "-vv", "-v" }, 3, 0)]
106 [InlineData(new[] { "-vvv" }, 3, 0)]
107 [InlineData(new[] { "-v", "-s", "-v", "-v" }, 3, 1)]
108 [InlineData(new[] { "-v", "-ss", "-v", "-v" }, 3, 2)]
109 [InlineData(new[] { "-v", "-s", "-sv", "-v" }, 3, 2)]
110 [InlineData(new[] { "-vsvv" }, 3, 1)]
111 [InlineData(new[] { "-vssvv" }, 3, 2)]
112 [InlineData(new[] { "-vsvsv" }, 3, 2)]
113 public void Parse_FlagCounter_options_with_short_name(string[] args, int verboseCount, int silentCount)
114 {
115 // Fixture setup
116 var expectedOptions = new Options_With_FlagCounter_Switches { Verbose = verboseCount, Silent = silentCount };
117 var sut = new Parser(with => with.AllowMultiInstance = true);
118
119 // Exercize system
120 var result = sut.ParseArguments<Options_With_FlagCounter_Switches>(args);
121
122 // Verify outcome
123 // ((NotParsed<Options_With_FlagCounter_Switches>)result).Errors.Should().BeEmpty();
124 ((Parsed<Options_With_FlagCounter_Switches>)result).Value.Should().BeEquivalentTo(expectedOptions);
125 // Teardown
126 }
127
128 [Fact]
130 {
131 // Fixture setup
132 var sut = Parser.Default;
133
134 // Exercize system
135 var result = sut.ParseArguments<Options_With_Switches>(new[] { "-i", "-i", "-o", "file" });
136
137 // Verify outcome
138 Assert.IsType<NotParsed<Options_With_Switches>>(result);
139 // NOTE: Once GetoptMode becomes the default, it will imply MultiInstance and the above check will fail because it will be Parsed.
140 // Teardown
141 }
142
143 [Fact]
145 {
146 // Fixture setup
147 var expectedOptions = new Simple_Options_With_Values
148 {
149 StringValue = "astring",
150 LongValue = 20L,
151 StringSequence = new[] { "--aaa", "-b", "--ccc" },
152 IntValue = 30
153 };
154 var sut = new Parser(with => with.EnableDashDash = true);
155
156 // Exercize system
157 var result =
158 sut.ParseArguments<Simple_Options_With_Values>(
159 new[] { "--stringvalue", "astring", "--", "20", "--aaa", "-b", "--ccc", "30" });
160
161 // Verify outcome
162 ((Parsed<Simple_Options_With_Values>)result).Value.Should().BeEquivalentTo(expectedOptions);
163 // Teardown
164 }
165
166 [Fact]
168 {
169 var text = "x1 x2 x3 -c x1"; // x1 is the same in -c option and first value
170 var args = text.Split();
171 var parser = new Parser(with =>
172 {
173 with.HelpWriter = Console.Out;
174 });
176 var options= (result as Parsed<Options_With_Value_Sequence_And_Normal_Option>).Value;
177 options.Compress.Should().BeEquivalentTo(new[] { "x1" });
178 options.InputDirs.Should().BeEquivalentTo(new[] { "x1","x2","x3" });
179 }
180
181 [Fact]
183 {
185 {
186 OptionSequence = new[] { "option1", "option2", "option3" },
187 ValueSequence = new[] { "value1", "value2", "value3" }
188 };
189
190 var sut = new Parser(with => with.EnableDashDash = true);
191
192 // Exercize system
193 var result =
195 new[] { "--option-seq", "option1", "option2", "option3", "--", "value1", "value2", "value3" });
196
197 // Verify outcome
198 ((Parsed<Options_With_Option_Sequence_And_Value_Sequence>)result).Value.Should().BeEquivalentTo(expectedOptions);
199 }
200
201 [Fact]
203 {
204 // Fixture setup
205 var expectedOptions = new Add_Verb { Patch = true, FileName = "--strange-fn" };
206 var sut = new Parser(with => with.EnableDashDash = true);
207
208 // Exercize system
209 var result = sut.ParseArguments(
210 new[] { "add", "-p", "--", "--strange-fn" },
211 typeof(Add_Verb),
212 typeof(Commit_Verb),
213 typeof(Clone_Verb));
214
215 // Verify outcome
216 Assert.IsType<Add_Verb>(((Parsed<object>)result).Value);
217 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
218 // Teardown
219 }
220
221 [Fact]
223 {
224 // Fixture setup
225 var args = new[] {"-"};
226 var expectedOptions = new Options_With_Switches();
227 var sut = new Parser();
228
229 // Exercize system
230 var result = sut.ParseArguments<Options_With_Switches>(args);
231
232 // Verify outcome
233 ((Parsed<Options_With_Switches>)result).Value.Should().BeEquivalentTo(expectedOptions);
234 // Teardown
235 }
236
237 [Fact]
238 public void Parse_verbs()
239 {
240 // Fixture setup
241 var expectedOptions = new Clone_Verb
242 {
243 Quiet = true,
244 Urls =
245 new[]
246 {
247 "http://gsscoder.github.com/",
248 "http://yes-to-nooo.github.com/"
249 }
250 };
251 var sut = new Parser();
252
253 // Exercize system
254 var result =
255 sut.ParseArguments(
256 new[] { "clone", "-q", "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" },
257 typeof(Add_Verb),
258 typeof(Commit_Verb),
259 typeof(Clone_Verb));
260
261 // Verify outcome
262 Assert.IsType<Clone_Verb>(((Parsed<object>)result).Value);
263 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
264 // Teardown
265 }
266
267 [Theory]
268 [InlineData("blabla", new[] { "commit", "-m", "blabla" })]
269 [InlineData("blabla", new[] { "commit", "-mblabla" })]
270 [InlineData("plapla", new[] { "commit", "-m", "plapla" })]
271 [InlineData("plapla", new[] { "commit", "-mplapla" })]
272 public void Parse_options_with_short_name_in_verbs_scenario(string message, string[] args)
273 {
274 // Fixture setup
275 var expectedOptions = new Commit_Verb() { Message = message };
276 var sut = new Parser();
277
278 // Exercize system
279 var result = sut.ParseArguments(
280 args,
281 typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb));
282
283 // Verify outcome
284 Assert.IsType<Commit_Verb>(((Parsed<object>)result).Value);
285 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
286 // Teardown
287 }
288
289 [Fact]
291 {
292 // Fixture setup
293 var sut = Parser.Default;
294
295 // Exercize system
296 var result = sut.ParseArguments(
297 new[] { "clone", "-q", "-q", "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" },
298 typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb));
299
300 // Verify outcome
301 Assert.IsType<NotParsed<object>>(result);
302 // NOTE: Once GetoptMode becomes the default, it will imply MultiInstance and the above check will fail because it will be Parsed.
303 // Teardown
304 }
305
306 [Fact]
308 {
309 // Fixture setup
310 var expectedOptions = new Clone_Verb
311 {
312 Quiet = true,
313 Urls =
314 new[]
315 {
316 "http://gsscoder.github.com/",
317 "http://yes-to-nooo.github.com/"
318 }
319 };
320 var sut = new Parser();
321
322 // Exercize system
323 var result =
324 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(
325 new[] { "clone", "-q", "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" });
326
327 // Verify outcome
328 Assert.IsType<Clone_Verb>(((Parsed<object>)result).Value);
329 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
330 // Teardown
331 }
332
333 [Fact]
335 {
336 // Fixture setup
337 var expectedOptions = new Immutable_Simple_Options("strvalue", new[] { 1, 2, 3 }, default(bool), default(long));
338 var sut = new Parser();
339
340 // Exercize system
341 var result = sut.ParseArguments<Immutable_Simple_Options>(new[] { "--stringvalue=strvalue", "-i1", "2", "3" });
342
343 // Verify outcome
344 ((Parsed<Immutable_Simple_Options>)result).Value.Should().BeEquivalentTo(expectedOptions);
345 // Teardown
346 }
347
348 [Fact]
350 {
351 // Fixture setup
352 var expectedError = new HelpRequestedError();
353 var sut = new Parser();
354
355 // Exercize system
356 var result = sut.ParseArguments<Immutable_Simple_Options>(new[] { "--help" });
357
358 // Verify outcome
359 ((NotParsed<Immutable_Simple_Options>)result).Errors.Should().HaveCount(x => x == 1);
360 ((NotParsed<Immutable_Simple_Options>)result).Errors.Should().ContainSingle(e => e.Equals(expectedError));
361 // Teardown
362 }
363
364 [Fact]
366 {
367 // Fixture setup
368 var help = new StringWriter();
369 var sut = new Parser(config => config.HelpWriter = help);
370
371 // Exercize system
372 sut.ParseArguments<Immutable_Simple_Options>(new[] { "--help" });
373 var result = help.ToString();
374
375 // Verify outcome
376 result.Length.Should().BeGreaterThan(0);
377 // Teardown
378 }
379
380 [Fact]
382 {
383 // Fixture setup
384 var expectedError = new VersionRequestedError();
385 var sut = new Parser();
386
387 // Exercize system
388 var result = sut.ParseArguments<Simple_Options>(new[] { "--version" });
389
390 // Verify outcome
391 ((NotParsed<Simple_Options>)result).Errors.Should().HaveCount(x => x == 1);
392 ((NotParsed<Simple_Options>)result).Errors.Should().ContainSingle(e => e.Equals(expectedError));
393 // Teardown
394 }
395
396 [Fact]
398 {
399 // Fixture setup
400 var help = new StringWriter();
401 var sut = new Parser(config => config.HelpWriter = help);
402
403 // Exercize system
404 sut.ParseArguments<Simple_Options>(new[] { "--version" });
405 var result = help.ToString();
406
407 // Verify outcome
408 result.Length.Should().BeGreaterThan(0);
409 var lines = result.ToNotEmptyLines().TrimStringArray();
410 lines.Should().HaveCount(x => x == 1);
411 lines[0].Should().Be(HeadingInfo.Default.ToString());
412 // Teardown
413 }
414
415 [Fact]
417 {
418 // Fixture setup
419 var help = new StringWriter();
420 var sut = new Parser(config => config.HelpWriter = help);
421
422 // Exercize system
423 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new string[] { });
424 var result = help.ToString();
425
426 // Verify outcome
427 result.Length.Should().BeGreaterThan(0);
428 var lines = result.ToNotEmptyLines().TrimStringArray();
429 lines[0].Should().Be(HeadingInfo.Default.ToString());
430 lines[1].Should().Be(CopyrightInfo.Default.ToString());
431 lines[2].Should().BeEquivalentTo("ERROR(S):");
432 lines[3].Should().BeEquivalentTo("No verb selected.");
433 lines[4].Should().BeEquivalentTo("add Add file contents to the index.");
434 lines[5].Should().BeEquivalentTo("commit Record changes to the repository.");
435 lines[6].Should().BeEquivalentTo("clone Clone a repository into a new directory.");
436 lines[7].Should().BeEquivalentTo("help Display more information on a specific command.");
437 lines[8].Should().BeEquivalentTo("version Display version information.");
438 // Teardown
439 }
440
441 [Fact]
443 {
444 // Fixture setup
445 var help = new StringWriter();
446 var sut = new Parser(config => config.HelpWriter = help);
447
448 // Exercise system
449 sut.ParseArguments<Add_Verb_As_Default, Commit_Verb, Clone_Verb>(new string[] {"--help" });
450 var result = help.ToString();
451
452 // Verify outcome
453 result.Length.Should().BeGreaterThan(0);
454 var lines = result.ToNotEmptyLines().TrimStringArray();
455 lines[0].Should().Be(HeadingInfo.Default.ToString());
456 lines[1].Should().Be(CopyrightInfo.Default.ToString());
457 lines[2].Should().BeEquivalentTo("add (Default Verb) Add file contents to the index.");
458 lines[3].Should().BeEquivalentTo("commit Record changes to the repository.");
459 lines[4].Should().BeEquivalentTo("clone Clone a repository into a new directory.");
460 lines[5].Should().BeEquivalentTo("help Display more information on a specific command.");
461 lines[6].Should().BeEquivalentTo("version Display version information.");
462
463 }
464 [Fact]
466 {
467 // Fixture setup
468 var help = new StringWriter();
469 var sut = new Parser(config => config.HelpWriter = help);
470
471 // Exercize system
472 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new[] { "--help" });
473 var result = help.ToString();
474
475 // Verify outcome
476 var lines = result.ToNotEmptyLines().TrimStringArray();
477 lines[0].Should().Be(HeadingInfo.Default.ToString());
478 lines[1].Should().Be(CopyrightInfo.Default.ToString());
479 lines[2].Should().BeEquivalentTo("add Add file contents to the index.");
480 lines[3].Should().BeEquivalentTo("commit Record changes to the repository.");
481 lines[4].Should().BeEquivalentTo("clone Clone a repository into a new directory.");
482 lines[5].Should().BeEquivalentTo("help Display more information on a specific command.");
483 lines[6].Should().BeEquivalentTo("version Display version information.");
484 // Teardown
485 }
486
487 [Theory]
488 [InlineData("--version")]
489 [InlineData("version")]
491 {
492 // Fixture setup
493 var help = new StringWriter();
494 var sut = new Parser(config => config.HelpWriter = help);
495
496 // Exercize system
497 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new[] { command });
498 var result = help.ToString();
499
500 // Verify outcome
501 result.Length.Should().BeGreaterThan(0);
502 var lines = result.ToNotEmptyLines().TrimStringArray();
503 lines.Should().HaveCount(x => x == 1);
504 lines[0].Should().Be(HeadingInfo.Default.ToString());
505 // Teardown
506 }
507
508 [Fact]
510 {
511 // Fixture setup
512 var help = new StringWriter();
513 var sut = new Parser(config => config.HelpWriter = help);
514
515 // Exercize system
516 sut.ParseArguments<Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>(new[] { "--weburl=value.com", "--ftpurl=value.org" });
517 var result = help.ToString();
518
519 // Verify outcome
520 var lines = result.ToNotEmptyLines().TrimStringArray();
521 lines[0].Should().Be(HeadingInfo.Default.ToString());
522 lines[1].Should().Be(CopyrightInfo.Default.ToString());
523 lines[2].Should().BeEquivalentTo("ERROR(S):");
524 lines[3].Should().BeEquivalentTo("Option: 'weburl' is not compatible with: 'ftpurl'.");
525 lines[4].Should().BeEquivalentTo("Option: 'ftpurl' is not compatible with: 'weburl'.");
526 lines[5].Should().BeEquivalentTo("--weburl Required.");
527 lines[6].Should().BeEquivalentTo("--ftpurl Required.");
528 lines[7].Should().BeEquivalentTo("-a");
529 lines[8].Should().BeEquivalentTo("--help Display this help screen.");
530 lines[9].Should().BeEquivalentTo("--version Display version information.");
531 // Teardown
532 }
533
534 [Fact]
536 {
537 // Fixture setup
538 var help = new StringWriter();
539 var sut = new Parser(config => config.HelpWriter = help);
540
541 // Exercize system
542 sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new[] { "commit", "--help" });
543 var result = help.ToString();
544
545 // Verify outcome
546 result.Length.Should().BeGreaterThan(0);
547 // Teardown
548 }
549
550 [Fact]
552 {
553 // Fixture setup
554 var help = new StringWriter();
555 var sut = new Parser(config =>
556 {
557 config.HelpWriter = help;
558 config.MaximumDisplayWidth = 80;
559 });
560
561 // Exercize system
563 new[] { "clone", "--badoption=@bad?value" });
564 var result = help.ToString();
565
566 // Verify outcome
567 var lines = result.ToNotEmptyLines().TrimStringArray();
568 lines[0].Should().Be(HeadingInfo.Default.ToString());
569 lines[1].Should().Be(CopyrightInfo.Default.ToString());
570 lines[2].Should().BeEquivalentTo("ERROR(S):");
571 lines[3].Should().BeEquivalentTo("Option 'badoption' is unknown.");
572 lines[4].Should().BeEquivalentTo("USAGE:");
573 lines[5].Should().BeEquivalentTo("Basic cloning:");
574 lines[6].Should().BeEquivalentTo("git clone https://github.com/gsscoder/csharpx");
575 lines[7].Should().BeEquivalentTo("Cloning quietly:");
576 lines[8].Should().BeEquivalentTo("git clone --quiet https://github.com/gsscoder/railwaysharp");
577 lines[9].Should().BeEquivalentTo("Cloning without hard links:");
578 lines[10].Should().BeEquivalentTo("git clone --no-hardlinks https://github.com/gsscoder/csharpx");
579 lines[11].Should().BeEquivalentTo("--no-hardlinks Optimize the cloning process from a repository on a local");
580 lines[12].Should().BeEquivalentTo("filesystem by copying files.");
581 lines[13].Should().BeEquivalentTo("-q, --quiet Suppress summary message.");
582 lines[14].Should().BeEquivalentTo("--help Display this help screen.");
583 lines[15].Should().BeEquivalentTo("--version Display version information.");
584 lines[16].Should().BeEquivalentTo("URLS (pos. 0) A list of url(s) to clone.");
585
586 // Teardown
587 }
588
589 [Fact]
591 {
592 // Fixture setup
593 var help = new StringWriter();
594 var sut = new Parser(config => config.HelpWriter = help);
595
596 // Exercize system
597 sut.ParseArguments<Secert_Verb, Add_Verb_With_Usage_Attribute>(new string[] { });
598 var result = help.ToString();
599
600 // Verify outcome
601 var lines = result.ToNotEmptyLines().TrimStringArray();
602 lines[0].Should().Be(HeadingInfo.Default.ToString());
603 lines[1].Should().Be(CopyrightInfo.Default.ToString());
604 lines[2].Should().BeEquivalentTo("ERROR(S):");
605 lines[3].Should().BeEquivalentTo("No verb selected.");
606 lines[4].Should().BeEquivalentTo("add Add file contents to the index.");
607 lines[5].Should().BeEquivalentTo("help Display more information on a specific command.");
608 lines[6].Should().BeEquivalentTo("version Display version information.");
609
610 // Teardown
611 }
612
613 [Fact]
615 {
616 // Fixture setup
617 var help = new StringWriter();
618 var sut = new Parser(config => config.HelpWriter = help);
619
620 // Exercize system
621 sut.ParseArguments<Secert_Verb, Add_Verb_With_Usage_Attribute>(new string[] { "secert", "--help" });
622 var result = help.ToString();
623
624 // Verify outcome
625 var lines = result.ToNotEmptyLines().TrimStringArray();
626 lines[0].Should().Be(HeadingInfo.Default.ToString());
627 lines[1].Should().Be(CopyrightInfo.Default.ToString());
628 lines[2].Should().BeEquivalentTo("-f, --force Allow adding otherwise ignored files.");
629 lines[3].Should().BeEquivalentTo("--help Display this help screen.");
630 lines[4].Should().BeEquivalentTo("--version Display version information.");
631
632 // Teardown
633 }
634
635 [Fact]
637 {
638 // Fixture setup
639 var expectedOptions = new Secert_Verb { Force = true, SecertOption = null};
640 var help = new StringWriter();
641 var sut = new Parser(config => config.HelpWriter = help);
642
643 // Exercize system
644 var result = sut.ParseArguments<Secert_Verb, Add_Verb_With_Usage_Attribute>(new string[] { "secert", "--force" });
645
646
647 // Verify outcome
648 result.Tag.Should().BeEquivalentTo(ParserResultType.Parsed);
649 result.GetType().Should().Be<Parsed<object>>();
650 result.TypeInfo.Current.Should().Be<Secert_Verb>();
651 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
652 // Teardown
653 }
654
655 [Fact]
657 {
658 // Fixture setup
659 var expectedOptions = new Secert_Verb { Force = true, SecertOption = "shhh" };
660 var help = new StringWriter();
661 var sut = new Parser(config => config.HelpWriter = help);
662
663 // Exercize system
664 var result = sut.ParseArguments<Secert_Verb, Add_Verb_With_Usage_Attribute>(new string[] { "secert", "--force", "--secert-option", "shhh" });
665
666 // Verify outcome
667 result.Tag.Should().BeEquivalentTo(ParserResultType.Parsed);
668 result.GetType().Should().Be<Parsed<object>>();
669 result.TypeInfo.Current.Should().Be<Secert_Verb>();
670 ((Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
671 // Teardown
672 }
673
674 [Fact]
676 {
677 // Fixture setup
678 var help = new StringWriter();
679 var sut = new Parser(config =>
680 {
681 config.HelpWriter = help;
682 config.MaximumDisplayWidth = 80;
683 });
684
685 // Exercize system
687 new[] { "help", "clone", "extra-arg" });
688 var result = help.ToString();
689
690 // Verify outcome
691 var lines = result.ToNotEmptyLines().TrimStringArray();
692 lines[0].Should().Be(HeadingInfo.Default.ToString());
693 lines[1].Should().Be(CopyrightInfo.Default.ToString());
694 lines[2].Should().BeEquivalentTo("--no-hardlinks Optimize the cloning process from a repository on a local");
695 lines[3].Should().BeEquivalentTo("filesystem by copying files.");
696 lines[4].Should().BeEquivalentTo("-q, --quiet Suppress summary message.");
697 lines[5].Should().BeEquivalentTo("--help Display this help screen.");
698 lines[6].Should().BeEquivalentTo("--version Display version information.");
699 lines[7].Should().BeEquivalentTo("value pos. 0");
700
701 // Teardown
702 }
703
704 [Theory]
705 [MemberData(nameof(IgnoreUnknownArgumentsData))]
707 string[] arguments,
708 Simple_Options expected)
709 {
710 // Fixture setup
711 var sut = new Parser(config => config.IgnoreUnknownArguments = true);
712
713 // Exercize system
714 var result = sut.ParseArguments<Simple_Options>(arguments);
715
716 // Verify outcome
717 result.Tag.Should().BeEquivalentTo(ParserResultType.Parsed);
718 result.WithParsed(opts => opts.Should().BeEquivalentTo(expected));
719
720 // Teardown
721 }
722
723 [Theory]
724 [MemberData(nameof(IgnoreUnknownArgumentsForVerbsData))]
726 string[] arguments,
727 Commit_Verb expected)
728 {
729 // Fixture setup
730 var sut = new Parser(config => config.IgnoreUnknownArguments = true);
731
732 // Exercize system
733 var result = sut.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(arguments);
734
735 // Verify outcome
736 result.Tag.Should().BeEquivalentTo(ParserResultType.Parsed);
737 result.WithParsed(opts => opts.Should().BeEquivalentTo(expected));
738
739 // Teardown
740 }
741
742 [Fact]
744 {
745 // Fixture setup
746 var help = new StringWriter();
747 var sut = new Parser(config =>
748 {
749 config.HelpWriter = help;
750 config.MaximumDisplayWidth = 80;
751 });
752
753 // Exercize system
755 new[] { "clone", "--bad-arg", "--help" });
756 var result = help.ToString();
757
758 // Verify outcome
759 var lines = result.ToNotEmptyLines().TrimStringArray();
760 lines[0].Should().Be(HeadingInfo.Default.ToString());
761 lines[1].Should().Be(CopyrightInfo.Default.ToString());
762 lines[2].Should().BeEquivalentTo("ERROR(S):");
763 lines[3].Should().BeEquivalentTo("Option 'bad-arg' is unknown.");
764 lines[4].Should().BeEquivalentTo("--no-hardlinks Optimize the cloning process from a repository on a local");
765 lines[5].Should().BeEquivalentTo("filesystem by copying files.");
766 lines[6].Should().BeEquivalentTo("-q, --quiet Suppress summary message.");
767 lines[7].Should().BeEquivalentTo("--help Display this help screen.");
768 lines[8].Should().BeEquivalentTo("--version Display version information.");
769 lines[9].Should().BeEquivalentTo("value pos. 0");
770
771 // Teardown
772 }
773
774 [Fact]
776 {
777 // Fixture setup
778 var expectedResult = new[]
779 {
780 new MutuallyExclusiveSetError(new NameInfo("", "weburl"), "theweb"),
781 new MutuallyExclusiveSetError(new NameInfo("", "somethingelse"), "theweb"),
782
783 };
784 var sut = new Parser();
785
786 // Exercize system
787 var result = sut.ParseArguments<Options_With_SetName_That_Ends_With_Previous_SetName>(
788 new[] { "--weburl", "value", "--somethingelse", "othervalue" });
789
790 // Verify outcome
791 ((NotParsed<Options_With_SetName_That_Ends_With_Previous_SetName>)result).Errors.Should().BeEquivalentTo(expectedResult);
792
793 }
794
795 [Fact]
797 {
798 var sameValues = new[] { "--stringvalue=test", "--shortandlong=test" };
799 var sut = new Parser(parserSettings => { parserSettings.IgnoreUnknownArguments = true; });
800 var result = sut.ParseArguments<Simple_Options>(sameValues);
801
802 result.MapResult(_ => true, _ => false).Should().BeTrue();
803 }
804
805 [Fact]
807 {
808 var sameValues = new[] { "--stringvalue=test1", "--shortandlong=test2" };
809 var sut = new Parser(parserSettings => { parserSettings.IgnoreUnknownArguments = true; });
810 var result = sut.ParseArguments<Simple_Options>(sameValues);
811
812 result.MapResult(_ => true, _ => false).Should().BeTrue();
813 }
814
815 public static IEnumerable<object[]> IgnoreUnknownArgumentsData
816 {
817 get
818 {
819 yield return new object[] { new[] { "--stringvalue=strdata0", "--unknown=valid" }, new Simple_Options { StringValue = "strdata0", IntSequence = Enumerable.Empty<int>() } };
820 yield return new object[] { new[] { "--stringvalue=strdata0", "1234", "--unknown", "-i", "1", "2", "3" }, new Simple_Options { StringValue = "strdata0", LongValue = 1234L, IntSequence = new[] { 1, 2, 3 } } };
821 yield return new object[] { new[] { "--stringvalue=strdata0", "-u" }, new Simple_Options { StringValue = "strdata0", IntSequence = Enumerable.Empty<int>() } };
822 }
823 }
824
825 public static IEnumerable<object[]> IgnoreUnknownArgumentsForVerbsData
826 {
827 get
828 {
829 yield return new object[] { new[] { "commit", "-up" }, new Commit_Verb { Patch = true } };
830 yield return new object[] { new[] { "commit", "--amend", "--unknown", "valid" }, new Commit_Verb { Amend = true } };
831 }
832 }
833
834 [Fact]
835 public static void Null_default()
836 {
837 Parser parser = new Parser();
839 .WithParsed(r =>
840 {
841 Assert.Null(r.User);
842 });
843 }
844
846 {
847 [Option('u', "user", Default = null)]
848 public string User { get; set; }
849 }
850
851 [Fact]
853 {
854 var parser = Parser.Default;
856 new[] { "arg", "-o", "arg" })
857 .WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
858 .WithParsed(args =>
859 {
860 Assert.Equal("arg", args.OptValue);
861 Assert.Equal("arg", args.PosValue);
862 });
863 }
864
865 [Fact]
867 {
868 var parser = Parser.Default;
869 var result = parser.ParseArguments(
870 new[] { "test", "arg", "-o", "arg" },
872 result
873 .WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
874 .WithParsed<Verb_With_Option_And_Value_Of_String_Type>(args =>
875 {
876 Assert.Equal("arg", args.OptValue);
877 Assert.Equal("arg", args.PosValue);
878 });
879 }
880
881 [Fact]
883 {
884 var parser = Parser.Default;
886 new[] { "zero", "one", "two" })
887 .WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
888 .WithParsed(args =>
889 {
890 Assert.Equal("zero", args.Arg0);
891 Assert.Equal("one", args.Arg1);
892 Assert.Equal("two", args.Arg2);
893 });
894
895 }
896
897
898 [Fact]
900 {
901 // Fixture setup
902 var help = new StringWriter();
903 var sut = new Parser(config => config.HelpWriter = help);
904
905 // Exercize system
906 sut.ParseArguments<Secert_Verb, Add_Verb_With_Usage_Attribute>(new string[] { });
907 var result = help.ToString();
908
909 // Verify outcome
910 var lines = result.ToLines().TrimStringArray();
911 lines[6].Should().BeEquivalentTo("add Add file contents to the index.");
912 lines[8].Should().BeEquivalentTo("help Display more information on a specific command.");
913 lines[10].Should().BeEquivalentTo("version Display version information.");
914 // Teardown
915 }
916
917
918 [Fact]
920 {
921 var parser = Parser.Default;
922 parser.ParseArguments<Default_Verb_One>(new[] { "-t" })
923 .WithNotParsed(errors => throw new InvalidOperationException("Must be parsed."))
924 .WithParsed(args =>
925 {
926 Assert.True(args.TestValueOne);
927 });
928 }
929
930 [Fact]
932 {
933 var parser = Parser.Default;
934 parser.ParseArguments<Default_Verb_One>(new[] { "default1", "-t" })
935 .WithNotParsed(errors => throw new InvalidOperationException("Must be parsed."))
936 .WithParsed(args =>
937 {
938 Assert.True(args.TestValueOne);
939 });
940 }
941
942 [Fact]
944 {
945 var parser = Parser.Default;
946 parser.ParseArguments<Default_Verb_One, Default_Verb_Two>(new string[] { })
947 .WithNotParsed(errors => Assert.IsType<MultipleDefaultVerbsError>(errors.First()))
948 .WithParsed(args => throw new InvalidOperationException("Should not be parsed."));
949 }
950
951 [Fact]
953 {
954 using (var sut = new Parser(settings => settings.AllowMultiInstance = true))
955 {
956 var longVal1 = 100;
957 var longVal2 = 200;
958 var longVal3 = 300;
959 var stringVal = "shortSeq1";
960
961 var result = sut.ParseArguments(
962 new[] { "sequence", "--long-seq", $"{longVal1}", "-s", stringVal, "--long-seq", $"{longVal2};{longVal3}" },
963 typeof(Add_Verb), typeof(Commit_Verb), typeof(SequenceOptions));
964
965 Assert.IsType<Parsed<object>>(result);
966 Assert.IsType<SequenceOptions>(((Parsed<object>)result).Value);
967 result.WithParsed<SequenceOptions>(verb =>
968 {
969 Assert.Equal(new long[] { longVal1, longVal2, longVal3 }, verb.LongSequence);
970 Assert.Equal(new[] { stringVal }, verb.StringSequence);
971 });
972 }
973 }
974
975 [Fact]
977 {
978 // NOTE: Once GetoptMode becomes the default, it will imply MultiInstance and this test will fail because the parser result will be Parsed.
979 using (var sut = new Parser(settings => settings.AllowMultiInstance = false))
980 {
981 var longVal1 = 100;
982 var longVal2 = 200;
983 var longVal3 = 300;
984 var stringVal = "shortSeq1";
985
986 var result = sut.ParseArguments(
987 new[] { "sequence", "--long-seq", $"{longVal1}", "-s", stringVal, "--long-seq", $"{longVal2};{longVal3}" },
988 typeof(Add_Verb), typeof(Commit_Verb), typeof(SequenceOptions));
989
990 Assert.IsType<NotParsed<object>>(result);
991 result.WithNotParsed(errors => Assert.All(errors, e =>
992 {
993 if (e is RepeatedOptionError)
994 {
995 // expected
996 }
997 else
998 {
999 throw new Exception($"{nameof(RepeatedOptionError)} expected");
1000 }
1001 }));
1002 }
1003 }
1004
1005 [Fact]
1007 {
1008 var parser = Parser.Default;
1009 parser.ParseArguments<Default_Verb_With_Empty_Name>(new[] { "-t" })
1010 .WithNotParsed(errors => throw new InvalidOperationException("Must be parsed."))
1011 .WithParsed(args =>
1012 {
1013 Assert.True(args.TestValue);
1014 });
1015 }
1016 //Fix Issue #409 for WPF
1017 [Fact]
1019 {
1020 // Arrange
1021
1022 //Act
1023 var sut = new Parser(config => config.HelpWriter = null);
1024 sut.ParseArguments<Simple_Options>(new[] {"--dummy"});
1025 //Assert
1026 sut.Settings.MaximumDisplayWidth.Should().BeGreaterThan(1);
1027 }
1028 }
1029}
Models an error generated when a user explicitly requests help.
Definition Error.cs:454
Models an error generated when multiple default verbs are defined.
Definition Error.cs:605
Models an error generated when a an option from another set is defined.
Definition Error.cs:401
Models name information, used in CommandLine.Error instances.
Definition NameInfo.cs:11
It contains a sequence of CommandLine.Error.
It contains an instance of type T with parsed values.
Provides methods to parse command line arguments.
Definition Parser.cs:21
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
Definition Parser.cs:216
static Parser Default
Gets the singleton instance created with basic defaults.
Definition Parser.cs:75
Models an error generated when an option is repeated two or more times.
Definition Error.cs:436
IEnumerable< string > StringSequence
Definition Verb_Fakes.cs:71
void Errors_of_type_MutuallyExclusiveSetError_are_properly_formatted()
void Parse_repeated_options_in_verbs_scenario_without_multi_instance()
void Parse_options_when_given_hidden_verb_with_hidden_option()
void Explicit_help_request_with_immutable_instance_generates_help_screen()
static IEnumerable< object[]> IgnoreUnknownArgumentsData
void Properly_formatted_help_screen_is_displayed_when_usage_is_defined_in_verb_scenario()
void Specific_verb_help_screen_should_be_displayed_regardless_other_argument()
void Explicit_help_request_with_immutable_instance_generates_help_requested_error()
void Explicit_version_request_generates_version_info_screen_in_verbs_scenario(string command)
void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_verb_selected_usage_displays_with_hidden_option()
void When_IgnoreUnknownArguments_is_set_valid_unknown_arguments_avoid_a_failure_parsing(string[] arguments, Simple_Options expected)
static void Arguments_with_the_same_values_when_unknown_arguments_are_ignored()
void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario_using_generic_overload()
void Parse_repeated_options_in_verbs_scenario_with_multi_instance()
void Explicit_help_request_with_specific_verb_generates_help_screen()
void Parse_options_with_short_name(string outputFile, string[] args)
void Explicit_version_request_generates_version_info_screen()
void Properly_formatted_help_screen_excludes_help_as_unknown_option()
void Parse_options_with_repeated_value_in_values_sequence_and_option()
static void Breaking_mutually_exclusive_set_constraint_with_both_set_name_with_gererates_Error()
void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario()
static void Arguments_with_the_different_values_when_unknown_arguments_are_ignored()
void Parse_options_with_short_name_in_verbs_scenario(string message, string[] args)
static IEnumerable< object[]> IgnoreUnknownArgumentsForVerbsData
void Parse_FlagCounter_options_with_short_name(string[] args, int verboseCount, int silentCount)
void When_IgnoreUnknownArguments_is_set_valid_unknown_arguments_avoid_a_failure_parsing_for_verbs(string[] arguments, Commit_Verb expected)
void Explicit_version_request_generates_version_requested_error()
void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_verb()
void Double_dash_help_dispalys_verbs_index_in_verbs_scenario()
void Parse_repeated_options_with_default_parser_in_verbs_scenario()
Models the heading part of an help text. You can assign it where you assign any System....
static HeadingInfo Default
Gets the default heading instance. The title is retrieved from AssemblyTitleAttribute,...
override string ToString()
Returns the heading as a System.String.
Models an error generated when a user explicitly requests version.
Definition Error.cs:504
ParserResultType
Discriminator enumeration of CommandLine.ParserResultType derivates.