예제 #1
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)"));
 }
예제 #2
0
        public void EvaluateUsingDefaultValuesForArguments()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            Assert.AreEqual(3, func.Apply(new BindingEnvironment(), null, null));
        }
예제 #3
0
        public void CreateSimpleDefinedFunction()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            ICommand body = new SetCommand("c", new NameExpression("a"));
            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            Assert.AreEqual(parameters, func.Parameters);
            Assert.AreEqual(body, func.Body);
        }
예제 #4
0
        public void EvaluateUsingEmptyListArgument()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, true) };
            ICommand body = new ReturnCommand(new NameExpression("b"));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(new BindingEnvironment(), new object[] { 1 }, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList<object>));

            var list = (IList<object>)result;

            Assert.AreEqual(0, list.Count);
        }
예제 #5
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);
        }
예제 #6
0
 public void FunctionAsString()
 {
     DefinedFunction function = new DefinedFunction("foo", null, null, null);
     Assert.AreEqual("<function foo>", ValueUtilities.AsString(function));
     Assert.AreEqual("<function foo>", ValueUtilities.AsPrintString(function));
 }
예제 #7
0
        public void ValuesInFunction()
        {
            ICommand body = new PassCommand();

            DefinedFunction func = new DefinedFunction("foo", null, body, null);

            func.SetValue("__doc__", "foo function");

            Assert.IsFalse(func.HasValue("bar"));
            Assert.IsTrue(func.HasValue("__doc__"));
            Assert.AreEqual("foo function", func.GetValue("__doc__"));
        }
예제 #8
0
        public void RaiseWhenOneParameterExpectedAndNoneIsProvided()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false) };
            CompositeCommand body = new CompositeCommand();

            Machine machine = new Machine();

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(machine.Environment, null, null);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() takes exactly 1 positional argument (0 given)", ex.Message);
            }
        }
예제 #9
0
        public void RaiseWhenMultipleValuesForKeywordArgument()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(new BindingEnvironment(), new object[] { 1 }, new Dictionary<string, object> { { "a", 2 } });
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() got multiple values for keyword argument 'a'", ex.Message);
            }
        }
예제 #10
0
        public void RaiseWhenFewParametersProvided()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand body = new CompositeCommand();

            Machine machine = new Machine();
            StringWriter writer = new StringWriter();
            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(machine.Environment, new object[] { 1 }, null);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() takes exactly 2 positional arguments (1 given)", ex.Message);
            }
        }
예제 #11
0
        public void GetAndUseDelegate()
        {
            ICommand body = new PassCommand();

            DefinedFunction func = new DefinedFunction("foo", null, body, null);
            var type = typeof(ThreadStart);

            // Activator.CreateInstance(typeof(ThreadStart), func.DoFunction);
        }
예제 #12
0
        public void ExecuteFunctionWithReturn()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand body = new CompositeCommand();
            body.AddCommand(new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)));

            Machine machine = new Machine();
            StringWriter writer = new StringWriter();
            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(machine.Environment, new object[] { 1, 2 }, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result);
        }
예제 #13
0
        public void ExecuteFunctionWithPrint()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand body = new CompositeCommand();
            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("a") })));
            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("b") })));

            Machine machine = new Machine();
            StringWriter writer = new StringWriter();
            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, machine.Environment);

            func.Apply(machine.Environment, new object[] { 1, 2 }, null);
            Assert.AreEqual("1\r\n2\r\n", writer.ToString());
        }
예제 #14
0
        public void EvaluateUsingTwoNamedArguments()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(new BindingEnvironment(), null, new Dictionary<string, object> { { "a", 2 }, { "b", 3 } });

            Assert.IsNotNull(result);
            Assert.AreEqual(5, result);
        }