- Orchard Command Handler argument support.

- support for string and params string[] arguments.
- Unit tests for boundary cases.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-04-06 12:36:41 -07:00
parent 7e36d8bccb
commit 7397b7dea3
3 changed files with 149 additions and 8 deletions

View File

@@ -64,12 +64,92 @@ namespace Orchard.Tests.Commands {
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
[Test]
public void TestCommandThatDoesNotReturnAValue() {
CommandContext commandContext = new CommandContext { Command = "Log" };
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.Null);
}
[Test]
public void TestNotExistingSwitch() {
CommandContext commandContext = new CommandContext { Command = "Foo", Switches = new NameValueCollection() };
commandContext.Switches.Add("ThisSwitchDoesNotExist", "Insignificant");
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
[Test]
public void TestCommandArgumentsArePassedCorrectly() {
CommandContext commandContext = new CommandContext {
Command = "Concat",
Switches = new NameValueCollection(),
Arguments = new[] {"left to ", "right"}
};
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.EqualTo("left to right"));
}
[Test]
public void TestCommandArgumentsArePassedCorrectlyWithAParamsParameters() {
CommandContext commandContext = new CommandContext {
Command = "ConcatParams",
Switches = new NameValueCollection(),
Arguments = new[] {"left to ", "right"}
};
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.EqualTo("left to right"));
}
[Test]
public void TestCommandArgumentsArePassedCorrectlyWithAParamsParameterAndNoArguments() {
CommandContext commandContext = new CommandContext {
Command = "ConcatParams",
Switches = new NameValueCollection()
};
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.EqualTo(""));
}
[Test]
public void TestCommandArgumentsArePassedCorrectlyWithNormalParametersAndAParamsParameters() {
CommandContext commandContext = new CommandContext {
Command = "ConcatAllParams",
Switches = new NameValueCollection(),
Arguments = new[] { "left-", "center-", "right" }
};
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.EqualTo("left-center-right"));
}
[Test]
public void TestCommandParamsMismatchWithoutParamsNotEnoughArguments() {
CommandContext commandContext = new CommandContext {
Command = "Concat",
Switches = new NameValueCollection(),
Arguments = new[] { "left to "}
};
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
[Test]
public void TestCommandParamsMismatchWithoutParamsTooManyArguments() {
CommandContext commandContext = new CommandContext {
Command = "Foo",
Switches = new NameValueCollection(),
Arguments = new[] { "left to " }
};
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
[Test]
public void TestCommandParamsMismatchWithParamsButNotEnoughArguments() {
CommandContext commandContext = new CommandContext {
Command = "ConcatAllParams",
Switches = new NameValueCollection(),
};
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
}
public class StubCommandHandler : DefaultOrchardCommandHandler {
@@ -109,5 +189,29 @@ namespace Orchard.Tests.Commands {
return trace;
}
public string Concat(string left, string right) {
return left + right;
}
public string ConcatParams(params string[] parameters) {
string concatenated = "";
foreach (var s in parameters) {
concatenated += s;
}
return concatenated;
}
public string ConcatAllParams(string leftmost, params string[] rest) {
string concatenated = leftmost;
foreach (var s in rest) {
concatenated += s;
}
return concatenated;
}
public void Log() {
return;
}
}
}

View File

@@ -1,12 +1,11 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.Specialized;
namespace Orchard.Commands {
public class CommandContext {
public string Input { get; set; }
public string Output { get; set; }
public string Command { get; set; }
public IEnumerable<string> Arguments { get; set; }
public string[] Arguments { get; set; }
public NameValueCollection Switches { get; set; }
}
}

View File

@@ -48,20 +48,58 @@ namespace Orchard.Commands {
foreach (MethodInfo methodInfo in GetType().GetMethods()) {
if (String.Equals(methodInfo.Name, context.Command, StringComparison.OrdinalIgnoreCase)) {
CheckMethodForSwitches(methodInfo, context.Switches);
context.Output = (string)methodInfo.Invoke(this, null);
return;
object[] invokeParameters = GetInvokeParametersForMethod(methodInfo, context.Arguments ?? new string[]{});
if (invokeParameters != null) {
context.Output = (string) methodInfo.Invoke(this, invokeParameters);
return;
}
}
foreach (OrchardCommandAttribute commandAttribute in methodInfo.GetCustomAttributes(typeof(OrchardCommandAttribute), false)) {
if (String.Equals(commandAttribute.Command, context.Command, StringComparison.OrdinalIgnoreCase)) {
CheckMethodForSwitches(methodInfo, context.Switches);
context.Output = (string)methodInfo.Invoke(this, null);
return;
object[] invokeParameters = GetInvokeParametersForMethod(methodInfo, context.Arguments ?? new string[]{});
if (invokeParameters != null) {
context.Output = (string) methodInfo.Invoke(this, invokeParameters);
return;
}
}
}
}
throw new InvalidOperationException(T("Command : ") + context.Command + T(" was not found"));
throw new InvalidOperationException(T("Command : ") + context.Command + T(" was not found "));
}
private static object[] GetInvokeParametersForMethod(MethodInfo methodInfo, string[] arguments) {
List<object> invokeParameters = new List<object>();
List<string> args = new List<string>(arguments);
ParameterInfo[] methodParameters = methodInfo.GetParameters();
bool methodHasParams = false;
if (methodParameters.Length == 0) {
if (args.Count == 0) return invokeParameters.ToArray();
return null;
}
if (methodParameters[methodParameters.Length - 1].ParameterType.IsAssignableFrom(typeof(string[]))) {
methodHasParams = true;
}
if (!methodHasParams && args.Count != methodParameters.Length) return null;
if (methodHasParams && (methodParameters.Length - args.Count >= 2)) return null;
for (int i = 0; i < args.Count; i++) {
if (methodParameters[i].ParameterType.IsAssignableFrom(typeof(string[]))) {
invokeParameters.Add(args.GetRange(i, args.Count - i).ToArray());
break;
}
invokeParameters.Add(arguments[i]);
}
if (methodHasParams && (methodParameters.Length - args.Count == 1)) invokeParameters.Add(new string[] { });
return invokeParameters.ToArray();
}
private void CheckMethodForSwitches(MethodInfo methodInfo, NameValueCollection switches) {