mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 03:58:13 +08:00
- Finishing up mapping of events to handlers.
- A few test cases for edge conditions around parameter mapping. --HG-- branch : dev
This commit is contained in:
@@ -51,6 +51,92 @@ namespace Orchard.Tests.Events {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(-2600));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod() {
|
||||
Assert.That(_eventHandler.Summary, Is.Null);
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = "a";
|
||||
arguments["b"] = "b";
|
||||
arguments["c"] = "c";
|
||||
_eventBus.Notify("ITestEventHandler.Concat", arguments);
|
||||
Assert.That(_eventHandler.Summary, Is.EqualTo("abc"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod2() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = 1000;
|
||||
arguments["b"] = 100;
|
||||
arguments["c"] = 10;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(1110));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod4() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = 1000;
|
||||
arguments["b"] = 100;
|
||||
arguments["c"] = 10;
|
||||
arguments["e"] = 1;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(1110));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod5() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = 1000;
|
||||
arguments["b"] = 100;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(2200));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod6() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = 1000;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(3000));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod7() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["a"] = 1000;
|
||||
arguments["e"] = 1;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(3000));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod8() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
arguments["e"] = 1;
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventParametersAreCorrectlyPassedToMatchingMethod9() {
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
_eventBus.Notify("ITestEventHandler.Sum", arguments);
|
||||
Assert.That(_eventHandler.Result, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventHandlerWontThrowIfMethodDoesNotExists() {
|
||||
Dictionary<string, object> arguments = new Dictionary<string, object>();
|
||||
Assert.DoesNotThrow(() => _eventBus.Notify("ITestEventHandler.NotExisting", arguments));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventBusThrowsIfMessageNameIsNotCorrectlyFormatted() {
|
||||
Assert.Throws<ArgumentException>(() => _eventBus.Notify("StubEventHandlerIncrement", new Dictionary<string, object>()));
|
||||
@@ -58,20 +144,41 @@ namespace Orchard.Tests.Events {
|
||||
|
||||
public interface ITestEventHandler : IEventHandler {
|
||||
void Increment();
|
||||
void Sum(int a);
|
||||
void Sum(int a, int b);
|
||||
void Sum(int a, int b, int c);
|
||||
void Substract(int a, int b);
|
||||
void Concat(string a, string b, string c);
|
||||
}
|
||||
|
||||
public class StubEventHandler : ITestEventHandler {
|
||||
public int Count { get; set; }
|
||||
public int Result { get; set; }
|
||||
public string Summary { get; set; }
|
||||
|
||||
public void Increment() {
|
||||
Count++;
|
||||
}
|
||||
|
||||
public void Sum(int a) {
|
||||
Result = 3 * a;
|
||||
}
|
||||
|
||||
public void Sum(int a, int b) {
|
||||
Result = 2 * ( a + b );
|
||||
}
|
||||
|
||||
public void Sum(int a, int b, int c) {
|
||||
Result = a + b + c;
|
||||
}
|
||||
|
||||
public void Substract(int a, int b) {
|
||||
Result = a - b;
|
||||
}
|
||||
|
||||
public void Concat(string a, string b, string c) {
|
||||
Summary = a + b + c;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
|
||||
@@ -48,21 +50,40 @@ namespace Orchard.Events {
|
||||
}
|
||||
|
||||
private static void TryInvokeMethod(IEventHandler eventHandler, string methodName, IDictionary<string, object> arguments) {
|
||||
foreach (var method in eventHandler.GetType().GetMethods()) {
|
||||
if (String.Equals(method.Name, methodName)) {
|
||||
List<object> parameters = new List<object>();
|
||||
foreach (var methodParameter in method.GetParameters()) {
|
||||
if (arguments.ContainsKey(methodParameter.Name)) {
|
||||
parameters.Add(arguments[methodParameter.Name]);
|
||||
}
|
||||
else {
|
||||
parameters.Add(new object());
|
||||
MethodInfo method = GetMatchingMethod(eventHandler, methodName, arguments);
|
||||
if (method != null) {
|
||||
List<object> parameters = new List<object>();
|
||||
foreach (var methodParameter in method.GetParameters()) {
|
||||
parameters.Add(arguments[methodParameter.Name]);
|
||||
}
|
||||
method.Invoke(eventHandler, parameters.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static MethodInfo GetMatchingMethod(IEventHandler eventHandler, string methodName, IDictionary<string, object> arguments) {
|
||||
List<MethodInfo> allMethods = new List<MethodInfo>(eventHandler.GetType().GetMethods());
|
||||
List<MethodInfo> candidates = new List<MethodInfo>(allMethods);
|
||||
|
||||
foreach (var method in allMethods) {
|
||||
if (String.Equals(method.Name, methodName, StringComparison.OrdinalIgnoreCase)) {
|
||||
ParameterInfo[] parameterInfos = method.GetParameters();
|
||||
foreach (var parameter in parameterInfos) {
|
||||
if (!arguments.ContainsKey(parameter.Name)) {
|
||||
candidates.Remove(method);
|
||||
break;
|
||||
}
|
||||
}
|
||||
method.Invoke(eventHandler, parameters.ToArray());
|
||||
break;
|
||||
}
|
||||
else {
|
||||
candidates.Remove(method);
|
||||
}
|
||||
}
|
||||
|
||||
if (candidates.Count != 0) {
|
||||
return candidates.OrderBy(x => x.GetParameters().Length).Last();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user