forked from icsharpcode/CodeConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodStatementTests.cs
More file actions
1716 lines (1555 loc) · 43.6 KB
/
MethodStatementTests.cs
File metadata and controls
1716 lines (1555 loc) · 43.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.Tests.TestRunners;
using Xunit;
namespace ICSharpCode.CodeConverter.Tests.CSharp.StatementTests;
public class MethodStatementTests : ConverterTestBase
{
[Fact]
public async Task EmptyStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
If True Then
End If
While True
End While
Do
Loop While True
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
if (true)
{
}
while (true)
{
}
do
{
}
while (true);
}
}");
}
[Fact]
public async Task AssignmentStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b As Integer
b = 0
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
int b;
b = 0;
}
}");
}
[Fact]
public async Task EnumAssignmentStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Enum MyEnum
AMember
End Enum
Class TestClass
Private Sub TestMethod(v as String)
Dim b As MyEnum = MyEnum.Parse(GetType(MyEnum), v)
b = MyEnum.Parse(GetType(MyEnum), v)
End Sub
End Class", @"using System;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
internal enum MyEnum
{
AMember
}
internal partial class TestClass
{
private void TestMethod(string v)
{
MyEnum b = (MyEnum)Conversions.ToInteger(Enum.Parse(typeof(MyEnum), v));
b = (MyEnum)Conversions.ToInteger(Enum.Parse(typeof(MyEnum), v));
}
}");
}
[Fact]
public async Task AssignmentStatementInDeclarationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b As Integer = 0
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
int b = 0;
}
}");
}
[Fact]
public async Task AssignmentStatementInVarDeclarationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b = 0
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
int b = 0;
}
}");
}
/// <summary>
/// Implicitly typed lambdas exist in vb but are not happening in C#. See discussion on https://github.com/dotnet/roslyn/issues/14
/// * For VB local declarations, inference happens. The closest equivalent in C# is a local function since Func/Action would be overly restrictive for some cases
/// * For VB field declarations, inference doesn't happen, it just uses "Object", but in C# lambdas can't be assigned to object so we have to settle for Func/Action for externally visible methods to maintain assignability.
/// </summary>
[Fact]
public async Task AssignmentStatementWithFuncAsync()
{
// BUG: pubWrite's body is missing a return statement
// pubWrite is an example of when the LambdaConverter could analyze ConvertedType at usages, realize the return type is never used, and convert it to an Action.
await TestConversionVisualBasicToCSharpAsync(@"Public Class TestFunc
Public pubIdent = Function(row As Integer) row
Public pubWrite = Function(row As Integer) Console.WriteLine(row)
Dim isFalse = Function(row As Integer) False
Dim write0 = Sub()
Console.WriteLine(0)
End Sub
Private Sub TestMethod()
Dim index = (Function(pList As List(Of String)) pList.All(Function(x) True)),
index2 = (Function(pList As List(Of String)) pList.All(Function(x) False)),
index3 = (Function(pList As List(Of Integer)) pList.All(Function(x) True))
Dim isTrue = Function(pList As List(Of String))
Return pList.All(Function(x) True)
End Function
Dim isTrueWithNoStatement = (Function(pList As List(Of String)) pList.All(Function(x) True))
Dim write = Sub() Console.WriteLine(1)
End Sub
End Class", @"using System;
using System.Collections.Generic;
using System.Linq;
public partial class TestFunc
{
public Func<int, int> pubIdent = (row) => row;
public Func<int, object> pubWrite = (row) => Console.WriteLine(row);
private bool isFalse(int row) => false;
private void write0() => Console.WriteLine(0);
private void TestMethod()
{
bool index(List<string> pList) => pList.All(x => true);
bool index2(List<string> pList) => pList.All(x => false);
bool index3(List<int> pList) => pList.All(x => true);
bool isTrue(List<string> pList) => pList.All(x => true);
bool isTrueWithNoStatement(List<string> pList) => pList.All(x => true);
void write() => Console.WriteLine(1);
}
}
1 source compilation errors:
BC30491: Expression does not produce a value.
2 target compilation errors:
CS0029: Cannot implicitly convert type 'void' to 'object'
CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type");
}
/// <summary>
/// Technically it's possible to use a type-inferred lambda within a for loop
/// Other than the above field/local declarations, candidates would be other things using <see cref="SplitVariableDeclarations"/>,
/// e.g. ForEach (no assignment involved), Using block (can't have a disposable lambda)
/// </summary>
[Fact]
public async Task ContrivedFuncInferenceExampleAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Friend Class ContrivedFuncInferenceExample
Private Sub TestMethod()
For index = (Function(pList As List(Of String)) pList.All(Function(x) True)) To New Blah() Step New Blah()
Dim buffer = index.Check(New List(Of String))
Console.WriteLine($""{buffer}"")
Next
End Sub
Class Blah
Public ReadOnly Check As Func(Of List(Of String), Boolean)
Public Sub New(Optional check As Func(Of List(Of String), Boolean) = Nothing)
check = check
End Sub
Public Shared Widening Operator CType(ByVal p1 As Func(Of List(Of String), Boolean)) As Blah
Return New Blah(p1)
End Operator
Public Shared Widening Operator CType(ByVal p1 As Blah) As Func(Of List(Of String), Boolean)
Return p1.Check
End Operator
Public Shared Operator -(ByVal p1 As Blah, ByVal p2 As Blah) As Blah
Return New Blah()
End Operator
Public Shared Operator +(ByVal p1 As Blah, ByVal p2 As Blah) As Blah
Return New Blah()
End Operator
Public Shared Operator <=(ByVal p1 As Blah, ByVal p2 As Blah) As Boolean
Return p1.Check(New List(Of String))
End Operator
Public Shared Operator >=(ByVal p1 As Blah, ByVal p2 As Blah) As Boolean
Return p2.Check(New List(Of String))
End Operator
End Class
End Class", @"using System;
using System.Collections.Generic;
using System.Linq;
internal partial class ContrivedFuncInferenceExample
{
private void TestMethod()
{
for (Blah index = (pList) => pList.All(x => true), loopTo = new Blah(); new Blah() >= 0 ? index <= loopTo : index >= loopTo; index += new Blah())
{
bool buffer = index.Check(new List<string>());
Console.WriteLine($""{buffer}"");
}
}
public partial class Blah
{
public readonly Func<List<string>, bool> Check;
public Blah(Func<List<string>, bool> check = null)
{
check = check;
}
public static implicit operator Blah(Func<List<string>, bool> p1)
{
return new Blah(p1);
}
public static implicit operator Func<List<string>, bool>(Blah p1)
{
return p1.Check;
}
public static Blah operator -(Blah p1, Blah p2)
{
return new Blah();
}
public static Blah operator +(Blah p1, Blah p2)
{
return new Blah();
}
public static bool operator <=(Blah p1, Blah p2)
{
return p1.Check(new List<string>());
}
public static bool operator >=(Blah p1, Blah p2)
{
return p2.Check(new List<string>());
}
}
}
2 target compilation errors:
CS1660: Cannot convert lambda expression to type 'ContrivedFuncInferenceExample.Blah' because it is not a delegate type
CS0019: Operator '>=' cannot be applied to operands of type 'ContrivedFuncInferenceExample.Blah' and 'int'");
}
[Fact]
public async Task ObjectInitializationStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b As String
b = New String(""test"")
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string b;
b = new string(""test"");
}
}");
}
[Fact]
public async Task TupleInitializationStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim totales As (fics As Integer, dirs As Integer) = (0, 0)
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
(int fics, int dirs) totales = (0, 0);
}
}");
}
[Fact]
public async Task ObjectInitializationStatementInDeclarationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b As String = New String(""test"")
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string b = new string(""test"");
}
}");
}
[Fact]
public async Task ObjectInitializationStatementInVarDeclarationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b = New String(""test"")
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string b = new string(""test"");
}
}");
}
[Fact]
public async Task EndStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
End
End Sub
End Class", @"using System;
internal partial class TestClass
{
private void TestMethod()
{
Environment.Exit(0);
}
}
1 source compilation errors:
BC30615: 'End' statement cannot be used in class library projects.");
}
[Fact]
public async Task StopStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Stop
End Sub
End Class", @"using System.Diagnostics;
internal partial class TestClass
{
private void TestMethod()
{
Debugger.Break();
}
}");
}
[Fact]
public async Task WithBlockAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
With New System.Text.StringBuilder
.Capacity = 20
?.Append(0)
End With
End Sub
End Class", @"using System.Text;
internal partial class TestClass
{
private void TestMethod()
{
{
var withBlock = new StringBuilder();
withBlock.Capacity = 20;
withBlock?.Append(0);
}
}
}");
}
[Fact]
public async Task WithBlockStruct634Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System
Public Structure SomeStruct
Public FieldA As Integer
Public FieldB As Integer
End Structure
Module Module1
Sub Main()
Dim myArray(0) As SomeStruct
With myArray(0)
.FieldA = 3
.FieldB = 4
End With
'Outputs: FieldA was changed to New FieldA value
Console.WriteLine($""FieldA was changed to {myArray(0).FieldA}"")
Console.WriteLine($""FieldB was changed to {myArray(0).FieldB}"")
Console.ReadLine
End Sub
End Module", @"using System;
public partial struct SomeStruct
{
public int FieldA;
public int FieldB;
}
internal static partial class Module1
{
public static void Main()
{
var myArray = new SomeStruct[1];
{
ref var withBlock = ref myArray[0];
withBlock.FieldA = 3;
withBlock.FieldB = 4;
}
// Outputs: FieldA was changed to New FieldA value
Console.WriteLine($""FieldA was changed to {myArray[0].FieldA}"");
Console.WriteLine($""FieldB was changed to {myArray[0].FieldB}"");
Console.ReadLine();
}
}");
}
[Fact]
public async Task WithBlock2Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Data.SqlClient
Class TestClass
Private Sub Save()
Using cmd As SqlCommand = new SqlCommand()
With cmd
.ExecuteNonQuery()
?.ExecuteNonQuery()
.ExecuteNonQuery
?.ExecuteNonQuery
End With
End Using
End Sub
End Class", @"using System.Data.SqlClient;
internal partial class TestClass
{
private void Save()
{
using (var cmd = new SqlCommand())
{
cmd.ExecuteNonQuery();
cmd?.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cmd?.ExecuteNonQuery();
}
}
}");
}
[Fact]
public async Task WithBlockValueAsync()
{
//Whitespace trivia bug on first statement in with block
await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass
Public Sub Stuff()
Dim str As SomeStruct
With Str
ReDim .ArrField(1)
ReDim .ArrProp(2)
End With
End Sub
End Class
Public Structure SomeStruct
Public ArrField As String()
Public Property ArrProp As String()
End Structure", @"
public partial class VisualBasicClass
{
public void Stuff()
{
var str = default(SomeStruct);
str.ArrField = new string[2];
str.ArrProp = new string[3];
}
}
public partial struct SomeStruct
{
public string[] ArrField;
public string[] ArrProp { get; set; }
}");
}
[Fact]
public async Task WithBlockMeClassAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class TestWithMe
Private _x As Integer
Sub S()
With Me
._x = 1
._x = 2
End With
End Sub
End Class", @"
public partial class TestWithMe
{
private int _x;
public void S()
{
_x = 1;
_x = 2;
}
}");
}
[Fact]
public async Task WithBlockMeStructAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Structure TestWithMe
Private _x As Integer
Sub S()
With Me
._x = 1
._x = 2
End With
End Sub
End Structure", @"
public partial struct TestWithMe
{
private int _x;
public void S()
{
_x = 1;
_x = 2;
}
}");
}
[Fact]
public async Task WithBlockForEachAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Collections.Generic
Public Class TestWithForEachClass
Private _x As Integer
Public Shared Sub Main()
Dim x = New List(Of TestWithForEachClass)()
For Each y In x
With y
._x = 1
System.Console.Write(._x)
End With
y = Nothing
Next
End Sub
End Class", @"using System;
using System.Collections.Generic;
public partial class TestWithForEachClass
{
private int _x;
public static void Main()
{
var x = new List<TestWithForEachClass>();
foreach (var y in x)
{
y._x = 1;
Console.Write(y._x);
y = null;
}
}
}
1 target compilation errors:
CS1656: Cannot assign to 'y' because it is a 'foreach iteration variable'");
}
[Fact]
public async Task NestedWithBlockAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
With New System.Text.StringBuilder
Dim withBlock as Integer = 3
With New System.Text.StringBuilder
Dim withBlock1 as Integer = 4
.Capacity = withBlock1
End With
.Length = withBlock
End With
End Sub
End Class", @"using System.Text;
internal partial class TestClass
{
private void TestMethod()
{
{
var withBlock2 = new StringBuilder();
int withBlock = 3;
{
var withBlock3 = new StringBuilder();
int withBlock1 = 4;
withBlock3.Capacity = withBlock1;
}
withBlock2.Length = withBlock;
}
}
}");
}
[Fact]
public async Task DeclarationStatementsAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Class Test
Private Sub TestMethod()
the_beginning:
Dim value As Integer = 1
Const myPIe As Double = 2 * System.Math.PI
Dim text = ""This is my text!""
GoTo the_beginning
End Sub
End Class", @"using System;
internal partial class Test
{
private void TestMethod()
{
the_beginning:
;
int value = 1;
const double myPIe = 2d * Math.PI;
string text = ""This is my text!"";
goto the_beginning;
}
}");
}
[Fact]
public async Task DeclarationStatementTwoVariablesAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Class Test
Private Sub TestMethod()
Dim x, y As Date
Console.WriteLine(x)
Console.WriteLine(y)
End Sub
End Class", @"using System;
internal partial class Test
{
private void TestMethod()
{
DateTime x = default, y = default;
Console.WriteLine(x);
Console.WriteLine(y);
}
}");
}
[Fact]
public async Task DeclareStatementLongAsync()
{
// Intentionally uses a type name with a different casing as the loop variable, i.e. "process" to test name resolution
await TestConversionVisualBasicToCSharpAsync(@"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);
}
}
}");
}
[Fact]
public async Task DeclareStatementVoidAsync()
{
// Intentionally uses a type name with a different casing as the loop variable, i.e. "process" to test name resolution
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Diagnostics
Imports System.Threading
Public Class AcmeClass
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))
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 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]
public async Task DeclareStatementWithAttributesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class AcmeClass
Friend Declare Ansi Function GetNumDevices Lib ""CP210xManufacturing.dll"" Alias ""CP210x_GetNumDevices"" (ByRef NumDevices As String) As Integer
End Class"
, @"using System.Runtime.InteropServices;
public partial class AcmeClass
{
[DllImport(""CP210xManufacturing.dll"", EntryPoint = ""CP210x_GetNumDevices"", CharSet = CharSet.Ansi)]
internal static extern int GetNumDevices(ref string NumDevices);
}");
}
[Fact]
public async Task IfStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(ByVal a As Integer)
Dim b As Integer
If a = 0 Then
b = 0
ElseIf a = 1 Then
b = 1
ElseIf a = 2 OrElse a = 3 Then
b = 2
Else
b = 3
End If
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod(int a)
{
int b;
if (a == 0)
{
b = 0;
}
else if (a == 1)
{
b = 1;
}
else if (a == 2 || a == 3)
{
b = 2;
}
else
{
b = 3;
}
}
}");
}
[Fact]
public async Task IfStatementWithMultiStatementLineAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Public Shared Sub MultiStatement(a As Integer)
If a = 0 Then Console.WriteLine(1) : Console.WriteLine(2) : Return
Console.WriteLine(3)
End Sub
End Class", @"using System;
internal partial class TestClass
{
public static void MultiStatement(int a)
{
if (a == 0)
{
Console.WriteLine(1);
Console.WriteLine(2);
return;
}
Console.WriteLine(3);
}
}");
}
[Fact]
public async Task NestedBlockStatementsKeepSameNestingAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Shared Function FindTextInCol(w As String, pTitleRow As Integer, startCol As Integer, needle As String) As Integer
For c As Integer = startCol To w.Length
If needle = """" Then
If String.IsNullOrWhiteSpace(w(c).ToString) Then
Return c
End If
Else
If w(c).ToString = needle Then
Return c
End If
End If
Next
Return -1
End Function
End Class", @"
internal partial class TestClass
{
public static int FindTextInCol(string w, int pTitleRow, int startCol, string needle)
{
for (int c = startCol, loopTo = w.Length; c <= loopTo; c++)
{
if (string.IsNullOrEmpty(needle))
{
if (string.IsNullOrWhiteSpace(w[c].ToString()))
{
return c;
}
}
else if ((w[c].ToString() ?? """") == (needle ?? """"))
{
return c;
}
}
return -1;
}
}");
}
[Fact]
public async Task SyncLockStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(ByVal nullObject As Object)
If nullObject Is Nothing Then Throw New ArgumentNullException(NameOf(nullObject))
SyncLock nullObject
Console.WriteLine(nullObject)
End SyncLock
End Sub
End Class", @"using System;
internal partial class TestClass
{
private void TestMethod(object nullObject)
{
if (nullObject is null)
throw new ArgumentNullException(nameof(nullObject));
lock (nullObject)
Console.WriteLine(nullObject);
}
}");
}
[Fact]
public async Task ThrowStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(ByVal nullObject As Object)
If nullObject Is Nothing Then Throw New ArgumentNullException(NameOf(nullObject))
End Sub
End Class", @"using System;
internal partial class TestClass
{
private void TestMethod(object nullObject)
{
if (nullObject is null)
throw new ArgumentNullException(nameof(nullObject));
}
}");
}
[Fact]
public async Task CallStatementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Call (Sub() Console.Write(""Hello""))
Call (Sub() Console.Write(""Hello""))()
Call TestMethod
Call TestMethod()
End Sub
End Class", @"using System;
internal partial class TestClass
{
private void TestMethod()
{
(() => Console.Write(""Hello""))();
(() => Console.Write(""Hello""))();
TestMethod();
TestMethod();
}
}
1 target compilation errors:
CS0149: Method name expected");
//BUG: Requires new Action wrapper
}
[Fact]
public async Task AddRemoveHandlerAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Public Event MyEvent As EventHandler
Private Sub TestMethod(ByVal e As EventHandler)
AddHandler Me.MyEvent, e
AddHandler Me.MyEvent, AddressOf MyHandler
End Sub
Private Sub TestMethod2(ByVal e As EventHandler)
RemoveHandler Me.MyEvent, e
RemoveHandler Me.MyEvent, AddressOf MyHandler
End Sub
Private Sub MyHandler(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class", @"using System;