コード例 #1
0
        private void TestReferenceTypeVariables()
        {
            ExpressionContext  context   = new ExpressionContext();
            VariableCollection variables = context.Variables;

            variables.Add("a", "string");
            variables.Add("b", 100);

            var    e      = new DynamicExpression <string>("a + b + a.tostring()", ExpressionLanguage.Flee);
            string result = e.Invoke(context);

            Assert.AreEqual("string" + 100 + "string", result);

            variables["a"].Value = "test";
            variables["b"].Value = 1;
            result = e.Invoke(context);
            Assert.AreEqual("test" + 1 + "test", result);

            // Test null value
            variables.Add("nullvar", null);

            var e2 = new DynamicExpression <bool>("nullvar = null", ExpressionLanguage.Flee);

            Assert.IsTrue(e2.Invoke(context));
        }
コード例 #2
0
        public void TestReferenceTypeVariables()
        {
            var context   = new ExpressionContext();
            var variables = context.Variables;

            variables.Add("a", "string");
            variables.Add("b", "100");

            var e      = new DynamicExpression <string>("a + b + a.tostring()", ExpressionLanguage.Flee);
            var result = e.Invoke(context);

            Assert.Equal("string" + 100 + "string", result);

            variables["a"].Value = "test";
            variables["b"].Value = "1";
            result = e.Invoke(context);
            Assert.Equal("test" + 1 + "test", result);

            // Test null value
            variables.Add("nullvar", null);

            var e2 = new DynamicExpression <bool>("nullvar = null", ExpressionLanguage.Flee);

            Assert.True(e2.Invoke(context));
        }
コード例 #3
0
        private void TestValueTypeVariables()
        {
            ExpressionContext  context   = new ExpressionContext();
            VariableCollection variables = context.Variables;

            variables.Add("a", 100);
            variables.Add("b", -100);
            variables.Add("c", DateTime.Now);

            var e1     = new DynamicExpression <int>("a+b", ExpressionLanguage.Flee);
            int result = e1.Invoke(context);

            Assert.AreEqual(100 + -100, result);

            variables["B"].Value = 1000;
            result = e1.Invoke(context);
            Assert.AreEqual(100 + 1000, result);

            var e2 = new DynamicExpression <string>("c.tolongdatestring() + c.Year.tostring()", ExpressionLanguage.Flee);

            Assert.AreEqual(DateTime.Now.ToLongDateString() + DateTime.Now.Year, e2.Invoke(context));

            // Test null value
            //variables["a"].Value = null;
            //e1 = new DynamicExpression<int>("a", ExpressionLanguage.Flee);
            //Assert.AreEqual(0, e1.Invoke(context));
        }
コード例 #4
0
        public void TestGenericEvaluate()
        {
            ExpressionContext context = default(ExpressionContext);

            context = new ExpressionContext();

            var e1 = new DynamicExpression <int>("1000", ExpressionLanguage.Flee);

            Assert.AreEqual(1000, e1.Invoke(context));

            var e2 = new DynamicExpression <double>("1000.25", ExpressionLanguage.Flee);

            Assert.AreEqual(1000.25, e2.Invoke(context));

            var e3 = new DynamicExpression <double>("1000", ExpressionLanguage.Flee);

            Assert.AreEqual(1000.0, e3.Invoke(context));

            var       e4 = new DynamicExpression <ValueType>("1000", ExpressionLanguage.Flee);
            ValueType vt = e4.Invoke(context);

            Assert.AreEqual(1000, vt);

            var    e5 = new DynamicExpression <object>("1000 + 2.5", ExpressionLanguage.Flee);
            object o  = e5.Invoke(context);

            Assert.AreEqual(1000 + 2.5, o);
        }
