Ejemplo n.º 1
0
        public void ExecuteIfCommandWithFalseCondition()
        {
            IfCommand ifcmd = new IfCommand(new ConstantExpression(false), new SetCommand("one", new ConstantExpression(1)));
            Machine machine = new Machine();

            ifcmd.Execute(machine.Environment);

            Assert.IsNull(machine.Environment.GetValue("one"));
        }
Ejemplo n.º 2
0
        public void ExecuteIfCommandWithTrueCondition()
        {
            IfCommand ifcmd = new IfCommand(new ConstantExpression(true), new SetCommand("one", new ConstantExpression(1)));
            Machine machine = new Machine();

            ifcmd.Execute(machine.Environment);

            Assert.IsNotNull(ifcmd.ThenCommand);
            Assert.IsNull(ifcmd.ElseCommand);
            Assert.AreEqual(1, machine.Environment.GetValue("one"));
        }
Ejemplo n.º 3
0
        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"));
        }
Ejemplo n.º 4
0
        public void CreateAndEvaluateSimpleWhileCommandWithContinue()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("a", 1);
            IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10));
            ICommand ifcmd = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new ContinueCommand());
            ICommand setcmd = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add));
            ICommand body = new CompositeCommand(new ICommand[] { setcmd, ifcmd, setcmd });

            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"));
        }