Refactor tests again

This commit is contained in:
Alexey Golub
2019-08-25 18:26:40 +03:00
parent 43a304bb26
commit 41c0493e66
13 changed files with 142 additions and 123 deletions

View File

@@ -11,17 +11,17 @@ namespace CliFx.Tests
{ {
// Make sure all builder methods work // Make sure all builder methods work
[Test] [Test]
public void Build_Smoke_Test() public void All_Smoke_Test()
{ {
// Arrange // Arrange
var builder = new CliApplicationBuilder(); var builder = new CliApplicationBuilder();
// Act // Act
builder builder
.AddCommand(typeof(EchoCommand)) .AddCommand(typeof(HelloWorldDefaultCommand))
.AddCommandsFrom(typeof(EchoCommand).Assembly) .AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly)
.AddCommands(new[] {typeof(EchoCommand)}) .AddCommands(new[] {typeof(HelloWorldDefaultCommand)})
.AddCommandsFrom(new[] {typeof(EchoCommand).Assembly}) .AddCommandsFrom(new[] {typeof(HelloWorldDefaultCommand).Assembly})
.AddCommandsFromThisAssembly() .AddCommandsFromThisAssembly()
.AllowDebugMode() .AllowDebugMode()
.AllowPreviewMode() .AllowPreviewMode()
@@ -34,9 +34,9 @@ namespace CliFx.Tests
.Build(); .Build();
} }
// Make sure builder can produce a default application // Make sure builder can produce an application with no parameters specified
[Test] [Test]
public void Build_Fallback_Smoke_Test() public void Build_Test()
{ {
// Arrange // Arrange
var builder = new CliApplicationBuilder(); var builder = new CliApplicationBuilder();

View File

@@ -17,95 +17,105 @@ namespace CliFx.Tests
private static IEnumerable<TestCaseData> GetTestCases_RunAsync() private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
{ {
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(HelloWorldDefaultCommand)},
new[] {"-m", "foo bar"}, new string[0],
"Hello world."
);
yield return new TestCaseData(
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
"foo bar" "foo bar"
); );
yield return new TestCaseData(
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
"one, two, three"
);
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoCommand)}, new[] {typeof(DivideCommand)},
new[] {"echo", "-m", "foo bar"}, new[] {"div", "-D", "24", "-d", "8"},
"foo bar" "3"
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--version"}, new[] {"--version"},
TestVersionText TestVersionText
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoCommand)}, new[] {typeof(ConcatCommand)},
new[] {"--version"}, new[] {"--version"},
TestVersionText TestVersionText
); );
}
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Smoke()
{
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(HelloWorldDefaultCommand)},
new[] {"-h"} new[] {"-h"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--help"} new[] {"--help"},
null
);
yield return new TestCaseData(
new[] {typeof(ConcatCommand)},
new string[0],
null
);
yield return new TestCaseData(
new[] {typeof(ConcatCommand)},
new[] {"-h"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(ConcatCommand)},
new[] {"--version"} new[] {"--help"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoCommand)}, new[] {typeof(ConcatCommand)},
new string[0] new[] {"concat", "-h"},
); null
yield return new TestCaseData(
new[] {typeof(EchoCommand)},
new[] {"-h"}
);
yield return new TestCaseData(
new[] {typeof(EchoCommand)},
new[] {"--help"}
);
yield return new TestCaseData(
new[] {typeof(EchoCommand)},
new[] {"--version"}
);
yield return new TestCaseData(
new[] {typeof(EchoCommand)},
new[] {"echo", "-h"}
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(ExceptionCommand)}, new[] {typeof(ExceptionCommand)},
new[] {"exc", "-h"} new[] {"exc", "-h"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(CommandExceptionCommand)}, new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-h"} new[] {"exc", "-h"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(ConcatCommand)},
new[] {"[preview]"} new[] {"[preview]"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(ExceptionCommand)}, new[] {typeof(ExceptionCommand)},
new[] {"exc", "[preview]"} new[] {"exc", "[preview]"},
null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoCommand)}, new[] {typeof(ConcatCommand)},
new[] {"echo", "[preview]", "-o", "value"} new[] {"concat", "[preview]", "-o", "value"},
null
); );
} }
@@ -113,38 +123,56 @@ namespace CliFx.Tests
{ {
yield return new TestCaseData( yield return new TestCaseData(
new Type[0], new Type[0],
new string[0] new string[0],
null, null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoCommand)}, new[] {typeof(ConcatCommand)},
new[] {"non-existing"} new[] {"non-existing"},
null, null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(ExceptionCommand)}, new[] {typeof(ExceptionCommand)},
new[] {"exc"} new[] {"exc"},
null, null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(CommandExceptionCommand)}, new[] {typeof(CommandExceptionCommand)},
new[] {"exc"} new[] {"exc"},
null, null
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(CommandExceptionCommand)}, new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-c", "666"} new[] {"exc"},
null, null
);
yield return new TestCaseData(
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"},
"foo bar", 666
); );
} }
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_RunAsync))] [TestCaseSource(nameof(GetTestCases_RunAsync))]
public async Task RunAsync_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments, string expectedStdOut) public async Task RunAsync_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdOut = null)
{ {
// Arrange // Arrange
using (var stdout = new StringWriter()) using (var stdoutStream = new StringWriter())
{ {
var console = new VirtualConsole(stdout); var console = new VirtualConsole(stdoutStream);
var application = new CliApplicationBuilder() var application = new CliApplicationBuilder()
.AddCommands(commandTypes) .AddCommands(commandTypes)
@@ -154,45 +182,27 @@ namespace CliFx.Tests
// Act // Act
var exitCode = await application.RunAsync(commandLineArguments); var exitCode = await application.RunAsync(commandLineArguments);
var stdOut = stdoutStream.ToString().Trim();
// Assert // Assert
exitCode.Should().Be(0); exitCode.Should().Be(0);
stdout.ToString().Trim().Should().Be(expectedStdOut);
}
}
[Test] if (expectedStdOut != null)
[TestCaseSource(nameof(GetTestCases_RunAsync_Smoke))] stdOut.Should().Be(expectedStdOut);
public async Task RunAsync_Smoke_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments) else
{ stdOut.Should().NotBeNullOrWhiteSpace();
// Arrange
using (var stdout = new StringWriter())
{
var console = new VirtualConsole(stdout);
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(commandLineArguments);
// Assert
exitCode.Should().Be(0);
stdout.ToString().Should().NotBeNullOrWhiteSpace();
} }
} }
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))] [TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments) public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdErr = null, int? expectedExitCode = null)
{ {
// Arrange // Arrange
using (var stderr = new StringWriter()) using (var stderrStream = new StringWriter())
{ {
var console = new VirtualConsole(TextWriter.Null, stderr); var console = new VirtualConsole(TextWriter.Null, stderrStream);
var application = new CliApplicationBuilder() var application = new CliApplicationBuilder()
.AddCommands(commandTypes) .AddCommands(commandTypes)
@@ -202,10 +212,18 @@ namespace CliFx.Tests
// Act // Act
var exitCode = await application.RunAsync(commandLineArguments); var exitCode = await application.RunAsync(commandLineArguments);
var stderr = stderrStream.ToString().Trim();
// Assert // Assert
exitCode.Should().NotBe(0); if (expectedExitCode != null)
stderr.ToString().Should().NotBeNullOrWhiteSpace(); exitCode.Should().Be(expectedExitCode);
else
exitCode.Should().NotBe(0);
if (expectedStdErr != null)
stderr.Should().Be(expectedStdErr);
else
stderr.Should().NotBeNullOrWhiteSpace();
} }
} }
} }

