Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal query expression syntax, and other minor improvements #30

Merged
merged 9 commits into from
Feb 24, 2018
12 changes: 12 additions & 0 deletions ICSharpCode.CodeConverter/CSharp/MethodBodyVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ public override SyntaxList<StatementSyntax> VisitForEachBlock(VBSyntax.ForEachBl
));
}


public override SyntaxList<StatementSyntax> VisitLabelStatement(VBSyntax.LabelStatementSyntax node)
{
return SingleStatement(SyntaxFactory.LabeledStatement(node.LabelToken.Text, SyntaxFactory.EmptyStatement()));
}

public override SyntaxList<StatementSyntax> VisitGoToStatement(VBSyntax.GoToStatementSyntax node)
{
return SingleStatement(SyntaxFactory.GotoStatement(SyntaxKind.GotoStatement,
SyntaxFactory.IdentifierName(node.Label.LabelToken.Text)));
}

public override SyntaxList<StatementSyntax> VisitSelectBlock(VBSyntax.SelectBlockSyntax node)
{
var expr = (ExpressionSyntax)node.SelectStatement.Expression.Accept(nodesVisitor);
Expand Down
211 changes: 177 additions & 34 deletions ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions ICSharpCode.CodeConverter/CSharp/VisualBasicConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ static SyntaxKind ConvertToken(VBasic.SyntaxKind t, TokenContext context = Token
return SyntaxKind.AssemblyKeyword;
case VBasic.SyntaxKind.AsyncKeyword:
return SyntaxKind.AsyncKeyword;
case VBasic.SyntaxKind.AscendingKeyword:
return SyntaxKind.AscendingKeyword;
case VBasic.SyntaxKind.DescendingKeyword:
return SyntaxKind.DescendingKeyword;
}
throw new NotSupportedException(t + " not supported!");
}
Expand Down
146 changes: 58 additions & 88 deletions Tests/CSharp/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void TestMethod(string str)
}");
}

[Fact(Skip = "Not implemented!")]
[Fact()]
public void ObjectInitializerExpression()
{
TestConversionVisualBasicToCSharp(@"Class StudentName
Expand All @@ -284,8 +284,7 @@ Class TestClass
Private Sub TestMethod(ByVal str As String)
Dim student2 As StudentName = New StudentName With {.FirstName = ""Craig"", .LastName = ""Playstead""}
End Sub
End Class", @"
class StudentName
End Class", @"class StudentName
{
public string LastName, FirstName;
}
Expand All @@ -294,31 +293,23 @@ class TestClass
{
private void TestMethod(string str)
{
StudentName student2 = new StudentName
{
FirstName = ""Craig"",
LastName = ""Playstead"",
};
StudentName student2 = new StudentName() { FirstName = ""Craig"", LastName = ""Playstead"" };
}
}");
}

[Fact(Skip = "Not implemented!")]
[Fact()]
public void ObjectInitializerExpression2()
{
TestConversionVisualBasicToCSharp(@"Class TestClass
Private Sub TestMethod(ByVal str As String)
Dim student2 = New With {Key .FirstName = ""Craig"", Key .LastName = ""Playstead""}
End Sub
End Class", @"
class TestClass
End Class", @"class TestClass
{
private void TestMethod(string str)
{
var student2 = new {
FirstName = ""Craig"",
LastName = ""Playstead"",
};
var student2 = new { FirstName = ""Craig"", LastName = ""Playstead"" };
}
}");
}
Expand Down Expand Up @@ -472,7 +463,7 @@ private async void TestMethod()
}");
}

