Browse Source

Python code converter now specifies types when creating an instance of a generic.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4172 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
f8b00cb742
  1. 54
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs
  3. 53
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs
  4. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -538,25 +538,6 @@ namespace ICSharpCode.PythonBinding @@ -538,25 +538,6 @@ namespace ICSharpCode.PythonBinding
return null;
}
/// <summary>
/// Saves the field information so it can be added to the
/// class constructor. This is done since python requires you
/// to initialize any class instance variables in the
/// __init__ method. For example, consider the equivalent
/// C# and Python code:
///
/// class Foo
/// {
/// private int i = 0;
/// }
///
/// class Foo:
/// def __init__(self):
/// i = 0
///
/// The only difference is that the initialization is moved to the
/// class constructor.
/// </summary>
public object VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data)
{
return null;
@ -834,7 +815,11 @@ namespace ICSharpCode.PythonBinding @@ -834,7 +815,11 @@ namespace ICSharpCode.PythonBinding
/// </summary>
public object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
{
Append(objectCreateExpression.CreateType.Type + "(");
Append(objectCreateExpression.CreateType.Type);
if (IsGenericType(objectCreateExpression)) {
AppendGenericTypes(objectCreateExpression);
}
Append("(");
// Add parameters.
bool firstParameter = true;
@ -1709,5 +1694,34 @@ namespace ICSharpCode.PythonBinding @@ -1709,5 +1694,34 @@ namespace ICSharpCode.PythonBinding
entryPointMethods.Add(method);
}
}
/// <summary>
/// Returns true if the object being created is a generic.
/// </summary>
static bool IsGenericType(ObjectCreateExpression expression)
{
return expression.CreateType.GenericTypes.Count > 0;
}
/// <summary>
/// Appends the types used when creating a generic surrounded by square brackets.
/// </summary>
void AppendGenericTypes(ObjectCreateExpression expression)
{
Append("[");
List<TypeReference> typeRefs = expression.CreateType.GenericTypes;
for (int i = 0; i < typeRefs.Count; ++i) {
if (i != 0) {
Append(", ");
}
TypeReference typeRef = typeRefs[i];
if (typeRef.IsArrayType) {
Append("System.Array[" + typeRef.Type + "]");
} else {
Append(typeRef.Type);
}
}
Append("]");
}
}
}

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

@ -36,7 +36,7 @@ namespace PythonBinding.Tests.Converter @@ -36,7 +36,7 @@ namespace PythonBinding.Tests.Converter
string expectedPython = "class Foo(object):\r\n" +
"\tdef Assign(self):\r\n" +
"\t\telements = System.Array.CreateInstance(System.Int32, 10)\r\n" +
"\t\tlist = List()\r\n" +
"\t\tlist = List[System.Array[System.Int32]]()\r\n" +
"\t\tlist.Add(elements.Clone())";
Assert.AreEqual(expectedPython, python);

53
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
// <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
{
/// <summary>
/// Tests that the code to create an instance of a generic list is converted to python
/// correctly.
///
/// C#: List<string> list = new List<string>();
///
/// Python: list = List[str]()
/// </summary>
[TestFixture]
public class GenericListConversionTestFixture
{
string csharp = "using System.Collections.Generic;\r\n" +
"\r\n" +
"class Foo\r\n" +
"{\r\n" +
" public Foo()\r\n" +
" {\r\n" +
" List<string> list = new List<string>();\r\n" +
" Dictionary<string, int> dictionary = new Dictionary<string, int>();\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharp);
string expectedPython = "from System.Collections.Generic import *\r\n" +
"class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" list = List[System.String]()\r\n" +
" dictionary = Dictionary[System.String, System.Int32]()";
Assert.AreEqual(expectedPython, python);
}
}
}

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

@ -106,6 +106,7 @@ @@ -106,6 +106,7 @@
<Compile Include="Converter\ConvertToPythonProjectCommandTestFixture.cs" />
<Compile Include="Converter\EventHandlerWithObjectCreationTestFixture.cs" />
<Compile Include="Converter\ForeachConversionWithMethodCallTestFixture.cs" />
<Compile Include="Converter\GenericListConversionTestFixture.cs" />
<Compile Include="Converter\IntegerMethodParameterTestFixture.cs" />
<Compile Include="Converter\BaseClassReferenceTestFixture.cs" />
<Compile Include="Converter\BinaryOperatorConversionTests.cs" />

Loading…
Cancel
Save