Browse Source

Support converting VB.NET for next loops to Ruby.

4.0
Matt Ward 14 years ago
parent
commit
c2674fba66
  1. 38
      src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/NRefactoryToRubyConverter.cs
  2. 80
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Converter/ForNextConversionTests.cs
  3. 1
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/RubyBinding.Tests.csproj

38
src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/NRefactoryToRubyConverter.cs

@ -457,7 +457,43 @@ namespace ICSharpCode.RubyBinding @@ -457,7 +457,43 @@ namespace ICSharpCode.RubyBinding
return null;
}
public override object TrackedVisitForNextStatement(ForNextStatement forNextStatement, object data)
{
// 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);
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();
AppendIndentedLine("end");
return null;
}
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
{

80
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Converter/ForNextConversionTests.cs

@ -0,0 +1,80 @@ @@ -0,0 +1,80 @@
// 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.RubyBinding;
using NUnit.Framework;
namespace RubyBinding.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 ConvertVBNetForNextToRuby()
{
NRefactoryToRubyConverter converter = new NRefactoryToRubyConverter(SupportedLanguage.VBNet);
converter.IndentString = " ";
string code = converter.Convert(vbnetForNextCode);
string expectedCode =
"class Foo\r\n" +
" def GetCount()\r\n" +
" count = 0\r\n" +
" i = 0\r\n" +
" while i <= 4\r\n" +
" count += 1\r\n" +
" i = i + 1\r\n" +
" end\r\n" +
" return count\r\n" +
" end\r\n" +
"end";
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 ConvertVBNetForNextWithStepToRuby()
{
NRefactoryToRubyConverter converter = new NRefactoryToRubyConverter(SupportedLanguage.VBNet);
converter.IndentString = " ";
string code = converter.Convert(vbnetForNextWithStepCode);
string expectedCode =
"class Foo\r\n" +
" def GetCount()\r\n" +
" count = 0\r\n" +
" i = 0\r\n" +
" while i <= 4\r\n" +
" count += 1\r\n" +
" i = i + 2\r\n" +
" end\r\n" +
" return count\r\n" +
" end\r\n" +
"end";
Assert.AreEqual(expectedCode, code);
}
}
}

1
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/RubyBinding.Tests.csproj

@ -103,6 +103,7 @@ @@ -103,6 +103,7 @@
<Compile Include="Converter\ForeachConversionTestFixture.cs" />
<Compile Include="Converter\ForeachConversionWithMethodCallTestFixture.cs" />
<Compile Include="Converter\ForLoopConversionTestFixture.cs" />
<Compile Include="Converter\ForNextConversionTests.cs" />
<Compile Include="Converter\GenerateMainMethodCallTestFixture.cs" />
<Compile Include="Converter\GenericListConversionTestFixture.cs" />
<Compile Include="Converter\IfBlockStatementConversionTestFixture.cs" />

Loading…
Cancel
Save