Beispiel #1
0
        public void GetTypeStoredInEnvironment()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("int", typeof(int));

            Type type = TypeUtilities.GetType(environment, "int");

            Assert.IsNotNull(type);
            Assert.AreEqual(type, typeof(int));
        }
Beispiel #2
0
        public void EvaluateArrayVariableExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("array", new string[] { "one", "two", "three" });

            IExpression expression = new ArrayExpression(new VariableExpression("array"), new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual("two", result);
        }
Beispiel #3
0
        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"));
        }
Beispiel #4
0
        public void EvaluateArrayDotExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();

            DynamicObject data = new DynamicObject();
            data.SetValue("Numbers", new string[] { "one", "two", "three" });

            environment.SetValue("data", data);

            IExpression expression = new ArrayExpression(new DotExpression(new VariableExpression("data"), "Numbers"), new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual("two", result);
        }
Beispiel #5
0
        public void EvaluateVariableExpressions()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("foo", "bar");
            environment.SetValue("one", 1);

            VariableExpression varFoo = new VariableExpression("foo");
            VariableExpression varOne = new VariableExpression("one");

            Assert.AreEqual("bar", varFoo.Evaluate(environment));
            Assert.AreEqual(1, varOne.Evaluate(environment));
        }
Beispiel #6
0
        public void EvaluateSimpleNewIClassicObjectArrayExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("ADynamicClass", new DynamicClass("ADynamicClass"));

            IExpression expression = new NewArrayExpression("ADynamicClass", new IExpression[] { new ConstantExpression(10) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IClassicObject[]));

            IClassicObject[] array = (IClassicObject[])result;

            Assert.AreEqual(10, array.Length);
        }
Beispiel #7
0
        public void EvaluateNewExpressionWithAliasedType()
        {
            IExpression expression = new NewExpression("Channel", null);
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("Channel", typeof(Channel));

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Channel));
        }
Beispiel #8
0
        public void EvaluateInvokeExpression()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("foo", function);

            IExpression expression = new InvokeExpression("foo", new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }
Beispiel #9
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);

            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("y", null);

            forcmd.Execute(environment);

            Assert.AreEqual(15, environment.GetValue("y"));
        }
Beispiel #10
0
        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"));
        }
Beispiel #11
0
        public void ExecuteGlobalCommand()
        {
            GlobalCommand command = new GlobalCommand("global");
            Machine machine = new Machine();

            IBindingEnvironment environment = new BindingEnvironment();

            command.Execute(environment);

            environment.SetValue("global", 100);

            Assert.IsFalse(environment.ContainsName("global"));
            Assert.IsTrue(machine.Environment.ContainsName("global"));
            Assert.AreEqual(100, machine.Environment.GetValue("global"));
        }
Beispiel #12
0
        public void ExecuteForEachCommand()
        {
            IExpression addToX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("y"), new VariableExpression("x"));
            ICommand setX = new SetVariableCommand("x", addToX);
            IExpression values = new ConstantExpression(new int[] { 1, 2, 3 });

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

            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("x", 0);

            foreachcmd.Execute(environment);

            Assert.AreEqual(6, environment.GetValue("x"));
        }