diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs index 19d2c8ebf9..c8c3841738 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs @@ -1086,6 +1086,9 @@ namespace ICSharpCode.PythonBinding public object VisitTypeOfExpression(TypeOfExpression typeOfExpression, object data) { + Append("clr.GetClrType("); + Append(typeOfExpression.TypeReference.Type); + Append(")"); return null; } @@ -1361,19 +1364,26 @@ namespace ICSharpCode.PythonBinding /// object CreateInitStatement(ForeachStatement foreachStatement) { - Append("enumerator = " + GetForeachVariableName(foreachStatement) + ".GetEnumerator()"); + Append("enumerator = "); + AppendForeachVariableName(foreachStatement); + Append(".GetEnumerator()"); return null; } /// /// Gets the name of the variable that is used in the - /// foreach loop as the item being iterated. + /// foreach loop as the item being iterated and appends the code. /// - string GetForeachVariableName(ForeachStatement foreachStatement) + void AppendForeachVariableName(ForeachStatement foreachStatement) { IdentifierExpression identifierExpression = foreachStatement.Expression as IdentifierExpression; - return identifierExpression.Identifier; + InvocationExpression invocationExpression = foreachStatement.Expression as InvocationExpression; + if (identifierExpression != null) { + Append(identifierExpression.Identifier); + } else if (invocationExpression != null) { + invocationExpression.AcceptVisitor(this, null); + } } /// @@ -1436,7 +1446,6 @@ namespace ICSharpCode.PythonBinding if (identifierExpression != null) { Append(identifierExpression.Identifier); } else if (objectCreateExpression != null) { - //if (objectCreateExpression.Parameters.Count > 0) { CreateDelegateCreateExpression(objectCreateExpression.Parameters[0]); } return null; diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs new file mode 100644 index 0000000000..d1a4f4c3cd --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs @@ -0,0 +1,44 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.NRefactory; +using ICSharpCode.PythonBinding; +using NUnit.Framework; + +namespace PythonBinding.Tests.Converter +{ + [TestFixture] + public class ForeachConversionWithMethodCallTestFixture + { + string csharp = "class Foo\r\n" + + "{\r\n" + + " public void PrintEnvironmentVariables()\r\n" + + " {\r\n" + + " foreach (Environment.SpecialFolder folder in Environment.SpecialFolder.GetValues(typeof(Environment.SpecialFolder)))\r\n" + + " {\r\n" + + " Console.WriteLine(\"{0}={1}\\n\", folder, Environment.GetFolderPath(folder));\r\n" + + " }\r\n" + + " }\r\n" + + "}"; + + [Test] + public void ConvertedPythonCode() + { + NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp); + converter.IndentString = " "; + string code = converter.Convert(csharp); + string expectedCode = "class Foo(object):\r\n" + + " def PrintEnvironmentVariables(self):\r\n" + + " enumerator = Environment.SpecialFolder.GetValues(clr.GetClrType(Environment.SpecialFolder)).GetEnumerator()\r\n" + + " while enumerator.MoveNext():\r\n" + + " folder = enumerator.Current\r\n" + + " Console.WriteLine(\"{0}={1}\\n\", folder, Environment.GetFolderPath(folder))"; + Assert.AreEqual(expectedCode, code); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index f5337884b1..cc7e6d6fcf 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -104,6 +104,7 @@ +