Browse Source

Support converting multiple class fields defined on one line when converting to Python.

4.0
Matt Ward 14 years ago
parent
commit
bdd0f33cda
  1. 28
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 86
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/MultipleFieldsOnSameLineTests.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -1471,9 +1471,8 @@ namespace ICSharpCode.PythonBinding
/// Checks that the field declaration has an initializer that /// Checks that the field declaration has an initializer that
/// sets an initial value. /// sets an initial value.
/// </summary> /// </summary>
static bool FieldHasInitialValue(FieldDeclaration fieldDeclaration) static bool FieldHasInitialValue(VariableDeclaration variableDeclaration)
{ {
VariableDeclaration variableDeclaration = fieldDeclaration.Fields[0];
Expression initializer = variableDeclaration.Initializer; Expression initializer = variableDeclaration.Initializer;
return !initializer.IsNull; return !initializer.IsNull;
} }
@ -1558,8 +1557,10 @@ namespace ICSharpCode.PythonBinding
// Check the current class's fields. // Check the current class's fields.
if (constructorInfo != null) { if (constructorInfo != null) {
foreach (FieldDeclaration field in constructorInfo.Fields) { foreach (FieldDeclaration field in constructorInfo.Fields) {
if (field.Fields[0].Name == name) { foreach (VariableDeclaration variable in field.Fields) {
return true; if (variable.Name == name) {
return true;
}
} }
} }
} }
@ -1696,10 +1697,7 @@ namespace ICSharpCode.PythonBinding
AppendDocstring(xmlDocComments); AppendDocstring(xmlDocComments);
if (constructorInfo.Fields.Count > 0) { if (constructorInfo.Fields.Count > 0) {
foreach (FieldDeclaration field in constructorInfo.Fields) { foreach (FieldDeclaration field in constructorInfo.Fields) {
// Ignore field if it has no initializer. CreateFieldInitialization(field);
if (FieldHasInitialValue(field)) {
CreateFieldInitialization(field);
}
} }
} }
@ -1731,11 +1729,15 @@ namespace ICSharpCode.PythonBinding
/// </summary> /// </summary>
void CreateFieldInitialization(FieldDeclaration field) void CreateFieldInitialization(FieldDeclaration field)
{ {
VariableDeclaration variable = field.Fields[0]; foreach (VariableDeclaration variable in field.Fields) {
string oldVariableName = variable.Name; // Ignore field if it has no initializer.
variable.Name = "self._" + variable.Name; if (FieldHasInitialValue(variable)) {
VisitVariableDeclaration(variable, null); string oldVariableName = variable.Name;
variable.Name = oldVariableName; variable.Name = "self._" + variable.Name;
VisitVariableDeclaration(variable, null);
variable.Name = oldVariableName;
}
}
} }
/// <summary> /// <summary>

86
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/MultipleFieldsOnSameLineTests.cs

@ -0,0 +1,86 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
[TestFixture]
public class MultipleFieldsOnSameLineTests
{
string csharpClassWithFieldsThatHaveInitialValues =
"class Foo\r\n" +
"{\r\n" +
" int i = 0, j = 1;\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithFieldsThatHaveInitialValues()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharpClassWithFieldsThatHaveInitialValues);
string expectedPython =
"class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" self._i = 0\r\n" +
" self._j = 1";
Assert.AreEqual(expectedPython, python);
}
string csharpClassWithTwoFieldsWhereFirstDoesNotHaveInitialValue =
"class Foo\r\n" +
"{\r\n" +
" int i, j = 1;\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithFieldsWhereFirstFieldDoesNotHaveInitialValue()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharpClassWithTwoFieldsWhereFirstDoesNotHaveInitialValue);
string expectedPython =
"class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" self._j = 1";
Assert.AreEqual(expectedPython, python);
}
string csharpClassWithTwoFieldsInitializedInMethod =
"class Foo\r\n" +
"{\r\n" +
" int i = 0;\r\n" +
" int j, k;\r\n" +
"\r\n" +
" public void Test()\r\n" +
" {\r\n" +
" j = 1;\r\n" +
" k = 3;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithTwoFieldsInitializedInMethod()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharpClassWithTwoFieldsInitializedInMethod);
string expectedPython =
"class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" self._i = 0\r\n" +
"\r\n" +
" def Test(self):\r\n" +
" self._j = 1\r\n" +
" self._k = 3";
Assert.AreEqual(expectedPython, python);
}
}
}

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

@ -150,6 +150,7 @@
<Compile Include="Converter\IfStatementConversionTestFixture.cs" /> <Compile Include="Converter\IfStatementConversionTestFixture.cs" />
<Compile Include="Converter\ModulusOperatorConversionTestFixture.cs" /> <Compile Include="Converter\ModulusOperatorConversionTestFixture.cs" />
<Compile Include="Converter\MultiLineCommentTestFixture.cs" /> <Compile Include="Converter\MultiLineCommentTestFixture.cs" />
<Compile Include="Converter\MultipleFieldsOnSameLineTests.cs" />
<Compile Include="Converter\NestedClassConversionTestFixture.cs" /> <Compile Include="Converter\NestedClassConversionTestFixture.cs" />
<Compile Include="Converter\NestedIfStatementConversionTestFixture.cs" /> <Compile Include="Converter\NestedIfStatementConversionTestFixture.cs" />
<Compile Include="Converter\NullConversionTestFixture.cs" /> <Compile Include="Converter\NullConversionTestFixture.cs" />

Loading…
Cancel
Save