Skip to content

Commit

Permalink
Use latest C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 11, 2024
1 parent 76e8d47 commit 6aa72e4
Show file tree
Hide file tree
Showing 30 changed files with 184 additions and 388 deletions.
2 changes: 1 addition & 1 deletion CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.5" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Validate(string value) {}
[Command]
public class MyCommand : ICommand
{
[CommandOption("foo", Validators = new[] {typeof(MyValidator)})]
[CommandOption("foo", Validators = new[] { typeof(MyValidator) })]
public string? Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand All @@ -48,7 +48,7 @@ public class MyValidator : BindingValidator<int>
[Command]
public class MyCommand : ICommand
{
[CommandOption("foo", Validators = new[] {typeof(MyValidator)})]
[CommandOption("foo", Validators = new[] { typeof(MyValidator) })]
public string? Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand All @@ -73,7 +73,7 @@ public class MyValidator : BindingValidator<string>
[Command]
public class MyCommand : ICommand
{
[CommandOption("foo", Validators = new[] {typeof(MyValidator)})]
[CommandOption("foo", Validators = new[] { typeof(MyValidator) })]
public string? Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Validate(string value) {}
[Command]
public class MyCommand : ICommand
{
[CommandParameter(0, Validators = new[] {typeof(MyValidator)})]
[CommandParameter(0, Validators = new[] { typeof(MyValidator) })]
public required string Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand All @@ -48,7 +48,7 @@ public class MyValidator : BindingValidator<int>
[Command]
public class MyCommand : ICommand
{
[CommandParameter(0, Validators = new[] {typeof(MyValidator)})]
[CommandParameter(0, Validators = new[] { typeof(MyValidator) })]
public required string Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand All @@ -73,7 +73,7 @@ public class MyValidator : BindingValidator<string>
[Command]
public class MyCommand : ICommand
{
[CommandParameter(0, Validators = new[] {typeof(MyValidator)})]
[CommandParameter(0, Validators = new[] { typeof(MyValidator) })]
public required string Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand Down
6 changes: 3 additions & 3 deletions CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ private Compilation Compile(string sourceCode)
// Parse the source code
var ast = SyntaxFactory.ParseSyntaxTree(
SourceText.From(sourceCodeWithUsings),
CSharpParseOptions.Default
CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview)
);

// Compile the code to IL
var compilation = CSharpCompilation.Create(
"CliFxTests_DynamicAssembly_" + Guid.NewGuid(),
new[] { ast },
Net70
[ast],
Net80
.References
.All
.Append(MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location)),
Expand Down
6 changes: 3 additions & 3 deletions CliFx.Benchmarks/Benchmarks.Cocona.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public partial class Benchmarks
public class CoconaCommand
{
public void Execute(
[Option("str", new[] { 's' })] string? strOption,
[Option("int", new[] { 'i' })] int intOption,
[Option("bool", new[] { 'b' })] bool boolOption
[Option("str", ['s'])] string? strOption,
[Option("int", ['i'])] int intOption,
[Option("bool", ['b'])] bool boolOption
) { }
}

Expand Down
6 changes: 3 additions & 3 deletions CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public Task<int> ExecuteAsync(string[] args)
{
var command = new RootCommand
{
new Option(new[] { "--str", "-s" }) { Argument = new Argument<string?>() },
new Option(new[] { "--int", "-i" }) { Argument = new Argument<int>() },
new Option(new[] { "--bool", "-b" }) { Argument = new Argument<bool>() }
new Option(["--str", "-s"]) { Argument = new Argument<string?>() },
new Option(["--int", "-i"]) { Argument = new Argument<int>() },
new Option(["--bool", "-b"]) { Argument = new Argument<bool>() }
};

command.Handler = CommandHandler.Create(
Expand Down
2 changes: 1 addition & 1 deletion CliFx.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CliFx.Benchmarks;
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public partial class Benchmarks
{
private static readonly string[] Arguments = { "--str", "hello world", "-i", "13", "-b" };
private static readonly string[] Arguments = ["--str", "hello world", "-i", "13", "-b"];

public static void Main() =>
BenchmarkRunner.Run<Benchmarks>(
Expand Down
4 changes: 2 additions & 2 deletions CliFx.Tests/ApplicationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public async Task I_can_create_an_application_with_a_custom_configuration()
var app = new CliApplicationBuilder()
.AddCommand<NoOpCommand>()
.AddCommandsFrom(typeof(NoOpCommand).Assembly)
.AddCommands(new[] { typeof(NoOpCommand) })
.AddCommandsFrom(new[] { typeof(NoOpCommand).Assembly })
.AddCommands([typeof(NoOpCommand)])
.AddCommandsFrom([typeof(NoOpCommand).Assembly])
.AddCommandsFromThisAssembly()
.AllowDebugMode()
.AllowPreviewMode()
Expand Down
2 changes: 1 addition & 1 deletion CliFx.Tests/CliFx.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.5" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
<PackageReference Include="CliWrap" Version="3.6.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
Expand Down
76 changes: 23 additions & 53 deletions CliFx.Tests/ConversionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "xyz" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -78,10 +75,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "xyz" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -128,7 +122,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "true", "-b", "false", "-c" },
["-f", "true", "-b", "false", "-c"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -167,10 +161,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "32" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "32"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -208,7 +199,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "32.14" },
["-f", "32.14"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -248,7 +239,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "1995-04-28Z" },
["-f", "1995-04-28Z"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -288,7 +279,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "12:34:56" },
["-f", "12:34:56"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -329,10 +320,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "two" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "two"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -374,10 +362,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-b", "123" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-b", "123"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -421,10 +406,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-b", "two" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-b", "two"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -468,10 +450,7 @@ public ValueTask ExecuteAsync(IConsole console)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "xyz" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>());

// Assert
exitCode.Should().Be(0);
Expand Down Expand Up @@ -534,7 +513,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "hello", "-b", "world" },
["-f", "hello", "-b", "world"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -580,7 +559,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "hello world" },
["-f", "hello world"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -622,7 +601,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "one", "two", "three" },
["-f", "one", "two", "three"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -664,7 +643,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "one", "two", "three" },
["-f", "one", "two", "three"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -706,7 +685,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "one", "two", "three" },
["-f", "one", "two", "three"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -748,7 +727,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "1", "13", "27" },
["-f", "1", "13", "27"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -787,10 +766,7 @@ public class Command : ICommand
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "xyz" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>());

// Assert
exitCode.Should().NotBe(0);
Expand Down Expand Up @@ -831,7 +807,7 @@ public class Command : ICommand

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "one", "two" },
["-f", "one", "two"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -867,7 +843,7 @@ public class Command : ICommand

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "12.34" },
["-f", "12.34"],
new Dictionary<string, string>()
);

Expand Down Expand Up @@ -898,7 +874,7 @@ public class ValidatorB : BindingValidator<int>
[Command]
public class Command : ICommand
{
[CommandOption('f', Validators = new[] {typeof(ValidatorA), typeof(ValidatorB)})]
[CommandOption('f', Validators = [typeof(ValidatorA), typeof(ValidatorB)])]
public int Foo { get; init; }

public ValueTask ExecuteAsync(IConsole console) => default;
Expand All @@ -912,10 +888,7 @@ public class Command : ICommand
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "12" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "12"], new Dictionary<string, string>());

// Assert
exitCode.Should().NotBe(0);
Expand Down Expand Up @@ -957,10 +930,7 @@ public class Command : ICommand
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "-f", "bar" },
new Dictionary<string, string>()
);
var exitCode = await application.RunAsync(["-f", "bar"], new Dictionary<string, string>());

// Assert
exitCode.Should().NotBe(0);
Expand Down
2 changes: 1 addition & 1 deletion CliFx.Tests/DirectivesSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Command : ICommand

// Act
var exitCode = await application.RunAsync(
new[] { "[preview]", "cmd", "param", "-abc", "--option", "foo" },
["[preview]", "cmd", "param", "-abc", "--option", "foo"],
new Dictionary<string, string> { ["ENV_QOP"] = "hello", ["ENV_KIL"] = "world" }
);

Expand Down
2 changes: 1 addition & 1 deletion CliFx.Tests/EnvironmentSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ValueTask ExecuteAsync(IConsole console)

// Act
var exitCode = await application.RunAsync(
new[] { "--foo", "42" },
["--foo", "42"],
new Dictionary<string, string> { ["ENV_FOO"] = "100", ["ENV_BAR"] = "200" }
);

Expand Down
Loading

0 comments on commit 6aa72e4

Please sign in to comment.