コード例 #5
0
        public void TestValueTypeOwner()
        {
            TestStruct        owner   = new TestStruct(100);
            ExpressionContext context = this.CreateGenericContext(owner);
            var options = new BoundExpressionOptions
            {
                AllowPrivateAccess = true
            };

            var e      = new DynamicExpression <int>("mya.compareto(100)", ExpressionLanguage.Flee);
            int result = e.Invoke(context, options);

            Assert.AreEqual(0, result);

            e      = new DynamicExpression <int>("myA", ExpressionLanguage.Flee);
            result = e.Invoke(context, options);
            Assert.AreEqual(100, result);

            e      = new DynamicExpression <int>("DoStuff()", ExpressionLanguage.Flee);
            result = e.Invoke(context);
            Assert.AreEqual(100, result);

            DateTime dt = DateTime.Now;

            context = this.CreateGenericContext(dt);

            e      = new DynamicExpression <int>("Month", ExpressionLanguage.Flee);
            result = e.Invoke(context);
            Assert.AreEqual(dt.Month, result);

            var e2 = new DynamicExpression <string>("tolongdatestring()", ExpressionLanguage.Flee);

            Assert.AreEqual(dt.ToLongDateString(), e2.Invoke(context));
        }
コード例 #6
0
        public void TestExpressionVariables()
        {
            ExpressionContext context1 = new ExpressionContext();
            context1.Imports.Add(new Import(typeof(Math)));
            var exp1 = new DynamicExpression("sin(pi)", ExpressionLanguage.Flee);
            var boundExp1 = exp1.Bind(context1);

            ExpressionContext context2 = new ExpressionContext();
            context2.Imports.Add(new Import(typeof(Math)));
            var exp2 = new DynamicExpression<double>("cos(pi/2)", ExpressionLanguage.Flee);
            var boundExp2 = exp2.Bind(context2);

            ExpressionContext context3 = new ExpressionContext();
            context3.Variables.Add("a", boundExp1);
            context3.Variables.Add("b", boundExp2);
            var exp3 = new DynamicExpression("cast(a, double) + b", ExpressionLanguage.Flee);

            double a = Math.Sin(Math.PI);
            double b = Math.Cos(Math.PI / 2);

            Assert.AreEqual(a + b, exp3.Invoke(context3));

            ExpressionContext context4 = new ExpressionContext();
            context4.Variables.Add("a", boundExp1);
            context4.Variables.Add("b", boundExp2);
            var exp4 = new DynamicExpression<double>("(cast(a, double) * b) + (b - cast(a, double))", ExpressionLanguage.Flee);

            Assert.AreEqual((a * b) + (b - a), exp4.Invoke(context4));
        }
コード例 #7
0
        public void TestLongBranchLogical1()
        {
            string            expressionText = this.GetIndividualTest("LongBranch1");
            ExpressionContext context        = new ExpressionContext();

            VariableCollection vc = context.Variables;

            vc.Add("M0100_ASSMT_REASON", "0");
            vc.Add("M0220_PRIOR_NOCHG_14D", "1");
            vc.Add("M0220_PRIOR_UNKNOWN", "1");
            vc.Add("M0220_PRIOR_UR_INCON", "1");
            vc.Add("M0220_PRIOR_CATH", "1");
            vc.Add("M0220_PRIOR_INTRACT_PAIN", "1");
            vc.Add("M0220_PRIOR_IMPR_DECSN", "1");
            vc.Add("M0220_PRIOR_DISRUPTIVE", "1");
            vc.Add("M0220_PRIOR_MEM_LOSS", "1");
            vc.Add("M0220_PRIOR_NONE", "1");

            vc.Add("M0220_PRIOR_UR_INCON_bool", true);
            vc.Add("M0220_PRIOR_CATH_bool", true);
            vc.Add("M0220_PRIOR_INTRACT_PAIN_bool", true);
            vc.Add("M0220_PRIOR_IMPR_DECSN_bool", true);
            vc.Add("M0220_PRIOR_DISRUPTIVE_bool", true);
            vc.Add("M0220_PRIOR_MEM_LOSS_bool", true);
            vc.Add("M0220_PRIOR_NONE_bool", true);
            vc.Add("M0220_PRIOR_NOCHG_14D_bool", true);
            vc.Add("M0220_PRIOR_UNKNOWN_bool", true);

            var e = new DynamicExpression(expressionText, ExpressionLanguage.Flee);
            // We only care that the expression is valid and can be evaluated
            object result = e.Invoke(context);
        }
