public void ObjectMethodFunctionTest() { ExpressionParser parser = new ExpressionParser(); parser.RegisterVariable("a", typeof(A)); INumericExpression expr = parser.ParseNumericExpression("1 + a.g(2,\"foo\",true).Id"); Assert.NotNull(expr); Assert.Equal("1 a.g(2,\"foo\",true).Id +", expr.ToString()); A a = new A { Bs = new List <B> { new B { Id = 1 }, new B { Id = 2 }, new B { Id = 3 }, } }; Assert.Equal(1 + a.g(2, "foo", true).Id, expr.Evaluate(new Dictionary <string, object> { { "a", a } })); }
public void NumericExpressionTest(string expression, string expectedRpn, double expectedValue) { ExpressionParser parser = new ExpressionParser(); parser.RegisterAssembly(GetType().Assembly); INumericExpression expr = parser.ParseNumericExpression(expression); Assert.NotNull(expr); Assert.Equal(expectedRpn, expr.ToString()); Assert.Equal(expectedValue, expr.Evaluate()); }
public void NumericFunctionTest() { Func <double, double> func = n => Math.Cos(n); ExpressionParser parser = new ExpressionParser(); parser.RegisterFunction("cos", func); INumericExpression expr = parser.ParseNumericExpression("1 + cos(12)"); Assert.NotNull(expr); Assert.Equal("1 cos(12) +", expr.ToString()); Assert.Equal(1 + Math.Cos(12), expr.Evaluate()); }
public void NumericArrayVariableTest() { ExpressionParser parser = new ExpressionParser(); parser.RegisterVariable <int[]>("a"); int[] a = new[] { 1, 2 }; INumericExpression expr = parser.ParseNumericExpression("1 + a[1]"); Assert.NotNull(expr); Assert.Equal("1 a[1] +", expr.ToString()); Assert.Equal(1 + a[1], expr.Evaluate(new Dictionary <string, object> { { "a", a } })); }
public void NumericVariableTest() { ExpressionParser parser = new ExpressionParser(); parser.RegisterVariable <int>("a"); int a = 2; INumericExpression expr = parser.ParseNumericExpression("1 + a"); Assert.NotNull(expr); Assert.Equal("1 a +", expr.ToString()); Assert.Equal(1 + a, expr.Evaluate(new Dictionary <string, object> { { "a", a } })); }
public void EnumFuncParam() { ExpressionParser parser = new ExpressionParser(); parser.RegisterAssembly(GetType().Assembly); Func <Color, Color> colorFunc = c => c; parser.RegisterFunction("color", colorFunc); INumericExpression expr = parser.ParseNumericExpression("color(Color.Green)"); Assert.NotNull(expr); Assert.Equal("color(Color.Green)", expr.ToString()); Assert.Equal((int)Color.Green, expr.Evaluate()); }
public void NumericMethodFunctionTest() { ExpressionParser parser = new ExpressionParser(); parser.RegisterVariable("a", typeof(A)); INumericExpression expr = parser.ParseNumericExpression("1 + a.f(2,\"foo\",true)"); Assert.NotNull(expr); Assert.Equal("1 a.f(2,\"foo\",true) +", expr.ToString()); A a = new A(); Assert.Equal(1 + a.f(2, "foo", true), expr.Evaluate(new Dictionary <string, object> { { "a", a } })); }
public void MemberAccessExpressionTest() { ExpressionParser parser = new ExpressionParser(); parser.RegisterVariable <A>("a"); INumericExpression expr = parser.ParseNumericExpression("1 + a.B.Id"); Assert.NotNull(expr); Assert.Equal("1 a.B.Id +", expr.ToString()); A a = new A { B = new B { Id = 12 } }; Assert.Equal(13, expr.Evaluate(new Dictionary <string, object> { { "a", a } })); }
public void ObjectFunctionTest() { B[] items = new[] { new B { Id = 12 }, new B { Id = 13 } }; Func <int, B> func = n => items[n]; ExpressionParser parser = new ExpressionParser(); parser.RegisterFunction("item", func); INumericExpression expr = parser.ParseNumericExpression("1 + item(1).Id"); Assert.NotNull(expr); Assert.Equal("1 item(1).Id +", expr.ToString()); Assert.Equal(1 + items[1].Id, expr.Evaluate()); }