Skip to content
Browse files

Break, Continue Command with first tests in For

  • Loading branch information...
1 parent b06b422 commit f16faa996ecf7f75b9202d1dd74db0e5bccc5427 @ajlopez committed Nov 18, 2012
View
34 Src/PythonSharp.Tests/Commands/ForCommandTests.cs
@@ -80,5 +80,39 @@ public void RaiseWhenForOverAnInteger()
Assert.AreEqual("'int' object is not iterable", ex.Message);
}
}
+
+ [TestMethod]
+ public void ExecuteSimpleForWithContinue()
+ {
+ ICommand ifcmd = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new ContinueCommand());
+ ICommand setcmd = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
+ ICommand body = new CompositeCommand(new ICommand[] { ifcmd, setcmd });
+
+ BindingEnvironment environment = new BindingEnvironment();
+ environment.SetValue("b", 0);
+
+ ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { 1, 2, 3 }), body);
+
+ command.Execute(environment);
+
+ Assert.AreEqual(4, environment.GetValue("b"));
+ }
+
+ [TestMethod]
+ public void ExecuteSimpleForWithBreak()
+ {
+ ICommand ifcmd = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new BreakCommand());
+ ICommand setcmd = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
+ ICommand body = new CompositeCommand(new ICommand[] { ifcmd, setcmd });
+
+ BindingEnvironment environment = new BindingEnvironment();
+ environment.SetValue("b", 0);
+
+ ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { 1, 2, 3 }), body);
+
+ command.Execute(environment);
+
+ Assert.AreEqual(1, environment.GetValue("b"));
+ }
}
}
View
4 Src/PythonSharp/BindingEnvironment.cs
@@ -35,6 +35,10 @@ public IContext GlobalContext
}
}
+ public bool WasContinue { get; set; }
+
+ public bool WasBreak { get; set; }
+
public bool HasReturnValue()
{
return this.hasReturnValue;
View
18 Src/PythonSharp/Commands/BreakCommand.cs
@@ -0,0 +1,18 @@
+namespace PythonSharp.Commands
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Expressions;
+ using PythonSharp.Language;
+
+ public class BreakCommand : ICommand
+ {
+ public void Execute(IContext context)
+ {
+ BindingEnvironment environment = (BindingEnvironment)context;
+ environment.WasBreak = true;
+ }
+ }
+}
View
2 Src/PythonSharp/Commands/CompositeCommand.cs
@@ -35,7 +35,7 @@ public void Execute(IContext context)
foreach (ICommand command in this.commands)
{
command.Execute(context);
- if (environment != null && environment.HasReturnValue())
+ if (environment != null && (environment.HasReturnValue() || environment.WasContinue || environment.WasBreak))
break;
}
}
View
18 Src/PythonSharp/Commands/ContinueCommand.cs
@@ -0,0 +1,18 @@
+namespace PythonSharp.Commands
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Expressions;
+ using PythonSharp.Language;
+
+ public class ContinueCommand : ICommand
+ {
+ public void Execute(IContext context)
+ {
+ BindingEnvironment environment = (BindingEnvironment)context;
+ environment.WasContinue = true;
+ }
+ }
+}
View
15 Src/PythonSharp/Commands/ForCommand.cs
@@ -38,9 +38,20 @@ public void Execute(IContext context)
{
context.SetValue(this.name, item);
this.command.Execute(context);
- if (environment != null && environment.HasReturnValue())
- return;
+ if (environment != null)
+ {
+ if (environment.HasReturnValue())
+ return;
+ }
+ if (environment.WasBreak)
+ {
+ environment.WasBreak = false;
+ break;
+ }
+ if (environment.WasContinue)
+ environment.WasContinue = false;
}
}
}
}
+
View
2 Src/PythonSharp/PythonSharp.csproj
@@ -48,6 +48,8 @@
<ItemGroup>
<Compile Include="Commands\ClassCommand.cs" />
<Compile Include="Commands\CompositeCommand.cs" />
+ <Compile Include="Commands\ContinueCommand.cs" />
+ <Compile Include="Commands\BreakCommand.cs" />
<Compile Include="Commands\SetIndexCommand.cs" />
<Compile Include="Commands\ForCommand.cs" />
<Compile Include="Commands\SetAttributeCommand.cs" />

0 comments on commit f16faa9

Please sign in to comment.
Something went wrong with that request. Please try again.