public void Parse_FloatLiteral_ReturnsFloatExpression()
        {
            var expression = (ConstantExpression)DynamicExpression.Parse(typeof(float), "1.0f");

            Assert.AreEqual(typeof(float), expression.Type);
            Assert.AreEqual(1.0f, expression.Value);
        }
        public void Parse_DoubleLiteral_ReturnsDoubleExpression()
        {
            var expression = (ConstantExpression)DynamicExpression.Parse(typeof(double), "1.0");

            Assert.AreEqual(typeof(double), expression.Type);
            Assert.AreEqual(1.0, expression.Value);
        }
示例#3
0
 public void Parse_StringLiteral_MissingClosingQuote()
 {
     string expectedRightValue = "\"test\\\"";
     var    expression         = DynamicExpression.Parse(
         new[] { Expression.Parameter(typeof(string), "Property1") },
         typeof(Boolean),
         string.Format("Property1 == {0}", expectedRightValue));
 }
示例#4
0
        public void Parse_ParameterExpressionMethodCall_ReturnsIntExpression()
        {
            var expression = DynamicExpression.Parse(
                new[] { Expression.Parameter(typeof(int), "x") },
                typeof(int),
                "x + 1");

            Assert.AreEqual(typeof(int), expression.Type);
        }
示例#5
0
        public void Parse_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpression()
        {
            var expression = DynamicExpression.Parse(
                new[] { Expression.Parameter(typeof(string), "Property1") },
                typeof(Boolean),
                string.Format("Property1 == {0}", "\"test\\\\string\""));

            string rightValue = ((BinaryExpression)expression).Right.ToString();

            Assert.AreEqual(typeof(Boolean), expression.Type);
            Assert.AreEqual("\"test\\string\"", rightValue);
        }
示例#6
0
        public void ParseWithSymbols(int x1, int y1)
        {
            const string exprString = "iif(x > y, x, y)";

            var x = Expression.Parameter(typeof(int), "x");
            var y = Expression.Parameter(typeof(int), "y");

            var symbols = new Dictionary <string, object>
            {
                { "x", x },
                { "y", y }
            };

            var body = DynamicExpression.Parse(null, exprString, symbols);
            var expr = Expression.Lambda(body, new[] { x, y });

            var dynamic = expr.Compile().DynamicInvoke(x1, y1);

            Console.WriteLine("iif(x > y, x, y) = {2}, (x={0}, y={1})", x1, y1, dynamic);
            Assert.AreEqual((x1 > y1) ? x1 : y1, dynamic);
        }
示例#7
0
        public void Parse_StringLiteralEmpty_ReturnsBooleanLambdaExpression()
        {
            var expression = DynamicExpression.Parse(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"\"");

            Assert.AreEqual(typeof(Boolean), expression.Type);
        }