Skip to content
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

Adds an optional case sensitive property to most tokens, defaulting to true #2284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions src/System.CommandLine.Tests/CommandLineConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_option_aliases_
.Be($"Duplicate alias '--dupe' found on command '{command.Name}'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_case_insensitive_sibling_option_aliases_on_the_root_command()
{
var option1 = new CliOption<string>("--dupe", false);
var option2 = new CliOption<string>("-y");
option2.Aliases.Add("--Dupe");

var command = new CliRootCommand()
{
option1,
option2
};

var config = new CliConfiguration(command);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be($"Duplicate alias '--dupe' found on command '{command.Name}'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_option_aliases_on_a_subcommand()
{
Expand Down Expand Up @@ -60,6 +85,33 @@ public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_option_aliases_
.Should()
.Be("Duplicate alias '--dupe' found on command 'subcommand'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_case_insensitive_sibling_option_aliases_on_a_subcommand()
{
var option1 = new CliOption<string>("--dupe", false);
var option2 = new CliOption<string>("--ok");
option2.Aliases.Add("--Dupe");

var command = new CliRootCommand
{
new CliCommand("subcommand")
{
option1,
option2
}
};

var config = new CliConfiguration(command);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be("Duplicate alias '--dupe' found on command 'subcommand'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_subcommand_aliases_on_the_root_command()
Expand All @@ -85,6 +137,30 @@ public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_subcommand_alia
.Should()
.Be($"Duplicate alias 'dupe' found on command '{rootCommand.Name}'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_case_insensitive_sibling_subcommand_aliases_on_the_root_command()
{
var command1 = new CliCommand("dupe", caseSensitive: false);
var command2 = new CliCommand("not-a-dupe");
command2.Aliases.Add("Dupe");

var rootCommand = new CliRootCommand
{
command1,
command2
};

var config = new CliConfiguration(rootCommand);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be($"Duplicate alias 'dupe' found on command '{rootCommand.Name}'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_subcommand_aliases_on_a_subcommand()
Expand All @@ -109,6 +185,29 @@ public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_subcommand_alia
.Should()
.Be("Duplicate alias 'dupe' found on command 'subcommand'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_case_insensitive_sibling_subcommand_aliases_on_a_subcommand()
{
var command = new CliRootCommand
{
new CliCommand("subcommand")
{
new CliCommand("dupe", caseSensitive: false),
new CliCommand("not-a-dupe") { Aliases = { "Dupe" } }
}
};

var config = new CliConfiguration(command);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be("Duplicate alias 'dupe' found on command 'subcommand'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_sibling_command_and_option_aliases_collide_on_the_root_command()
Expand All @@ -134,6 +233,30 @@ public void ThrowIfInvalid_throws_if_sibling_command_and_option_aliases_collide_
.Should()
.Be($"Duplicate alias 'dupe' found on command '{rootCommand.Name}'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_case_insensitive_sibling_command_and_option_aliases_collide_on_the_root_command()
{
var option = new CliOption<string>("dupe", caseSensitive: false);
var command = new CliCommand("not-a-dupe");
command.Aliases.Add("Dupe");

var rootCommand = new CliRootCommand
{
option,
command
};

var config = new CliConfiguration(rootCommand);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be($"Duplicate alias 'dupe' found on command '{rootCommand.Name}'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_sibling_command_and_option_aliases_collide_on_a_subcommand()
Expand Down Expand Up @@ -162,6 +285,33 @@ public void ThrowIfInvalid_throws_if_sibling_command_and_option_aliases_collide_
.Should()
.Be("Duplicate alias 'dupe' found on command 'subcommand'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_case_insensitive_sibling_command_and_option_aliases_collide_on_a_subcommand()
{
var option = new CliOption<string>("dupe", caseSensitive: false);
var command = new CliCommand("not-a-dupe");
command.Aliases.Add("Dupe");

var rootCommand = new CliRootCommand
{
new CliCommand("subcommand")
{
option,
command
}
};

var config = new CliConfiguration(rootCommand);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be("Duplicate alias 'dupe' found on command 'subcommand'.");
}

[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_global_option_aliases_on_the_root_command()
Expand All @@ -185,6 +335,28 @@ public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_global_option_a
.Should()
.Be($"Duplicate alias '--dupe' found on command '{command.Name}'.");
}
[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_case_insensitive_sibling_global_option_aliases_on_the_root_command()
{
var option1 = new CliOption<string>("--dupe", caseSensitive: false) { Recursive = true };
var option2 = new CliOption<string>("-y") { Recursive = true };
option2.Aliases.Add("--Dupe");

var command = new CliRootCommand();
command.Options.Add(option1);
command.Options.Add(option2);

var config = new CliConfiguration(command);

var validate = () => config.ThrowIfInvalid();

validate.Should()
.Throw<CliConfigurationException>()
.Which
.Message
.Should()
.Be($"Duplicate alias '--dupe' found on command '{command.Name}'.");
}

[Fact]
public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_local_option_alias()
Expand All @@ -204,6 +376,24 @@ public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_

validate.Should().NotThrow();
}
[Fact]
public void ThrowIfInvalid_does_not_throw_if_case_insensitive_global_option_alias_is_the_same_as_local_option_alias()
{
var rootCommand = new CliRootCommand
{
new CliCommand("subcommand")
{
new CliOption<string>("--dupe")
}
};
rootCommand.Options.Add(new CliOption<string>("--Dupe", caseSensitive: false) { Recursive = true });

var config = new CliConfiguration(rootCommand);

var validate = () => config.ThrowIfInvalid();

validate.Should().NotThrow();
}

[Fact]
public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_subcommand_alias()
Expand All @@ -223,6 +413,24 @@ public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_

validate.Should().NotThrow();
}
[Fact]
public void ThrowIfInvalid_does_not_throw_if_case_insensitive_global_option_alias_is_the_same_as_subcommand_alias()
{
var rootCommand = new CliRootCommand
{
new CliCommand("subcommand")
{
new CliCommand("--dupe")
}
};
rootCommand.Options.Add(new CliOption<string>("--Dupe", caseSensitive: false) { Recursive = true });

var config = new CliConfiguration(rootCommand);

var validate = () => config.ThrowIfInvalid();

validate.Should().NotThrow();
}

[Fact]
public void ThrowIfInvalid_throws_if_a_command_is_its_own_parent()
Expand Down
Loading