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); }
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 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)")); }
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")); }
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); }
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); }
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); }
public void SetGetMethod() { DefinedClass klass = new DefinedClass("Spam"); IFunction method = new NativeMethod(this.DummyMethod); klass.SetMethod("foo", method); var result = klass.GetMethod("foo"); Assert.IsNotNull(result); Assert.AreEqual(method, result); Assert.IsTrue(klass.HasMethod("foo")); Assert.IsTrue(klass.HasValue("foo")); }
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 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")); }
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); } }
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); }