コード例 #8
0
        public void TestImports()
        {
            ExpressionContext context;

            context = new ExpressionContext();
            // Import math type directly
            context.Imports.Add(new Import(typeof(Math)));

            // Should be able to see PI without qualification
            var e = new DynamicExpression <double>("pi", ExpressionLanguage.Flee);

            Assert.AreEqual(Math.PI, e.Invoke(context));

            context = new ExpressionContext(null, MyValidExpressionsOwner);
            // Import math type with prefix
            context.Imports.Add(new Import("math", typeof(Math)));

            // Should be able to see pi by qualifying with Math
            e = new DynamicExpression <double>("math.pi", ExpressionLanguage.Flee);
            Assert.AreEqual(Math.PI, e.Invoke(context));

            // Import nothing
            context = new ExpressionContext();
            // Should not be able to see PI
            this.AssertCompileException("pi");
            this.AssertCompileException("math.pi");

            // Test importing of builtin types
            new DynamicExpression <double>("double.maxvalue", ExpressionLanguage.Flee).Bind(null);
            new DynamicExpression <string>("string.concat(\"a\", \"b\")", ExpressionLanguage.Flee).Bind(null);
            new DynamicExpression <long>("long.maxvalue * 2", ExpressionLanguage.Flee).Bind(null);
        }
コード例 #9
0
        private void DoTestCheckedExpressions(string[] arr)
        {
            string expression     = arr[0];
            bool   @checked       = bool.Parse(arr[1]);
            bool   shouldOverflow = bool.Parse(arr[2]);

            var imports = new[]
            {
                new Import(typeof(Math))
            };

            try
            {
                var e = new DynamicExpression(expression, Language);

                e.Invoke(
                    new ExpressionContext(imports, MyValidExpressionsOwner),
                    new BoundExpressionOptions {
                    Checked = @checked
                }
                    );

                Assert.False(shouldOverflow);
            }
            catch (OverflowException)
            {
                Assert.True(shouldOverflow);
            }
        }
コード例 #10
0
        public void TestExpressionVariables()
        {
            ExpressionContext context1 = new ExpressionContext();

            context1.Imports.Add(new Import(typeof(Math)));
            var exp1      = new DynamicExpression("sin(pi)", ExpressionLanguage.Flee);
            var boundExp1 = exp1.Bind(context1);

            ExpressionContext context2 = new ExpressionContext();

            context2.Imports.Add(new Import(typeof(Math)));
            var exp2      = new DynamicExpression <double>("cos(pi/2)", ExpressionLanguage.Flee);
            var boundExp2 = exp2.Bind(context2);

            ExpressionContext context3 = new ExpressionContext();

            context3.Variables.Add("a", boundExp1);
            context3.Variables.Add("b", boundExp2);
            var exp3 = new DynamicExpression("cast(a, double) + b", ExpressionLanguage.Flee);

            double a = Math.Sin(Math.PI);
            double b = Math.Cos(Math.PI / 2);

            Assert.AreEqual(a + b, exp3.Invoke(context3));

            ExpressionContext context4 = new ExpressionContext();

            context4.Variables.Add("a", boundExp1);
            context4.Variables.Add("b", boundExp2);
            var exp4 = new DynamicExpression <double>("(cast(a, double) * b) + (b - cast(a, double))", ExpressionLanguage.Flee);

            Assert.AreEqual((a * b) + (b - a), exp4.Invoke(context4));
        }
コード例 #11
0
        public void TestStringNewlineEscape()
        {
            var    e        = new DynamicExpression <string>("\"a\\r\\nb\"", ExpressionLanguage.Flee);
            string s        = e.Invoke();
            string expected = string.Format("a{0}b", ControlChars.CrLf);

            Assert.AreEqual(expected, s);
        }
