public void GetValueFromDynamicObject()
        {
            DynamicObject dynobj = new DynamicObject();
            dynobj.SetValue("FirstName", "Adam");

            Assert.AreEqual("Adam", ObjectUtilities.GetValue(dynobj, "FirstName"));
        }
Beispiel #2
0
        public void EvaluateCallObject()
        {
            Machine machine = new Machine();
            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.Call(person, new object[] { 10 }));
        }
        public void GetIndexedValuesFromDynamicObject()
        {
            DynamicObject obj = new DynamicObject();
            obj.SetValue("Name", "Adam");
            obj.SetValue("GetAge", new Function(new string[] { "n" }, null));

            Assert.AreEqual("Adam", ObjectUtilities.GetIndexedValue(obj, new object[] { "Name" }));

            object f = ObjectUtilities.GetIndexedValue(obj, new object[] { "GetAge" });
            Assert.IsNotNull(f);
            Assert.IsInstanceOfType(f, typeof(Function));
        }
Beispiel #4
0
        public void EvaluateArrayDotExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();

            DynamicObject data = new DynamicObject();
            data.SetValue("Numbers", new string[] { "one", "two", "three" });

            environment.SetValue("data", data);

            IExpression expression = new ArrayExpression(new DotExpression(new VariableExpression("data"), "Numbers"), new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual("two", result);
        }
Beispiel #5
0
        public void Execute(IBindingEnvironment environment)
        {
            DynamicObject dynobj = new DynamicObject();

            int k = 0;

            if (this.memberExpressions != null)
                foreach (IExpression expression in this.memberExpressions)
                {
                    string name = this.memberNames[k++];
                    object value = null;

                    if (expression != null)
                        value = expression.Evaluate(environment);

                    dynobj.SetValue(name, value);
                }

            environment.SetValue(this.name, dynobj);
        }
Beispiel #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);
        }
Beispiel #7
0
        private static object ResolveToObject(VariableExpression expression, IBindingEnvironment environment)
        {
            string name = expression.VariableName;

            object obj = environment.GetValue(name);

            if (obj == null)
            {
                obj = new DynamicObject();

                // TODO Review if Local or not
                environment.SetValue(name, obj);
            }

            return obj;
        }
Beispiel #8
0
 public ObjectEnvironment(DynamicObject obj, IBindingEnvironment parent)
 {
     this.obj = obj;
     this.parent = parent;
 }
Beispiel #9
0
 public ObjectEnvironment(DynamicObject obj)
     : this(obj, null)
 {
 }
Beispiel #10
0
 public void SetupDynamicObject()
 {
     this.machine = new Machine();
     this.dynobj = new DynamicObject();
 }
Beispiel #11
0
        public void EvaluateMultipleSetExpressionUsingDynamicObject()
        {
            DynamicObject dynobj = new DynamicObject();

            MultipleSetExpression expression = new MultipleSetExpression(new ConstantExpression(dynobj), new string[] { "FirstName", "LastName" }, new IExpression[] { new ConstantExpression("John"), new ConstantExpression("Doe") });

            object result = expression.Evaluate(new BindingEnvironment());

            Assert.IsNotNull(result);
            Assert.IsTrue(result == dynobj);

            Assert.AreEqual("John", dynobj.GetValue("FirstName"));
            Assert.AreEqual("Doe", dynobj.GetValue("LastName"));
        }
 public void SetUp()
 {
     this.dynobj = new DynamicObject();
     this.environment = new ObjectEnvironment(this.dynobj);
 }