2020-02-29 09:31:13 +00:00
|
|
|
using System;
|
|
|
|
using RecrownedGTK.Tools.CommandProcessor;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace RecrownedGTK.Tools.Tests.CommandProcessor
|
|
|
|
{
|
|
|
|
public class CommandEngineTest
|
|
|
|
{
|
|
|
|
CommandEngine commandEngine;
|
|
|
|
DummyInput dummyInput;
|
|
|
|
DummyOutput dummyOutput;
|
|
|
|
DummyCommand[] dummyCommands;
|
|
|
|
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, string help, params EngineCommandArgument[] engineCommandArguments) : base(help, invoker) {
|
|
|
|
this.commandArguments = engineCommandArguments;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:44:25 +00:00
|
|
|
public override bool Run(IUserInput userInput, IUserOutput userOutput, params string[] arguments)
|
2020-02-29 09:31:13 +00:00
|
|
|
{
|
|
|
|
if (arguments != null && arguments[0] != null) {
|
|
|
|
userOutput.Output(arguments[0] + " argument");
|
|
|
|
} else {
|
|
|
|
userOutput.Output(userInput.GetInput() + " input");
|
|
|
|
}
|
2020-05-18 21:44:25 +00:00
|
|
|
return true;
|
2020-02-29 09:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
commandEngine = new CommandEngine();
|
|
|
|
dummyInput = new DummyInput();
|
|
|
|
dummyOutput = new DummyOutput();
|
|
|
|
commandEngine.commands.AddRange(this.dummyCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
[OneTimeSetUp]
|
|
|
|
public void InitialSetup() {
|
|
|
|
dummyCommands = new DummyCommand[] {
|
|
|
|
new DummyCommand("pineapple", "pineapple helped."),
|
|
|
|
new DummyCommand("pear", "pear helped."),
|
|
|
|
new DummyCommand("apple", "apple helped.")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ProcessTestNull()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
commandEngine.Process(dummyInput, dummyOutput, null);
|
|
|
|
} catch (ArgumentNullException) {
|
|
|
|
Assert.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert.Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ProcessTestNonExistingCommand() {
|
|
|
|
try {
|
|
|
|
commandEngine.Process(dummyInput, dummyOutput, "invalid");
|
|
|
|
} catch (ArgumentException) {
|
|
|
|
Assert.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert.Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase("pear")]
|
|
|
|
[TestCase("pineapple")]
|
|
|
|
[TestCase("apple")]
|
|
|
|
public void ProcessTestNormalCommand(string command) {
|
|
|
|
dummyInput.dummyInput = command;
|
|
|
|
commandEngine.Process(dummyInput, dummyOutput, command);
|
|
|
|
Assert.AreEqual(dummyInput.dummyInput + " input", dummyOutput.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ProcessTestCommandWithArgument() {
|
|
|
|
EngineCommandArgument engineCommandArgument = new EngineCommandArgument("argument", "test help", false);
|
|
|
|
DummyCommand dummyCommand = new DummyCommand("test", "helped.", engineCommandArgument);
|
|
|
|
dummyInput.dummyInput = "test";
|
|
|
|
commandEngine.commands.Add(dummyCommand);
|
|
|
|
|
|
|
|
commandEngine.Process(dummyInput, dummyOutput, "test argument");
|
|
|
|
|
|
|
|
Assert.AreEqual("argument argument", dummyOutput.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase("pear", true)]
|
|
|
|
[TestCase("pineapple", true)]
|
|
|
|
[TestCase("apple", true)]
|
|
|
|
[TestCase("potato", false)]
|
|
|
|
[TestCase("tomato", false)]
|
|
|
|
[TestCase("banana", false)]
|
|
|
|
public void ContainsCommandTest(string command, bool expected) {
|
|
|
|
Assert.AreEqual(expected, commandEngine.ContainsCommand(command));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetCommandTest() {
|
|
|
|
EngineCommandArgument engineCommandArgument = new EngineCommandArgument("argument", "test help", false);
|
|
|
|
DummyCommand dummyCommand = new DummyCommand("test", "helped.", engineCommandArgument);
|
|
|
|
commandEngine.commands.Add(dummyCommand);
|
|
|
|
|
|
|
|
Assert.AreEqual(dummyCommand, commandEngine.GetCommand("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetCommandTestDoesNotExist() {
|
|
|
|
EngineCommandArgument engineCommandArgument = new EngineCommandArgument("argument", "test help", false);
|
|
|
|
|
|
|
|
Assert.IsNull(commandEngine.GetCommand("test"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|