Permalink
Browse files

TryCommand with Finally, first tests

  • Loading branch information...
1 parent f5abd8c commit 8d6b2911e84bb91d206cee117c63d134d44d6dd6 @ajlopez committed Nov 20, 2012
@@ -0,0 +1,66 @@
+namespace PythonSharp.Tests.Commands
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Commands;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Expressions;
+
+ [TestClass]
+ public class TryCommandTests
+ {
+ [TestMethod]
+ 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"));
+ }
+
+ [TestMethod]
+ public void CreateAndExecuteTryCommandWithFinally()
+ {
+ BindingEnvironment environment = new BindingEnvironment();
+ ICommand body = new SetCommand("a", new ConstantExpression(1));
+ ICommand @finally = new SetCommand("b", new ConstantExpression(2));
+ TryCommand command = new TryCommand(body);
+ command.SetFinally(@finally);
+
+ command.Execute(environment);
+
+ Assert.AreEqual(1, environment.GetValue("a"));
+ Assert.AreEqual(2, environment.GetValue("b"));
+ }
+
+ [TestMethod]
+ 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"));
+ }
+ }
+}
@@ -53,6 +53,7 @@
<Compile Include="Commands\SetAttributeCommandTests.cs" />
<Compile Include="Commands\SetCommandTests.cs" />
<Compile Include="Commands\SetIndexCommandTests.cs" />
+ <Compile Include="Commands\TryCommandTests.cs" />
<Compile Include="Commands\WhileCommandTests.cs" />
<Compile Include="EvaluateTests.cs" />
<Compile Include="ExamplesTests.cs" />
@@ -0,0 +1,40 @@
+namespace PythonSharp.Commands
+{
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ using PythonSharp.Expressions;
+ using PythonSharp.Language;
+
+ public class TryCommand : ICommand
+ {
+ private ICommand command;
+ private ICommand finallyCommand;
+
+ public TryCommand(ICommand command)
+ {
+ this.command = command;
+ }
+
+ public ICommand Command { get { return this.command; } }
+
+ public void SetFinally(ICommand finallyCommand)
+ {
+ this.finallyCommand = finallyCommand;
+ }
+
+ public void Execute(IContext context)
+ {
+ try
+ {
+ this.command.Execute(context);
+ }
+ finally
+ {
+ if (this.finallyCommand != null)
+ this.finallyCommand.Execute(context);
+ }
+ }
+ }
+}
@@ -50,6 +50,7 @@
<Compile Include="Commands\CompositeCommand.cs" />
<Compile Include="Commands\ContinueCommand.cs" />
<Compile Include="Commands\BreakCommand.cs" />
+ <Compile Include="Commands\TryCommand.cs" />
<Compile Include="Commands\SetIndexCommand.cs" />
<Compile Include="Commands\ForCommand.cs" />
<Compile Include="Commands\SetAttributeCommand.cs" />
View
@@ -86,7 +86,8 @@ loop variants
= raise exception if named parameter precedes positional parameter
= named parameters in invocation
+ parameters with default values
-= *arg
++ *arg
+*arg at invocation
global, nonlocal (sect 9.2.1 Doc 3.3)
@@ -118,18 +119,18 @@ Import
- Escaped charactes in strings "\n\r"
- multiple args in print: print 'The Killer', joke
= if "python" in text:
- print text
-- for x in mylist:
- print x
-- while X > Y:
- print 'hello'
-- while True:
+ print(text)
++ for x in mylist:
+ print(x)
++ while X > Y:
+ print('hello')
++ while True:
pass
-- while True:
++ while True:
if not line: break
-- elif
-- else
++ elif
++ else
+ pass
+ break
+ continue

0 comments on commit 8d6b291

Please sign in to comment.