コード例 #12
0
        protected void DoTest(DynamicExpression expression, ExpressionContext expressionContext, string result, Type resultType, CultureInfo testCulture)
        {
            if (ReferenceEquals(resultType, typeof(object)))
            {
                Type expectedType = Type.GetType(result, false, true);

                if (expectedType == null)
                {
                    // Try to get the type from the Tests assembly
                    result       = string.Format("{0}.{1}", typeof(ExpressionTests).Namespace, result);
                    expectedType = this.GetType().Assembly.GetType(result, true, true);
                }

                object expressionResult = expression.Invoke(expressionContext, new BoundExpressionOptions
                {
                    AllowPrivateAccess = true,
                    ResultType         = resultType
                });

                if (object.ReferenceEquals(expectedType, typeof(void)))
                {
                    Assert.NotNull(expressionResult);
                }
                else
                {
                    Assert.IsType(expectedType, expressionResult);
                }
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(resultType);

                object expectedResult = tc.ConvertFromString(null, CultureInfo.CurrentCulture, result);
                object actualResult   = expression.Invoke(expressionContext, new BoundExpressionOptions
                {
                    AllowPrivateAccess = true,
                    ResultType         = resultType
                });

                expectedResult = RoundIfReal(expectedResult);
                actualResult   = RoundIfReal(actualResult);

                Assert.Equal(expectedResult, actualResult);
            }
        }
コード例 #13
0
        public void TestLongBranchLogical2()
        {
            string            expressionText = this.GetIndividualTest("LongBranch2");
            ExpressionContext context        = new ExpressionContext();

            var e = new DynamicExpression <bool>(expressionText, ExpressionLanguage.Flee);

            Assert.IsFalse(e.Invoke(context));
        }
コード例 #14
0
        public void TestLongBranchLogical2()
        {
            var expressionText = GetIndividualTest("LongBranch2");

            var expr = new DynamicExpression <bool>(expressionText, ExpressionLanguage.Flee);

            var context = new ExpressionContext();

            Assert.False(expr.Invoke(context));
        }
コード例 #15
0
        public void TestNonPublicImports()
        {
            // ...until we set an owner that is in the same module
            var context = new ExpressionContext(null, new OverloadTestExpressionOwner());

            context.Imports.Add(new Import(typeof(TestImport)));

            var e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);

            Assert.AreEqual(100, (int)e.Invoke(context));
        }
コード例 #16
0
        public void TestValueTypeVariables()
        {
            var context   = new ExpressionContext();
            var variables = context.Variables;

            variables.Add("a", 100);
            variables.Add("b", -100);
            variables.Add("c", DateTime.Now);

            var e1     = new DynamicExpression <int>("a+b", ExpressionLanguage.Flee);
            var result = e1.Invoke(context);

            Assert.Equal(100 + -100, result);

            variables["B"].Value = 1000;
            result = e1.Invoke(context);
            Assert.Equal(100 + 1000, result);

            var e2 = new DynamicExpression <string>("c.tolongdatestring() + c.Year.tostring()", ExpressionLanguage.Flee);

            Assert.Equal(DateTime.Now.ToLongDateString() + DateTime.Now.Year, e2.Invoke(context));
        }
コード例 #17
0
 private void DoTestOverloadResolution(string expression, ExpressionContext context, int expectedResult)
 {
     try
     {
         var e      = new DynamicExpression <int>(expression, ExpressionLanguage.Flee);
         int result = e.Invoke(context);
         Assert.AreEqual(expectedResult, result);
     }
     catch (Exception)
     {
         Assert.AreEqual(-1, expectedResult);
     }
 }
コード例 #18
0
        public void TestImportsNamespaces()
        {
            ExpressionContext context = new ExpressionContext();

            context.Imports.Add(new Import("ns1", typeof(Math)));
            context.Imports.Add(new Import("ns1", typeof(string)));

            var e = new DynamicExpression("ns1.cos(100)", ExpressionLanguage.Flee);

            Assert.AreEqual(Math.Cos(100), (double)e.Invoke(context));

            e = new DynamicExpression("ns1.concat(\"a\", \"b\")", ExpressionLanguage.Flee);
            Assert.AreEqual(string.Concat("a", "b"), e.Invoke(context));
        }
