Browse Source

Support converting VB.NET for next loops to Python.

4.0
Matt Ward 14 years ago
parent
commit
b2e0f592f7
  1. 44
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 74
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForNextConversionTests.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -595,9 +595,51 @@ namespace ICSharpCode.PythonBinding @@ -595,9 +595,51 @@ namespace ICSharpCode.PythonBinding
return null;
}
/// <summary>
/// Converts from an NRefactory VB.NET for next loop:
///
/// for i As Integer = 0 To 4
/// Next
///
/// to Python's:
///
/// i = 0
/// while i &lt; 5:
/// </summary>
public override object TrackedVisitForNextStatement(ForNextStatement forNextStatement, object data)
{
Console.WriteLine("VisitForNextStatement");
// Convert the for loop's initializers.
string variableName = forNextStatement.VariableName;
AppendIndented(variableName);
Append(" = ");
forNextStatement.Start.AcceptVisitor(this, data);
AppendLine();
// Convert the for loop's test expression.
AppendIndented("while ");
Append(variableName);
Append(" <= ");
forNextStatement.End.AcceptVisitor(this, data);
Append(":");
AppendLine();
// Visit the for loop's body.
IncreaseIndent();
forNextStatement.EmbeddedStatement.AcceptVisitor(this, data);
// Convert the for loop's increment statement.
AppendIndented(variableName);
Append(" = ");
Append(variableName);
Append(" + ");
if (forNextStatement.Step.IsNull) {
Append("1");
} else {
forNextStatement.Step.AcceptVisitor(this, data);
}
AppendLine();
DecreaseIndent();
return null;
}

74
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForNextConversionTests.cs

@ -0,0 +1,74 @@ @@ -0,0 +1,74 @@
// 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 ForNextConversionTests
{
string vbnetForNextCode =
"Public Class Foo\r\n" +
" Public Function GetCount() As Integer\r\n" +
" Dim count As Integer = 0\r\n" +
" For i As Integer = 0 To 4\r\n" +
" count += 1\r\n" +
" Next\r\n" +
" Return count\r\n" +
" End Function\r\n" +
"End Class\r\n";
[Test]
public void ConvertVBNetForNextToPython()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.VBNet);
converter.IndentString = " ";
string code = converter.Convert(vbnetForNextCode);
string expectedCode =
"class Foo(object):\r\n" +
" def GetCount(self):\r\n" +
" count = 0\r\n" +
" i = 0\r\n" +
" while i <= 4:\r\n" +
" count += 1\r\n" +
" i = i + 1\r\n" +
" return count";
Assert.AreEqual(expectedCode, code);
}
string vbnetForNextWithStepCode =
"Public Class Foo\r\n" +
" Public Function GetCount() As Integer\r\n" +
" Dim count As Integer = 0\r\n" +
" For i As Integer = 0 To 4 Step 2\r\n" +
" count += 1\r\n" +
" Next\r\n" +
" Return count\r\n" +
" End Function\r\n" +
"End Class\r\n";
[Test]
public void ConvertVBNetForNextWithStepToPython()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.VBNet);
converter.IndentString = " ";
string code = converter.Convert(vbnetForNextWithStepCode );
string expectedCode =
"class Foo(object):\r\n" +
" def GetCount(self):\r\n" +
" count = 0\r\n" +
" i = 0\r\n" +
" while i <= 4:\r\n" +
" count += 1\r\n" +
" i = i + 2\r\n" +
" return count";
Assert.AreEqual(expectedCode, code);
}
}
}

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

@ -122,6 +122,7 @@ @@ -122,6 +122,7 @@
<Compile Include="Converter\ConvertToPythonProjectCommandTestFixture.cs" />
<Compile Include="Converter\EventHandlerWithObjectCreationTestFixture.cs" />
<Compile Include="Converter\ForeachConversionWithMethodCallTestFixture.cs" />
<Compile Include="Converter\ForNextConversionTests.cs" />
<Compile Include="Converter\GenerateMainMethodCallTestFixture.cs" />
<Compile Include="Converter\GenericListConversionTestFixture.cs" />
<Compile Include="Converter\IntegerMethodParameterTestFixture.cs" />

Loading…
Cancel
Save