Skip to content

Commit

Permalink
Characterize remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Mar 9, 2020
1 parent e74c6a5 commit d578963
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 133 deletions.
2 changes: 1 addition & 1 deletion CodeConverter/Util/CSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static CSharpCompilation CreateCSharpCompilation()

public static CSharpCompilationOptions CreateCompilationOptions()
{
return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
}

public static CSharpParseOptions ParseOptions { get; } = new CSharpParseOptions(LanguageVersion.Latest);
Expand Down
11 changes: 1 addition & 10 deletions Tests/CSharp/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2303,16 +2303,7 @@ public async Task TestExternDllImport()
await TestConversionVisualBasicToCSharp(
@"<DllImport(""kernel32.dll"", SetLastError:=True)>
Private Shared Function OpenProcess(ByVal dwDesiredAccess As AccessMask, ByVal bInheritHandle As Boolean, ByVal dwProcessId As UInteger) As IntPtr
End Function
5 source compilation errors:
CS0246: The type or namespace name 'AccessMask' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'IntPtr' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'SetLastError' could not be found (are you missing a using directive or an assembly reference?)
1 target compilation errors:
BC30002: Type 'AccessMask' is not defined.", @"[DllImport(""kernel32.dll"", SetLastError = true)]
End Function", @"[DllImport(""kernel32.dll"", SetLastError = true)]
private static extern IntPtr OpenProcess(AccessMask dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
1 source compilation errors:
Expand Down
40 changes: 12 additions & 28 deletions Tests/CSharp/NamespaceLevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,76 +409,60 @@ internal partial class test : IComparable
1 source compilation errors:
BC30149: Class 'test' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'IComparable'.
1 target compilation errors:
CS0535: 'test' does not implement interface member 'IComparable.CompareTo(object)'
1 source compilation errors:
BC30149: Class 'test' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'IComparable'.
1 target compilation errors:
CS0535: 'test' does not implement interface member 'IComparable.CompareTo(object)'");
}

[Fact]
public async Task ClassImplementsInterface2()
{
await TestConversionVisualBasicToCSharp(@"Class test
await TestConversionVisualBasicToCSharp(@"Class ClassImplementsInterface2
Implements System.IComparable
End Class",
@"using System;
internal partial class test : IComparable
internal partial class ClassImplementsInterface2 : IComparable
{
}
1 source compilation errors:
BC30149: Class 'test' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'IComparable'.
BC30149: Class 'ClassImplementsInterface2' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'IComparable'.
1 target compilation errors:
CS0535: 'test' does not implement interface member 'IComparable.CompareTo(object)'
1 source compilation errors:
BC30149: Class 'test' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'IComparable'.
1 target compilation errors:
CS0535: 'test' does not implement interface member 'IComparable.CompareTo(object)'");
CS0535: 'ClassImplementsInterface2' does not implement interface member 'IComparable.CompareTo(object)'");
}

[Fact]
public async Task ClassInheritsClass()
{
await TestConversionVisualBasicToCSharp(@"Imports System.IO
Class test
Class ClassInheritsClass
Inherits InvalidDataException
End Class",
@"using System.IO;
internal partial class test : InvalidDataException
internal partial class ClassInheritsClass : InvalidDataException
{
}
1 source compilation errors:
BC30299: 'test' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
1 target compilation errors:
CS0509: 'test': cannot derive from sealed type 'InvalidDataException'
1 source compilation errors:
BC30299: 'test' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
BC30299: 'ClassInheritsClass' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
1 target compilation errors:
CS0509: 'test': cannot derive from sealed type 'InvalidDataException'");
CS0509: 'ClassInheritsClass': cannot derive from sealed type 'InvalidDataException'");
}

[Fact]
public async Task ClassInheritsClass2()
{
await TestConversionVisualBasicToCSharp(@"Class test
await TestConversionVisualBasicToCSharp(@"Class ClassInheritsClass2
Inherits System.IO.InvalidDataException
End Class",
@"using System.IO;
internal partial class test : InvalidDataException
internal partial class ClassInheritsClass2 : InvalidDataException
{
}
1 source compilation errors:
BC30299: 'test' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
1 target compilation errors:
CS0509: 'test': cannot derive from sealed type 'InvalidDataException'
1 source compilation errors:
BC30299: 'test' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
BC30299: 'ClassInheritsClass2' cannot inherit from class 'InvalidDataException' because 'InvalidDataException' is declared 'NotInheritable'.
1 target compilation errors:
CS0509: 'test': cannot derive from sealed type 'InvalidDataException'");
CS0509: 'ClassInheritsClass2': cannot derive from sealed type 'InvalidDataException'");
}

[Fact]
Expand Down
64 changes: 51 additions & 13 deletions Tests/CSharp/StatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,17 +1114,55 @@ private void TestMethod()
}");
}

[Theory]
[InlineData("Sub", "", "void")]
[InlineData("Function", " As Long", "long")]
public async Task DeclareStatement(string vbMethodDecl,string vbType, string csType)
[Fact]
public async Task DeclareStatementLong()
{
// Intentionally uses a type name with a different casing as the loop variable, i.e. "process" to test name resolution
await TestConversionVisualBasicToCSharp(@"Imports System.Diagnostics
Imports System.Threading
Public Class AcmeClass
Private Declare Sub SetForegroundWindow Lib ""user32"" (ByVal hwnd As Int32)
Public Shared Sub Main()
For Each proc In Process.GetProcesses().Where(Function(p) Not String.IsNullOrEmpty(p.MainWindowTitle))
SetForegroundWindow(proc.MainWindowHandle.ToInt32())
Thread.Sleep(1000)
Next
End Sub
End Class"
, @"using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
public partial class AcmeClass
{
[DllImport(""user32"")]
private static extern void SetForegroundWindow(int hwnd);
public static void Main()
{
foreach (var proc in Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)))
{
SetForegroundWindow(proc.MainWindowHandle.ToInt32());
Thread.Sleep(1000);
}
}
}
1 target compilation errors:
CS1547: Keyword 'void' cannot be used in this context");
}

[Fact]
public async Task DeclareStatementVoid()
{
// Intentionally uses a type name with a different casing as the loop variable, i.e. "process" to test name resolution
await TestConversionVisualBasicToCSharp($@"Imports System.Diagnostics
Imports System.Threading
Public Class AcmeClass
Private Declare {vbMethodDecl} SetForegroundWindow Lib ""user32"" (ByVal hwnd As Int32){vbType}
Private Declare Function SetForegroundWindow Lib ""user32"" (ByVal hwnd As Int32) As Long
Public Shared Sub Main()
For Each proc In Process.GetProcesses().Where(Function(p) Not String.IsNullOrEmpty(p.MainWindowTitle))
Expand All @@ -1133,25 +1171,25 @@ For Each proc In Process.GetProcesses().Where(Function(p) Not String.IsNullOrEmp
Next
End Sub
End Class"
, $@"using System.Diagnostics;
, @"using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
public partial class AcmeClass
{{
{
[DllImport(""user32"")]
private static extern {csType} SetForegroundWindow(int hwnd);
private static extern long SetForegroundWindow(int hwnd);
public static void Main()
{{
{
foreach (var proc in Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)))
{{
{
SetForegroundWindow(proc.MainWindowHandle.ToInt32());
Thread.Sleep(1000);
}}
}}
}}");
}
}
}");
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class TestConstants
/// Set to false
/// Commit
/// </summary>
public static bool RecharacterizeByWritingExpectedOverActual { get; } = false;
public static bool RecharacterizeByWritingExpectedOverActual { get; } = true;

public static string GetTestDataDirectory()
{
Expand Down
26 changes: 9 additions & 17 deletions Tests/TestRunners/ConverterTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ public ConverterTestBase(string rootNamespace = null)
.WithOptionStrict(OptionStrict.Off)
.WithOptionInfer(true);
EmptyNamespaceOptionStrictOff = new TextConversionOptions(DefaultReferences.NetStandard2) {
RootNamespaceOverride = string.Empty, TargetCompilationOptionsOverride = options
RootNamespaceOverride = string.Empty, TargetCompilationOptionsOverride = options,
ShowCompilationErrors = true
};
}

public async Task TestConversionCSharpToVisualBasic(string csharpCode, string expectedVisualBasicCode, bool expectSurroundingMethodBlock = false, bool expectCompilationErrors = false, TextConversionOptions conversion = null, bool hasLineCommentConversionIssue = false)
public async Task TestConversionCSharpToVisualBasic(string csharpCode, string expectedVisualBasicCode, bool expectSurroundingMethodBlock = false, bool expectCompilationErrors = false, TextConversionOptions conversionOptions = null, bool hasLineCommentConversionIssue = false)
{
expectedVisualBasicCode = AddSurroundingMethodBlock(expectedVisualBasicCode, expectSurroundingMethodBlock);

await TestConversionCSharpToVisualBasicWithoutComments(csharpCode, expectedVisualBasicCode, conversion);
conversionOptions ??= new TextConversionOptions(DefaultReferences.NetStandard2) { ShowCompilationErrors = !expectSurroundingMethodBlock };
await AssertConvertedCodeResultEquals<CSToVBConversion>(csharpCode, expectedVisualBasicCode, conversionOptions);
if (_testCstoVbCommentsByDefault && !hasLineCommentConversionIssue) {
await AssertLineCommentsConvertedInSameOrder<CSToVBConversion>(csharpCode, conversion, "//", LineCanHaveCSharpComment);
await AssertLineCommentsConvertedInSameOrder<CSToVBConversion>(csharpCode, conversionOptions, "//", LineCanHaveCSharpComment);
}
}

Expand Down Expand Up @@ -90,18 +92,14 @@ private static string AddSurroundingMethodBlock(string expectedVisualBasicCode,
return expectedVisualBasicCode;
}

private async Task TestConversionCSharpToVisualBasicWithoutComments(string csharpCode, string expectedVisualBasicCode, TextConversionOptions conversionOptions = null)
{
await AssertConvertedCodeResultEquals<CSToVBConversion>(csharpCode, expectedVisualBasicCode, conversionOptions);
}

/// <summary>
/// <paramref name="missingSemanticInfo"/> is currently unused but acts as documentation, and in future will be used to decide whether to check if the input/output compiles
/// </summary>
public async Task TestConversionVisualBasicToCSharp(string visualBasicCode, string expectedCsharpCode, bool expectSurroundingBlock = false, bool missingSemanticInfo = false, bool hasLineCommentConversionIssue = false)
{
if (expectSurroundingBlock) expectedCsharpCode = SurroundWithBlock(expectedCsharpCode);
await TestConversionVisualBasicToCSharpWithoutComments(visualBasicCode, expectedCsharpCode);
var conversionOptions = new TextConversionOptions(DefaultReferences.NetStandard2) { ShowCompilationErrors = !expectSurroundingBlock };
await AssertConvertedCodeResultEquals<VBToCSConversion>(visualBasicCode, expectedCsharpCode, conversionOptions);

if (_testVbtoCsCommentsByDefault && !hasLineCommentConversionIssue) {
await AssertLineCommentsConvertedInSameOrder<VBToCSConversion>(visualBasicCode, null, "'", _ => true);
Expand All @@ -114,15 +112,9 @@ private static string SurroundWithBlock(string expectedCsharpCode)
return $"{{\r\n {indentedStatements}\r\n}}";
}

public async Task TestConversionVisualBasicToCSharpWithoutComments(string visualBasicCode, string expectedCsharpCode)
{
await AssertConvertedCodeResultEquals<VBToCSConversion>(visualBasicCode, expectedCsharpCode);
}

protected async Task<string> Convert<TLanguageConversion>(string inputCode, TextConversionOptions conversionOptions = default) where TLanguageConversion : ILanguageConversion, new()
{
var textConversionOptions = conversionOptions ?? new TextConversionOptions(DefaultReferences.NetStandard2) { RootNamespaceOverride = _rootNamespace };
textConversionOptions.ShowCompilationErrors = true;
var textConversionOptions = conversionOptions ?? new TextConversionOptions(DefaultReferences.NetStandard2) { RootNamespaceOverride = _rootNamespace, ShowCompilationErrors = true };
var conversionResult = await ProjectConversion.ConvertText<TLanguageConversion>(inputCode, textConversionOptions);
return (conversionResult.ConvertedCode ?? "") + (conversionResult.GetExceptionsAsString() ?? "");
}
Expand Down
17 changes: 13 additions & 4 deletions Tests/VB/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Public Overloads Sub TestMethod()
TestMethod(3)
System.Console.WriteLine(""Shadowed implementation"")
End Sub
End Class", conversion: EmptyNamespaceOptionStrictOff);
End Class", conversionOptions: EmptyNamespaceOptionStrictOff);
}


Expand Down Expand Up @@ -474,7 +474,7 @@ End Sub
<Extension()>
Public Sub TestMethod2Parameters(ByVal str As String, ByVal __ As Action(Of String))
End Sub
End Module", conversion: EmptyNamespaceOptionStrictOff);
End Module", conversionOptions: EmptyNamespaceOptionStrictOff);
}

[Fact]
Expand Down Expand Up @@ -960,7 +960,16 @@ await TestConversionCSharpToVisualBasic(
@"[DllImport(""kernel32.dll"", SetLastError = true)]
static extern IntPtr OpenProcess(AccessMask dwDesiredAccess, bool bInheritHandle, uint dwProcessId);", @"<DllImport(""kernel32.dll"", SetLastError:=True)>
Private Shared Function OpenProcess(ByVal dwDesiredAccess As AccessMask, ByVal bInheritHandle As Boolean, ByVal dwProcessId As UInteger) As IntPtr
End Function");
End Function
5 source compilation errors:
CS0246: The type or namespace name 'AccessMask' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'IntPtr' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
CS0246: The type or namespace name 'SetLastError' could not be found (are you missing a using directive or an assembly reference?)
1 target compilation errors:
BC30002: Type 'AccessMask' is not defined.");
}

[Fact]
Expand Down Expand Up @@ -1324,7 +1333,7 @@ public object TestMethod(System.Type param1, System.Globalization.CultureInfo pa
Public Function TestMethod(ByVal param1 As System.Type, ByVal param2 As System.Globalization.CultureInfo) As Object
Return Nothing
End Function
End Class", conversion: EmptyNamespaceOptionStrictOff);
End Class", conversionOptions: EmptyNamespaceOptionStrictOff);
}

[Fact]// The stack trace displayed will change from time to time. Feel free to update this characterization test appropriately.
Expand Down
Loading

0 comments on commit d578963

Please sign in to comment.