/// <summary> /// The main entry point for the application. /// </summary> static void Main() { Variable v = new Variable("machine"); Command c = new ForCommand(ExampleMachine.Dublin(), v, new PrintCommand(v)); c.Execute(); }
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); Context context = new Context(); context.SetValue("y", null); forcmd.Execute(context); Assert.AreEqual(15, context.GetValue("y")); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { MachineComposite dublin = ExampleMachine.Dublin(); Variable v = new Variable("machine"); Command c = new ForCommand(dublin, v, new ShutDownCommand(v)); c.Execute(); }
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 ExecuteForCommand() { IExpression fromexpr = new ConstantExpression(1); IExpression toexpr = new ConstantExpression(4); ICommand body = new ExpressionCommand(new AssignExpression("a", new BinaryOperatorExpression(BinaryOperator.Multiply, new NameExpression("a"), new NameExpression("k")))); ForCommand fcmd = new ForCommand("k", fromexpr, toexpr, body); Context context = new Context(); context.SetValue("a", 1); fcmd.Execute(context); Assert.AreEqual(24, context.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 ForWithAddExpression() { var command = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a"))); var forcommand = new ForCommand("k", new ConstantExpression(new int[] { 1, 2, 3, 4 }), command); Context context = new Context(); var result = forcommand.Execute(context); Assert.IsNull(result); Assert.AreEqual("k", forcommand.VariableName); Assert.IsNotNull(forcommand.Expression); Assert.IsNotNull(forcommand.Command); Assert.AreEqual(4, context.GetValue("k")); Assert.AreEqual(10, context.GetValue("a")); }
public void CreateAndExecuteForCommand() { var command = new SetVariableCommand("a", new VariableExpression("k")); var forcommand = new ForCommand("k", new ConstantExpression((new int[] { 1, 2, 3 })), command); Context context = new Context(); context.SetValue("a", 0); var result = forcommand.Execute(context); Assert.IsNull(result); Assert.AreEqual("k", forcommand.VariableName); Assert.IsNotNull(forcommand.Expression); Assert.IsNotNull(forcommand.Command); Assert.AreEqual(3, context.GetValue("k")); Assert.AreEqual(3, context.GetValue("a")); }
public void RaiseWhenForOverAnInteger() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("b", 0); ForCommand command = new ForCommand("a", new ConstantExpression(1), null); try { command.Execute(environment); Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(TypeError)); Assert.AreEqual("'int' object is not iterable", ex.Message); } }