Exemplo n.º 1
0
        private static string AsName(IExpression expression)
        {
            if (expression is VariableExpression)
            {
                return(((VariableExpression)expression).VariableName);
            }

            if (expression is DotExpression)
            {
                DotExpression dot = (DotExpression)expression;

                return(AsName(dot.Expression) + "." + dot.Name);
            }

            return(null);
        }
Exemplo n.º 2
0
        private static IDictionary ResolveToDictionary(DotExpression expression, IBindingEnvironment environment)
        {
            object obj = ResolveToObject(expression.Expression, environment);

            if (obj is DynamicObject)
            {
                DynamicObject dynobj = (DynamicObject)obj;

                obj = dynobj.GetValue(expression.Name);

                if (obj == null)
                {
                    obj = new Hashtable();
                    dynobj.SetValue(expression.Name, obj);
                }

                return (IDictionary)obj;
            }

            return (IDictionary)ObjectUtilities.GetValue(obj, expression.Name);
        }
Exemplo n.º 3
0
        public void ResolveDotExpressionToObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            IExpression expression = new DotExpression(new VariableExpression("Project"), "Title");

            object obj = ExpressionUtilities.ResolveToObject(expression, environment);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(IObject));

            object project = environment.GetValue("Project");

            Assert.IsNotNull(project);
            Assert.IsInstanceOfType(project, typeof(IObject));

            object title = ((IObject)project).GetValue("Title");

            Assert.IsNotNull(title);
            Assert.IsInstanceOfType(title, typeof(IObject));

            Assert.AreEqual(obj, title);
        }
Exemplo n.º 4
0
        public void ResolveDotExpressionToDictionary()
        {
            BindingEnvironment environment = new BindingEnvironment();
            IExpression expression = new DotExpression(new VariableExpression("Project"), "Entities");

            object obj = ExpressionUtilities.ResolveToDictionary(expression, environment);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(IDictionary));

            object project = environment.GetValue("Project");

            Assert.IsNotNull(project);
            Assert.IsInstanceOfType(project, typeof(IObject));

            object entities = ((IObject)project).GetValue("Entities");

            Assert.IsNotNull(entities);
            Assert.IsInstanceOfType(entities, typeof(IDictionary));

            Assert.AreEqual(obj, entities);
        }
Exemplo n.º 5
0
        private static void SetValue(DotExpression expression, object value, IBindingEnvironment environment)
        {
            if (expression.Arguments != null)
                throw new InvalidOperationException("Invalid left value");

            object obj = ResolveToObject(expression.Expression, environment);

            ObjectUtilities.SetValue(obj, expression.Name, value);
        }
Exemplo n.º 6
0
        private static object ResolveToObject(DotExpression expression, IBindingEnvironment environment)
        {
            object obj = ResolveToObject(expression.Expression, environment);

            if (obj is DynamicObject)
            {
                DynamicObject dynobj = (DynamicObject)obj;

                obj = dynobj.GetValue(expression.Name);

                if (obj == null)
                {
                    obj = new DynamicObject();
                    dynobj.SetValue(expression.Name, obj);
                }

                return obj;
            }

            return ObjectUtilities.GetValue(obj, expression.Name);
        }
Exemplo n.º 7
0
        public void EvaluateDotExpressionOnString()
        {
            IExpression expression = new DotExpression(new ConstantExpression("foo"), "Length");

            Assert.AreEqual(3, expression.Evaluate(null));
        }
Exemplo n.º 8
0
        public void EvaluateDotExpressionOnInteger()
        {
            IExpression expression = new DotExpression(new ConstantExpression(1), "ToString", new List<IExpression>());

            Assert.AreEqual("1", expression.Evaluate(null));
        }
Exemplo n.º 9
0
        public void EvaluateDotExpressionAsTypeInvocation()
        {
            DotExpression dot = new DotExpression(new DotExpression(new DotExpression(new VariableExpression("System"), "IO"), "File"), "Exists", new IExpression[] { new ConstantExpression("unknown.txt") });

            Assert.IsFalse((bool)dot.Evaluate(new BindingEnvironment()));
        }
Exemplo n.º 10
0
        private IExpression ParseTermExpression()
        {
            IExpression expression = this.ParseSimpleTermExpression();

            while (this.TryParse(TokenType.Operator, ".") || this.TryParse(TokenType.Separator, "[", "("))
            {
                if (this.TryParse(TokenType.Separator, "("))
                    expression = new InvokeExpressionExpression(expression, this.ParseArgumentList());
                else if (this.TryParse(TokenType.Operator, "."))
                {
                    this.lexer.NextToken();
                    string name = this.ParseName();
                    List<IExpression> arguments = null;

                    if (this.TryParse(TokenType.Separator, "("))
                        arguments = this.ParseArgumentList();

                    expression = new DotExpression(expression, name, arguments);
                }
                else
                    expression = new ArrayExpression(expression, this.ParseArrayArgumentList());
            }

            return expression;
        }
Exemplo n.º 11
0
        public void ExecuteSetCommandWithObjectProperty()
        {
            Machine machine = new Machine();
            TextWriter outwriter = new StringWriter();
            machine.Environment.SetValue("machine", machine);
            machine.Environment.SetValue("out", outwriter);

            DotExpression dotexpr = new DotExpression(new VariableExpression("machine"), "Out");
            SetCommand command = new SetCommand(dotexpr, new VariableExpression("out"));

            command.Execute(machine.Environment);

            Assert.AreEqual(machine.Out, outwriter);
        }
Exemplo n.º 12
0
        public void ExecuteSetCommandWithDotExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "FirstName");
            SetCommand command = new SetCommand(dotexpr, new ConstantExpression("bar"));

            command.Execute(environment);

            object obj = environment.GetValue("foo");

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(DynamicObject));

            DynamicObject dynobj = (DynamicObject)obj;

            Assert.AreEqual("bar", dynobj.GetValue("FirstName"));
        }
Exemplo n.º 13
0
        public void ExecuteSetArrayCommandWithDotExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "Values");
            SetArrayCommand command = new SetArrayCommand(dotexpr, new IExpression[] { new ConstantExpression(0) }, new ConstantExpression("bar"));

            command.Execute(environment);

            object obj = environment.GetValue("foo");

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(DynamicObject));

            DynamicObject dynobj = (DynamicObject)obj;

            object obj2 = dynobj.GetValue("Values");

            Assert.IsNotNull(obj2);
            Assert.IsInstanceOfType(obj2, typeof(IList));

            IList list = (IList)obj2;

            Assert.AreEqual(1, list.Count);
            Assert.AreEqual("bar", list[0]);
        }