public void ExecuteCompositeCommand() { BindingEnvironment environment = new BindingEnvironment(); SetVariableCommand command1 = new SetVariableCommand("foo", new ConstantExpression("bar")); SetVariableCommand command2 = new SetVariableCommand("one", new ConstantExpression(1)); SetVariableCommand command3 = new SetVariableCommand("bar", new VariableExpression("foo")); List <ICommand> commands = new List <ICommand>(); commands.Add(command1); commands.Add(command2); commands.Add(command3); CompositeCommand command = new CompositeCommand(commands); environment.SetValue("foo", null); environment.SetValue("one", null); command.Execute(environment); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.AreEqual(1, environment.GetValue("one")); Assert.AreEqual("bar", environment.GetValue("bar")); }
public void ExecuteWhileCommand() { IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("x")); IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("y"), new ConstantExpression(1)); ICommand setX = new SetVariableCommand("x", incrementX); ICommand setY = new SetVariableCommand("y", decrementY); List <ICommand> commands = new List <ICommand>(); commands.Add(setX); commands.Add(setY); ICommand command = new CompositeCommand(commands); IExpression yexpr = new VariableExpression("y"); WhileCommand whilecmd = new WhileCommand(yexpr, command); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("x", 0); environment.SetValue("y", 5); whilecmd.Execute(environment); Assert.AreEqual(0, environment.GetValue("y")); Assert.AreEqual(5, environment.GetValue("x")); }
public void ExecCommandList() { BindingEnvironment environment = new BindingEnvironment(); this.exec.Apply(environment, new object[] { "a = 1; b = 2" }, null); Assert.AreEqual(1, environment.GetValue("a")); Assert.AreEqual(2, environment.GetValue("b")); }
public void ExecuteSetLocalVariable() { BindingEnvironment environment = new BindingEnvironment(); Assert.IsNull(environment.GetValue("foo")); SetLocalVariableCommand cmd = new SetLocalVariableCommand("foo", new ConstantExpression("bar")); cmd.Execute(environment); Assert.AreEqual("bar", environment.GetValue("foo")); }
public void CreateAndExecuteTryCommandWithFinally() { BindingEnvironment environment = new BindingEnvironment(); ICommand body = new SetCommand("a", new ConstantExpression(1)); ICommand @finally = new SetCommand("b", new ConstantExpression(2)); TryCommand command = new TryCommand(body); command.SetFinally(@finally); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("a")); Assert.AreEqual(2, environment.GetValue("b")); }
public void SetValueThruBindingEnvironmentIfDefinedInObject() { this.environment.SetValue("Age", null); BindingEnvironment binding = new BindingEnvironment(this.environment); binding.SetValue("Age", 800); binding.SetValue("Local", 100); Assert.AreEqual(800, binding.GetValue("Age")); Assert.AreEqual(100, binding.GetValue("Local")); Assert.AreEqual(800, this.environment.GetValue("Age")); Assert.IsNull(this.environment.GetValue("Local")); }
public void ExecuteIfCommandElseWhenFalse() { IExpression condition = new ConstantExpression(false); ICommand setXCommand = new SetVariableCommand("x", new ConstantExpression(1)); ICommand setYCommand = new SetVariableCommand("y", new ConstantExpression(2)); IfCommand command = new IfCommand(condition, setXCommand, setYCommand); BindingEnvironment environment = new BindingEnvironment(); command.Execute(environment); Assert.IsNull(environment.GetValue("x")); Assert.AreEqual(2, environment.GetValue("y")); }
public void EvaluateIfCommandWithNullAsCondition() { IExpression condition = new ConstantExpression(null); ICommand thenCommand = new SetCommand("a", new ConstantExpression(1)); ICommand elseCommand = new SetCommand("b", new ConstantExpression(2)); IfCommand command = new IfCommand(condition, thenCommand, elseCommand); BindingEnvironment environment = new BindingEnvironment(); command.Execute(environment); Assert.IsNull(environment.GetValue("a")); Assert.AreEqual(2, environment.GetValue("b")); }
public void ExecuteSetArrayCommandWithDotExpression() { BindingEnvironment environment = new BindingEnvironment(); DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "Values"); SetArrayCommand command = new SetArrayCommand(dotexpr, new IExpression[] { new ConstantExpression(0) }, new ConstantExpression("bar")); command.Execute(environment); object obj = environment.GetValue("foo"); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(DynamicObject)); DynamicObject dynobj = (DynamicObject)obj; object obj2 = dynobj.GetValue("Values"); Assert.IsNotNull(obj2); Assert.IsInstanceOfType(obj2, typeof(IList)); IList list = (IList)obj2; Assert.AreEqual(1, list.Count); Assert.AreEqual("bar", list[0]); }
public void ExecuteSetCommandWithComplexDotExpression() { BindingEnvironment environment = new BindingEnvironment(); DotExpression dot = new DotExpression(new VariableExpression("foo"), "Address"); DotExpression dotexpr = new DotExpression(dot, "Street"); SetCommand command = new SetCommand(dotexpr, new ConstantExpression("bar")); command.Execute(environment); object obj = environment.GetValue("foo"); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(DynamicObject)); DynamicObject dynobj = (DynamicObject)obj; object obj2 = dynobj.GetValue("Address"); Assert.IsNotNull(obj2); Assert.IsInstanceOfType(obj2, typeof(DynamicObject)); DynamicObject dynobj2 = (DynamicObject)obj2; Assert.AreEqual("bar", dynobj2.GetValue("Street")); }
public void ExecuteForCommand() { ICommand setX = new SetVariableCommand("x", new ConstantExpression(0)); ICommand setY = new SetVariableCommand("y", new ConstantExpression(0)); List <ICommand> commands = new List <ICommand>(); commands.Add(setX); commands.Add(setY); ICommand initialCommand = new CompositeCommand(commands); IExpression condition = new CompareExpression(ComparisonOperator.Less, new VariableExpression("x"), new ConstantExpression(6)); IExpression addXtoY = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("y"), new VariableExpression("x")); ICommand addToY = new SetVariableCommand("y", addXtoY); ICommand endCommand = new SetVariableCommand("x", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new ConstantExpression(1))); ForCommand forcmd = new ForCommand(initialCommand, condition, endCommand, addToY); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("y", null); forcmd.Execute(environment); Assert.AreEqual(15, environment.GetValue("y")); }
public void CanCreate() { BindingEnvironment environment = new BindingEnvironment(); Assert.IsNotNull(environment); Assert.AreSame(environment, environment.GetValue("Environment")); }
public void SetAndGetValue() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("one", 1); Assert.AreEqual(1, environment.GetValue("one")); }
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 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 GetValueFromParent() { BindingEnvironment parent = new BindingEnvironment(); parent.SetValue("one", 1); BindingEnvironment environment = new BindingEnvironment(parent); Assert.AreEqual(1, environment.GetValue("one")); }
public void ExecSimpleText() { BindingEnvironment environment = new BindingEnvironment(); this.exec.Apply(environment, new object[] { "a = 1" }, null); Assert.AreEqual(1, environment.GetValue("a")); }
public void SetAndGetValue() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("foo", "bar"); Assert.AreEqual("bar", environment.GetValue("foo")); }
public void GetValueDefinedInParent() { BindingEnvironment parent = new BindingEnvironment(); parent.SetValue("foo", "bar"); BindingEnvironment environment = new BindingEnvironment(parent); Assert.AreEqual("bar", environment.GetValue("foo")); }
public void SetVariable() { IExpression expression = new ConstantExpression(1); BindingEnvironment environment = new BindingEnvironment(); ICommand command = new SetCommand("spam", expression); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("spam")); }
public void ExecuteSetCommandWithVariable() { BindingEnvironment environment = new BindingEnvironment(); SetCommand command = new SetCommand(new VariableExpression("foo"), new ConstantExpression("bar")); command.Execute(environment); Assert.AreEqual("bar", environment.GetValue("foo")); }
public void ExecuteSetCommand() { BindingEnvironment environment = new BindingEnvironment(); ICommand command = new SetCommand("one", new ConstantExpression(1)); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("one")); }
public void ExecuteIfCommandWhenTrue() { IExpression condition = new ConstantExpression(true); ICommand setCommand = new SetVariableCommand("x", new ConstantExpression(1)); IfCommand command = new IfCommand(condition, setCommand); BindingEnvironment environment = new BindingEnvironment(); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("x")); }
public void DefineAndSetGlobalValue() { Machine machine = new Machine(); BindingEnvironment environment = new BindingEnvironment(); environment.DefineGlobal("global"); environment.SetValue("global", 100); Assert.AreEqual(100, environment.GetValue("global")); Assert.AreEqual(100, machine.Environment.GetValue("global")); }
public void SetLocalValue() { BindingEnvironment parent = new BindingEnvironment(); parent.SetValue("foo", "bar"); BindingEnvironment environment = new BindingEnvironment(parent); environment.SetValue("foo", "newbar"); Assert.AreEqual("newbar", environment.GetValue("foo")); Assert.AreEqual("bar", parent.GetValue("foo")); }
public void ResolveVariableExpressionToDictionary() { BindingEnvironment environment = new BindingEnvironment(); IExpression expression = new VariableExpression("foo"); object obj = ExpressionUtilities.ResolveToDictionary(expression, environment); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IDictionary)); Assert.AreEqual(obj, environment.GetValue("foo")); }
public void ExecuteSimpleForOnEmptyList() { ICommand body = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("b", 0); ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { }), body); command.Execute(environment); Assert.AreEqual(0, environment.GetValue("b")); }
public void ExecuteClassCommandWithEmptyMethod() { BindingEnvironment environment = new BindingEnvironment(); ClassCommand command = new ClassCommand("Spam", new DefCommand("foo", null, new PassCommand())); command.Execute(environment); var result = (DefinedClass)environment.GetValue("Spam"); Assert.AreEqual("Spam", result.Name); Assert.IsNotNull(result.GetMethod("foo")); }
public void SetValueThruLocalEnviroment() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("one", 0); LocalBindingEnvironment local = new LocalBindingEnvironment(environment); local.SetValue("one", 1); Assert.AreEqual(1, local.GetValue("one")); Assert.AreEqual(1, environment.GetValue("one")); }
public void SetUndefinedValueUsingTwoLocalEnviroments() { BindingEnvironment environment = new BindingEnvironment(); LocalBindingEnvironment local = new LocalBindingEnvironment(environment); LocalBindingEnvironment local2 = new LocalBindingEnvironment(local); local2.SetValue("one", 1); Assert.AreEqual(1, local.GetValue("one")); Assert.AreEqual(1, local2.GetValue("one")); Assert.AreEqual(1, environment.GetValue("one")); }
public void CreateSimpleClassWithEmptyDocString() { BindingEnvironment environment = new BindingEnvironment(); ClassCommand command = new ClassCommand("Spam", new PassCommand()); command.Execute(environment); var result = environment.GetValue("Spam"); var dclass = (DefinedClass)result; Assert.IsNull(dclass.GetValue("__doc__")); Assert.IsTrue(dclass.HasValue("__doc__")); }
public void ExecuteFinallyEvenWhenRaiseException() { BindingEnvironment environment = new BindingEnvironment(); ICommand body = new SetCommand("a", new NameExpression("c")); ICommand @finally = new SetCommand("b", new ConstantExpression(2)); TryCommand command = new TryCommand(body); command.SetFinally(@finally); try { command.Execute(environment); Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NameError)); Assert.AreEqual("name 'c' is not defined", ex.Message); } Assert.IsNull(environment.GetValue("a")); Assert.AreEqual(2, environment.GetValue("b")); }
public void ExecuteCompositeCommandWithReturn() { SetCommand command1 = new SetCommand("foo", new ConstantExpression("bar")); ReturnCommand command2 = new ReturnCommand(new ConstantExpression("spam")); SetCommand command3 = new SetCommand("one", new ConstantExpression(1)); CompositeCommand command = new CompositeCommand(); command.AddCommand(command1); command.AddCommand(command2); command.AddCommand(command3); Machine machine = new Machine(); BindingEnvironment environment = new BindingEnvironment(machine.Environment); command.Execute(environment); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.IsNull(environment.GetValue("one")); Assert.IsTrue(environment.HasReturnValue()); Assert.AreEqual("spam", environment.GetReturnValue()); Assert.IsNotNull(command.Commands); }
public void DefineAndSetGlobalValueThruLocalEnvironment() { Machine machine = new Machine(); BindingEnvironment environment = new BindingEnvironment(); LocalBindingEnvironment local = new LocalBindingEnvironment(environment); local.DefineGlobal("global"); local.SetValue("global", 100); Assert.AreEqual(100, local.GetValue("global")); Assert.AreEqual(100, environment.GetValue("global")); Assert.AreEqual(100, machine.Environment.GetValue("global")); }
public void CreateAndEvaluateSimpleWhileCommand() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10)); ICommand body = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add)); WhileCommand command = new WhileCommand(condition, body); command.Execute(environment); Assert.AreEqual(condition, command.Condition); Assert.AreEqual(body, command.Command); Assert.AreEqual(10, environment.GetValue("a")); }
public void ExecuteSimpleForWithContinue() { ICommand ifcmd = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new ContinueCommand()); ICommand setcmd = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)); ICommand body = new CompositeCommand(new ICommand[] { ifcmd, setcmd }); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("b", 0); ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { 1, 2, 3 }), body); command.Execute(environment); Assert.AreEqual(4, environment.GetValue("b")); }
public void ExecuteSimpleClassCommand() { BindingEnvironment environment = new BindingEnvironment(); ClassCommand command = new ClassCommand("Spam", new PassCommand()); command.Execute(environment); var result = environment.GetValue("Spam"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedClass)); var dclass = (DefinedClass)result; Assert.AreEqual("Spam", dclass.Name); Assert.IsNull(dclass.GetValue("__doc__")); Assert.IsTrue(dclass.HasValue("__doc__")); }
public void ResolveDotExpressionToDictionary() { BindingEnvironment environment = new BindingEnvironment(); IExpression expression = new DotExpression(new VariableExpression("Project"), "Entities"); object obj = ExpressionUtilities.ResolveToDictionary(expression, environment); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IDictionary)); object project = environment.GetValue("Project"); Assert.IsNotNull(project); Assert.IsInstanceOfType(project, typeof(IObject)); object entities = ((IObject)project).GetValue("Entities"); Assert.IsNotNull(entities); Assert.IsInstanceOfType(entities, typeof(IDictionary)); Assert.AreEqual(obj, entities); }
public void ResolveDotExpressionToObject() { BindingEnvironment environment = new BindingEnvironment(); IExpression expression = new DotExpression(new VariableExpression("Project"), "Title"); object obj = ExpressionUtilities.ResolveToObject(expression, environment); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IObject)); object project = environment.GetValue("Project"); Assert.IsNotNull(project); Assert.IsInstanceOfType(project, typeof(IObject)); object title = ((IObject)project).GetValue("Title"); Assert.IsNotNull(title); Assert.IsInstanceOfType(title, typeof(IObject)); Assert.AreEqual(obj, title); }
public void ExecuteClassCommandWithInheritance() { DefinedClass fooclass = new DefinedClass("Foo"); DefinedClass barclass = new DefinedClass("Bar"); BindingEnvironment environment = new BindingEnvironment(); ClassCommand command = new ClassCommand("Spam", new IExpression[] { new ConstantExpression(fooclass), new ConstantExpression(barclass) }, new PassCommand()); command.Execute(environment); var result = environment.GetValue("Spam"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedClass)); var dclass = (DefinedClass)result; Assert.AreEqual("Spam", dclass.Name); Assert.IsNotNull(dclass.Bases); Assert.AreEqual(2, dclass.Bases.Count); Assert.AreEqual(fooclass, dclass.Bases[0]); Assert.AreEqual(barclass, dclass.Bases[1]); Assert.IsNull(dclass.GetValue("__doc__")); Assert.IsTrue(dclass.HasValue("__doc__")); }
public object Evaluate(BindingEnvironment environment) { return environment.GetValue(this.name); }
public void SetValueThruLocalAndNormalEnviroment() { BindingEnvironment parent = new BindingEnvironment(); LocalBindingEnvironment toplocal = new LocalBindingEnvironment(parent); toplocal.SetLocalValue("one", 0); BindingEnvironment environment = new BindingEnvironment(toplocal); LocalBindingEnvironment local = new LocalBindingEnvironment(environment); local.SetValue("one", 1); Assert.AreEqual(1, local.GetValue("one")); Assert.AreEqual(1, environment.GetValue("one")); Assert.AreEqual(1, toplocal.GetValue("one")); Assert.IsNull(parent.GetValue("one")); }
public void GetNullIfUnknownName() { BindingEnvironment environment = new BindingEnvironment(); Assert.IsNull(environment.GetValue("foo")); }