コード例 #19
0
        public void TestFieldWithSameNameAsVariable()
        {
            ExpressionContext context = new ExpressionContext(null, new Monitor());
            context.Variables.Add("doubleA", new ExpressionOwner());
            this.AssertCompileException("doubleA.doubleA", context);

            // But it should work for a public member
            context = new ExpressionContext();
            Monitor m = new Monitor();
            context.Variables.Add("I", m);

            var e = new DynamicExpression("i.i", ExpressionLanguage.Flee);
            Assert.AreEqual(m.I, (int)e.Invoke(context));
        }
コード例 #20
0
        public void TestNestedTypeImport()
        {
            ExpressionContext context = new ExpressionContext();

            // Should be able to import public nested type
            context.Imports.Add(new Import(typeof(NestedA.NestedPublicB)));
            var e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);

            Assert.AreEqual(100, (int)e.Invoke(context));

            // Should be able to import nested internal type now
            context = new ExpressionContext(null, new OverloadTestExpressionOwner());
            context.Imports.Add(new Import(typeof(NestedA.NestedInternalB)));
            e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);
            Assert.AreEqual(100, (int)e.Invoke(context));
        }
コード例 #21
0
        public void TestFieldWithSameNameAsVariable()
        {
            ExpressionContext context = new ExpressionContext(null, new Monitor());

            context.Variables.Add("doubleA", new ExpressionOwner());
            this.AssertCompileException("doubleA.doubleA", context);

            // But it should work for a public member
            context = new ExpressionContext();
            Monitor m = new Monitor();

            context.Variables.Add("I", m);

            var e = new DynamicExpression("i.i", ExpressionLanguage.Flee);

            Assert.AreEqual(m.I, (int)e.Invoke(context));
        }
コード例 #22
0
        private static void ParseWhen(string when, out Func <bool> predicate)
        {
            if (string.IsNullOrEmpty(when))
            {
                predicate = () => true;
                return;
            }
            var expression = new DynamicExpression(when, ExpressionLanguage.Csharp);


            predicate = () =>
            {
                var context = new ExpressionContext();
                ContextActionService.StateVariables.ForEach(v => context.Variables.Add(v.Key, v.Value));
                var result = expression.Invoke(context, WhenExpressionOptions);
                return((bool)result);
            };
        }
コード例 #23
0
ファイル: TestBase.cs プロジェクト: dpiatkowski/Expressions
        protected void Resolve(ExpressionContext expressionContext, string expression, object expected, BoundExpressionOptions options)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var dynamicExpression = new DynamicExpression(
                expression,
                ExpressionLanguage.Csharp
                );

            var actual = dynamicExpression.Invoke(
                expressionContext ?? new ExpressionContext(),
                options
                );

            Assert.Equal(expected, actual);
        }
コード例 #24
0
        public static bool Evaluate(string questionCode, string displayLogic, string response)
        {
            var isInverse = displayLogic.Contains("!=");

            displayLogic = displayLogic.Replace(questionCode, "a");

            if (isInverse)
            {
                displayLogic = displayLogic.Replace("!=", "==");
            }

            var expression = new DynamicExpression(displayLogic, ExpressionLanguage.Csharp);

            var context = new ExpressionContext();

            context.Variables.Add("a", response);

            var result = (bool)expression.Invoke(context);

            return(isInverse ? !result : result);
        }
コード例 #25
0
        public static bool EvaluateArgs(string args, Player player,
                                        ExpressionLanguage language = ExpressionLanguage.Csharp)
        {
            var expression = new DynamicExpression(args, language);

            // Only create context once
            if (player.Context == null)
            {
                player.Context = new ExpressionContext(null, player, true);
                player.Context.Variables.Add("Player", player);

                foreach (var spell in player.Spells.DistinctBy(s => s.Name))
                {
                    player.Context.Variables.Add(spell.Name, spell);
                }
            }

            var res = expression.Invoke(player.Context);

            return((bool)res);
        }
