public void CallPutsInteger() { StringWriter writer = new StringWriter(); CallExpression expr = new CallExpression("puts", new IExpression[] { new ConstantExpression(123) }); Machine machine = new Machine(); PutsFunction puts = new PutsFunction(writer); machine.RootContext.Self.Class.SetInstanceMethod("puts", puts); Assert.IsNull(expr.Evaluate(machine.RootContext)); Assert.AreEqual("123\r\n", writer.ToString()); }
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); }
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); }
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); }
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); }
public void CallFind() { Collection collection = new Collection("people"); CollectionObject collobj = new CollectionObject(collection); Context context = new Context(); context.SetMember("people", collobj); CallExpression expression = new CallExpression(new DotExpression(new NameExpression("people"), "find"), new IExpression[] { }); var result = expression.Evaluate(context); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IEnumerable <DynamicDocument>)); }
public void EvaluateCallExpression() { Context context = new Context(); Function function = new Function(context, new object[] { new Variable("X"), new Variable("Y") }, new AddExpression(new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y")))); context.SetValue("add/2", function); CallExpression expr = new CallExpression(new AtomExpression(new Atom("add")), new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }); Assert.IsFalse(expr.HasVariable()); Assert.IsNotNull(expr.NameExpression); Assert.IsInstanceOfType(expr.NameExpression, typeof(AtomExpression)); Assert.IsNotNull(expr.ArgumentExpressions); Assert.AreEqual(2, expr.ArgumentExpressions.Count); Assert.AreEqual(3, expr.Evaluate(context)); }
public void ExecuteCallExpression() { StringWriter writer = new StringWriter(); Print print = new Print(writer); Context context = new Context(); IList <ICommand> commandlist = new List <ICommand>(); CallExpression callexpr = new CallExpression(new ConstantExpression(print), new IExpression[] { new ConstantExpression("Hello, World") }); object result = callexpr.Evaluate(context); Assert.IsNull(result); writer.Close(); Assert.AreEqual("Hello, World\r\n", writer.ToString()); }
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); }
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); }
public void EvaluateReturnCallExpression() { Context context = new Context(); context.SetValue("one", 1); context.SetValue("return", new Return()); var expr = new CallExpression(new NameExpression("return"), new IExpression[] { new NameExpression("one") }, null); var result = expr.Evaluate(context); Assert.IsNotNull(result); Assert.AreEqual(1, result); Assert.IsTrue(context.HasReturnValue); Assert.AreEqual(1, context.ReturnValue); }
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); } }
public void EvaluateCallExpression() { Function func = new Function(new string[] { "a", "b" }, new ReturnCommand(new BinaryOperatorExpression(BinaryOperator.Add, new NameExpression("a"), new NameExpression("b")))); var context = new Context(); context.SetValue("add", func); IList <IExpression> exprs = new List <IExpression>() { new ConstantExpression(1), new ConstantExpression(2) }; var expr = new CallExpression("add", exprs); var result = expr.Evaluate(context); Assert.IsNotNull(result); Assert.AreEqual(3, result); }
public void ExecuteCallExpression() { Context context = new Context(); IList <ICommand> commandlist = new List <ICommand>(); commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1))); commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2))); commandlist.Add(new ExpressionCommand(new VariableExpression("b"))); CompositeCommand commands = new CompositeCommand(commandlist); DefinedMethod method = new DefinedMethod(null, commands); CallExpression callexpr = new CallExpression(new ConstantExpression(method), null); object result = callexpr.Evaluate(context); Assert.AreEqual(2, result); }
public void EvaluateMakeVectorCallExpression() { Context context = new Context(); context.SetValue("one", 1); context.SetValue("two", 2); context.SetValue("three", 3); context.SetValue("c", new MakeVector()); var expr = new CallExpression(new NameExpression("c"), new IExpression[] { new NameExpression("one"), new NameExpression("two"), new NameExpression("three") }, null); var result = expr.Evaluate(context); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(Vector)); var vector = (Vector)result; Assert.AreEqual(3, vector.Length); Assert.AreEqual(1, vector[0]); Assert.AreEqual(2, vector[1]); Assert.AreEqual(3, vector[2]); }