SetValue() public méthode

public SetValue ( string name, object value ) : void
name string
value object
Résultat void
Exemple #1
0
        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"));
        }
Exemple #2
0
        public void ExecuteDeleteCommand()
        {
            DynamicObject dynobj = new DynamicObject();
            dynobj.SetValue("name", "Adam");
            dynobj.SetValue("age", 800);

            DeleteCommand cmd = new DeleteCommand(new DotExpression(new VariableExpression("adam"), "name"));

            var context = new Context();
            context.SetValue("adam", dynobj);

            cmd.Execute(context);

            Assert.IsTrue(dynobj.HasName("age"));
            Assert.IsFalse(dynobj.HasName("name"));
        }
        public void ExecuteForEachCommand()
        {
            IExpression addToX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("b"), new VariableExpression("a"));
            ICommand setX = new SetVariableCommand("a", addToX);
            IExpression values = new ConstantExpression(new int [] { 1, 2, 3 } );

            ForEachCommand foreachcmd = new ForEachCommand("b", values, setX);

            Context context = new Context();

            context.SetValue("a", 0);

            foreachcmd.Execute(context);

            Assert.AreEqual(6, context.GetValue("a"));
        }
Exemple #4
0
        public void ExecuteWhileCommand()
        {
            IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("a"));
            IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("b"), new ConstantExpression(1));
            ICommand setX = new SetVariableCommand("a", incrementX);
            ICommand setY = new SetVariableCommand("b", decrementY);
            List<ICommand> commands = new List<ICommand>();
            commands.Add(setX);
            commands.Add(setY);
            ICommand command = new CompositeCommand(commands);
            IExpression yexpr = new VariableExpression("b");

            WhileCommand whilecmd = new WhileCommand(yexpr, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("b", 5);

            whilecmd.Execute(context);

            Assert.AreEqual(0, context.GetValue("b"));
            Assert.AreEqual(5, context.GetValue("a"));
        }