Browse Source

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
shortcuts
Matt Ward 16 years ago
parent
commit
3bc847b185
  1. 40
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs
  3. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs
  4. 61
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs
  5. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

40
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs

@ -772,10 +772,11 @@ namespace ICSharpCode.PythonBinding @@ -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 @@ -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 @@ -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 @@ -1587,19 +1593,33 @@ namespace ICSharpCode.PythonBinding
/// <summary>
/// Adds the method or constructor parameters.
/// </summary>
void AddParameters(List<ParameterDeclarationExpression> parameters)
void AddParameters(ParametrizedNode method)
{
Append("(");
List<ParameterDeclarationExpression> 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;
}
/// <summary>
/// Creates assignments of the form:
/// i = 1

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs

@ -39,6 +39,8 @@ namespace PythonBinding.Tests.Converter @@ -39,6 +39,8 @@ namespace PythonBinding.Tests.Converter
[TestFixtureSetUp]
public void SetUpFixture()
{
MSBuildEngineHelper.InitMSBuildEngine();
List<LanguageBindingDescriptor> bindings = new List<LanguageBindingDescriptor>();
using (TextReader reader = PythonBindingAddInFile.ReadAddInFile()) {
AddIn addin = AddIn.Load(reader, String.Empty);

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ObjectInitializerConversionTestFixture.cs

@ -34,7 +34,7 @@ namespace PythonBinding.Tests.Converter @@ -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 @@ -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);

61
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
/// <summary>
/// Static methods should not use "self" and they should by defined by using "staticmethod".
/// </summary>
[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);
}
}
}

1
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -140,6 +140,7 @@ @@ -140,6 +140,7 @@
<Compile Include="Converter\CSharpClassWithNamespaceConversionTestFixture.cs" />
<Compile Include="Converter\EmptyCSharpClassConversionTestFixture.cs" />
<Compile Include="Converter\StaticClassReferenceTestFixture.cs" />
<Compile Include="Converter\StaticMethodConversionTestFixture.cs" />
<Compile Include="Converter\SwitchStatementConversionTestFixture.cs" />
<Compile Include="Converter\TernaryOperatorConversionTestFixture.cs" />
<Compile Include="Converter\ThrowExceptionConversionTestFixture.cs" />

Loading…
Cancel
Save