Skip to content

Commit

Permalink
Validate that the error message when an invalid argument is passed co… (
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGhiondea authored Jun 19, 2019
1 parent b1ca0cc commit a570027
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
79 changes: 79 additions & 0 deletions test/CommandLineTests.Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,5 +681,84 @@ public void HelpWhenPassMoreParametersThanExpected()
);
}

[Fact]
public void EnsureThatRightParameterIsReportedForGroups()
{
// Invalid option should be in exception text
var commandLine = "Command1 req1 -opt1=value";

TestWriter _printer = new TestWriter();

var options = Helpers.Parse<Groups1>(commandLine, _printer);

Validate(_printer,
new TextAndColor(ConsoleColor.Red, "Error"),
new TextAndColor(ConsoleColor.Black, $": Could not find argument -opt1=value {Environment.NewLine}"),
new TextAndColor(ConsoleColor.Black, "Usage: "),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.White, "testhost.exe"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Green, "Command1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "p1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Black, "[-"),
new TextAndColor(ConsoleColor.Yellow, "opt1"),
new TextAndColor(ConsoleColor.Black, " value] "),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.White, "testhost.exe"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Green, "Command2"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "opt1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Black, "[-"),
new TextAndColor(ConsoleColor.Yellow, "opt1"),
new TextAndColor(ConsoleColor.Black, " value] "),
new TextAndColor(ConsoleColor.Black, "For detailed information run '"),
new TextAndColor(ConsoleColor.White, "testhost --help"),
new TextAndColor(ConsoleColor.Black, "'.")
);
}

[Fact]
public void ErrorWhenNoRequiredParametersInGroupSpecified()
{
// Invalid option should be in exception text
var commandLine = "";

TestWriter _printer = new TestWriter();

var options = Helpers.Parse<Groups1>(commandLine, _printer);

Validate(_printer,
new TextAndColor(ConsoleColor.Red, "Error"),
new TextAndColor(ConsoleColor.Black, $": Required parameters have not been specified {Environment.NewLine}"),
new TextAndColor(ConsoleColor.Black, "Usage: "),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.White, "testhost.exe"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Green, "Command1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "p1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Black, "[-"),
new TextAndColor(ConsoleColor.Yellow, "opt1"),
new TextAndColor(ConsoleColor.Black, " value] "),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.White, "testhost.exe"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Green, "Command2"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "opt1"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Black, "[-"),
new TextAndColor(ConsoleColor.Yellow, "opt1"),
new TextAndColor(ConsoleColor.Black, " value] "),
new TextAndColor(ConsoleColor.Black, "For detailed information run '"),
new TextAndColor(ConsoleColor.White, "testhost --help"),
new TextAndColor(ConsoleColor.Black, "'.")
);
}
}
}
9 changes: 8 additions & 1 deletion test/CommandLineTests.Negative.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System.IO;
using Xunit;

namespace CommandLine.Tests
{
Expand Down Expand Up @@ -116,5 +117,11 @@ public void HelpForInvalidType()
var options = Helpers.Parse<object>("");
Assert.Null(options);
}

[Fact]
public void MismatchedQuotes()
{
Assert.Throws<InvalidDataException>(() => Helpers.Parse<object>("@\" foo \""));
}
}
}

0 comments on commit a570027

Please sign in to comment.