Permalink
Browse files

Exit function, Context Functions with tests and machine

  • Loading branch information...
1 parent aa973ed commit a1e2bbfd58a43de8848c8222c7ef24927361f1d6 @ajlopez committed Nov 15, 2012
@@ -0,0 +1,4 @@
+"Module 1"
+
+def foo():
+ pass
@@ -0,0 +1,4 @@
+"Module 2"
+
+def foo():
+ pass
@@ -54,6 +54,16 @@
<Name>PythonSharp</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <None Include="Lib\module2\__init__.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Lib\module1.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -0,0 +1,62 @@
+namespace PythonSharp.Tests.Functions
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Functions;
+ using PythonSharp.Language;
+
+ [TestClass]
+ public class ContextFunctionTests
+ {
+ [TestMethod]
+ public void GetLocalContext()
+ {
+ BindingEnvironment global = new BindingEnvironment();
+ BindingEnvironment local = new BindingEnvironment(global);
+ ContextFunction function = new ContextFunction("locals", false);
+
+ var result = function.Apply(local, null, null);
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual(local, result);
+ Assert.IsFalse(function.IsGlobal);
+ Assert.AreEqual("locals", function.Name);
+ }
+
+ [TestMethod]
+ public void GetGlobalContext()
+ {
+ BindingEnvironment global = new BindingEnvironment();
+ BindingEnvironment local = new BindingEnvironment(global);
+ ContextFunction function = new ContextFunction("globals", true);
+
+ var result = function.Apply(local, null, null);
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual(global, result);
+ Assert.IsTrue(function.IsGlobal);
+ Assert.AreEqual("globals", function.Name);
+ }
+
+ [TestMethod]
+ public void RaiseWhenTwoParameters()
+ {
+ ContextFunction function = new ContextFunction("locals", false);
+
+ try
+ {
+ function.Apply(null, new object[] { 1, 2 }, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("locals() takes no arguments (2 given)", ex.Message);
+ }
+ }
+ }
+}
@@ -0,0 +1,49 @@
+namespace PythonSharp.Tests.Functions
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Functions;
+ using PythonSharp.Language;
+
+ [TestClass]
+ public class ExitFunctionTests
+ {
+ [TestMethod]
+ public void RaiseMoreThanTwoParameters()
+ {
+ ExitFunction function = new ExitFunction();
+
+ try
+ {
+ function.Apply(null, new object[] { 1, 2, 3, 4 }, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("range expected at most 1 arguments, got 4", ex.Message);
+ }
+ }
+
+ [TestMethod]
+ public void RaiseIfSomeParameterIsFloat()
+ {
+ ExitFunction function = new ExitFunction();
+
+ try
+ {
+ function.Apply(null, new object[] { 1.0 }, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("'float' object cannot be interpreted as an integer", ex.Message);
+ }
+ }
+ }
+}
@@ -7,6 +7,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PythonSharp;
using PythonSharp.Functions;
+ using PythonSharp.Language;
[TestClass]
public class MachineTest
@@ -42,6 +43,22 @@ public void BuiltinFunctions()
Assert.IsInstanceOfType(machine.Environment.GetValue("exec"), typeof(ExecFunction));
Assert.IsNotNull(machine.Environment.GetValue("dir"));
Assert.IsInstanceOfType(machine.Environment.GetValue("dir"), typeof(DirFunction));
+ Assert.IsNotNull(machine.Environment.GetValue("exit"));
+ Assert.IsInstanceOfType(machine.Environment.GetValue("exit"), typeof(ExitFunction));
+
+ var globals = machine.Environment.GetValue("globals");
+ Assert.IsNotNull(globals);
+ Assert.IsInstanceOfType(globals, typeof(ContextFunction));
+ var ctxfunction = (ContextFunction)globals;
+ Assert.IsTrue(ctxfunction.IsGlobal);
+ Assert.AreEqual("globals", ctxfunction.Name);
+
+ var locals = machine.Environment.GetValue("locals");
+ Assert.IsNotNull(globals);
+ Assert.IsInstanceOfType(globals, typeof(ContextFunction));
+ ctxfunction = (ContextFunction)locals;
+ Assert.IsFalse(ctxfunction.IsGlobal);
+ Assert.AreEqual("locals", ctxfunction.Name);
}
}
}
@@ -70,6 +70,8 @@
<Compile Include="Expressions\ParameterExpressionTests.cs" />
<Compile Include="Expressions\SlicedExpressionTests.cs" />
<Compile Include="Expressions\SliceExpressionTests.cs" />
+ <Compile Include="Functions\ContextFunctionTests.cs" />
+ <Compile Include="Functions\ExitFunctionTests.cs" />
<Compile Include="Functions\DirFunctionTests.cs" />
<Compile Include="Functions\ExecFunctionTests.cs" />
<Compile Include="Functions\EvalFunctionTests.cs" />
@@ -0,0 +1,39 @@
+namespace PythonSharp.Functions
+{
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Language;
+
+ public class ContextFunction : IFunction
+ {
+ private string name;
+ private bool isglobal;
+
+ public ContextFunction(string name, bool isglobal)
+ {
+ this.isglobal = isglobal;
+ this.name = name;
+ }
+
+ public bool IsGlobal { get { return this.isglobal; } }
+
+ public string Name { get { return this.name; } }
+
+ public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
+ {
+ int nargs = arguments == null ? 0 : arguments.Count;
+
+ if (nargs != 0)
+ throw new TypeError(string.Format("{0}() takes no arguments ({1} given)", this.name, nargs));
+
+ if (this.isglobal)
+ return context.GlobalContext;
+
+ return context;
+ }
+ }
+}
@@ -0,0 +1,30 @@
+namespace PythonSharp.Functions
+{
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Language;
+
+ public class ExitFunction : IFunction
+ {
+ public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
+ {
+ int nargs = arguments == null ? 0 : arguments.Count;
+
+ if (nargs > 1)
+ throw new TypeError(string.Format("range expected at most 1 arguments, got {0}", nargs));
+
+ int value = 0;
+
+ if (nargs > 0)
+ value = Numbers.ToInteger(arguments[0]);
+
+ System.Environment.Exit(value);
+
+ return null;
+ }
+ }
+}
@@ -21,6 +21,9 @@ public Machine()
this.environment.SetValue("eval", new EvalFunction());
this.environment.SetValue("exec", new ExecFunction());
this.environment.SetValue("dir", new DirFunction());
+ this.environment.SetValue("exit", new ExitFunction());
+ this.environment.SetValue("locals", new ContextFunction("locals", false));
+ this.environment.SetValue("globals", new ContextFunction("globals", true));
this.environment.SetValue("__machine__", this);
}
@@ -84,6 +84,8 @@
<Compile Include="Expressions\NegateExpression.cs" />
<Compile Include="Expressions\NotExpression.cs" />
<Compile Include="Expressions\ParameterExpression.cs" />
+ <Compile Include="Functions\ContextFunction.cs" />
+ <Compile Include="Functions\ExitFunction.cs" />
<Compile Include="Functions\DirFunction.cs" />
<Compile Include="Functions\ExecFunction.cs" />
<Compile Include="Functions\EvalFunction.cs" />

0 comments on commit a1e2bbf

Please sign in to comment.