コード例 #26
0
        public void TestGenericEvaluate()
        {
            ExpressionContext context = default(ExpressionContext);
            context = new ExpressionContext();

            var e1 = new DynamicExpression<int>("1000", ExpressionLanguage.Flee);
            Assert.AreEqual(1000, e1.Invoke(context));

            var e2 = new DynamicExpression<double>("1000.25", ExpressionLanguage.Flee);
            Assert.AreEqual(1000.25, e2.Invoke(context));

            var e3 = new DynamicExpression<double>("1000", ExpressionLanguage.Flee);
            Assert.AreEqual(1000.0, e3.Invoke(context));

            var e4 = new DynamicExpression<ValueType>("1000", ExpressionLanguage.Flee);
            ValueType vt = e4.Invoke(context);
            Assert.AreEqual(1000, vt);

            var e5 = new DynamicExpression<object>("1000 + 2.5", ExpressionLanguage.Flee);
            object o = e5.Invoke(context);
            Assert.AreEqual(1000 + 2.5, o);
        }
コード例 #27
0
        private void TestReferenceTypeVariables()
        {
            ExpressionContext context = new ExpressionContext();
            VariableCollection variables = context.Variables;

            variables.Add("a", "string");
            variables.Add("b", 100);

            var e = new DynamicExpression<string>("a + b + a.tostring()", ExpressionLanguage.Flee);
            string result = e.Invoke(context);
            Assert.AreEqual("string" + 100 + "string", result);

            variables["a"].Value = "test";
            variables["b"].Value = 1;
            result = e.Invoke(context);
            Assert.AreEqual("test" + 1 + "test", result);

            // Test null value
            variables.Add("nullvar", null);

            var e2 = new DynamicExpression<bool>("nullvar = null", ExpressionLanguage.Flee);
            Assert.IsTrue(e2.Invoke(context));
        }
コード例 #28
0
        private void TestValueTypeVariables()
        {
            ExpressionContext context = new ExpressionContext();
            VariableCollection variables = context.Variables;

            variables.Add("a", 100);
            variables.Add("b", -100);
            variables.Add("c", DateTime.Now);

            var e1 = new DynamicExpression<int>("a+b", ExpressionLanguage.Flee);
            int result = e1.Invoke(context);
            Assert.AreEqual(100 + -100, result);

            variables["B"].Value = 1000;
            result = e1.Invoke(context);
            Assert.AreEqual(100 + 1000, result);

            var e2 = new DynamicExpression<string>("c.tolongdatestring() + c.Year.tostring()", ExpressionLanguage.Flee);
            Assert.AreEqual(DateTime.Now.ToLongDateString() + DateTime.Now.Year, e2.Invoke(context));

            // Test null value
            //variables["a"].Value = null;
            //e1 = new DynamicExpression<int>("a", ExpressionLanguage.Flee);
            //Assert.AreEqual(0, e1.Invoke(context));
        }
コード例 #29
0
        public void TestLongBranchLogical1()
        {
            string expressionText = this.GetIndividualTest("LongBranch1");
            ExpressionContext context = new ExpressionContext();

            VariableCollection vc = context.Variables;

            vc.Add("M0100_ASSMT_REASON", "0");
            vc.Add("M0220_PRIOR_NOCHG_14D", "1");
            vc.Add("M0220_PRIOR_UNKNOWN", "1");
            vc.Add("M0220_PRIOR_UR_INCON", "1");
            vc.Add("M0220_PRIOR_CATH", "1");
            vc.Add("M0220_PRIOR_INTRACT_PAIN", "1");
            vc.Add("M0220_PRIOR_IMPR_DECSN", "1");
            vc.Add("M0220_PRIOR_DISRUPTIVE", "1");
            vc.Add("M0220_PRIOR_MEM_LOSS", "1");
            vc.Add("M0220_PRIOR_NONE", "1");

            vc.Add("M0220_PRIOR_UR_INCON_bool", true);
            vc.Add("M0220_PRIOR_CATH_bool", true);
            vc.Add("M0220_PRIOR_INTRACT_PAIN_bool", true);
            vc.Add("M0220_PRIOR_IMPR_DECSN_bool", true);
            vc.Add("M0220_PRIOR_DISRUPTIVE_bool", true);
            vc.Add("M0220_PRIOR_MEM_LOSS_bool", true);
            vc.Add("M0220_PRIOR_NONE_bool", true);
            vc.Add("M0220_PRIOR_NOCHG_14D_bool", true);
            vc.Add("M0220_PRIOR_UNKNOWN_bool", true);

            var e = new DynamicExpression(expressionText, ExpressionLanguage.Flee);
            // We only care that the expression is valid and can be evaluated
            object result = e.Invoke(context);
        }
