Ejemplo n.º 1
0
        public void CallNativeObjectMethodWithoutArguments()
        {
            Person person = new Person() { FirstName = "Adam", LastName = "Doe" };
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("adam", person);
            CallExpression expression = new CallExpression(new AttributeExpression(new NameExpression("adam"), "GetName"), null);

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(person.GetName(), result);
        }
Ejemplo n.º 2
0
        public void CallNativeObjectMethodWithArguments()
        {
            Calculator calculator = new Calculator();
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("calculator", calculator);
            CallExpression expression = new CallExpression(new AttributeExpression(new NameExpression("calculator"), "Add"), new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) });

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(calculator.Add(1, 2), result);
        }
Ejemplo n.º 3
0
        public void CallLen()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("len", new LenFunction());
            CallExpression expression = new CallExpression(new NameExpression("len"), new IExpression[] { new ConstantExpression("spam") });

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(4, result);
            Assert.IsNotNull(expression.TargetExpression);
            Assert.IsNotNull(expression.ArgumentExpressions);
        }
Ejemplo n.º 4
0
        public void CallNativeType()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("FileInfo", typeof(System.IO.FileInfo));
            CallExpression expression = new CallExpression(new NameExpression("FileInfo"), new IExpression[] { new ConstantExpression("unknown.txt") });

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(System.IO.FileInfo));

            var fileinfo = (System.IO.FileInfo)result;

            Assert.AreEqual("unknown.txt", fileinfo.Name);
        }
Ejemplo n.º 5
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.º 6
0
        public void CallNativeTypeStaticMethod()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("File", typeof(System.IO.File));
            CallExpression expression = new CallExpression(new AttributeExpression(new NameExpression("File"), "Exists"), new IExpression[] { new ConstantExpression("unknown.txt") });

            var result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(bool));
            Assert.AreEqual(false, result);
        }
Ejemplo n.º 7
0
        public void RaiseWhenCallLenWithNullArguments()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("len", new LenFunction());
            CallExpression expression = new CallExpression(new NameExpression("len"), null);

            try
            {
                expression.Evaluate(environment);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("len() takes exactly one argument (0 given)", ex.Message);
            }
        }