public void CanInvokeVoidMethod() { string input = "Hello"; dynamic dynoAgent = _serviceAgent; Action <ActionCallInfo> assertCallInfo = info => { Assert.IsNotNull(info); Assert.AreEqual("VoidMethod", info.MethodName); Assert.IsNull(info.ReturnType); Assert.AreEqual(1, info.UnnamedArguments.Count); Assert.AreEqual(input, info.UnnamedArguments.First()); }; _listenerMock.Setup(x => x.CanHandle(It.IsAny <ActionCallInfo>())) .Callback(assertCallInfo) .Returns(true); _listenerMock.Setup(x => x.Handle(It.IsAny <ActionListenerContext>())) .Callback <ActionListenerContext>(context => { ActionCallInfo info = context.CallInfo; assertCallInfo(info); }) .Returns(true); dynoAgent.VoidMethod(input); _listenerMock.VerifyAll(); }
public void CanInvokeFuncMethod() { string input = "Testing func"; string output = "This is output"; dynamic dynoAgent = _serviceAgent; Action <ActionCallInfo> assertCallInfo = info => { Assert.IsNotNull(info); Assert.AreEqual("FuncMethod", info.MethodName); Assert.AreEqual(typeof(string), info.ReturnType); Assert.AreEqual(1, info.UnnamedArguments.Count); Assert.AreEqual(input, info.UnnamedArguments.First()); }; _listenerMock.Setup(x => x.CanHandle(It.IsAny <ActionCallInfo>())) .Callback(assertCallInfo) .Returns(true); _listenerMock.Setup(x => x.Handle(It.IsAny <ActionListenerContext>())) .Callback <ActionListenerContext>(context => { ActionCallInfo info = context.CallInfo; assertCallInfo(info); context.Result = output; }) .Returns(true); string result = dynoAgent.FuncMethod(input); Assert.AreEqual(output, result); _listenerMock.VerifyAll(); }
public object ExecuteListeners(ActionCallInfo callInfo) { object[] actionListeners = _resolvingService.ResolveAll(typeof(IActionListener)); IEnumerable <IActionListener> handlers = actionListeners.Cast <IActionListener>().Where(h => h.CanHandle(callInfo)); var context = new ActionListenerContext(callInfo); bool wasHandled = handlers.Any(actionListener => actionListener.Handle(context)); if (!wasHandled) { throw new InvalidOperationException("No handler for call"); } return(context.Result); }
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { var callInfo = new ActionCallInfo { NamedArguments = binder.CallInfo .ArgumentNames .Reverse() .Zip(args.Reverse(), (x, y) => new Tuple <string, object>(x, y)) .ToList(), UnnamedArguments = args.Take(binder.CallInfo.ArgumentCount - binder.CallInfo.ArgumentNames.Count).ToList(), MethodName = binder.Name }; result = binder.ResultDiscarded() ? ExecuteListeners(callInfo) : new ServiceAgent(_resolvingService) { CallInfo = callInfo }; return(true); }
public bool CanHandle(ActionCallInfo callInfo) { return(callInfo.NamedArguments.Any(x => x.Item1.Equals("log", StringComparison.OrdinalIgnoreCase))); }
public ActionListenerContext(ActionCallInfo callInfo) { CallInfo = callInfo; }
public bool CanHandle(ActionCallInfo callInfo) { return(callInfo.UnnamedArguments.Any(x => x is string)); }
public bool CanHandle(ActionCallInfo callInfo) { return(true); }