예제 #1
0
        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 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 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);
        }
예제 #5
0
        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));
        }
예제 #6
0
        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]);
        }
        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);
            }
        }