public void SetVariable()
        {
            Context context = new Context();
            SetVariableCommand command = new SetVariableCommand("One", new ConstantExpression(1));
            object result = command.Execute(context);

            Assert.AreEqual(1, result);
            Assert.AreEqual(1, context.GetValue("One"));
        }
Exemple #2
0
        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"));
        }
Exemple #3
0
        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"));
        }
Exemple #4
0
        public void IfTrueThen()
        {
            var condition = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10));
            var thencommand = new SetVariableCommand("k", new ConstantExpression(1));

            var ifcommand = new IfCommand(condition, thencommand);

            Context context = new Context();
            context.SetValue("a", 1);
            context.SetValue("k", 0);

            var result = ifcommand.Execute(context);

            Assert.IsNull(result);
            Assert.IsNotNull(ifcommand.Condition);
            Assert.IsNotNull(ifcommand.ThenCommand);
            Assert.IsNull(ifcommand.ElseCommand);

            Assert.AreEqual(1, context.GetValue("k"));
        }
Exemple #5
0
        public void WhileWithAddExpression()
        {
            var expression = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10));
            var inccommand = new SetVariableCommand("k", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new ConstantExpression(1)));
            var addcommand = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a")));
            var command = new CompositeCommand(new ICommand[] { inccommand, addcommand });
            var whilecommand = new WhileCommand(expression, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("k", 0);

            var result = whilecommand.Execute(context);

            Assert.IsNull(result);
            Assert.IsNotNull(whilecommand.Condition);
            Assert.IsNotNull(whilecommand.Command);
            Assert.AreEqual(4, context.GetValue("k"));
            Assert.AreEqual(10, context.GetValue("a"));
        }