mirror of
https://github.com/spectreconsole/examples.git
synced 2025-10-25 15:19:18 +00:00
47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
using Spectre.Console;
|
|
using Spectre.Console.Cli;
|
|
using Spectre.Console.Testing;
|
|
|
|
namespace Testing;
|
|
|
|
[TestClass]
|
|
public class CommandAppTests
|
|
{
|
|
/// <summary>
|
|
/// A Spectre.Console Command
|
|
/// </summary>
|
|
public class HelloWorldCommand : Command
|
|
{
|
|
private readonly IAnsiConsole _console;
|
|
|
|
public HelloWorldCommand(IAnsiConsole console)
|
|
{
|
|
// nb. AnsiConsole should not be called directly by the command
|
|
// since this doesn't play well with testing. Instead,
|
|
// the command should inject a IAnsiConsole and use that.
|
|
|
|
_console = console;
|
|
}
|
|
|
|
public override int Execute(CommandContext context)
|
|
{
|
|
_console.WriteLine("Hello world.");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Should_Output_Hello_World()
|
|
{
|
|
// Given
|
|
var app = new CommandAppTester();
|
|
app.SetDefaultCommand<HelloWorldCommand>();
|
|
|
|
// When
|
|
var result = app.Run();
|
|
|
|
// Then
|
|
Assert.AreEqual(result.ExitCode, 0);
|
|
Assert.AreEqual(result.Output, "Hello world.");
|
|
}
|
|
} |