示例#1
0
        public void EvaluateWithThisObject()
        {
            Context context = new Context();
            ICommand body = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new DotExpression(new VariableExpression("this"), "Age")));
            Function function = new Function(new string[] { "x" }, body);
            DynamicObject person = new DynamicObject();
            person.SetValue("Age", 30);

            Assert.AreEqual(40, function.Invoke(context, person, new object[] { 10 }));
        }
示例#2
0
        public void FunctionLength()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            object result = function.GetValue("length");

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }
示例#3
0
        public void InvokeSimpleFunction()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            Context context = new Context();
            context.SetValue("x", 2);

            object result = function.Invoke(context, null, new object[] { 1 });

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }
示例#4
0
        private static ICallable BuildFactorialFunction(IContext context)
        {
            IExpression condition = new CompareExpression(ComparisonOperator.LessEqual, new VariableExpression("n"), new ConstantExpression(1));

            ICommand return1 = new ReturnCommand(new ConstantExpression(1));
            ICommand return2 = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Multiply,
                new VariableExpression("n"),
                new InvokeExpression(new VariableExpression("Factorial"), new IExpression[] { new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("n"), new ConstantExpression(1)) })));

            ICommand ifcmd = new IfCommand(condition, return1, return2);
            ICallable factorial = new Function(new string[] { "n" }, ifcmd, context);

            return factorial;
        }
示例#5
0
        public void DefineMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("GetName", function);

            object result = dynobj.GetValue("GetName");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ICallable));
            Assert.IsTrue(result == function);
        }
示例#6
0
        public void InvokeNativeMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("Name", "Adam");

            object result = dynobj.Invoke("GetValue", new object[] { "Name" });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
        public void InvokeMethod()
        {
            ICommand body = new ReturnCommand(new DotExpression(new VariableExpression("this"), "Name"));
            Function function = new Function(null, body);

            Assert.AreEqual(0, function.Arity);

            dynobj.SetValue("Name", "Adam");
            dynobj.SetValue("GetName", function);

            object result = dynobj.Invoke("GetName", new object[] { });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
示例#8
-1
        public void FunctionHasCallMethod()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            object result = function.GetValue("call");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ICallable));
        }