-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 2279 #2282
base: main
Are you sure you want to change the base?
Issue 2279 #2282
Changes from all commits
e023a78
c3c1333
7a87dc9
43d2c18
1847bcc
4d8d7e8
abe31f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.CommandLine.Completions; | ||
using ApprovalTests.Namers; | ||
using FluentAssertions; | ||
using Xunit; | ||
using System.CommandLine; | ||
using System.Threading; | ||
|
||
namespace System.CommandLine.Tests | ||
{ | ||
|
@@ -33,6 +36,128 @@ public void CommandLineText_preserves_command_line_prior_to_splitting_when_compl | |
.Be(commandLine); | ||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_parsed_when_option_other_than_last_is_in_name_equals_sign_value_format() | ||
{ | ||
|
||
CliRootCommand command = new CliRootCommand | ||
{ | ||
new CliCommand("inner") | ||
{ | ||
new CliOption<string>("--optionOne"), | ||
new CliOption<string>("--optionTwo") | ||
} | ||
}; | ||
|
||
var commandLine = "inner --optionOne=argument1 --optionTwo argument2"; | ||
|
||
var parseResult = command.Parse(commandLine); | ||
parseResult.GetCompletions(); | ||
|
||
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "argument1"); | ||
Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); | ||
Assert.True(parseResult.Tokens[4].Value == "argument2"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally avoid the use of I'd suggest changing the assertions to the following, which addresses both concerns: parseResult.Tokens.Should().BeEquivalentSequenceTo("--optionOne", "argument1", "--optionTwo", "argument2"); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried
and i get this error Expected item[0] to be System.String, but found System.CommandLine.Parsing.CliToken I also tried this
and got the following error Expected object to be inner, but found inner. Any ideas how to make this work? |
||
|
||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_parsed_when_last_option_is_in_name_equals_sign_value_format() | ||
{ | ||
|
||
CliRootCommand command = new CliRootCommand | ||
{ | ||
new CliCommand("inner") | ||
{ | ||
new CliOption<string>("--optionOne"), | ||
new CliOption<string>("--optionTwo") | ||
} | ||
}; | ||
|
||
var commandLine = "inner --optionOne argument1 --optionTwo=argument2"; | ||
|
||
var parseResult = command.Parse(commandLine); | ||
parseResult.GetCompletions(); | ||
|
||
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "argument1"); | ||
Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); | ||
Assert.True(parseResult.Tokens[4].Value == "argument2"); | ||
|
||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_parsed_when_equal_sign_used_in_multiple_option_params() | ||
{ | ||
|
||
CliRootCommand command = new CliRootCommand | ||
{ | ||
new CliCommand("inner") | ||
{ | ||
new CliOption<string>("--optionOne"), | ||
new CliOption<string>("--optionTwo") | ||
} | ||
}; | ||
|
||
var commandLine = "inner --optionOne=argument1 --optionTwo=argument2"; | ||
|
||
var parseResult = command.Parse(commandLine); | ||
parseResult.GetCompletions(); | ||
|
||
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "argument1"); | ||
Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); | ||
Assert.True(parseResult.Tokens[4].Value == "argument2"); | ||
|
||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_parsed_when_equal_sign_used_in_option_value() | ||
{ | ||
|
||
CliRootCommand command = new CliRootCommand | ||
{ | ||
new CliCommand("inner") | ||
{ | ||
new CliOption<string>("--optionOne"), | ||
new CliOption<string>("--optionTwo") | ||
} | ||
}; | ||
|
||
var commandLine = "inner --optionOne -=Yay=-"; | ||
|
||
var parseResult = command.Parse(commandLine); | ||
parseResult.GetCompletions(); | ||
|
||
Assert.True(parseResult.Tokens[0].Value == "inner"); | ||
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "-=Yay=-"); | ||
|
||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_parsed_when_equal_sign_used_in_option_value_and_as_option_value_spacer() | ||
{ | ||
|
||
CliRootCommand command = new CliRootCommand | ||
{ | ||
new CliCommand("inner") | ||
{ | ||
new CliOption<string>("--optionOne") | ||
} | ||
}; | ||
|
||
var commandLine = "inner --optionOne=-=Yay=-"; | ||
|
||
var parseResult = command.Parse(commandLine); | ||
parseResult.GetCompletions(); | ||
|
||
Assert.True(parseResult.Tokens[0].Value == "inner"); | ||
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "-=Yay=-"); | ||
|
||
} | ||
|
||
[Fact] | ||
public void CommandLineText_is_preserved_when_adjusting_position() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test name is a little hard to understand, and
CommandLinetext_is_parsed
isn't very specific about the expectation. I think the expectation is that the tokens don't include the delimiters? (Also, that it doesn't throw, but that typically goes without saying.)A more sentence-style test name is encouraged, e.g. something like
When_equals_sign_is_used_as_delimiter_then_it_is_not_included_in_tokens
.Some of these tests seem to differ only by the arguments being parsed, so combining them into a
[Theory]
might give you fewer tests to name.