diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs index 1f8472e23b..8da5f3401e 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs @@ -538,25 +538,6 @@ namespace ICSharpCode.PythonBinding return null; } - /// - /// 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. - /// public object VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data) { return null; @@ -834,7 +815,11 @@ namespace ICSharpCode.PythonBinding /// 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 entryPointMethods.Add(method); } } + + /// + /// Returns true if the object being created is a generic. + /// + static bool IsGenericType(ObjectCreateExpression expression) + { + return expression.CreateType.GenericTypes.Count > 0; + } + + /// + /// Appends the types used when creating a generic surrounded by square brackets. + /// + void AppendGenericTypes(ObjectCreateExpression expression) + { + Append("["); + List 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("]"); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs index 076d22b7d0..618f5d99a0 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs @@ -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); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs new file mode 100644 index 0000000000..2925c86589 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs @@ -0,0 +1,53 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.NRefactory; +using ICSharpCode.PythonBinding; +using NUnit.Framework; + +namespace PythonBinding.Tests.Converter +{ + /// + /// Tests that the code to create an instance of a generic list is converted to python + /// correctly. + /// + /// C#: List list = new List(); + /// + /// Python: list = List[str]() + /// + [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 list = new List();\r\n" + + " Dictionary dictionary = new Dictionary();\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); + } + + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 4dc8268fc4..3ead7b825f 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -106,6 +106,7 @@ +