mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor tests again
This commit is contained in:
@@ -11,17 +11,17 @@ namespace CliFx.Tests
|
||||
{
|
||||
// Make sure all builder methods work
|
||||
[Test]
|
||||
public void Build_Smoke_Test()
|
||||
public void All_Smoke_Test()
|
||||
{
|
||||
// Arrange
|
||||
var builder = new CliApplicationBuilder();
|
||||
|
||||
// Act
|
||||
builder
|
||||
.AddCommand(typeof(EchoCommand))
|
||||
.AddCommandsFrom(typeof(EchoCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(EchoCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(EchoCommand).Assembly})
|
||||
.AddCommand(typeof(HelloWorldDefaultCommand))
|
||||
.AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(HelloWorldDefaultCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(HelloWorldDefaultCommand).Assembly})
|
||||
.AddCommandsFromThisAssembly()
|
||||
.AllowDebugMode()
|
||||
.AllowPreviewMode()
|
||||
@@ -34,9 +34,9 @@ namespace CliFx.Tests
|
||||
.Build();
|
||||
}
|
||||
|
||||
// Make sure builder can produce a default application
|
||||
// Make sure builder can produce an application with no parameters specified
|
||||
[Test]
|
||||
public void Build_Fallback_Smoke_Test()
|
||||
public void Build_Test()
|
||||
{
|
||||
// Arrange
|
||||
var builder = new CliApplicationBuilder();
|
||||
|
||||
@@ -17,95 +17,105 @@ namespace CliFx.Tests
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {"-m", "foo bar"},
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new string[0],
|
||||
"Hello world."
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
|
||||
"foo bar"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new[] {"echo", "-m", "foo bar"},
|
||||
"foo bar"
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
|
||||
"one, two, three"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {typeof(DivideCommand)},
|
||||
new[] {"div", "-D", "24", "-d", "8"},
|
||||
"3"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[] {"--version"},
|
||||
TestVersionText
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"--version"},
|
||||
TestVersionText
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Smoke()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {"-h"}
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[] {"-h"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {"--help"}
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[] {"--help"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {"--version"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new string[0],
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new string[0]
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"-h"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new[] {"-h"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"--help"},
|
||||
null
|
||||
);
|
||||
|
||||
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"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-h"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"exc", "-h"}
|
||||
new[] {"exc", "-h"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc", "-h"}
|
||||
new[] {"exc", "-h"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {"[preview]"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"[preview]"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"exc", "[preview]"}
|
||||
new[] {"exc", "[preview]"},
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new[] {"echo", "[preview]", "-o", "value"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "[preview]", "-o", "value"},
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -113,38 +123,56 @@ namespace CliFx.Tests
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new Type[0],
|
||||
new string[0]
|
||||
new string[0],
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoCommand)},
|
||||
new[] {"non-existing"}
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"non-existing"},
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"exc"}
|
||||
new[] {"exc"},
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc"}
|
||||
new[] {"exc"},
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
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]
|
||||
[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
|
||||
using (var stdout = new StringWriter())
|
||||
using (var stdoutStream = new StringWriter())
|
||||
{
|
||||
var console = new VirtualConsole(stdout);
|
||||
var console = new VirtualConsole(stdoutStream);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommands(commandTypes)
|
||||
@@ -154,45 +182,27 @@ namespace CliFx.Tests
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(commandLineArguments);
|
||||
var stdOut = stdoutStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdout.ToString().Trim().Should().Be(expectedStdOut);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Smoke))]
|
||||
public async Task RunAsync_Smoke_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
// 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();
|
||||
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)
|
||||
public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
|
||||
string expectedStdErr = null, int? expectedExitCode = null)
|
||||
{
|
||||
// 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()
|
||||
.AddCommands(commandTypes)
|
||||
@@ -202,10 +212,18 @@ namespace CliFx.Tests
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(commandLineArguments);
|
||||
var stderr = stderrStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
if (expectedExitCode != null)
|
||||
exitCode.Should().Be(expectedExitCode);
|
||||
else
|
||||
exitCode.Should().NotBe(0);
|
||||
stderr.ToString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
if (expectedStdErr != null)
|
||||
stderr.Should().Be(expectedStdErr);
|
||||
else
|
||||
stderr.Should().NotBeNullOrWhiteSpace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace CliFx.Tests.Services
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_CreateCommand()
|
||||
{
|
||||
yield return new TestCaseData(GetCommandSchema(typeof(EchoCommand)));
|
||||
yield return new TestCaseData(GetCommandSchema(typeof(HelloWorldDefaultCommand)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -108,7 +108,8 @@ namespace CliFx.Tests.Services
|
||||
|
||||
[Test]
|
||||
[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
|
||||
var initializer = new CommandInitializer();
|
||||
|
||||
@@ -203,7 +203,8 @@ namespace CliFx.Tests.Services
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_ParseCommandInput))]
|
||||
public void ParseCommandInput_Test(IReadOnlyList<string> commandLineArguments, CommandInput expectedCommandInput)
|
||||
public void ParseCommandInput_Test(IReadOnlyList<string> commandLineArguments,
|
||||
CommandInput expectedCommandInput)
|
||||
{
|
||||
// Arrange
|
||||
var parser = new CommandInputParser();
|
||||
|
||||
@@ -278,7 +278,8 @@ namespace CliFx.Tests.Services
|
||||
|
||||
[Test]
|
||||
[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
|
||||
var converter = new CommandOptionInputConverter();
|
||||
|
||||
@@ -38,15 +38,10 @@ namespace CliFx.Tests.Services
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EchoDefaultCommand)},
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[]
|
||||
{
|
||||
new CommandSchema(typeof(EchoDefaultCommand), null, null,
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(typeof(EchoDefaultCommand).GetProperty(nameof(EchoDefaultCommand.Message)),
|
||||
"message", 'm', true, null)
|
||||
})
|
||||
new CommandSchema(typeof(HelloWorldDefaultCommand), null, null, new CommandOptionSchema[0])
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -86,7 +81,8 @@ namespace CliFx.Tests.Services
|
||||
|
||||
[Test]
|
||||
[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
|
||||
var commandSchemaResolver = new CommandSchemaResolver();
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace CliFx.Tests.Services
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type)),
|
||||
GetCommandSchema(typeof(EchoCommand))
|
||||
GetCommandSchema(typeof(HelloWorldDefaultCommand))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -89,13 +89,14 @@ namespace CliFx.Tests.Services
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_RenderHelpText))]
|
||||
public void RenderHelpText_Test(HelpTextSource source, IReadOnlyList<string> expectedSubstrings)
|
||||
public void RenderHelpText_Test(HelpTextSource source,
|
||||
IReadOnlyList<string> expectedSubstrings)
|
||||
{
|
||||
// Arrange
|
||||
using (var stdout = new StringWriter())
|
||||
{
|
||||
var renderer = new HelpTextRenderer();
|
||||
var console = new VirtualConsole(stdout);
|
||||
var renderer = new HelpTextRenderer();
|
||||
|
||||
// Act
|
||||
renderer.RenderHelpText(console, source);
|
||||
|
||||
@@ -8,7 +8,14 @@ namespace CliFx.Tests.Services
|
||||
[TestFixture]
|
||||
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]
|
||||
public void All_Smoke_Test()
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace CliFx.Tests.Services
|
||||
[TestFixture]
|
||||
public class VirtualConsoleTests
|
||||
{
|
||||
// Make sure console uses specified streams and doesn't leak to System.Console
|
||||
[Test]
|
||||
public void All_Smoke_Test()
|
||||
{
|
||||
@@ -26,11 +27,16 @@ namespace CliFx.Tests.Services
|
||||
|
||||
// Assert
|
||||
console.Input.Should().BeSameAs(stdin);
|
||||
console.Input.Should().NotBeSameAs(Console.In);
|
||||
console.IsInputRedirected.Should().BeTrue();
|
||||
console.Output.Should().BeSameAs(stdout);
|
||||
console.Output.Should().NotBeSameAs(Console.Out);
|
||||
console.IsOutputRedirected.Should().BeTrue();
|
||||
console.Error.Should().BeSameAs(stderr);
|
||||
console.Error.Should().NotBeSameAs(Console.Error);
|
||||
console.IsErrorRedirected.Should().BeTrue();
|
||||
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
||||
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class EchoDefaultCommand : EchoCommand
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,12 @@ using CliFx.Services;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("echo")]
|
||||
public class EchoCommand : ICommand
|
||||
[Command]
|
||||
public class HelloWorldDefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("message", 'm', IsRequired = true)]
|
||||
public string Message { get; set; }
|
||||
|
||||
public Task ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(Message);
|
||||
console.Output.WriteLine("Hello world.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user