mirror of
https://github.com/spectreconsole/examples.git
synced 2025-10-25 15:19:18 +00:00
Add CommandApp unit testing example (#2)
* Added console interactivity test * Added `Should_Render_Panel` test
This commit is contained in:
47
examples/Cli/Testing/CommandAppTests.cs
Normal file
47
examples/Cli/Testing/CommandAppTests.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
43
examples/Cli/Testing/ConsoleTests.cs
Normal file
43
examples/Cli/Testing/ConsoleTests.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using Spectre.Console;
|
||||||
|
using Spectre.Console.Testing;
|
||||||
|
|
||||||
|
namespace Testing;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class ConsoleTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Should_Render_Panel()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new TestConsole();
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Write(new Panel(new Text("Hello World")));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Assert.AreEqual(console.Output, """"
|
||||||
|
┌─────────────┐
|
||||||
|
│ Hello World │
|
||||||
|
└─────────────┘
|
||||||
|
|
||||||
|
"""");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Should_Select_Orange()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new TestConsole();
|
||||||
|
console.Input.PushTextWithEnter("Orange");
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Prompt(
|
||||||
|
new TextPrompt<string>("Favorite fruit?")
|
||||||
|
.AddChoice("Banana")
|
||||||
|
.AddChoice("Orange"));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Assert.AreEqual(console.Output, "Favorite fruit? [Banana/Orange]: Orange\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
30
examples/Cli/Testing/Testing.csproj
Normal file
30
examples/Cli/Testing/Testing.csproj
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
<ExampleName>Testing</ExampleName>
|
||||||
|
<ExampleDescription>Demonstrates how to unit test a Spectre.Console console application.</ExampleDescription>
|
||||||
|
<ExampleGroup>Cli</ExampleGroup>
|
||||||
|
<ExampleVisible>false</ExampleVisible>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
|
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||||
|
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Shared\Shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -75,9 +75,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Layout", "Console\Layout\La
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Json", "Console\Json\Json.csproj", "{ABE3E734-0756-4D5A-B28A-E6E526D9927D}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Json", "Console\Json\Json.csproj", "{ABE3E734-0756-4D5A-B28A-E6E526D9927D}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Help", "Cli\Help\Help.csproj", "{BAB490D6-FF8D-462B-B2B0-933384D629DB}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Help", "Cli\Help\Help.csproj", "{BAB490D6-FF8D-462B-B2B0-933384D629DB}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorations", "Console\Decorations\Decorations.csproj", "{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorations", "Console\Decorations\Decorations.csproj", "{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing", "Cli\Testing\Testing.csproj", "{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -521,6 +523,18 @@ Global
|
|||||||
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x64.Build.0 = Release|Any CPU
|
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x86.ActiveCfg = Release|Any CPU
|
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x86.Build.0 = Release|Any CPU
|
{FC5852F1-E01F-4DF7-9B49-CA19A9EE670F}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -533,6 +547,7 @@ Global
|
|||||||
{37024E79-A857-4EB2-9B50-F724ED34E5EB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C}
|
{37024E79-A857-4EB2-9B50-F724ED34E5EB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C}
|
||||||
{DD8EC1B0-F50C-44E4-8399-2D560F95E572} = {2571F1BD-6556-4F96-B27B-B6190E1BF13A}
|
{DD8EC1B0-F50C-44E4-8399-2D560F95E572} = {2571F1BD-6556-4F96-B27B-B6190E1BF13A}
|
||||||
{BAB490D6-FF8D-462B-B2B0-933384D629DB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C}
|
{BAB490D6-FF8D-462B-B2B0-933384D629DB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C}
|
||||||
|
{3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF} = {4682E9B7-B54C-419D-B92F-470DA4E5674C}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {3EE724C5-CAB4-410D-AC63-8D4260EF83ED}
|
SolutionGuid = {3EE724C5-CAB4-410D-AC63-8D4260EF83ED}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
|
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
|
||||||
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
|
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
|
||||||
<PackageReference Include="Spectre.Console.Json" Version="0.49.1" />
|
<PackageReference Include="Spectre.Console.Json" Version="0.49.1" />
|
||||||
|
<PackageReference Include="Spectre.Console.Testing" Version="0.49.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user