コード例 #30
0
        public void TestOnDemandVariables()
        {
            var e = new DynamicExpression("a + b", ExpressionLanguage.Flee);

            Assert.AreEqual(100 + 100, (int)e.Invoke(new TestExpressionContext()));
        }
コード例 #31
0
 public void TestStringNewlineEscape()
 {
     var e = new DynamicExpression<string>("\"a\\r\\nb\"", ExpressionLanguage.Flee);
     string s = e.Invoke();
     string expected = string.Format("a{0}b", ControlChars.CrLf);
     Assert.AreEqual(expected, s);
 }
コード例 #32
0
        public void TestValueTypeOwner()
        {
            TestStruct owner = new TestStruct(100);
            ExpressionContext context = this.CreateGenericContext(owner);
            var options = new BoundExpressionOptions
            {
                AllowPrivateAccess = true
            };

            var e = new DynamicExpression<int>("mya.compareto(100)", ExpressionLanguage.Flee);
            int result = e.Invoke(context, options);
            Assert.AreEqual(0, result);

            e = new DynamicExpression<int>("myA", ExpressionLanguage.Flee);
            result = e.Invoke(context, options);
            Assert.AreEqual(100, result);

            e = new DynamicExpression<int>("DoStuff()", ExpressionLanguage.Flee);
            result = e.Invoke(context);
            Assert.AreEqual(100, result);

            DateTime dt = DateTime.Now;
            context = this.CreateGenericContext(dt);

            e = new DynamicExpression<int>("Month", ExpressionLanguage.Flee);
            result = e.Invoke(context);
            Assert.AreEqual(dt.Month, result);

            var e2 = new DynamicExpression<string>("tolongdatestring()", ExpressionLanguage.Flee);
            Assert.AreEqual(dt.ToLongDateString(), e2.Invoke(context));
        }
コード例 #33
0
        public void TestNonPublicImports()
        {
            // ...until we set an owner that is in the same module
            var context = new ExpressionContext(null, new OverloadTestExpressionOwner());
            context.Imports.Add(new Import(typeof(TestImport)));

            var e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);
            Assert.AreEqual(100, (int)e.Invoke(context));
        }
コード例 #34
0
 public void TestOnDemandVariables()
 {
     var e = new DynamicExpression("a + b", ExpressionLanguage.Flee);
     Assert.AreEqual(100 + 100, (int)e.Invoke(new TestExpressionContext()));
 }
コード例 #35
0
        public void TestNestedTypeImport()
        {
            ExpressionContext context = new ExpressionContext();

            // Should be able to import public nested type
            context.Imports.Add(new Import(typeof(NestedA.NestedPublicB)));
            var e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);
            Assert.AreEqual(100, (int)e.Invoke(context));

            // Should be able to import nested internal type now
            context = new ExpressionContext(null, new OverloadTestExpressionOwner());
            context.Imports.Add(new Import(typeof(NestedA.NestedInternalB)));
            e = new DynamicExpression("DoStuff()", ExpressionLanguage.Flee);
            Assert.AreEqual(100, (int)e.Invoke(context));
        }
コード例 #36
0
        public void TestImports()
        {
            ExpressionContext context;

            context = new ExpressionContext();
            // Import math type directly
            context.Imports.Add(new Import(typeof(Math)));

            // Should be able to see PI without qualification
            var e = new DynamicExpression<double>("pi", ExpressionLanguage.Flee);
            Assert.AreEqual(Math.PI, e.Invoke(context));

            context = new ExpressionContext(null, MyValidExpressionsOwner);
            // Import math type with prefix
            context.Imports.Add(new Import("math", typeof(Math)));

            // Should be able to see pi by qualifying with Math
            e = new DynamicExpression<double>("math.pi", ExpressionLanguage.Flee);
            Assert.AreEqual(Math.PI, e.Invoke(context));

            // Import nothing
            context = new ExpressionContext();
            // Should not be able to see PI
            this.AssertCompileException("pi");
            this.AssertCompileException("math.pi");

            // Test importing of builtin types
            new DynamicExpression<double>("double.maxvalue", ExpressionLanguage.Flee).Bind(null);
            new DynamicExpression<string>("string.concat(\"a\", \"b\")", ExpressionLanguage.Flee).Bind(null);
            new DynamicExpression<long>("long.maxvalue * 2", ExpressionLanguage.Flee).Bind(null);
        }
