From 3bc847b1858ba383c67851e681faca9ff93fff2e Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 10 May 2009 14:06:52 +0000 Subject: [PATCH] Static class methods now use staticmethod when converted to Python. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4070 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/NRefactoryToPythonConverter.cs | 40 +++++++++--- ...onvertToPythonProjectCommandTestFixture.cs | 2 + .../ObjectInitializerConversionTestFixture.cs | 4 +- .../StaticMethodConversionTestFixture.cs | 61 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 5 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs index c8c3841738..7e870544ea 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs @@ -772,10 +772,11 @@ namespace ICSharpCode.PythonBinding public object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) { // Add method name. - AppendIndented("def " + methodDeclaration.Name); + string methodName = methodDeclaration.Name; + AppendIndented("def " + methodName); // Add the parameters. - AddParameters(methodDeclaration.Parameters); + AddParameters(methodDeclaration); methodParameters = methodDeclaration.Parameters; AppendNewLine(); @@ -789,6 +790,11 @@ namespace ICSharpCode.PythonBinding DecreaseIndent(); AppendNewLine(); + if (IsStatic(methodDeclaration)) { + AppendIndentedLine(methodDeclaration.Name + " = staticmethod(" + methodDeclaration.Name + ")"); + AppendNewLine(); + } + return null; } @@ -1531,7 +1537,7 @@ namespace ICSharpCode.PythonBinding { if (constructorInfo.Constructor != null) { AppendIndented("def __init__"); - AddParameters(constructorInfo.Constructor.Parameters); + AddParameters(constructorInfo.Constructor); methodParameters = constructorInfo.Constructor.Parameters; } else { AppendIndented("def __init__(self):"); @@ -1587,19 +1593,33 @@ namespace ICSharpCode.PythonBinding /// /// Adds the method or constructor parameters. /// - void AddParameters(List parameters) + void AddParameters(ParametrizedNode method) { + Append("("); + List parameters = method.Parameters; if (parameters.Count > 0) { - Append("(self"); - foreach (ParameterDeclarationExpression parameter in parameters) { - Append(", " + parameter.ParameterName); + if (!IsStatic(method)) { + Append("self, "); + } + for (int i = 0; i < parameters.Count; ++i) { + if (i > 0) { + Append(", "); + } + Append(parameters[i].ParameterName); } - Append("):"); } else { - Append("(self):"); + if (!IsStatic(method)) { + Append("self"); + } } + Append("):"); } - + + bool IsStatic(ParametrizedNode method) + { + return (method.Modifier & Modifiers.Static) == Modifiers.Static; + } + /// /// Creates assignments of the form: /// i = 1 diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs index 62d57f03a5..44a1fd3465 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs @@ -39,6 +39,8 @@ namespace PythonBinding.Tests.Converter [TestFixtureSetUp] public void SetUpFixture() { + MSBuildEngineHelper.InitMSBuildEngine(); + List bindings = new List(); using (TextReader reader = PythonBindingAddInFile.ReadAddInFile()) { AddIn addin = AddIn.Load(reader, String.Empty); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs index 738fa214b3..4c22769c66 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs @@ -34,7 +34,7 @@ namespace PythonBinding.Tests.Converter " set { lastName = value; }\r\n" + " }\r\n" + "\r\n" + - " public static Class1 Create()\r\n" + + " public Class1 Clone()\r\n" + " {\r\n" + " return new Class1 { Name = \"First\", LastName = \"Last\" };\r\n" + " }\r\n" + @@ -66,7 +66,7 @@ namespace PythonBinding.Tests.Converter "\r\n" + "\tLastName = property(fget=get_LastName, fset=set_LastName)\r\n" + "\r\n" + - "\tdef Create(self):\r\n" + + "\tdef Clone(self):\r\n" + "\t\treturn Class1(Name = \"First\", LastName = \"Last\")"; Assert.AreEqual(expectedPython, python); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs new file mode 100644 index 0000000000..a7b02dd964 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs @@ -0,0 +1,61 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.CodeDom; +using System.CodeDom.Compiler; +using ICSharpCode.NRefactory; +using ICSharpCode.PythonBinding; +using NUnit.Framework; + +namespace PythonBinding.Tests.Converter +{ + /// + /// Static methods should not use "self" and they should by defined by using "staticmethod". + /// + [TestFixture] + public class StaticMethodConversionTestFixture + { + string csharp = "class Foo\r\n" + + "{\r\n" + + " static void Main(string[] args)\r\n" + + " {\r\n" + + " }\r\n" + + "\r\n" + + " static void Stop()\r\n" + + " {\r\n" + + " }\r\n" + + "\r\n" + + " public void Run()\r\n" + + " {\r\n" + + " }\r\n" + + "}"; + + [Test] + public void ConvertedPythonCode() + { + NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp); + converter.IndentString = " "; + string python = converter.Convert(csharp); + string expectedPython = "class Foo(object):\r\n" + + " def Main(args):\r\n" + + " pass\r\n" + + "\r\n" + + " Main = staticmethod(Main)\r\n" + + "\r\n" + + " def Stop():\r\n" + + " pass\r\n" + + "\r\n" + + " Stop = staticmethod(Stop)\r\n" + + "\r\n" + + " def Run(self):\r\n" + + " pass"; + + Assert.AreEqual(expectedPython, python); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index aad2aec93c..3b553837bb 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -140,6 +140,7 @@ +