2020-02-29 09:31:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:44:25 +00:00
|
|
|
public override bool Run(IUserInput userInput, IUserOutput userOutput, params string[] arguments)
|
2020-02-29 09:31:23 +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:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[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() {
|
2020-02-29 09:34:52 +00:00
|
|
|
string expected = "helped\nPossible arguments: arg. (* are required arguments.)\n";
|
|
|
|
Assert.AreEqual(expected, dummyCommand.Help().Replace("\r", ""));
|
2020-02-29 09:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void HelpTestArgument() {
|
|
|
|
Assert.AreEqual(dummyCommand.Help("arg"), "arg help");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|