コード例 #1
0
ファイル: EvaluateTests.cs プロジェクト: ajlopez/PythonSharp
 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)"));
 }
コード例 #2
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);
        }
コード例 #3
0
ファイル: TypesTests.cs プロジェクト: jose2190/PythonSharp
        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);
        }
コード例 #4
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);
        }