View File

@@ -17,7 +17,7 @@ namespace CliFx.Tests.Services
private static IEnumerable<TestCaseData> GetTestCases_CreateCommand() private static IEnumerable<TestCaseData> GetTestCases_CreateCommand()
{ {
yield return new TestCaseData(GetCommandSchema(typeof(EchoCommand))); yield return new TestCaseData(GetCommandSchema(typeof(HelloWorldDefaultCommand)));
} }
[Test] [Test]

View File

@@ -108,7 +108,8 @@ namespace CliFx.Tests.Services
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_InitializeCommand))] [TestCaseSource(nameof(GetTestCases_InitializeCommand))]
public void InitializeCommand_Test(ICommand command, CommandSchema commandSchema, CommandInput commandInput, ICommand expectedCommand) public void InitializeCommand_Test(ICommand command, CommandSchema commandSchema, CommandInput commandInput,
ICommand expectedCommand)
{ {
// Arrange // Arrange
var initializer = new CommandInitializer(); var initializer = new CommandInitializer();

View File

@@ -203,7 +203,8 @@ namespace CliFx.Tests.Services
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_ParseCommandInput))] [TestCaseSource(nameof(GetTestCases_ParseCommandInput))]
public void ParseCommandInput_Test(IReadOnlyList<string> commandLineArguments, CommandInput expectedCommandInput) public void ParseCommandInput_Test(IReadOnlyList<string> commandLineArguments,
CommandInput expectedCommandInput)
{ {
// Arrange // Arrange
var parser = new CommandInputParser(); var parser = new CommandInputParser();

View File

@@ -278,7 +278,8 @@ namespace CliFx.Tests.Services
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_ConvertOptionInput))] [TestCaseSource(nameof(GetTestCases_ConvertOptionInput))]
public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType, object expectedConvertedValue) public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType,
object expectedConvertedValue)
{ {
// Arrange // Arrange
var converter = new CommandOptionInputConverter(); var converter = new CommandOptionInputConverter();

View File

@@ -38,15 +38,10 @@ namespace CliFx.Tests.Services
); );
yield return new TestCaseData( yield return new TestCaseData(
new[] {typeof(EchoDefaultCommand)}, new[] {typeof(HelloWorldDefaultCommand)},
new[] new[]
{ {
new CommandSchema(typeof(EchoDefaultCommand), null, null, new CommandSchema(typeof(HelloWorldDefaultCommand), null, null, new CommandOptionSchema[0])
new[]
{
new CommandOptionSchema(typeof(EchoDefaultCommand).GetProperty(nameof(EchoDefaultCommand.Message)),
"message", 'm', true, null)
})
} }
); );
} }
@@ -86,7 +81,8 @@ namespace CliFx.Tests.Services
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_GetCommandSchemas))] [TestCaseSource(nameof(GetTestCases_GetCommandSchemas))]
public void GetCommandSchemas_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<CommandSchema> expectedCommandSchemas) public void GetCommandSchemas_Test(IReadOnlyList<Type> commandTypes,
IReadOnlyList<CommandSchema> expectedCommandSchemas)
{ {
// Arrange // Arrange
var commandSchemaResolver = new CommandSchemaResolver(); var commandSchemaResolver = new CommandSchemaResolver();

View File

@@ -19,7 +19,7 @@ namespace CliFx.Tests.Services
{ {
yield return new TestCaseData( yield return new TestCaseData(
new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type)), new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type)),
GetCommandSchema(typeof(EchoCommand)) GetCommandSchema(typeof(HelloWorldDefaultCommand))
); );
} }

