Use C#8 features and cleanup

This commit is contained in:
Alexey Golub
2019-11-13 20:31:48 +02:00
parent 85caa275ae
commit 9690c380d3
66 changed files with 381 additions and 523 deletions

View File

@@ -19,104 +19,104 @@ namespace CliFx.Tests
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
{
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] {typeof(HelloWorldDefaultCommand)},
new string[0],
"Hello world."
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-i", "foo", "-i", "bar", "-s", " " },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
"foo bar"
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-i", "one", "two", "three", "-s", ", " },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
"one, two, three"
);
yield return new TestCaseData(
new[] { typeof(DivideCommand) },
new[] { "div", "-D", "24", "-d", "8" },
new[] {typeof(DivideCommand)},
new[] {"div", "-D", "24", "-d", "8"},
"3"
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "--version" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--version"},
TestVersionText
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "--version" },
new[] {typeof(ConcatCommand)},
new[] {"--version"},
TestVersionText
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "-h" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"-h"},
null
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "--help" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--help"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] {typeof(ConcatCommand)},
new string[0],
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "-h" },
new[] {typeof(ConcatCommand)},
new[] {"-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "--help" },
new[] {typeof(ConcatCommand)},
new[] {"--help"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-h" },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc", "-h" },
new[] {typeof(ExceptionCommand)},
new[] {"exc", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-h" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "[preview]" },
new[] {typeof(ConcatCommand)},
new[] {"[preview]"},
null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc", "[preview]" },
new[] {typeof(ExceptionCommand)},
new[] {"exc", "[preview]"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "[preview]", "-o", "value" },
new[] {typeof(ConcatCommand)},
new[] {"concat", "[preview]", "-o", "value"},
null
);
}
@@ -130,38 +130,38 @@ namespace CliFx.Tests
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "non-existing" },
new[] {typeof(ConcatCommand)},
new[] {"non-existing"},
null, null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc" },
new[] {typeof(ExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-m", "foo bar" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-m", "foo bar"},
"foo bar", null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-m", "foo bar", "-c", "666" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-m", "foo bar", "-c", "666"},
"foo bar", 666
);
}
@@ -169,95 +169,92 @@ namespace CliFx.Tests
[Test]
[TestCaseSource(nameof(GetTestCases_RunAsync))]
public async Task RunAsync_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdOut = null)
string? expectedStdOut = null)
{
// Arrange
using (var stdoutStream = new StringWriter())
{
var console = new VirtualConsole(stdoutStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
await using var stdoutStream = new StringWriter();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.Build();
var console = new VirtualConsole(stdoutStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stdOut = stdoutStream.ToString().Trim();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.Build();
// Assert
exitCode.Should().Be(0);
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stdOut = stdoutStream.ToString().Trim();
if (expectedStdOut != null)
stdOut.Should().Be(expectedStdOut);
else
stdOut.Should().NotBeNullOrWhiteSpace();
}
// Assert
exitCode.Should().Be(0);
if (expectedStdOut != null)
stdOut.Should().Be(expectedStdOut);
else
stdOut.Should().NotBeNullOrWhiteSpace();
}
[Test]
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdErr = null, int? expectedExitCode = null)
string? expectedStdErr = null, int? expectedExitCode = null)
{
// Arrange
using (var stderrStream = new StringWriter())
{
var console = new VirtualConsole(TextWriter.Null, stderrStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
await using var stderrStream = new StringWriter();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.UseConsole(console)
.Build();
var console = new VirtualConsole(TextWriter.Null, stderrStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stderr = stderrStream.ToString().Trim();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.UseConsole(console)
.Build();
// Assert
if (expectedExitCode != null)
exitCode.Should().Be(expectedExitCode);
else
exitCode.Should().NotBe(0);
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stderr = stderrStream.ToString().Trim();
if (expectedStdErr != null)
stderr.Should().Be(expectedStdErr);
else
stderr.Should().NotBeNullOrWhiteSpace();
}
// Assert
if (expectedExitCode != null)
exitCode.Should().Be(expectedExitCode);
else
exitCode.Should().NotBe(0);
if (expectedStdErr != null)
stderr.Should().Be(expectedStdErr);
else
stderr.Should().NotBeNullOrWhiteSpace();
}
[Test]
public async Task RunAsync_Cancellation_Test()
{
// Arrange
using (var stdoutStream = new StringWriter())
using (var cancellationTokenSource = new CancellationTokenSource())
{
var console = new VirtualConsole(stdoutStream, cancellationTokenSource.Token);
var application = new CliApplicationBuilder()
.AddCommand(typeof(CancellableCommand))
.UseConsole(console)
.Build();
var args = new[] { "cancel" };
using var cancellationTokenSource = new CancellationTokenSource();
await using var stdoutStream = new StringWriter();
// Act
var runTask = application.RunAsync(args);
cancellationTokenSource.Cancel();
var exitCode = await runTask.ConfigureAwait(false);
var stdOut = stdoutStream.ToString().Trim();
var console = new VirtualConsole(stdoutStream, cancellationTokenSource.Token);
// Assert
exitCode.Should().Be(-2146233029);
stdOut.Should().Be("Printed");
}
var application = new CliApplicationBuilder()
.AddCommand(typeof(CancellableCommand))
.UseConsole(console)
.Build();
var args = new[] {"cancel"};
// Act
var runTask = application.RunAsync(args);
cancellationTokenSource.Cancel();
var exitCode = await runTask.ConfigureAwait(false);
var stdOut = stdoutStream.ToString().Trim();
// Assert
exitCode.Should().Be(-2146233029);
stdOut.Should().Be("Printed");
}
}
}