From 579c42ca8275fafc05564928baa6fdd035de0e46 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sat, 7 Sep 2024 14:50:28 +0100 Subject: [PATCH 1/5] Initial check-in of Should_Output_Hello_World() test --- examples/Cli/Testing/CommandAppTests.cs | 47 +++++++++++++++++++++++++ examples/Cli/Testing/Testing.csproj | 26 ++++++++++++++ examples/Examples.sln | 19 ++++++++-- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 examples/Cli/Testing/CommandAppTests.cs create mode 100644 examples/Cli/Testing/Testing.csproj diff --git a/examples/Cli/Testing/CommandAppTests.cs b/examples/Cli/Testing/CommandAppTests.cs new file mode 100644 index 0000000..27612bb --- /dev/null +++ b/examples/Cli/Testing/CommandAppTests.cs @@ -0,0 +1,47 @@ +using Spectre.Console; +using Spectre.Console.Cli; +using Spectre.Console.Testing; + +namespace Testing; + +[TestClass] +public class CommandAppTests +{ + /// + /// A Spectre.Console Command + /// + 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(); + + // When + var result = app.Run(); + + // Then + Assert.AreEqual(result.ExitCode, 0); + Assert.AreEqual(result.Output, "Hello world."); + } +} \ No newline at end of file diff --git a/examples/Cli/Testing/Testing.csproj b/examples/Cli/Testing/Testing.csproj new file mode 100644 index 0000000..e49a17e --- /dev/null +++ b/examples/Cli/Testing/Testing.csproj @@ -0,0 +1,26 @@ + + + + net8.0 + enable + enable + false + true + Testing + Demonstrates how to unit test a Spectre.Console console application. + Cli + false + + + + + + + + + + + + + + diff --git a/examples/Examples.sln b/examples/Examples.sln index 136e2a8..239bce8 100644 --- a/examples/Examples.sln +++ b/examples/Examples.sln @@ -75,9 +75,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Layout", "Console\Layout\La EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Json", "Console\Json\Json.csproj", "{ABE3E734-0756-4D5A-B28A-E6E526D9927D}" 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 -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 Global 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|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -533,6 +547,7 @@ Global {37024E79-A857-4EB2-9B50-F724ED34E5EB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C} {DD8EC1B0-F50C-44E4-8399-2D560F95E572} = {2571F1BD-6556-4F96-B27B-B6190E1BF13A} {BAB490D6-FF8D-462B-B2B0-933384D629DB} = {4682E9B7-B54C-419D-B92F-470DA4E5674C} + {3BBDD42E-C1B4-4FDB-B87A-1C16CB2241AF} = {4682E9B7-B54C-419D-B92F-470DA4E5674C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3EE724C5-CAB4-410D-AC63-8D4260EF83ED} From ec1df379760e7a6fe96156aef0ff4383e6c696de Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:33:46 +0100 Subject: [PATCH 2/5] Added console interactivity test --- examples/Cli/Testing/ConsoleInputTests.cs | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/Cli/Testing/ConsoleInputTests.cs diff --git a/examples/Cli/Testing/ConsoleInputTests.cs b/examples/Cli/Testing/ConsoleInputTests.cs new file mode 100644 index 0000000..0e1a48c --- /dev/null +++ b/examples/Cli/Testing/ConsoleInputTests.cs @@ -0,0 +1,26 @@ +using Spectre.Console; +using Spectre.Console.Cli; +using Spectre.Console.Testing; + +namespace Testing; + +[TestClass] +public class ConsoleInputTests +{ + [TestMethod] + public void Should_Select_Orange() + { + // Given + var console = new TestConsole(); + console.Input.PushTextWithEnter("Orange"); + + // When + console.Prompt( + new TextPrompt("Favorite fruit?") + .AddChoice("Banana") + .AddChoice("Orange")); + + // Then + Assert.AreEqual(console.Output, "Favorite fruit? [Banana/Orange]: Orange\n"); + } +} \ No newline at end of file From ab42531e336ac68f1cc043823d91ac5605e49711 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:51:12 +0100 Subject: [PATCH 3/5] Added Should_Render_Panel test --- examples/Cli/Testing/ConsoleInputTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/Cli/Testing/ConsoleInputTests.cs b/examples/Cli/Testing/ConsoleInputTests.cs index 0e1a48c..0e988e2 100644 --- a/examples/Cli/Testing/ConsoleInputTests.cs +++ b/examples/Cli/Testing/ConsoleInputTests.cs @@ -7,6 +7,24 @@ namespace Testing; [TestClass] public class ConsoleInputTests { + [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() { From b58a42df286d682d67ed3164360ed4bba24b730c Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:36:07 +0100 Subject: [PATCH 4/5] Minor rename --- examples/Cli/Testing/{ConsoleInputTests.cs => ConsoleTests.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename examples/Cli/Testing/{ConsoleInputTests.cs => ConsoleTests.cs} (96%) diff --git a/examples/Cli/Testing/ConsoleInputTests.cs b/examples/Cli/Testing/ConsoleTests.cs similarity index 96% rename from examples/Cli/Testing/ConsoleInputTests.cs rename to examples/Cli/Testing/ConsoleTests.cs index 0e988e2..a770702 100644 --- a/examples/Cli/Testing/ConsoleInputTests.cs +++ b/examples/Cli/Testing/ConsoleTests.cs @@ -5,7 +5,7 @@ namespace Testing; [TestClass] -public class ConsoleInputTests +public class ConsoleTests { [TestMethod] public void Should_Render_Panel() From 34b2cdc00546fc5ebdc80a5e60df73a365cad7b8 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:02:53 +0100 Subject: [PATCH 5/5] Last minor revisions (fingers crossed) --- examples/Cli/Testing/ConsoleTests.cs | 1 - examples/Cli/Testing/Testing.csproj | 4 ++++ examples/Shared/Shared.csproj | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/Cli/Testing/ConsoleTests.cs b/examples/Cli/Testing/ConsoleTests.cs index a770702..ec0cb54 100644 --- a/examples/Cli/Testing/ConsoleTests.cs +++ b/examples/Cli/Testing/ConsoleTests.cs @@ -1,5 +1,4 @@ using Spectre.Console; -using Spectre.Console.Cli; using Spectre.Console.Testing; namespace Testing; diff --git a/examples/Cli/Testing/Testing.csproj b/examples/Cli/Testing/Testing.csproj index e49a17e..3ef1ad5 100644 --- a/examples/Cli/Testing/Testing.csproj +++ b/examples/Cli/Testing/Testing.csproj @@ -19,6 +19,10 @@ + + + + diff --git a/examples/Shared/Shared.csproj b/examples/Shared/Shared.csproj index a6dcbd2..d5cc440 100644 --- a/examples/Shared/Shared.csproj +++ b/examples/Shared/Shared.csproj @@ -11,6 +11,7 @@ +