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 })); }
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); }
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); }
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; }
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); }
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); }
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)); }