Skip to content

Commit a570027

Browse files
authored
Validate that the error message when an invalid argument is passed co… (#39)
1 parent b1ca0cc commit a570027

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

test/CommandLineTests.Help.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,5 +681,84 @@ public void HelpWhenPassMoreParametersThanExpected()
681681
);
682682
}
683683

684+
[Fact]
685+
public void EnsureThatRightParameterIsReportedForGroups()
686+
{
687+
// Invalid option should be in exception text
688+
var commandLine = "Command1 req1 -opt1=value";
689+
690+
TestWriter _printer = new TestWriter();
691+
692+
var options = Helpers.Parse<Groups1>(commandLine, _printer);
693+
694+
Validate(_printer,
695+
new TextAndColor(ConsoleColor.Red, "Error"),
696+
new TextAndColor(ConsoleColor.Black, $": Could not find argument -opt1=value {Environment.NewLine}"),
697+
new TextAndColor(ConsoleColor.Black, "Usage: "),
698+
new TextAndColor(ConsoleColor.Black, " "),
699+
new TextAndColor(ConsoleColor.White, "testhost.exe"),
700+
new TextAndColor(ConsoleColor.Black, " "),
701+
new TextAndColor(ConsoleColor.Green, "Command1"),
702+
new TextAndColor(ConsoleColor.Black, " "),
703+
new TextAndColor(ConsoleColor.Cyan, "p1"),
704+
new TextAndColor(ConsoleColor.Black, " "),
705+
new TextAndColor(ConsoleColor.Black, "[-"),
706+
new TextAndColor(ConsoleColor.Yellow, "opt1"),
707+
new TextAndColor(ConsoleColor.Black, " value] "),
708+
new TextAndColor(ConsoleColor.Black, " "),
709+
new TextAndColor(ConsoleColor.White, "testhost.exe"),
710+
new TextAndColor(ConsoleColor.Black, " "),
711+
new TextAndColor(ConsoleColor.Green, "Command2"),
712+
new TextAndColor(ConsoleColor.Black, " "),
713+
new TextAndColor(ConsoleColor.Cyan, "opt1"),
714+
new TextAndColor(ConsoleColor.Black, " "),
715+
new TextAndColor(ConsoleColor.Black, "[-"),
716+
new TextAndColor(ConsoleColor.Yellow, "opt1"),
717+
new TextAndColor(ConsoleColor.Black, " value] "),
718+
new TextAndColor(ConsoleColor.Black, "For detailed information run '"),
719+
new TextAndColor(ConsoleColor.White, "testhost --help"),
720+
new TextAndColor(ConsoleColor.Black, "'.")
721+
);
722+
}
723+
724+
[Fact]
725+
public void ErrorWhenNoRequiredParametersInGroupSpecified()
726+
{
727+
// Invalid option should be in exception text
728+
var commandLine = "";
729+
730+
TestWriter _printer = new TestWriter();
731+
732+
var options = Helpers.Parse<Groups1>(commandLine, _printer);
733+
734+
Validate(_printer,
735+
new TextAndColor(ConsoleColor.Red, "Error"),
736+
new TextAndColor(ConsoleColor.Black, $": Required parameters have not been specified {Environment.NewLine}"),
737+
new TextAndColor(ConsoleColor.Black, "Usage: "),
738+
new TextAndColor(ConsoleColor.Black, " "),
739+
new TextAndColor(ConsoleColor.White, "testhost.exe"),
740+
new TextAndColor(ConsoleColor.Black, " "),
741+
new TextAndColor(ConsoleColor.Green, "Command1"),
742+
new TextAndColor(ConsoleColor.Black, " "),
743+
new TextAndColor(ConsoleColor.Cyan, "p1"),
744+
new TextAndColor(ConsoleColor.Black, " "),
745+
new TextAndColor(ConsoleColor.Black, "[-"),
746+
new TextAndColor(ConsoleColor.Yellow, "opt1"),
747+
new TextAndColor(ConsoleColor.Black, " value] "),
748+
new TextAndColor(ConsoleColor.Black, " "),
749+
new TextAndColor(ConsoleColor.White, "testhost.exe"),
750+
new TextAndColor(ConsoleColor.Black, " "),
751+
new TextAndColor(ConsoleColor.Green, "Command2"),
752+
new TextAndColor(ConsoleColor.Black, " "),
753+
new TextAndColor(ConsoleColor.Cyan, "opt1"),
754+
new TextAndColor(ConsoleColor.Black, " "),
755+
new TextAndColor(ConsoleColor.Black, "[-"),
756+
new TextAndColor(ConsoleColor.Yellow, "opt1"),
757+
new TextAndColor(ConsoleColor.Black, " value] "),
758+
new TextAndColor(ConsoleColor.Black, "For detailed information run '"),
759+
new TextAndColor(ConsoleColor.White, "testhost --help"),
760+
new TextAndColor(ConsoleColor.Black, "'.")
761+
);
762+
}
684763
}
685764
}

test/CommandLineTests.Negative.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using System.IO;
2+
using Xunit;
23

34
namespace CommandLine.Tests
45
{
@@ -116,5 +117,11 @@ public void HelpForInvalidType()
116117
var options = Helpers.Parse<object>("");
117118
Assert.Null(options);
118119
}
120+
121+
[Fact]
122+
public void MismatchedQuotes()
123+
{
124+
Assert.Throws<InvalidDataException>(() => Helpers.Parse<object>("@\" foo \""));
125+
}
119126
}
120127
}

0 commit comments

Comments
 (0)