public void Method_with_generic_param() { var target = new Interpreter(); var x = new MyTestService(); var y = "davide"; double z = 5; int? w = null; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), new Parameter("z", z.GetType(), z), new Parameter("w", typeof(int?), w) }; Assert.AreEqual(x.MethodWithGenericParam(x), target.Eval("x.MethodWithGenericParam(x)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(y), target.Eval("x.MethodWithGenericParam(y)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(z), target.Eval("x.MethodWithGenericParam(z)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(w), target.Eval("x.MethodWithGenericParam(w)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(y, x), target.Eval("x.MethodWithGenericParam(y, x)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(y, y), target.Eval("x.MethodWithGenericParam(y, y)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(y, z), target.Eval("x.MethodWithGenericParam(y, z)", parameters)); Assert.AreEqual(x.MethodWithGenericParam(y, w), target.Eval("x.MethodWithGenericParam(y, w)", parameters)); }
public void Null_conditional_property() { var target = new Interpreter().SetVariable("x", null, typeof(MyTestService)); MyTestService x = null; Assert.IsNull(target.Eval("x?.AProperty")); }
public void Indexer_Setter_MultiDimensional() { var target = new Interpreter(); var x = new[, ] { { 11, 12, 13, 14 }, { 21, 22, 23, 24 }, { 31, 32, 33, 34 } }; target.SetVariable("x", x); var y = new MyTestService(); target.SetVariable("y", y); var span = TimeSpan.FromDays(3.5); target.SetVariable("span", span); target.Eval("x[1, 2] = 7"); Assert.AreEqual(x, new[, ] { { 11, 12, 13, 14 }, { 21, 22, 7, 24 }, { 31, 32, 33, 34 } }); target.Eval("y[y.Today, 2] = span"); Assert.AreEqual(y.AField, y.Today.AddDays(2).Add(span)); target.Eval("y[y.Today] = span"); Assert.AreEqual(y.AField, y.Today.AddDays(3).Add(span)); }
public void Overload_paramsArray_methods_with_compatible_type_params() { var target = new Interpreter(); var x = new MyTestService(); target.SetVariable("x", x); Assert.AreEqual(3, target.Eval("x.OverloadMethodWithParamsArray(2, 3, 1)")); }
static int Main () { MyTestService svc = new MyTestService (); if (svc.SayHello ("Mono") != "Hello Mono") return 1; return 0; }
public void ToString_Method_on_a_custom_type() { var service = new MyTestService(); var target = new Interpreter() .SetVariable("service", service); Assert.AreEqual("DynamicExpresso.UnitTest.MemberInvocationTest+MyTestService", target.Eval("service.ToString()")); }
public void GetType_Method_on_a_custom_type() { var service = new MyTestService(); var target = new Interpreter() .SetVariable("service", service); Assert.AreEqual(typeof(MyTestService), target.Eval("service.GetType()")); }
public void Static_Properties_And_Methods_Of_Custom_Types() { var target = new Interpreter() .Reference(typeof(Uri)) .Reference(typeof(MyTestService)); Assert.AreEqual(Uri.UriSchemeHttp, target.Eval("Uri.UriSchemeHttp")); Assert.AreEqual(MyTestService.MyStaticMethod(), target.Eval("MyTestService.MyStaticMethod()")); }
public void Chaining_Methods() { var x = new MyTestService(); var target = new Interpreter(); target.SetVariable("x", x); Assert.AreEqual(x.HelloWorld().ToUpper(), target.Eval("x.HelloWorld().ToUpper()")); }
public void Method_Property_Field_basic_test() { var x = new MyTestService(); var target = new Interpreter().SetVariable("x", x); Assert.AreEqual(x.HelloWorld(), target.Eval("x.HelloWorld()")); Assert.AreEqual(x.AProperty, target.Eval("x.AProperty")); Assert.AreEqual(x.AField, target.Eval("x.AField")); }
static int Main () { MyTestService svc = new MyTestService (); DateTime date = svc.SayHello ("Mono"); if (date != DateTime.MinValue) { Console.WriteLine (date.ToLongDateString ()); return 1; } return 0; }
public void ServicePublishTwoMessage() { var service = new MyTestService(MessageBus); Start(); service.DoStuffAndPublishMessage(2); MessageBroker.Published.Should().Contain(m => m.Topic == "MyPublishTopic"); MessageBroker.Published.Count().Should().Be(2); }
public void Void_Method() { var service = new MyTestService(); var target = new Interpreter() .SetVariable("service", service); Assert.AreEqual(0, service.VoidMethodCalls); target.Eval("service.VoidMethod()"); Assert.AreEqual(1, service.VoidMethodCalls); Assert.AreEqual(typeof(void), target.Parse("service.VoidMethod()").ReturnType); }
public void Should_Understand_ReturnType_Of_expressions() { var target = new Interpreter(); var x = new MyTestService(); var y = 5; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), }; Assert.AreEqual(typeof(bool), target.Parse("x.AProperty > y && x.HelloWorld().Length == 10", parameters).ReturnType); Assert.AreEqual(typeof(int), target.Parse("x.AProperty * (4 + 65) / x.AProperty", parameters).ReturnType); }
public void RequestResponseFromService() { var service = new MyTestService(MessageBus); MessageBus.RequestHandler <MyRequestDto, MyResponseDto>(MyTestServiceConfig.MyTestRequest, service.MyTestRequestHandler); Start(); var response = MessageBroker.Request <MyRequestDto, MyResponseDto>(MyTestServiceConfig.MyTestRequest, new MyRequestDto { Name = "MyRequest", Count = 1 }); response.Name.Should().Be("MyRequest"); response.Count.Should().Be(1); }
public void RegisterTwoSubscriberToSameTopicAndSendOneMessage() { var service = new MyTestService(MessageBus); MessageBus.SubscribeHandler <MySubscribeDto>(MyTestServiceConfig.MyTestSubscribe, service.MyTestSubscribeHandler); MessageBus.SubscribeHandler <MySubscribeDto>(MyTestServiceConfig.MyTestSubscribe, service.MyTestSubscribeHandler); Start(); MessageBroker.Publish(MyTestServiceConfig.MyTestSubscribe, new MySubscribeDto { Name = "A" }); MessageBroker.Published.Count().Should().Be(2); }
public void Indexer() { var target = new Interpreter(); var x = "ciao"; target.SetVariable("x", x); var y = new MyTestService(); target.SetVariable("y", y); Assert.AreEqual(x[2], target.Eval("x[2]")); Assert.AreEqual(y[2], target.Eval("y[2]")); Assert.AreEqual(y[2].ToString(), target.Eval("y[2].ToString()")); }
public void RegisterOneSubscriberAndSendOneMessageThatFailsTheSubscriber() { var service = new MyTestService(MessageBus); MessageBus.SubscribeHandler <MySubscribeDto>(MyTestServiceConfig.MyTestSubscribe, service.MyTestSubscribeHandler); Start(); MessageBroker.Publish(MyTestServiceConfig.MyTestSubscribe, new MySubscribeDto { Name = "Exception" }); MessageBroker.Published.Count().Should().Be(0); MessageBroker.SubscriberHandlerFailed.Count().Should().Be(1); MessageBroker.SubscriberHandlerCompleted.Count().Should().Be(0); }
public void ParamsArray_methods_are_not_called_when_there_is_an_exact_method_match() { var target = new Interpreter(); var x = new MyTestService(); target.SetVariable("x", x); target.Eval("x.AmbiguousMethod(DateTime.Now, 2, 3)"); Assert.AreEqual(1, x.AmbiguousMethod_NormalCalls); Assert.AreEqual(0, x.AmbiguousMethod_ParamsArrayCalls); target.Eval("x.AmbiguousMethod(DateTime.Now, 2, 3, 4)"); Assert.AreEqual(1, x.AmbiguousMethod_NormalCalls); Assert.AreEqual(1, x.AmbiguousMethod_ParamsArrayCalls); }
public void Methods_Fields_And_Properties_By_Default_Are_Case_Sensitive() { var target = new Interpreter(); var x = new MyTestService(); var parameters = new[] { new Parameter("x", x.GetType(), x) }; Assert.AreEqual(x.HelloWorld(), target.Eval("x.HelloWorld()", parameters)); Assert.AreEqual(x.HELLOWORLD(), target.Eval("x.HELLOWORLD()", parameters)); Assert.AreEqual(x.AProperty, target.Eval("x.AProperty", parameters)); Assert.AreEqual(x.APROPERTY, target.Eval("x.APROPERTY", parameters)); Assert.AreEqual(x.AField, target.Eval("x.AField", parameters)); Assert.AreEqual(x.AFIELD, target.Eval("x.AFIELD", parameters)); }
public void Method_with_optional_null_param() { var target = new Interpreter(); var x = new MyTestService(); var y = "1"; var z = "2"; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), new Parameter("z", z.GetType(), z), }; Assert.AreEqual(x.MethodWithOptionalNullParam(y), target.Eval("x.MethodWithOptionalNullParam(y)", parameters)); Assert.AreEqual(x.MethodWithOptionalNullParam(y, z), target.Eval("x.MethodWithOptionalNullParam(y, z)", parameters)); }
public void Complex_expression() { var target = new Interpreter(); var x = new MyTestService(); var y = 5; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), }; Assert.AreEqual(x.AProperty > y && x.HelloWorld().Length == 10, target.Eval("x.AProperty >\t y && \r\n x.HelloWorld().Length == 10", parameters)); Assert.AreEqual(x.AProperty * (4 + 65) / x.AProperty, target.Eval("x.AProperty * (4 + 65) / x.AProperty", parameters)); Assert.AreEqual(Convert.ToString(x.AProperty * (4 + 65) / x.AProperty), target.Eval("Convert.ToString(x.AProperty * (4 + 65) / x.AProperty)", parameters)); }
public void Complex_parameters() { var target = new Interpreter(); var x = new MyTestService(); var y = new Uri("http://www.google.com"); var z = CultureInfo.GetCultureInfo("en-US"); var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), new Parameter("z", z.GetType(), z) }; Assert.AreEqual(x, target.Eval("x", parameters)); Assert.AreEqual(y, target.Eval("y", parameters)); Assert.AreEqual(z, target.Eval("z", parameters)); }
public void Indexer_Getter_MultiDimensional() { var target = new Interpreter(); var x = new[, ] { { 11, 12, 13, 14 }, { 21, 22, 23, 24 }, { 31, 32, 33, 34 } }; target.SetVariable("x", x); var y = new MyTestService(); target.SetVariable("y", y); Assert.AreEqual(x[1, 2], target.Eval("x[1, 2]")); Assert.AreEqual(y[y.Today, 2], target.Eval("y[y.Today, 2]")); Assert.AreEqual(y[y.Today], target.Eval("y[y.Today]")); }
public void Methods_Fields_and_Properties_On_Parameters() { var target = new Interpreter(); var x = new MyTestService(); var y = "davide"; var z = 5; var w = DateTime.Today; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), new Parameter("z", z.GetType(), z), new Parameter("w", w.GetType(), w) }; Assert.AreEqual(x.HelloWorld(), target.Eval("x.HelloWorld()", parameters)); Assert.AreEqual(x.CallMethod(y, z, w), target.Eval("x.CallMethod( y, z,w)", parameters)); Assert.AreEqual(x.AProperty + 1, target.Eval("x.AProperty + 1", parameters)); Assert.AreEqual(x.AField, target.Eval("x.AField", parameters)); }
public void Method_with_params_array() { var target = new Interpreter(); var x = new MyTestService(); target.SetVariable("x", x); Assert.AreEqual(0, x.MethodWithParamsArrayCalls); Assert.AreEqual(x.MethodWithParamsArray(DateTime.Now, 2, 1, 34), target.Eval("x.MethodWithParamsArray(DateTime.Now, 2, 1, 34)")); Assert.AreEqual(2, x.MethodWithParamsArrayCalls); var myParamArray = new int[] { 2, 1, 34 }; target.SetVariable("myParamArray", myParamArray); Assert.AreEqual(x.MethodWithParamsArray(DateTime.Now, myParamArray), target.Eval("x.MethodWithParamsArray(DateTime.Now, myParamArray)")); Assert.AreEqual(4, x.MethodWithParamsArrayCalls); }
public void Method_with_nullable_param() { var target = new Interpreter(); var x = new MyTestService(); var y = "davide"; var z = 5; int?w = null; var parameters = new[] { new Parameter("x", x.GetType(), x), new Parameter("y", y.GetType(), y), new Parameter("z", z.GetType(), z), new Parameter("w", typeof(int?), w) }; Assert.AreEqual(x.MethodWithNullableParam(y, z), target.Eval("x.MethodWithNullableParam(y, z)", parameters)); Assert.AreEqual(x.MethodWithNullableParam(y, w), target.Eval("x.MethodWithNullableParam(y, w)", parameters)); Assert.AreEqual(x.MethodWithNullableParam(y, 30), target.Eval("x.MethodWithNullableParam(y, 30)", parameters)); Assert.AreEqual(x.MethodWithNullableParam(y, null), target.Eval("x.MethodWithNullableParam(y, null)", parameters)); }
public void ServiceCallRequestAsyncGetMockResponse() { var service = new MyTestService(MessageBus); MessageBroker.MockResponse <MyRequestDto, MyResponseDto>(MyTestServiceConfig.MyTestRequest, r => r.Name == "YourName", new MyResponseDto { Name = "MyResponse1" }); MessageBroker.MockResponse <MyRequestDto, MyResponseDto>(MyTestServiceConfig.MyTestRequest, r => r.Name == "MyName", new MyResponseDto { Name = "MyResponse2" }); MessageBroker.MockResponse(MyTestServiceConfig.MyTestRequest, new MyResponseDto { Name = "MyResponse3" }); Start(); service.DoStuffAndRequestAsyncData(); service.Name.Should().Be("MyResponse2"); }
public void Indexer_Getter() { var target = new Interpreter(); var x = "ciao"; target.SetVariable("x", x); var y = new MyTestService(); target.SetVariable("y", y); var z = new[] { 7, 8, 9, 10 }; target.SetVariable("z", z); Assert.AreEqual(x[2], target.Eval("x[2]")); Assert.AreEqual(y[2], target.Eval("y[2]")); Assert.AreEqual(y[2].ToString(), target.Eval("y[2].ToString()")); Assert.AreEqual(y[(short)2], target.Eval("y[(Int16)2]")); Assert.AreEqual(z[2], target.Eval("z[2]")); }
public void Indexer_Setter() { var target = new Interpreter(); var x = new System.Text.StringBuilder("time"); target.SetVariable("x", x); var y = new MyTestService(); target.SetVariable("y", y); var z = new[] { 7, 8, 9, 10 }; target.SetVariable("z", z); target.Eval("x[2] = 'r'"); Assert.AreEqual(x.ToString(), "tire"); target.Eval("y[(Int16)9] = y.Today"); Assert.AreEqual(y.AField, y.Today.AddYears(9)); target.Eval("y[(Int64)7] = y.Today"); Assert.AreEqual(y.AField, y.Today.AddSeconds(7)); target.Eval("z[2] = 4"); Assert.AreEqual(z, new[] { 7, 8, 4, 10 }); }
public void Parse_An_Expression_And_Invoke_It_With_Different_Parameters() { var service = new MyTestService(); var target = new Interpreter() .SetVariable("service", service); var func = target.Parse("x > 4 ? service.VoidMethod() : service.VoidMethod2()", new Parameter("x", typeof(int))); Assert.AreEqual(typeof(void), func.ReturnType); Assert.AreEqual(0, service.VoidMethodCalled); Assert.AreEqual(0, service.VoidMethod2Called); func.Invoke(new Parameter("x", 5)); Assert.AreEqual(1, service.VoidMethodCalled); Assert.AreEqual(0, service.VoidMethod2Called); func.Invoke(new Parameter("x", 2)); Assert.AreEqual(1, service.VoidMethodCalled); Assert.AreEqual(1, service.VoidMethod2Called); }