Browse Source

Support converting multiple class fields defined on one line when converting to Ruby.

4.0
Matt Ward 14 years ago
parent
commit
441df0e328
  1. 29
      src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/NRefactoryToRubyConverter.cs
  2. 93
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Converter/MultipleFieldsOnSameLineTests.cs
  3. 1
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/RubyBinding.Tests.csproj

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

@ -1030,8 +1030,10 @@ namespace ICSharpCode.RubyBinding
// Check the current class's fields. // Check the current class's fields.
if (constructorInfo != null) { if (constructorInfo != null) {
foreach (FieldDeclaration field in constructorInfo.Fields) { foreach (FieldDeclaration field in constructorInfo.Fields) {
if (field.Fields[0].Name == name) { foreach (VariableDeclaration variable in field.Fields) {
return true; if (variable.Name == name) {
return true;
}
} }
} }
} }
@ -1226,13 +1228,9 @@ namespace ICSharpCode.RubyBinding
// Add fields at start of constructor. // Add fields at start of constructor.
IncreaseIndent(); IncreaseIndent();
// AppendDocstring(xmlDocComments);
if (constructorInfo.Fields.Count > 0) { if (constructorInfo.Fields.Count > 0) {
foreach (FieldDeclaration field in constructorInfo.Fields) { foreach (FieldDeclaration field in constructorInfo.Fields) {
// Ignore field if it has no initializer. CreateFieldInitialization(field);
if (FieldHasInitialValue(field)) {
CreateFieldInitialization(field);
}
} }
} }
@ -1260,20 +1258,23 @@ namespace ICSharpCode.RubyBinding
/// Checks that the field declaration has an initializer that /// Checks that the field declaration has an initializer that
/// sets an initial value. /// sets an initial value.
/// </summary> /// </summary>
static bool FieldHasInitialValue(FieldDeclaration fieldDeclaration) static bool FieldHasInitialValue(VariableDeclaration variableDeclaration)
{ {
VariableDeclaration variableDeclaration = fieldDeclaration.Fields[0];
Expression initializer = variableDeclaration.Initializer; Expression initializer = variableDeclaration.Initializer;
return !initializer.IsNull; return !initializer.IsNull;
} }
void CreateFieldInitialization(FieldDeclaration field) void CreateFieldInitialization(FieldDeclaration field)
{ {
VariableDeclaration variable = field.Fields[0]; foreach (VariableDeclaration variable in field.Fields) {
string oldVariableName = variable.Name; // Ignore field if it has no initializer.
variable.Name = "@" + variable.Name; if (FieldHasInitialValue(variable)) {
VisitVariableDeclaration(variable, null); string oldVariableName = variable.Name;
variable.Name = oldVariableName; variable.Name = "@" + variable.Name;
VisitVariableDeclaration(variable, null);
variable.Name = oldVariableName;
}
}
} }
/// <summary> /// <summary>

93
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Converter/MultipleFieldsOnSameLineTests.cs

@ -0,0 +1,93 @@
// 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 MultipleFieldsOnSameLineTests
{
string csharpClassWithFieldsThatHaveInitialValues =
"class Foo\r\n" +
"{\r\n" +
" int i = 0, j = 1;\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithFieldsThatHaveInitialValues()
{
NRefactoryToRubyConverter converter = new NRefactoryToRubyConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string ruby = converter.Convert(csharpClassWithFieldsThatHaveInitialValues);
string expectedRuby =
"class Foo\r\n" +
" def initialize()\r\n" +
" @i = 0\r\n" +
" @j = 1\r\n" +
" end\r\n" +
"end";
Assert.AreEqual(expectedRuby, ruby);
}
string csharpClassWithTwoFieldsWhereFirstDoesNotHaveInitialValue =
"class Foo\r\n" +
"{\r\n" +
" int i, j = 1;\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithFieldsWhereFirstFieldDoesNotHaveInitialValue()
{
NRefactoryToRubyConverter converter = new NRefactoryToRubyConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string ruby = converter.Convert(csharpClassWithTwoFieldsWhereFirstDoesNotHaveInitialValue);
string expectedRuby =
"class Foo\r\n" +
" def initialize()\r\n" +
" @j = 1\r\n" +
" end\r\n" +
"end";
Assert.AreEqual(expectedRuby, ruby);
}
string csharpClassWithTwoFieldsInitializedInMethod =
"class Foo\r\n" +
"{\r\n" +
" int i = 0;\r\n" +
" int j, k;\r\n" +
"\r\n" +
" public void Test()\r\n" +
" {\r\n" +
" j = 1;\r\n" +
" k = 3;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertCSharpClassWithTwoFieldsInitializedInMethod()
{
NRefactoryToRubyConverter converter = new NRefactoryToRubyConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string ruby = converter.Convert(csharpClassWithTwoFieldsInitializedInMethod);
string expectedRuby =
"class Foo\r\n" +
" def initialize()\r\n" +
" @i = 0\r\n" +
" end\r\n" +
"\r\n" +
" def Test()\r\n" +
" @j = 1\r\n" +
" @k = 3\r\n" +
" end\r\n" +
"end";
Assert.AreEqual(expectedRuby, ruby);
}
}
}

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

@ -123,6 +123,7 @@
<Compile Include="Converter\MethodWithBodyConversionTestFixture.cs" /> <Compile Include="Converter\MethodWithBodyConversionTestFixture.cs" />
<Compile Include="Converter\ModulusOperatorConversionTestFixture.cs" /> <Compile Include="Converter\ModulusOperatorConversionTestFixture.cs" />
<Compile Include="Converter\MultiLineCommentConversionTestFixture.cs" /> <Compile Include="Converter\MultiLineCommentConversionTestFixture.cs" />
<Compile Include="Converter\MultipleFieldsOnSameLineTests.cs" />
<Compile Include="Converter\NestedClassConversionTestFixture.cs" /> <Compile Include="Converter\NestedClassConversionTestFixture.cs" />
<Compile Include="Converter\NestedIfStatementConversionTestFixture.cs" /> <Compile Include="Converter\NestedIfStatementConversionTestFixture.cs" />
<Compile Include="Converter\NullConversionTestFixture.cs" /> <Compile Include="Converter\NullConversionTestFixture.cs" />

Loading…
Cancel
Save