Browse Source

Python code converter now supports read-only and write-only property conversions.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4761 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
7b77da2dc0
  1. 49
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 55
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyWithGetterTestFixture.cs
  3. 48
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyWithSetterTestFixture.cs
  4. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -951,21 +951,24 @@ namespace ICSharpCode.PythonBinding @@ -951,21 +951,24 @@ namespace ICSharpCode.PythonBinding
string propertyName = propertyDeclaration.Name;
// Add get statements.
AppendIndentedLine("def get_" + propertyName + "(self):");
IncreaseIndent();
propertyDeclaration.GetRegion.Block.AcceptVisitor(this, data);
DecreaseIndent();
AppendLine();
if (propertyDeclaration.HasGetRegion) {
AppendIndentedLine("def get_" + propertyName + "(self):");
IncreaseIndent();
propertyDeclaration.GetRegion.Block.AcceptVisitor(this, data);
DecreaseIndent();
AppendLine();
}
// Add set statements.
AppendIndentedLine("def set_" + propertyName + "(self, value):");
IncreaseIndent();
propertyDeclaration.SetRegion.Block.AcceptVisitor(this, data);
DecreaseIndent();
AppendLine();
if (propertyDeclaration.HasSetRegion) {
AppendIndentedLine("def set_" + propertyName + "(self, value):");
IncreaseIndent();
propertyDeclaration.SetRegion.Block.AcceptVisitor(this, data);
DecreaseIndent();
AppendLine();
}
// Add property definition.
AppendIndentedLine(String.Concat(propertyName, " = property(fget=get_", propertyName, ", fset=set_", propertyName, ")"));
AppendPropertyDecorator(propertyDeclaration);
AppendLine();
return null;
@ -1940,5 +1943,27 @@ namespace ICSharpCode.PythonBinding @@ -1940,5 +1943,27 @@ namespace ICSharpCode.PythonBinding
{
return (node is TypeDeclaration) || (node is MethodDeclaration) || (node is ConstructorDeclaration);
}
void AppendPropertyDecorator(PropertyDeclaration propertyDeclaration)
{
string propertyName = propertyDeclaration.Name;
AppendIndented(propertyName);
Append(" = property(");
bool addedParameter = false;
if (propertyDeclaration.HasGetRegion) {
Append("fget=get_" + propertyName);
addedParameter = true;
}
if (propertyDeclaration.HasSetRegion) {
if (addedParameter) {
Append(", ");
}
Append("fset=set_" + propertyName);
}
Append(")");
AppendLine();
}
}
}

55
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyWithGetterTestFixture.cs

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
// <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 ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
[TestFixture]
public class PropertyWithGetterTestFixture
{
string csharp = "class Foo\r\n" +
"{\r\n" +
" int count = 0;\r\n" +
" int i = 0;\r\n" +
" public int Count {\r\n" +
" get {\r\n" +
" if (i == 0) {\r\n" +
" return 10;\r\n" +
" } else {\r\n" +
" return count;\r\n" +
" }\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 __init__(self):\r\n" +
" self._count = 0\r\n" +
" self._i = 0\r\n" +
"\r\n" +
" def get_Count(self):\r\n" +
" if self._i == 0:\r\n" +
" return 10\r\n" +
" else:\r\n" +
" return self._count\r\n" +
"\r\n" +
" Count = property(fget=get_Count)";
Assert.AreEqual(expectedPython, python, python);
}
}
}

48
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyWithSetterTestFixture.cs

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
// <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 ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
[TestFixture]
public class PropertyWithSetterTestFixture
{
string csharp = "class Foo\r\n" +
"{\r\n" +
" int count = 0;\r\n" +
" int i = 0;\r\n" +
" public int Count {\r\n" +
" set {\r\n" +
" count = value;\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 __init__(self):\r\n" +
" self._count = 0\r\n" +
" self._i = 0\r\n" +
"\r\n" +
" def set_Count(self, value):\r\n" +
" self._count = value\r\n" +
"\r\n" +
" Count = property(fset=set_Count)";
Assert.AreEqual(expectedPython, python, python);
}
}
}

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

@ -141,6 +141,8 @@ @@ -141,6 +141,8 @@
<Compile Include="Converter\ProjectHasStartupObjectTestFixture.cs" />
<Compile Include="Converter\PropertyConversionTestFixture.cs" />
<Compile Include="Converter\PropertyWithGetSetStatementsTestfixture.cs" />
<Compile Include="Converter\PropertyWithGetterTestFixture.cs" />
<Compile Include="Converter\PropertyWithSetterTestFixture.cs" />
<Compile Include="Converter\RemoveHandlerConversionTestFixture.cs" />
<Compile Include="Converter\SingleClassMethodConversionTestFixture.cs" />
<Compile Include="Converter\ConvertCSharpToPythonMenuCommandTestFixture.cs" />

Loading…
Cancel
Save