From 3ea72a6f95abf8464212302f9059ebc8cb031aa1 Mon Sep 17 00:00:00 2001 From: nightblade9 Date: Fri, 22 Nov 2024 20:02:13 -0500 Subject: [PATCH] Unit tests for a couple of commands. Fix a couple of unused-allocation issues. --- .../Commands/DoNothingCommandTests.cs | 15 ++++++++++++ .../Commands/ShowHelpCommandTests.cs | 24 +++++++++++++++++++ .../Commands/DoNothingCommand.cs | 2 +- .../Commands/QuitGameCommand.cs | 4 ++-- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 source/TextBlade.Core.Tests/Commands/DoNothingCommandTests.cs create mode 100644 source/TextBlade.Core.Tests/Commands/ShowHelpCommandTests.cs diff --git a/source/TextBlade.Core.Tests/Commands/DoNothingCommandTests.cs b/source/TextBlade.Core.Tests/Commands/DoNothingCommandTests.cs new file mode 100644 index 0000000..acc8155 --- /dev/null +++ b/source/TextBlade.Core.Tests/Commands/DoNothingCommandTests.cs @@ -0,0 +1,15 @@ +using NUnit.Framework; +using TextBlade.Core.Commands; + +namespace TextBlade.Core.Tests.Commands; + +[TestFixture] +public class DoNothingCommandTests +{ + [Test] + public void Execute_DoesNothing_Literally() + { + // Arrange/Act/Assert + Assert.That(new DoNothingCommand().Execute(null, null), Is.Empty); + } +} diff --git a/source/TextBlade.Core.Tests/Commands/ShowHelpCommandTests.cs b/source/TextBlade.Core.Tests/Commands/ShowHelpCommandTests.cs new file mode 100644 index 0000000..b9eee53 --- /dev/null +++ b/source/TextBlade.Core.Tests/Commands/ShowHelpCommandTests.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using TextBlade.Core.Commands; + +namespace TextBlade.Core.Tests.Commands; + +[TestFixture] +public class ShowHelpCommandTests +{ + [Test] + public void Execute_ReturnsABunchOfCommands() + { + // Arrange/Act + var actuals = new ShowHelpCommand().Execute(null, null); + + // Assert + // Check a bunch of commands + foreach (var expectedCommand in new string[] {"credits", "help", "quit"}) + { + var expected = $"[red]{expectedCommand}[/]".ToUpperInvariant(); + var actual = actuals.Single(a => a.ToUpperInvariant().Contains(expected)); + Assert.That(actual.ToUpperInvariant(), Does.Contain(expected)); + } + } +} diff --git a/source/TextBlade.Core/Commands/DoNothingCommand.cs b/source/TextBlade.Core/Commands/DoNothingCommand.cs index 5486205..f691e3f 100644 --- a/source/TextBlade.Core/Commands/DoNothingCommand.cs +++ b/source/TextBlade.Core/Commands/DoNothingCommand.cs @@ -7,6 +7,6 @@ public class DoNothingCommand : ICommand { public IEnumerable Execute(IGame game, List party) { - return new string[0]; + return Array.Empty(); } } diff --git a/source/TextBlade.Core/Commands/QuitGameCommand.cs b/source/TextBlade.Core/Commands/QuitGameCommand.cs index e078962..c799273 100644 --- a/source/TextBlade.Core/Commands/QuitGameCommand.cs +++ b/source/TextBlade.Core/Commands/QuitGameCommand.cs @@ -13,11 +13,11 @@ public IEnumerable Execute(IGame game, List party) if (input.KeyChar != 'y' && input.KeyChar != 'Y') { Console.WriteLine("Cancelling ..."); - return new string[0]; + return Array.Empty(); } Console.WriteLine("Bye!"); Environment.Exit(0); - return null; // Makes compiler go brrrr + return null; // Unreachable code. Makes compiler go brrrr. } }