Skip to content

use collection expressions #2618

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

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
6 changes: 3 additions & 3 deletions src/Common/ArgumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class ArgumentBuilder

static ArgumentBuilder()
{
_ctor = typeof(Argument<string>).GetConstructor(new[] { typeof(string) });
_ctor = typeof(Argument<string>).GetConstructor([typeof(string)]);
}

public static Argument CreateArgument(Type valueType, string name = "value")
Expand All @@ -34,9 +34,9 @@ internal static Argument CreateArgument(ParameterInfo argsParam)

var argumentType = typeof(Bridge<>).MakeGenericType(argsParam.ParameterType);

var ctor = argumentType.GetConstructor(new[] { typeof(string), argsParam.ParameterType });
var ctor = argumentType.GetConstructor([typeof(string), argsParam.ParameterType]);

return (Argument)ctor.Invoke(new object[] { argsParam.Name, argsParam.DefaultValue });
return (Argument)ctor.Invoke([argsParam.Name, argsParam.DefaultValue]);
}

private sealed class Bridge<T> : Argument<T>
Expand Down
8 changes: 4 additions & 4 deletions src/Common/OptionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class OptionBuilder

static OptionBuilder()
{
_ctor = typeof(Option<string>).GetConstructor(new[] { typeof(string), typeof(string[]) });
_ctor = typeof(Option<string>).GetConstructor([typeof(string), typeof(string[])]);
}

