Esempio n. 1
0
        public void Execute(IContext context)
        {
            BindingEnvironment env = new BindingEnvironment(context);
            IList<IType> bases = null;

            if (this.baseExpressions != null && this.baseExpressions.Count > 0)
            {
                bases = new List<IType>();

                foreach (var expr in this.baseExpressions)
                    bases.Add((IType)expr.Evaluate(context));
            }

            DefinedClass klass = new DefinedClass(this.name, bases, context);
            this.body.Execute(klass);
            foreach (var name in env.GetNames())
            {
                var value = env.GetValue(name);
                var deffunc = value as DefinedFunction;

                if (deffunc != null)
                    klass.SetMethod(deffunc.Name, deffunc);
            }

            klass.SetValue("__doc__", this.doc);

            context.SetValue(this.name, klass);
        }
Esempio n. 2
0
 public void EvaluateMethodCall()
 {
     DefinedClass klass = new DefinedClass("Spam");
     DefinedFunction function = new DefinedFunction("get", new Parameter[] { new Parameter("self", null, false), new Parameter("a", null, false) }, new ReturnCommand(new NameExpression("a")), null);
     klass.SetMethod(function.Name, function);
     DynamicObject foo = (DynamicObject)klass.Apply(this.machine.Environment, null, null);
     this.machine.Environment.SetValue("foo", foo);
     Assert.AreEqual(2, this.Evaluate("foo.get(2)"));
 }
Esempio n. 3
0
        public void CreateInstance()
        {
            DefinedClass klass = new DefinedClass("Spam");
            var result = klass.Apply(null, null, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DynamicObject));
            Assert.AreEqual(klass, ((DynamicObject)result).Class);
        }
Esempio n. 4
0
        public void GetDynamicObjectType()
        {
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject foo = (DynamicObject)klass.Apply(null, null, null);

            var result = Types.GetType(foo);

            Assert.IsNotNull(result);
            Assert.AreEqual(klass, result);
        }
        public void GetUndefinedAttributeAsNull()
        {
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);

            var result = dynobj.GetValue("foo");

            Assert.IsNull(result);
            Assert.IsFalse(dynobj.HasValue("foo"));
        }
        public static DefinedClass CreateClassWithMethods(string name)
        {
            DefinedClass klass = new DefinedClass(name);

            klass.SetMethod("dummy", new NativeMethod(DummyMethod));
            klass.SetMethod("getSelf", new NativeMethod(SelfMethod));
            klass.SetMethod("getValue", new NativeMethod(GetValueMethod));

            return klass;
        }
Esempio n. 7
0
        public void GetMethodFromSecondSuperClass()
        {
            DefinedClass proto = new DefinedClass("proto");
            DefinedClass super = new DefinedClass("object");
            IFunction method = new NativeMethod(this.DummyMethod);
            super.SetValue("foo", method);
            DefinedClass klass = new DefinedClass("Spam", new IType[] { proto, super });

            var result = klass.GetMethod("foo");
            Assert.IsNotNull(result);
            Assert.IsTrue(klass.HasMethod("foo"));
        }
        public void GetValueFromClass()
        {
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);

            klass.SetValue("one", 1);
            var result = dynobj.GetValue("one");

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
            Assert.IsTrue(dynobj.HasValue("one"));
        }
        public void GetMethodFromClass()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction function = new NativeMethod(DummyMethod);
            klass.SetMethod("foo", function);
            DynamicObject dynobj = new DynamicObject(klass);

            var result = dynobj.GetValue("foo");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IFunction));
            Assert.AreEqual(function, result);
        }
Esempio n. 10
0
        public void DefineMethodUsingSetValue()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction method = new NativeMethod(this.DummyMethod);

            klass.SetValue("foo", method);

            var result = klass.GetMethod("foo");
            Assert.IsNotNull(result);
            Assert.AreEqual(method, result);
            Assert.IsTrue(klass.HasMethod("foo"));
            Assert.IsTrue(klass.HasValue("foo"));
        }
Esempio n. 11
0
        public void InvokeGetValueMethod()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction function = new NativeMethod(GetValueMethod);
            klass.SetMethod("foo", function);
            DynamicObject dynobj = new DynamicObject(klass);
            dynobj.SetValue("one", 1);

            var result = dynobj.Invoke("foo", null, new object[] { "one" }, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }
        public void SetAttributeInDynamicObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);
            environment.SetValue("foo", dynobj);

            SetAttributeCommand command = new SetAttributeCommand(new NameExpression("foo"), "one", new ConstantExpression(1));

            command.Execute(environment);

            Assert.IsTrue(dynobj.HasValue("one"));
            Assert.AreEqual(1, dynobj.GetValue("one"));
        }
Esempio n. 13
0
        public void CreateInstanceWithConstructor()
        {
            DefinedClass klass = new DefinedClass("Spam");
            ICommand body = new SetAttributeCommand(new NameExpression("self"), "name", new NameExpression("name"));
            DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body, null);
            klass.SetMethod(constructor.Name, constructor);
            var result = klass.Apply(null, new object[] { "Adam" }, null);

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

            var dynobj = (DynamicObject)result;
            Assert.AreEqual(klass, dynobj.Class);
            Assert.IsTrue(dynobj.HasValue("name"));
            var name = dynobj.GetValue("name");
            Assert.IsNotNull(name);
            Assert.AreEqual("Adam", name);
        }
