Ejemplo n.º 1
0
        public void GetUndefinedAttributeAsNull()
        {
            DefinedClass klass = new DefinedClass("Spam");

            Assert.IsNull(klass.GetValue("foo"));
            Assert.IsFalse(klass.HasValue("foo"));
        }
Ejemplo n.º 2
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);
        }
        public void ClassAsString()
        {
            DefinedClass klass = new DefinedClass("Spam");

            Assert.AreEqual("<class 'Spam'>", ValueUtilities.AsString(klass));
            Assert.AreEqual("<class 'Spam'>", ValueUtilities.AsPrintString(klass));
        }
Ejemplo n.º 4
0
        public void NewDynamicObject()
        {
            DefinedClass  klass  = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);

            Assert.IsNotNull(dynobj.Class);
            Assert.AreEqual(klass, dynobj.Class);
        }
Ejemplo n.º 5
0
        public void GetUndefinedMethodAsNull()
        {
            DefinedClass klass = new DefinedClass("Spam");

            var result = klass.GetMethod("foo");

            Assert.IsNull(result);
            Assert.IsFalse(klass.HasMethod("foo"));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        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"));
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
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"));
        }
Ejemplo n.º 11
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)"));
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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"));
        }
Ejemplo n.º 14
0
        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"));
        }
Ejemplo n.º 15
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"));
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public void GetMethodFromSuperClass()
        {
            DefinedClass super  = new DefinedClass("object");
            IFunction    method = new NativeMethod(this.DummyMethod);

            super.SetValue("foo", method);
            DefinedClass klass = new DefinedClass("Spam", new IType[] { super });

            var result = klass.GetMethod("foo");

            Assert.IsNotNull(result);
            Assert.IsTrue(klass.HasMethod("foo"));
        }
Ejemplo n.º 18
0
        public void CallObjectMethod()
        {
            DefinedClass       klass       = DynamicObjectTests.CreateClassWithMethods("Spam");
            DynamicObject      dynobj      = (DynamicObject)klass.Apply(null, null, null);
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("foo", dynobj);
            CallExpression expression = new CallExpression(new AttributeExpression(new NameExpression("foo"), "getSelf"), null);

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(dynobj, result);
        }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
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"));
        }
Ejemplo n.º 21
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"));
        }
Ejemplo n.º 22
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"));
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
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);
            }
        }
Ejemplo n.º 26
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__"));
        }
Ejemplo n.º 27
0
        public void DefineClassWithName()
        {
            DefinedClass klass = new DefinedClass("Spam");

            Assert.AreEqual("Spam", klass.Name);
        }