View File

@@ -89,13 +89,14 @@ namespace CliFx.Tests.Services
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_RenderHelpText))] [TestCaseSource(nameof(GetTestCases_RenderHelpText))]
public void RenderHelpText_Test(HelpTextSource source, IReadOnlyList<string> expectedSubstrings) public void RenderHelpText_Test(HelpTextSource source,
IReadOnlyList<string> expectedSubstrings)
{ {
// Arrange // Arrange
using (var stdout = new StringWriter()) using (var stdout = new StringWriter())
{ {
var renderer = new HelpTextRenderer();
var console = new VirtualConsole(stdout); var console = new VirtualConsole(stdout);
var renderer = new HelpTextRenderer();
// Act // Act
renderer.RenderHelpText(console, source); renderer.RenderHelpText(console, source);

View File

@@ -8,7 +8,14 @@ namespace CliFx.Tests.Services
[TestFixture] [TestFixture]
public class SystemConsoleTests public class SystemConsoleTests
{ {
// Test that it correctly wraps around System.Console [TearDown]
public void TearDown()
{
// Reset console color so it doesn't carry on into next tests
Console.ResetColor();
}
// Make sure console correctly wraps around System.Console
[Test] [Test]
public void All_Smoke_Test() public void All_Smoke_Test()
{ {

View File

@@ -9,6 +9,7 @@ namespace CliFx.Tests.Services
[TestFixture] [TestFixture]
public class VirtualConsoleTests public class VirtualConsoleTests
{ {
// Make sure console uses specified streams and doesn't leak to System.Console
[Test] [Test]
public void All_Smoke_Test() public void All_Smoke_Test()
{ {
@@ -26,11 +27,16 @@ namespace CliFx.Tests.Services
// Assert // Assert
console.Input.Should().BeSameAs(stdin); console.Input.Should().BeSameAs(stdin);
console.Input.Should().NotBeSameAs(Console.In);
console.IsInputRedirected.Should().BeTrue(); console.IsInputRedirected.Should().BeTrue();
console.Output.Should().BeSameAs(stdout); console.Output.Should().BeSameAs(stdout);
console.Output.Should().NotBeSameAs(Console.Out);
console.IsOutputRedirected.Should().BeTrue(); console.IsOutputRedirected.Should().BeTrue();
console.Error.Should().BeSameAs(stderr); console.Error.Should().BeSameAs(stderr);
console.Error.Should().NotBeSameAs(Console.Error);
console.IsErrorRedirected.Should().BeTrue(); console.IsErrorRedirected.Should().BeTrue();
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
} }
} }
} }

View File

@@ -1,9 +0,0 @@
using CliFx.Attributes;
namespace CliFx.Tests.TestCommands
{
[Command]
public class EchoDefaultCommand : EchoCommand
{
}
}

View File

@@ -4,15 +4,12 @@ using CliFx.Services;
namespace CliFx.Tests.TestCommands namespace CliFx.Tests.TestCommands
{ {
[Command("echo")] [Command]
public class EchoCommand : ICommand public class HelloWorldDefaultCommand : ICommand
{ {
[CommandOption("message", 'm', IsRequired = true)]
public string Message { get; set; }
public Task ExecuteAsync(IConsole console) public Task ExecuteAsync(IConsole console)
{ {
console.Output.WriteLine(Message); console.Output.WriteLine("Hello world.");
return Task.CompletedTask; return Task.CompletedTask;
} }
} }