From cea1d98bc728a93318c5c3a96d58d465ccc11f85 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 29 Feb 2020 04:31:23 -0500 Subject: [PATCH] Tests for the engine commands. --- .../CommandProcessor/EngineCommandTest.cs | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 RecrownedGTK.Tools.Tests/CommandProcessor/EngineCommandTest.cs diff --git a/RecrownedGTK.Tools.Tests/CommandProcessor/EngineCommandTest.cs b/RecrownedGTK.Tools.Tests/CommandProcessor/EngineCommandTest.cs new file mode 100644 index 0000000..e02176b --- /dev/null +++ b/RecrownedGTK.Tools.Tests/CommandProcessor/EngineCommandTest.cs @@ -0,0 +1,145 @@ +using System; +using RecrownedGTK.Tools.CommandProcessor; +using NUnit.Framework; + +namespace RecrownedGTK.Tools.Tests.CommandProcessor { + public class EngineCommandTest { + string[] fakeArguments; + DummyInput dummyInput; + DummyOutput dummyOutput; + DummyCommand dummyCommand; + EngineCommandArgument dummyCommandArgument; + private class DummyInput : IUserInput { + public string dummyInput; + public string GetInput() { + return dummyInput; + } + } + + private class DummyOutput : IUserOutput + { + public bool cleared; + public string output; + public string wrappedOutput; + public void ClearOutput() + { + cleared = true; + } + + public void Output(string output) + { + this.output = output; + } + + public void WrappedOutput(string output) + { + this.wrappedOutput = output; + } + } + + private class DummyCommand : EngineCommand + { + public DummyCommand(string invoker, params EngineCommandArgument[] engineCommandArguments) : base("helped", invoker) { + this.commandArguments = engineCommandArguments; + } + + public override void Run(IUserInput userInput, IUserOutput userOutput, params string[] arguments) + { + if (arguments != null && arguments[0] != null) { + userOutput.Output(arguments[0] + " argument"); + } else { + userOutput.Output(userInput.GetInput() + " input"); + } + + + } + } + + [SetUp] + public void Setup() + { + dummyInput = new DummyInput(); + dummyOutput = new DummyOutput(); + dummyCommandArgument = new EngineCommandArgument("arg", "arg help", false); + dummyCommand = new DummyCommand("invoke", dummyCommandArgument); + } + + [OneTimeSetUp] + public void InitialSetup() { + fakeArguments = new string[] {"fake1", "fake2", "fake3", "fake4", "fake5", "fake6", "arg"}; + } + + [Test] + public void RunTestNoArguments() { + dummyInput.dummyInput = "input"; + dummyCommand.Run(dummyInput, dummyOutput, null); + Assert.AreEqual(dummyOutput.output, "input input"); + } + + [Test] + public void RunTestWithArguments() { + dummyCommand.Run(dummyInput, dummyOutput, "args"); + Assert.AreEqual(dummyOutput.output, "args argument"); + } + + public void EngineCommandArgumentOfTestValid() { + Assert.AreEqual(dummyCommandArgument, dummyCommand.EngineCommandArgumentOf("args")); + } + + public void EngineCommandArgumentOfTestNull() { + Assert.IsNull(dummyCommand.EngineCommandArgumentOf("nothing here")); + } + + [Test] + public void ValidateTestFalse() { + Assert.IsFalse(dummyCommand.Validate("notexisting")); + } + + [Test] + public void ValidateTestTrue() { + Assert.IsTrue(dummyCommand.Validate("arg")); + } + + [Test] + public void IndexOfArgumentTestInvald() { + try { + EngineCommand.IndexOfArgument("fake argument", new string[] {"argument", "argument_again"}); + } catch (ArgumentException) { + Assert.Pass(); + } + Assert.Fail(); + } + + [TestCase("fake1", 0)] + [TestCase("fake3", 2)] + [TestCase("fake6", 5)] + [TestCase("arg", 6)] + public void IndexOfArgumentTestValidWithString(string argument, int expected) { + Assert.AreEqual(EngineCommand.IndexOfArgument(argument, this.fakeArguments), expected); + } + + [Test] + public void IndexOfArgumentTestValidWithObject() { + Assert.AreEqual(6, EngineCommand.IndexOfArgument(dummyCommandArgument, fakeArguments)); + } + + [TestCase("fake1", true)] + [TestCase("fake3", true)] + [TestCase("arg", true)] + [TestCase("fake8", false)] + public void HasArgumentTest(string argument, bool expected) { + Assert.AreEqual(EngineCommand.HasArgument(argument, fakeArguments), expected); + } + + [Test] + public void HelpTestNoArgument() { + string expected = "helped\r\nPossible arguments: arg. (* are required arguments.)\r\n"; + Assert.AreEqual(expected, dummyCommand.Help()); + } + + [Test] + public void HelpTestArgument() { + Assert.AreEqual(dummyCommand.Help("arg"), "arg help"); + } + } +} \ No newline at end of file