Browse Source

Python code converter now converts 'foreach (string key in dictionary.Keys)' statements correctly.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4256 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
5a90f84c1b
  1. 10
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 70
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionTestFixture.cs
  3. 28
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs

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

@ -1118,9 +1118,6 @@ namespace ICSharpCode.PythonBinding @@ -1118,9 +1118,6 @@ namespace ICSharpCode.PythonBinding
return null;
}
/// <summary>
/// Converts a unary operator expression to a code dom expression.
/// </summary>
public object VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data)
{
switch (unaryOperatorExpression.Op) {
@ -1209,10 +1206,6 @@ namespace ICSharpCode.PythonBinding @@ -1209,10 +1206,6 @@ namespace ICSharpCode.PythonBinding
return null;
}
/// <summary>
/// Converts an NRefactory MemberReferenceExpression to a
/// code dom's CodeFieldReferenceExpression.
/// </summary>
public object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data)
{
memberReferenceExpression.TargetObject.AcceptVisitor(this, data);
@ -1387,10 +1380,13 @@ namespace ICSharpCode.PythonBinding @@ -1387,10 +1380,13 @@ namespace ICSharpCode.PythonBinding
{
IdentifierExpression identifierExpression = foreachStatement.Expression as IdentifierExpression;
InvocationExpression invocationExpression = foreachStatement.Expression as InvocationExpression;
MemberReferenceExpression memberRefExpression = foreachStatement.Expression as MemberReferenceExpression;
if (identifierExpression != null) {
Append(identifierExpression.Identifier);
} else if (invocationExpression != null) {
invocationExpression.AcceptVisitor(this, null);
} else if (memberRefExpression != null) {
memberRefExpression.AcceptVisitor(this, null);
}
}

70
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionTestFixture.cs

@ -18,33 +18,65 @@ namespace PythonBinding.Tests.Converter @@ -18,33 +18,65 @@ namespace PythonBinding.Tests.Converter
[TestFixture]
public class ForeachConversionTestFixture
{
string csharp = "class Foo\r\n" +
string intArrayCode = "class Foo\r\n" +
"{\r\n" +
"\tpublic int GetCount(int[] items)\r\n" +
"\t{\r\n" +
"\t\tint count = 0;\r\n" +
"\t\tforeach (int item in items) {\r\n" +
"\t\t\tcount++;\r\n" +
"\t\t}\r\n" +
"\t\treturn count;\r\n" +
"\t}\r\n" +
" public int GetCount(int[] items)\r\n" +
" {\r\n" +
" int count = 0;\r\n" +
" foreach (int item in items) {\r\n" +
" count++;\r\n" +
" }\r\n" +
" return count;\r\n" +
" }\r\n" +
"}";
string dictionaryCode = "class Foo\r\n" +
"{\r\n" +
" public void DisplayItems()\r\n" +
" {\r\n" +
" Dictionary<string, string> record = new Dictionary<string, string>();\r\n" +
" record.Add(\"a\", \"b\");\r\n" +
" record.Add(\"c\", \"d\");\r\n" +
" foreach (string key in record.Keys) {\r\n" +
" Console.WriteLine(\"Key: \" + key);\r\n" +
" }\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
public void ConvertedIntArrayCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
string code = converter.Convert(csharp);
converter.IndentString = " ";
string code = converter.Convert(intArrayCode);
string expectedCode = "class Foo(object):\r\n" +
"\tdef GetCount(self, items):\r\n" +
"\t\tcount = 0\r\n" +
"\t\tenumerator = items.GetEnumerator()\r\n" +
"\t\twhile enumerator.MoveNext():\r\n" +
"\t\t\titem = enumerator.Current\r\n" +
"\t\t\tcount += 1\r\n" +
"\t\treturn count";
" def GetCount(self, items):\r\n" +
" count = 0\r\n" +
" enumerator = items.GetEnumerator()\r\n" +
" while enumerator.MoveNext():\r\n" +
" item = enumerator.Current\r\n" +
" count += 1\r\n" +
" return count";
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedDictionaryCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(dictionaryCode);
string expectedCode = "class Foo(object):\r\n" +
" def DisplayItems(self):\r\n" +
" record = Dictionary[str, str]()\r\n" +
" record.Add(\"a\", \"b\")\r\n" +
" record.Add(\"c\", \"d\")\r\n" +
" enumerator = record.Keys.GetEnumerator()\r\n" +
" while enumerator.MoveNext():\r\n" +
" key = enumerator.Current\r\n" +
" Console.WriteLine(\"Key: \" + key)";
Assert.AreEqual(expectedCode, code);
}
}
}

28
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs

@ -25,18 +25,7 @@ namespace PythonBinding.Tests.Converter @@ -25,18 +25,7 @@ namespace PythonBinding.Tests.Converter
" }\r\n" +
" }\r\n" +
"}";
string intCode = "class Foo\r\n" +
"{\r\n" +
" public void PrintIntegers(int[] items)\r\n" +
" {\r\n" +
" foreach (int i in items)\r\n" +
" {\r\n" +
" Console.WriteLine(i);\r\n" +
" }\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedEnvironmentSpecialFolderCode()
{
@ -50,21 +39,6 @@ namespace PythonBinding.Tests.Converter @@ -50,21 +39,6 @@ namespace PythonBinding.Tests.Converter
" folder = enumerator.Current\r\n" +
" Console.WriteLine(\"{0}={1}\\n\", folder, Environment.GetFolderPath(folder))";
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedIntCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(intCode);
string expectedCode = "class Foo(object):\r\n" +
" def PrintIntegers(self, items):\r\n" +
" enumerator = items.GetEnumerator()\r\n" +
" while enumerator.MoveNext():\r\n" +
" i = enumerator.Current\r\n" +
" Console.WriteLine(i)";
Assert.AreEqual(expectedCode, code);
}
}
}

Loading…
Cancel
Save