コード例 #37
0
        public void TestLongBranchLogical2()
        {
            string expressionText = this.GetIndividualTest("LongBranch2");
            ExpressionContext context = new ExpressionContext();

            var e = new DynamicExpression<bool>(expressionText, ExpressionLanguage.Flee);
            Assert.IsFalse(e.Invoke(context));
        }
コード例 #38
0
ファイル: BulkTests.cs プロジェクト: parsnips/Expressions
        private void DoTestCheckedExpressions(string[] arr)
        {
            string expression = arr[0];
            bool @checked = bool.Parse(arr[1]);
            bool shouldOverflow = bool.Parse(arr[2]);

            var imports = new[]
            {
                new Import(typeof(Math))
               };

            ExpressionContext context = new ExpressionContext(imports, MyValidExpressionsOwner);

            try
            {
                var e = new DynamicExpression(expression, Language);
                e.Invoke(context, new BoundExpressionOptions
                {
                    Checked = @checked
                });
                Assert.IsFalse(shouldOverflow);
            }
            catch (OverflowException)
            {
                Assert.IsTrue(shouldOverflow);
            }
        }
コード例 #39
0
 private void DoTestOverloadResolution(string expression, ExpressionContext context, int expectedResult)
 {
     try
     {
         var e = new DynamicExpression<int>(expression, ExpressionLanguage.Flee);
         int result = e.Invoke(context);
         Assert.AreEqual(expectedResult, result);
     }
     catch (Exception)
     {
         Assert.AreEqual(-1, expectedResult);
     }
 }
コード例 #40
0
        protected void DoTest(DynamicExpression expression, ExpressionContext expressionContext, string result, Type resultType, CultureInfo testCulture)
        {
            if (ReferenceEquals(resultType, typeof(object)))
            {
                Type expectedType = Type.GetType(result, false, true);

                if (expectedType == null)
                {
                    // Try to get the type from the Tests assembly
                    result = string.Format("{0}.{1}", typeof(ExpressionTests).Namespace, result);
                    expectedType = this.GetType().Assembly.GetType(result, true, true);
                }

                object expressionResult = expression.Invoke(expressionContext, new BoundExpressionOptions
                {
                    AllowPrivateAccess = true,
                    ResultType = resultType
                });

                if (object.ReferenceEquals(expectedType, typeof(void)))
                {
                    Assert.IsNull(expressionResult);
                }
                else
                {
                    Assert.That(expressionResult, Is.InstanceOf(expectedType));
                }

            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(resultType);

                object expectedResult = tc.ConvertFromString(null, CultureInfo.CurrentCulture, result);
                object actualResult = expression.Invoke(expressionContext, new BoundExpressionOptions
                {
                    AllowPrivateAccess = true,
                    ResultType = resultType
                });

                expectedResult = RoundIfReal(expectedResult);
                actualResult = RoundIfReal(actualResult);

                Assert.AreEqual(expectedResult, actualResult);
            }
        }
コード例 #41
0
        public void TestImportsNamespaces()
        {
            ExpressionContext context = new ExpressionContext();
            context.Imports.Add(new Import("ns1", typeof(Math)));
            context.Imports.Add(new Import("ns1", typeof(string)));

            var e = new DynamicExpression("ns1.cos(100)", ExpressionLanguage.Flee);
            Assert.AreEqual(Math.Cos(100), (double)e.Invoke(context));

            e = new DynamicExpression("ns1.concat(\"a\", \"b\")", ExpressionLanguage.Flee);
            Assert.AreEqual(string.Concat("a", "b"), e.Invoke(context));
        }