Esempio n. 14
0
        public void ExecuteClassCommandWithInheritance()
        {
            DefinedClass fooclass = new DefinedClass("Foo");
            DefinedClass barclass = new DefinedClass("Bar");
            BindingEnvironment environment = new BindingEnvironment();
            ClassCommand command = new ClassCommand("Spam", new IExpression[] { new ConstantExpression(fooclass), new ConstantExpression(barclass) }, new PassCommand());

            command.Execute(environment);

            var result = environment.GetValue("Spam");
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefinedClass));

            var dclass = (DefinedClass)result;

            Assert.AreEqual("Spam", dclass.Name);
            Assert.IsNotNull(dclass.Bases);
            Assert.AreEqual(2, dclass.Bases.Count);
            Assert.AreEqual(fooclass, dclass.Bases[0]);
            Assert.AreEqual(barclass, dclass.Bases[1]);
            Assert.IsNull(dclass.GetValue("__doc__"));
            Assert.IsTrue(dclass.HasValue("__doc__"));
        }
Esempio n. 15
0
        public void SetGetAttribute()
        {
            DefinedClass klass = new DefinedClass("Spam");
            klass.SetValue("one", 1);

            var result = klass.GetValue("one");

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
            Assert.IsTrue(klass.HasValue("one"));
        }
Esempio n. 16
0
        public void DefineClassWithName()
        {
            DefinedClass klass = new DefinedClass("Spam");

            Assert.AreEqual("Spam", klass.Name);
        }
Esempio n. 17
0
 public void ClassAsString()
 {
     DefinedClass klass = new DefinedClass("Spam");
     Assert.AreEqual("<class 'Spam'>", ValueUtilities.AsString(klass));
     Assert.AreEqual("<class 'Spam'>", ValueUtilities.AsPrintString(klass));
 }
Esempio n. 18
0
        public void RaiseWhenThereIsAValueInsteadOfAMethod()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction function = new NativeMethod(GetValueMethod);
            klass.SetMethod("foo", function);
            DynamicObject dynobj = new DynamicObject(klass);
            dynobj.SetValue("foo", 1);

            try
            {
                dynobj.Invoke("foo", null, new object[] { "one" }, null);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("'int' object is not callable", ex.Message);
            }
        }
Esempio n. 19
0
 public void RedefineMethodAsObjectValue()
 {
     DefinedClass klass = new DefinedClass("Spam");
     IFunction function = new NativeMethod(GetValueMethod);
     klass.SetMethod("foo", function);
     DynamicObject dynobj = new DynamicObject(klass);
     dynobj.SetValue("foo", 1);
     Assert.AreEqual(1, dynobj.GetValue("foo"));
 }
Esempio n. 20
0
        public void NewDynamicObject()
        {
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);

            Assert.IsNotNull(dynobj.Class);
            Assert.AreEqual(klass, dynobj.Class);
        }
Esempio n. 21
0
        public void GetValueFromSuperClass()
        {
            DefinedClass super = new DefinedClass("object");
            super.SetValue("foo", 1);
            DefinedClass klass = new DefinedClass("Spam", new IType[] { super });

            var result = klass.GetValue("foo");
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
            Assert.IsTrue(klass.HasValue("foo"));
        }
Esempio n. 22
0
        public void ReplaceMethodWithAttribute()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction method = new NativeMethod(this.DummyMethod);

            klass.SetMethod("foo", method);
            klass.SetValue("foo", 1);

            var result = klass.GetMethod("foo");
            Assert.IsNull(result);
            Assert.IsFalse(klass.HasMethod("foo"));
            Assert.AreEqual(1, klass.GetValue("foo"));
            Assert.IsTrue(klass.HasValue("foo"));
        }
Esempio n. 23
0
        public void GetUndefinedMethodAsNull()
        {
            DefinedClass klass = new DefinedClass("Spam");

            var result = klass.GetMethod("foo");
            Assert.IsNull(result);
            Assert.IsFalse(klass.HasMethod("foo"));
        }
Esempio n. 24
0
        public void InvokeMethodDefinedInClass()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction function = new NativeMethod(DummyMethod);
            klass.SetMethod("foo", function);
            DynamicObject dynobj = new DynamicObject(klass);

            var result = dynobj.Invoke("foo", null, null, null);

            Assert.IsNull(result);
        }
Esempio n. 25
0
        public void InvokeMethodThatReturnsSelf()
        {
            DefinedClass klass = new DefinedClass("Spam");
            IFunction function = new NativeMethod(SelfMethod);
            klass.SetMethod("foo", function);
            DynamicObject dynobj = new DynamicObject(klass);

            var result = dynobj.Invoke("foo", null, null, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(dynobj, result);
        }
Esempio n. 26
0
        public void GetUndefinedAttributeAsNull()
        {
            DefinedClass klass = new DefinedClass("Spam");

            Assert.IsNull(klass.GetValue("foo"));
            Assert.IsFalse(klass.HasValue("foo"));
        }