public void CompileAndEvaluateComplexListExpression() { Parser parser = new Parser("[1, 2, [a, b], 'spam']"); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); environment.SetValue("b", 2); IExpression expression = parser.CompileExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(ListExpression)); Assert.IsFalse(((ListExpression)expression).IsReadOnly); object result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList list = (IList)result; Assert.AreEqual(4, list.Count); Assert.AreEqual(1, list[0]); Assert.AreEqual(2, list[1]); Assert.IsNotNull(list[2]); Assert.IsInstanceOfType(list[2], typeof(IList)); Assert.AreEqual("spam", list[3]); IList list2 = (IList)list[2]; Assert.IsNotNull(list2); Assert.AreEqual(2, list2.Count); Assert.AreEqual(1, list2[0]); Assert.AreEqual(2, list2[1]); }
public void GetNativeMethod() { AttributeExpression expression = new AttributeExpression(new ConstantExpression(1), "GetType"); BindingEnvironment environment = new BindingEnvironment(); var result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(MethodInfo)); }
public void EvaluateAttributeExpressionOnClassProperty() { AttributeExpression expression = new AttributeExpression(new NameExpression("Calculator"), "Value"); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("Calculator", typeof(Calculator)); object result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.AreEqual(3, result); }
public void EvaluateAttributeExpressionOnNativeObjectProperty() { AttributeExpression expression = new AttributeExpression(new NameExpression("adam"), "FirstName"); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("adam", new Person() { FirstName = "Adam" }); object result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual("Adam", result); }
public Machine() { this.environment = new BindingEnvironment(); this.environment.SetValue("len", new LenFunction()); this.environment.SetValue("print", new PrintFunction(this)); this.environment.SetValue("range", new RangeFunction()); this.environment.SetValue("eval", new EvalFunction()); this.environment.SetValue("exec", new ExecFunction()); this.environment.SetValue("dir", new DirFunction()); this.environment.SetValue("exit", new ExitFunction()); this.environment.SetValue("id", new IdFunction()); this.environment.SetValue("locals", new ContextFunction("locals", false)); this.environment.SetValue("globals", new ContextFunction("globals", true)); this.environment.SetValue("__machine__", this); }
public void EvaluateAttributeExpression() { AttributeExpression expression = new AttributeExpression(new NameExpression("module"), "foo"); BindingEnvironment environment = new BindingEnvironment(); BindingEnvironment modenv = new BindingEnvironment(); modenv.SetValue("foo", "bar"); environment.SetValue("module", modenv); object result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual("bar", result); Assert.IsNotNull(expression.Expression); Assert.AreEqual("foo", expression.Name); }
public void RaiseWhenEvaluateAttributeExpression() { AttributeExpression expression = new AttributeExpression(new NameExpression("module"), "spam"); BindingEnvironment environment = new BindingEnvironment(); BindingEnvironment modenv = new BindingEnvironment(); environment.SetValue("module", modenv); try { expression.Evaluate(environment); Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(AttributeError)); Assert.AreEqual("'module' object has no attribute 'spam'", ex.Message); } }
public void CompileExpressionCommandWithName() { Parser parser = new Parser("a+2"); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); ICommand command = parser.CompileCommand(); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(ExpressionCommand)); command.Execute(environment); ExpressionCommand exprcommand = (ExpressionCommand)command; Assert.IsNotNull(exprcommand.Expression); Assert.AreEqual(3, exprcommand.Expression.Evaluate(environment)); }
public void CompileAndEvaluateListWithExpressionsExpression() { Parser parser = new Parser("[a+1, b+2, c]"); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); environment.SetValue("b", 2); environment.SetValue("c", "spam"); IExpression expression = parser.CompileExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(ListExpression)); Assert.IsFalse(((ListExpression)expression).IsReadOnly); object result = expression.Evaluate(environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList list = (IList)result; Assert.AreEqual(3, list.Count); Assert.AreEqual(2, list[0]); Assert.AreEqual(4, list[1]); Assert.AreEqual("spam", list[2]); }