Exemplo n.º 1
0
        public void CreateAndExecuteDefineFunctionCommand()
        {
            IEnumerable <ICommand> commands = new ICommand[] {
                new SetVariableCommand("a", new ConstantExpression(1)),
                new SetVariableCommand("b", new ConstantExpression(2))
            };

            CompositeCommand      body    = new CompositeCommand(commands);
            DefineFunctionCommand command = new DefineFunctionCommand("foo", null, body);

            Context context = new Context();

            Assert.IsNull(command.Execute(context));
            Assert.AreEqual("foo", command.Name);
            Assert.IsNull(command.ArgumentNames);
            Assert.AreEqual(body, command.Command);

            var result = context.GetValue("foo");

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

            DefinedFunction dfunc = (DefinedFunction)result;

            Assert.AreEqual(2, dfunc.Call(context, null));
        }
        public void CreateAndExecuteDefineFunctionCommand()
        {
            IEnumerable<ICommand> commands = new ICommand[] {
                new SetVariableCommand("a", new ConstantExpression(1)),
                new SetVariableCommand("b", new ConstantExpression(2))
            };

            CompositeCommand body = new CompositeCommand(commands);
            DefineFunctionCommand command = new DefineFunctionCommand("foo", null, body);

            Context context = new Context();
            Assert.IsNull(command.Execute(context));
            Assert.AreEqual("foo", command.Name);
            Assert.IsNull(command.ArgumentNames);
            Assert.AreEqual(body, command.Command);

            var result = context.GetValue("foo");

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

            DefinedFunction dfunc = (DefinedFunction)result;

            Assert.AreEqual(2, dfunc.Call(context, null));
        }
Exemplo n.º 3
0
        public void ParseMainFunctionDefinition()
        {
            Parser parser = new Parser("main() { a = 1; b = 2; }");
            var    result = parser.ParseCommand();

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

            DefineFunctionCommand command = (DefineFunctionCommand)result;

            Assert.AreEqual("main", command.Name);
            Assert.That(command.Command is CompositeCommand);
        }
Exemplo n.º 4
0
        public void ParseMainFunctionDefinition()
        {
            Parser parser = new Parser("main() { a = 1; b = 2; }");
            var    result = parser.ParseCommand();

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

            DefineFunctionCommand command = (DefineFunctionCommand)result;

            Assert.AreEqual("main", command.Name);
            Assert.IsInstanceOfType(command.Command, typeof(CompositeCommand));
        }
Exemplo n.º 5
0
        public void ParseIntFunctionDefinitionWithTwoParameters()
        {
            Parser parser = new Parser("int myfun(int a, int b) { return a+b; }");
            var    result = parser.ParseCommand();

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

            DefineFunctionCommand command = (DefineFunctionCommand)result;

            Assert.AreEqual("myfun", command.Name);
            Assert.IsNotNull(command.ArgumentNames);
            Assert.AreEqual(2, command.ArgumentNames.Count());
            Assert.That(command.Command is CompositeCommand);
        }
Exemplo n.º 6
0
        public void ParseIntFunctionDefinitionWithParameter()
        {
            Parser parser = new Parser("int myfun(int a) { return a+1; }");
            var    result = parser.ParseCommand();

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

            DefineFunctionCommand command = (DefineFunctionCommand)result;

            Assert.AreEqual("myfun", command.Name);
            Assert.IsNotNull(command.ArgumentNames);
            Assert.AreEqual(1, command.ArgumentNames.Count());
            Assert.IsInstanceOfType(command.Command, typeof(CompositeCommand));
        }
Exemplo n.º 7
0
        public void ParseFunctionDefinition()
        {
            ICommand command = ParseCommand("function Abs(x) { if (x<0) return -x; else return x * factorial(x-1); }");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(DefineFunctionCommand));

            DefineFunctionCommand defcmd = (DefineFunctionCommand)command;

            Assert.AreEqual("Abs", defcmd.FunctionName);
            Assert.AreEqual(1, defcmd.ParameterNames.Length);
            Assert.IsFalse(defcmd.HasVariableParameters);
            Assert.AreEqual("x", defcmd.ParameterNames[0]);
            Assert.IsNotNull(defcmd.Body);
            Assert.IsInstanceOfType(defcmd.Body, typeof(CompositeCommand));
            Assert.IsFalse(defcmd.IsDefault);
        }
Exemplo n.º 8
0
        public void ParseFunctionDefinitionWithVariableArguments()
        {
            ICommand command = ParseCommand("function Foo(par1,pars...) { return par1 + pars.Count; }");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(DefineFunctionCommand));

            DefineFunctionCommand defcmd = (DefineFunctionCommand)command;

            Assert.AreEqual("Foo", defcmd.FunctionName);
            Assert.AreEqual(2, defcmd.ParameterNames.Length);
            Assert.IsTrue(defcmd.HasVariableParameters);
            Assert.AreEqual("par1", defcmd.ParameterNames[0]);
            Assert.AreEqual("pars", defcmd.ParameterNames[1]);
            Assert.IsNotNull(defcmd.Body);
            Assert.IsInstanceOfType(defcmd.Body, typeof(CompositeCommand));
            Assert.IsFalse(defcmd.IsDefault);
        }