internal static Option CreateOption(string name, Type valueType, string description = null)
Expand All @@ -24,7 +24,7 @@ internal static Option CreateOption(string name, Type valueType, string descript
var ctor = optionType.GetConstructor(new[] { typeof(string), typeof(string[]) });
#endif

var option = (Option)ctor.Invoke(new object[] { name, Array.Empty<string>() });
var option = (Option)ctor.Invoke([name, Array.Empty<string>()]);

option.Description = description;

Expand All @@ -40,9 +40,9 @@ internal static Option CreateOption(string name, Type valueType, string descript

var optionType = typeof(Bridge<>).MakeGenericType(valueType);

var ctor = optionType.GetConstructor(new[] { typeof(string), typeof(Func<object>), typeof(string) });
var ctor = optionType.GetConstructor([typeof(string), typeof(Func<object>), typeof(string)]);

var option = (Option)ctor.Invoke(new object[] { name, defaultValueFactory, description });
var option = (Option)ctor.Invoke([name, defaultValueFactory, description]);

return option;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public DotnetSuggestEndToEndTests(ITestOutputHelper output)

PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome();

_environmentVariables = new[] {
_environmentVariables =
[
("DOTNET_ROOT", _dotnetHostDir.FullName),
("INTERNAL_TEST_DOTNET_SUGGEST_HOME", _testRoot)};
("INTERNAL_TEST_DOTNET_SUGGEST_HOME", _testRoot)
];
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.CommandLine.Suggest.Tests
{
public class GlobalToolsSuggestionRegistrationTests
{
public static IEnumerable<string> FilesNameWithoutExtensionUnderDotnetProfileToolsExample = new[] { "dotnet-suggest", "t-rex" };
public static IEnumerable<string> FilesNameWithoutExtensionUnderDotnetProfileToolsExample = ["dotnet-suggest", "t-rex"];
[Fact]
public void Path_is_in_global_tools()
{
Expand Down Expand Up @@ -50,11 +50,12 @@ public void Global_tools_can_be_found()

registrationPairs
.Should()
.BeEquivalentTo( new [] {
.BeEquivalentTo([
new Registration(
Path.Combine(dotnetProfileDirectory, "tools", "dotnet-suggest")),
new Registration(
Path.Combine(dotnetProfileDirectory, "tools", "t-rex"))});
Path.Combine(dotnetProfileDirectory, "tools", "t-rex"))
]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public async Task List_command_gets_all_executable_names()
var dispatcher = new SuggestionDispatcher(testSuggestionProvider);
dispatcher.Configuration.Output = new StringWriter();

await dispatcher.InvokeAsync(new[] { "list" });
await dispatcher.InvokeAsync(["list"]);

dispatcher.Configuration.Output
.ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.CommandLine.Suggest.Tests
{
internal class TestSuggestionRegistration : ISuggestionRegistration
{
private readonly List<Registration> _suggestionRegistrations = new();
private readonly List<Registration> _suggestionRegistrations = [];

public TestSuggestionRegistration(params Registration[] suggestionRegistrations)
{
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Tests/Binding/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ClassWithComplexTypes
public string StringOption { get; set; }
public bool BoolOption { get; set; }
public List<ClassWithMultiLetterSetters> ListOptionDefaultNull { get; set; }
public List<ClassWithCtorParameter<string>> ListOptionDefaultEmpty { get; set; } = new ();
public List<ClassWithCtorParameter<string>> ListOptionDefaultEmpty { get; set; } = [];
}

public class ClassWithListTypePropertiesAndDefaultCtor
Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void Command_argument_of_FileInfo_returns_null_when_argument_is_not_provi
public void Argument_of_FileInfo_that_is_empty_results_in_an_informative_error()
{
var option = new Option<FileInfo>("--file");
var result = new RootCommand { option }.Parse(new string[] { "--file", "" });
var result = new RootCommand { option }.Parse(["--file", ""]);

result.Errors
.Should()
Expand Down Expand Up @@ -691,7 +691,7 @@ public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parse

[Fact]
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter()
=> GetValue(new Option<int[]>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo(new[] { 1, 2, 3 });
=> GetValue(new Option<int[]>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo([1, 2, 3]);

[Theory]
[InlineData(0, 100_000, typeof(string[]))]
Expand Down Expand Up @@ -736,11 +736,11 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types(

[Fact]
public void Values_can_be_correctly_converted_to_List_of_int_without_the_parser_specifying_a_custom_converter()
=> GetValue(new Option<List<int>>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo(new[] {1, 2, 3});
=> GetValue(new Option<List<int>>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo([1, 2, 3]);

[Fact]
public void Values_can_be_correctly_converted_to_IEnumerable_of_int_without_the_parser_specifying_a_custom_converter()
=> GetValue(new Option<IEnumerable<int>>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo(new[] { 1, 2, 3 });
=> GetValue(new Option<IEnumerable<int>>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo([1, 2, 3]);

[Fact]
public void Enum_values_can_be_correctly_converted_based_on_enum_value_name_without_the_parser_specifying_a_custom_converter()
Expand Down
10 changes: 5 additions & 5 deletions src/System.CommandLine.Tests/CompletionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void CommandLineText_is_unavailable_when_string_array_is_parsed()
}
};

var parseResult = command.Parse(new[] { "verb", "-x", "123" });
var parseResult = command.Parse(["verb", "-x", "123"]);

parseResult.GetCompletionContext()
.Should()
Expand Down Expand Up @@ -137,7 +137,7 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_i
new Option<string>("--option2")
};

string textToMatch = command.Parse(new[] { "the-command", "opt" })
string textToMatch = command.Parse(["the-command", "opt"])
.GetCompletionContext()
.WordToComplete;

Expand All @@ -153,7 +153,7 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
new Option<string>("--option2")
};

string textToMatch = command.Parse(new[] { "the-command" })
string textToMatch = command.Parse(["the-command"])
.GetCompletionContext()
.WordToComplete;

Expand All @@ -169,7 +169,7 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
new Option<string>("--option2")
};

string textToMatch = command.Parse(new[] { "the-command", "--option1" })
string textToMatch = command.Parse(["the-command", "--option1"])
.GetCompletionContext()
.WordToComplete;

Expand All @@ -189,7 +189,7 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
new Argument<string>("arg")
};

string textToMatch = command.Parse(new[] { "the-command", "--option1", "a" })
string textToMatch = command.Parse(["the-command", "--option1", "a"])
.GetCompletionContext()
.WordToComplete;

Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void When_a_subcommand_has_been_specified_then_its_sibling_commands_will_
result.GetCompletions()
.Select(item => item.Label)
.Should()
.NotContain(new[]{"apple", "banana", "cherry"});
.NotContain(["apple", "banana", "cherry"]);
}

[Fact]
Expand Down Expand Up @@ -343,7 +343,7 @@ public void When_a_subcommand_has_been_specified_then_its_sibling_commands_alias
result.GetCompletions()
.Select(item => item.Label)
.Should()
.NotContain(new[] { "apl", "bnn" });
.NotContain(["apl", "bnn"]);
}

[Fact] // https://github.com/dotnet/command-line-api/issues/1494
Expand Down Expand Up @@ -671,7 +671,7 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
public void Option_argument_completions_can_be_provided_using_a_delegate()
{
var option = new Option<string>("-x");
option.CompletionSources.Add(_ => new[] { "vegetable", "mineral", "animal" });
option.CompletionSources.Add(_ => ["vegetable", "mineral", "animal"]);

var command = new Command("the-command")
{
Expand Down Expand Up @@ -806,7 +806,7 @@ public void When_parsing_from_array_if_the_proximate_option_is_completed_then_co
new Option<string>("--langVersion")
};
var configuration = new CommandLineConfiguration(command);
var completions = command.Parse(new[]{"--framework","net8.0","--l"}, configuration).GetCompletions();
var completions = command.Parse(["--framework","net8.0","--l"], configuration).GetCompletions();

completions.Select(item => item.Label)
.Should()
Expand Down
36 changes: 18 additions & 18 deletions src/System.CommandLine.Tests/CustomParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_one_token()
new RootCommand { argument }.Parse("1,2,3")
.GetValue(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
.BeEquivalentTo([1, 2, 3]);
}

[Fact]
Expand All @@ -185,7 +185,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke
new RootCommand { argument }.Parse("1 2 3")
.GetValue(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
.BeEquivalentTo([1, 2, 3]);
}

[Fact]
Expand Down Expand Up @@ -631,12 +631,12 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
{
result.OnlyTake(3);

return new[]
{
return
[
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
];
}
};
var argument2 = new Argument<int[]>("two")
Expand All @@ -655,13 +655,13 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
parseResult.GetResult(argument1)
.GetValueOrDefault<int[]>()
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 },
.BeEquivalentTo([1, 2, 3],
options => options.WithStrictOrdering());

parseResult.GetResult(argument2)
.GetValueOrDefault<int[]>()
.Should()
.BeEquivalentTo(new[] { 4, 5, 6, 7, 8 },
.BeEquivalentTo([4, 5, 6, 7, 8],
options => options.WithStrictOrdering());
}

Expand Down Expand Up @@ -698,15 +698,15 @@ public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_the
{
result.OnlyTake(3);

return new[]
{
return
[
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
];
}
};

var command = new RootCommand
{
argument1
Expand All @@ -716,7 +716,7 @@ public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_the

parseResult.UnmatchedTokens
.Should()
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
.BeEquivalentTo(["4", "5", "6", "7", "8"],
options => options.WithStrictOrdering());
}

Expand All @@ -729,12 +729,12 @@ public void When_custom_parser_passes_on_tokens_the_argument_result_tokens_refle
{
result.OnlyTake(3);

return new[]
{
return
[
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
];
}
};
var argument2 = new Argument<int[]>("two")
Expand All @@ -753,14 +753,14 @@ public void When_custom_parser_passes_on_tokens_the_argument_result_tokens_refle
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "1", "2", "3" },
.BeEquivalentTo(["1", "2", "3"],
options => options.WithStrictOrdering());

parseResult.GetResult(argument2)
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
.BeEquivalentTo(["4", "5", "6", "7", "8"],
options => options.WithStrictOrdering());
}

Expand Down Expand Up @@ -889,7 +889,7 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
CustomParser = ctx =>
{
ctx.OnlyTake(3);
return new[] { "1", "2", "3" };
return ["1", "2", "3"];
},
Arity = ArgumentArity.ZeroOrMore
};
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Tests/GetValueByNameParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void Array_of_T_can_be_cast_to_IEnumerable_of_T()

ParseResult parseResult = command.Parse("1 2 3");

parseResult.GetValue<IEnumerable<int>>("name").Should().BeEquivalentTo(new int[] { 1, 2, 3 });
parseResult.GetValue<IEnumerable<int>>("name").Should().BeEquivalentTo([1, 2, 3]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void Help_customized_sections_can_be_wrapped()

IEnumerable<Func<HelpContext, bool>> CustomLayout(HelpContext _)
{
yield return ctx => { ctx.HelpBuilder.WriteColumns(new[] { new TwoColumnHelpRow("12345678", "1234567890") }, ctx); return true; };
yield return ctx => { ctx.HelpBuilder.WriteColumns([new TwoColumnHelpRow("12345678", "1234567890")], ctx); return true; };
}
}

Expand Down
Loading
Loading