[Fact(Skip = "Not implemented!")]
[Fact]
public void Linq1()
{
TestConversionVisualBasicToCSharp(@"Private Shared Sub SimpleQuery()
Expand All @@ -483,20 +474,19 @@ For Each n In res
Console.WriteLine(n)
Next
End Sub",
@"static void SimpleQuery()
@"private static void SimpleQuery()
{
int[] numbers = { 7, 9, 5, 3, 6 };

int[] numbers = new[] { 7, 9, 5, 3, 6 };"/*TODO Remove need for new[]*/ + @"
var res = from n in numbers
where n > 5
select n;
where n > 5
select n;

foreach (var n in res)
Console.WriteLine(n);
System.Console.WriteLine(n);
}");
}

[Fact(Skip = "Not implemented!")]
[Fact]
public void Linq2()
{
TestConversionVisualBasicToCSharp(@"Public Shared Sub Linq40()
Expand All @@ -511,30 +501,27 @@ For Each n In g.Numbers
Next
Next
End Sub",
@"public static void Linq40()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var numberGroups =
from n in numbers
group n by n % 5 into g
select new { Remainder = g.Key, Numbers = g };

foreach (var g in numberGroups)
{
Console.WriteLine($""Numbers with a remainder of {g.Remainder} when divided by 5:"");
foreach (var n in g.Numbers)
{
Console.WriteLine(n);
}
}
}");
@"public static void Linq40()
{
int[] numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };"/*TODO Remove need for new[]*/ + @"
var numberGroups = from n in numbers
group n by n % 5 into g
select new { Remainder = g.Key, Numbers = g };

foreach (var g in numberGroups)
{
System.Console.WriteLine($""Numbers with a remainder of {g.Remainder} when divided by 5:"");

foreach (var n in g.Numbers)
System.Console.WriteLine(n);
}
}");
}

[Fact(Skip = "Not implemented!")]
[Fact()]
public void Linq3()
{
TestConversionVisualBasicToCSharp(@"Class Product
TestConversionVisualBasicToCSharpWithoutComments(@"Class Product
Public Category As String
Public ProductName As String
End Class
Expand All @@ -550,37 +537,29 @@ For Each v In q
Next
End Sub
End Class",
@"class Product {
@"class Product
{
public string Category;
public string ProductName;
}

class Test {
public void Linq102()
{
string[] categories = new string[]{
""Beverages"",
""Condiments"",
""Vegetables"",
""Dairy Products"",
""Seafood"" };

Product[] products = GetProductList();

var q =
from c in categories
class Test
{
public void Linq102()
{
string[] categories = new string[] { ""Beverages"", ""Condiments"", ""Vegetables"", ""Dairy Products"", ""Seafood"" };
Product[] products = GetProductList();
var q = from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName };

foreach (var v in q)
{
Console.WriteLine($""{v.ProductName}: {v.Category}"");
}
select new { Category = c, p.ProductName };

foreach (var v in q)
Console.WriteLine($""{v.ProductName}: {v.Category}"");
}
}");
}

[Fact(Skip = "Not implemented!")]
[Fact]
public void Linq4()
{
TestConversionVisualBasicToCSharp(@"Public Sub Linq103()
Expand All @@ -595,29 +574,20 @@ For Each p In v.Products
Console.WriteLine("" "" & p.ProductName)
Next
Next
End Sub", @"public void Linq103()
{
string[] categories = new string[]{
""Beverages"",
""Condiments"",
""Vegetables"",
""Dairy Products"",
""Seafood"" };

var products = GetProductList();

var q =
from c in categories
End Sub", @"public void Linq103()
{
string[] categories = new string[] { ""Beverages"", ""Condiments"", ""Vegetables"", ""Dairy Products"", ""Seafood"" };
var products = GetProductList();
var q = from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps };

foreach (var v in q)
{
Console.WriteLine(v.Category + "":"");
foreach (var p in v.Products)
{
Console.WriteLine("" "" + p.ProductName);
}
select new { Category = c, Products = ps };

foreach (var v in q)
{
System.Console.WriteLine(v.Category + "":"");

foreach (var p in v.Products)
System.Console.WriteLine("" "" + p.ProductName);
}
}");
}
Expand Down
21 changes: 7 additions & 14 deletions Tests/CSharp/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,23 @@ public sealed void TestMethod<T, T2, T3>(out T argument, ref T2 argument2, T3 ar
}");
}

[Fact(Skip = "Not implemented!")]
[Fact]
public void TestExtensionMethod()
{
TestConversionVisualBasicToCSharp(
@"Imports System.Runtime.CompilerServices

Module TestClass
<Extension()>
@"Module TestClass
<System.Runtime.CompilerServices.Extension()>
Sub TestMethod(ByVal str As String)
End Sub
End Module", @"using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic;

static class TestClass
End Module", @"static class TestClass
{
public static void TestMethod(this String str)
public static void TestMethod(this string str)
{
}
}");
}

[Fact(Skip = "Not implemented!")]
[Fact]
public void TestExtensionMethodWithExistingImport()
{
TestConversionVisualBasicToCSharp(
Expand All @@ -191,7 +184,7 @@ End Sub

static class TestClass
{
public static void TestMethod(this String str)
public static void TestMethod(this string str)
{
}
}");
Expand Down
Loading