1+ using System . Threading . Tasks ;
2+ using ICSharpCode . CodeConverter . Tests . TestRunners ;
3+ using Xunit ;
4+
5+ namespace ICSharpCode . CodeConverter . Tests . CSharp . MemberTests ;
6+
7+ public class ConstructorTests : ConverterTestBase
8+ {
9+ [ Fact ]
10+ public async Task TestConstructorVisibilityAsync ( )
11+ {
12+ await TestConversionVisualBasicToCSharpAsync ( @"Class Class1
13+ Sub New(x As Boolean)
14+ End Sub
15+ End Class" , @"
16+ internal partial class Class1
17+ {
18+ public Class1(bool x)
19+ {
20+ }
21+ }" ) ;
22+ }
23+
24+ [ Fact ]
25+ public async Task TestModuleConstructorAsync ( )
26+ {
27+ await TestConversionVisualBasicToCSharpAsync (
28+ @"Module Module1
29+ Sub New()
30+ Dim someValue As Integer = 0
31+ End Sub
32+ End Module" , @"
33+ internal static partial class Module1
34+ {
35+ static Module1()
36+ {
37+ int someValue = 0;
38+ }
39+ }" ) ;
40+ }
41+
42+ [ Fact ]
43+ public async Task TestHoistedOutParameterAsync ( )
44+ {
45+ await TestConversionVisualBasicToCSharpAsync (
46+ @"Public Class ClassWithProperties
47+ Public Property Property1 As String
48+ End Class
49+
50+ Public Class VisualBasicClass
51+ Public Sub New()
52+ Dim x As New Dictionary(Of String, String)()
53+ Dim y As New ClassWithProperties()
54+
55+ If (x.TryGetValue(""x"", y.Property1)) Then
56+ Debug.Print(y.Property1)
57+ End If
58+ End Sub
59+ End Class" , @"using System.Collections.Generic;
60+ using System.Diagnostics;
61+
62+ public partial class ClassWithProperties
63+ {
64+ public string Property1 { get; set; }
65+ }
66+
67+ public partial class VisualBasicClass
68+ {
69+ public VisualBasicClass()
70+ {
71+ var x = new Dictionary<string, string>();
72+ var y = new ClassWithProperties();
73+
74+ bool localTryGetValue() { string argvalue = y.Property1; var ret = x.TryGetValue(""x"", out argvalue); y.Property1 = argvalue; return ret; }
75+
76+ if (localTryGetValue())
77+ {
78+ Debug.Print(y.Property1);
79+ }
80+ }
81+ }" ) ;
82+ }
83+
84+ [ Fact ]
85+ public async Task TestHoistedOutParameterLambdaUsingByRefParameterAsync ( )
86+ {
87+ await TestConversionVisualBasicToCSharpAsync (
88+ @"Public Class SomeClass
89+ Sub S(Optional ByRef x As Integer = -1)
90+ Dim i As Integer = 0
91+ If F1(x, i) Then
92+ ElseIf F2(x, i) Then
93+ ElseIf F3(x, i) Then
94+ End If
95+ End Sub
96+
97+ Function F1(x As Integer, ByRef o As Object) As Boolean : End Function
98+ Function F2(ByRef x As Integer, ByRef o As Object) As Boolean : End Function
99+ Function F3(ByRef x As Object, ByRef o As Object) As Boolean : End Function
100+ End Class" , @"using System.Runtime.InteropServices;
101+ using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
102+
103+ public partial class SomeClass
104+ {
105+ public void S([Optional, DefaultParameterValue(-1)] ref int x)
106+ {
107+ int i = 0;
108+ bool localF1(ref int x) { object argo = i; var ret = F1(x, ref argo); i = Conversions.ToInteger(argo); return ret; }
109+ bool localF2(ref int x) { object argo1 = i; var ret = F2(ref x, ref argo1); i = Conversions.ToInteger(argo1); return ret; }
110+ bool localF3(ref int x) { object argx = x; object argo2 = i; var ret = F3(ref argx, ref argo2); x = Conversions.ToInteger(argx); i = Conversions.ToInteger(argo2); return ret; }
111+
112+ if (localF1(ref x))
113+ {
114+ }
115+ else if (localF2(ref x))
116+ {
117+ }
118+ else if (localF3(ref x))
119+ {
120+ }
121+ }
122+
123+ public bool F1(int x, ref object o)
124+ {
125+ return default;
126+ }
127+ public bool F2(ref int x, ref object o)
128+ {
129+ return default;
130+ }
131+ public bool F3(ref object x, ref object o)
132+ {
133+ return default;
134+ }
135+ }" ) ;
136+ }
137+
138+ [ Fact ]
139+ public async Task TestConstructorAsync ( )
140+ {
141+ await TestConversionVisualBasicToCSharpAsync (
142+ @"Class TestClass(Of T As {Class, New}, T2 As Structure, T3)
143+ Public Sub New(<Out> ByRef argument As T, ByRef argument2 As T2, ByVal argument3 As T3)
144+ End Sub
145+ End Class" , @"
146+ internal partial class TestClass<T, T2, T3>
147+ where T : class, new()
148+ where T2 : struct
149+ {
150+ public TestClass(out T argument, ref T2 argument2, T3 argument3)
151+ {
152+ }
153+ }
154+ 1 target compilation errors:
155+ CS0177: The out parameter 'argument' must be assigned to before control leaves the current method" ) ;
156+ }
157+
158+ [ Fact ]
159+ public async Task TestConstructorWithImplicitPublicAccessibilityAsync ( )
160+ {
161+ await TestConversionVisualBasicToCSharpAsync (
162+ @"Sub New()
163+ End Sub" , @"public SurroundingClass()
164+ {
165+ }" ) ;
166+ }
167+
168+ [ Fact ]
169+ public async Task TestStaticConstructorAsync ( )
170+ {
171+ await TestConversionVisualBasicToCSharpAsync (
172+ @"Shared Sub New()
173+ End Sub" , @"static SurroundingClass()
174+ {
175+ }" ) ;
176+ }
177+
178+ [ Fact ]
179+ public async Task TestConstructorStaticLocalConvertedToFieldAsync ( )
180+ {
181+ await TestConversionVisualBasicToCSharpAsync (
182+ @"Class StaticLocalConvertedToField
183+ Sub New(x As Boolean)
184+ Static sPrevPosition As Integer = 7 ' Comment moves with declaration
185+ Console.WriteLine(sPrevPosition)
186+ End Sub
187+ Sub New(x As Integer)
188+ Static sPrevPosition As Integer
189+ Console.WriteLine(sPrevPosition)
190+ End Sub
191+ End Class" , @"using System;
192+
193+ internal partial class StaticLocalConvertedToField
194+ {
195+ private int _sPrevPosition = 7; // Comment moves with declaration
196+ public StaticLocalConvertedToField(bool x)
197+ {
198+ Console.WriteLine(_sPrevPosition);
199+ }
200+
201+ private int _sPrevPosition1 = default;
202+ public StaticLocalConvertedToField(int x)
203+ {
204+ Console.WriteLine(_sPrevPosition1);
205+ }
206+ }" ) ;
207+ }
208+ }
0 commit comments