public void DefineVariableWithInitialValue()
        {
            Context context            = new Context();
            DefineVariableCommand expr = new DefineVariableCommand(null, "a", new ConstantExpression(1));

            Assert.AreEqual(1, expr.Execute(context));
            Assert.IsTrue(context.HasVariable("a"));
            Assert.AreEqual(1, context.GetValue("a"));
            Assert.AreEqual("a", expr.Name);
            Assert.IsNull(expr.TypeExpression);
        }
        public void DefineVariableWithTypeAndInitialValue()
        {
            Context               context  = new Context();
            IExpression           typeexpr = new VariableExpression("List");
            DefineVariableCommand expr     = new DefineVariableCommand(typeexpr, "a", new ConstantExpression(1));

            Assert.AreEqual(1, expr.Execute(context));
            Assert.IsTrue(context.HasVariable("a"));
            Assert.AreEqual(1, context.GetValue("a"));
            Assert.AreEqual("a", expr.Name);
            Assert.AreEqual(typeexpr, expr.TypeExpression);
        }
Exemplo n.º 3
0
        public void ParseDefineVariable()
        {
            Parser parser = new Parser("var a;");
            var    result = parser.ParseCommand();

            Assert.IsNotNull(result);
            Assert.That(result is DefineVariableCommand);

            DefineVariableCommand dvcmd = (DefineVariableCommand)result;

            Assert.AreEqual("a", dvcmd.Name);
            Assert.IsNull(dvcmd.Expression);
        }
Exemplo n.º 4
0
        public void ParseDefineVariableWithValue()
        {
            Parser parser = new Parser("var a = 1;");
            var    result = parser.ParseCommand();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefineVariableCommand));

            DefineVariableCommand dvcmd = (DefineVariableCommand)result;

            Assert.AreEqual("a", dvcmd.Name);
            Assert.IsNotNull(dvcmd.Expression);
            Assert.IsInstanceOfType(dvcmd.Expression, typeof(ConstantExpression));
        }
Exemplo n.º 5
0
        private ICommand ParseDefineVariableCommand(IExpression typeexpression, string name)
        {
            DefineVariableCommand command;

            if (this.TryParseToken("=", TokenType.Operator))
            {
                command = new DefineVariableCommand(typeexpression, name, this.ParseExpression());
            }
            else
            {
                command = new DefineVariableCommand(typeexpression, name);
            }

            this.ParseToken(";", TokenType.Separator);

            return(command);
        }