예제 #1
0
        public void CreateAndExecuteTryCommand()
        {
            BindingEnvironment environment = new BindingEnvironment();
            ICommand body = new SetCommand("a", new ConstantExpression(1));
            TryCommand command = new TryCommand(body);

            command.Execute(environment);

            Assert.AreEqual(1, environment.GetValue("a"));
        }
예제 #2
0
        public void ExecuteFinallyEvenWhenRaiseException()
        {
            BindingEnvironment environment = new BindingEnvironment();
            ICommand body = new SetCommand("a", new NameExpression("c"));
            ICommand @finally = new SetCommand("b", new ConstantExpression(2));
            TryCommand command = new TryCommand(body);
            command.SetFinally(@finally);

            try
            {
                command.Execute(environment);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(NameError));
                Assert.AreEqual("name 'c' is not defined", ex.Message);
            }

            Assert.IsNull(environment.GetValue("a"));
            Assert.AreEqual(2, environment.GetValue("b"));
        }
예제 #3
0
        private ICommand CompileTryCommand()
        {
            ICommand command = this.CompileSuite();

            var trycommand = new TryCommand(command);

            int indent = this.lexer.NextIndent();

            while (indent == this.indent)
            {
                if (this.TryCompile(TokenType.Name, "finally"))
                {
                    if (trycommand.Finally != null)
                        throw new SyntaxError("invalid syntax");

                    ICommand finallycommand = this.CompileSuite();
                    trycommand.SetFinally(finallycommand);
                }
                else
                    break;

                indent = this.lexer.NextIndent();
            }

            this.lexer.PushIndent(indent);

            return trycommand;
        }