-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathExpressionTests.cs
More file actions
2917 lines (2633 loc) · 73.3 KB
/
ExpressionTests.cs
File metadata and controls
2917 lines (2633 loc) · 73.3 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.ExpressionTests;
public class ExpressionTests : ConverterTestBase
{
[Fact]
public async Task ComparingStringsUsesCoerceToNonNullOnlyWhenNeededAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a as String)
Dim result = a = ("""")
result = """" = a
result = a = (String.Empty)
result = String.Empty = a
result = a = (Nothing)
result = Nothing = a
result = a Is Nothing
result = a IsNot Nothing
result = Not(a IsNot Nothing)
result = a = a
result = a = (""test"")
result = ""test"" = a
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod(string a)
{
bool result = string.IsNullOrEmpty(a);
result = string.IsNullOrEmpty(a);
result = string.IsNullOrEmpty(a);
result = string.IsNullOrEmpty(a);
result = string.IsNullOrEmpty(a);
result = string.IsNullOrEmpty(a);
result = a is null;
result = a is not null;
result = a is null;
result = (a ?? """") == (a ?? """");
result = a == ""test"";
result = ""test"" == a;
}
}");
}
[Fact]
public async Task DynamicTestAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class C
Public Function IsPointWithinBoundaryBox(ByVal dblLat As Double, dblLon As Double, ByVal boundbox As Object) As Boolean
If boundbox IsNot Nothing Then
Dim boolInLatBounds As Boolean = (dblLat <= boundbox.north) And (dblLat >= boundbox.south) 'Less then highest (northmost) lat, AND more than lowest (southmost) lat
Dim boolInLonBounds As Boolean = (dblLon >= boundbox.west) And (dblLon <= boundbox.east) 'More than lowest (westmost) lat, AND less than highest (eastmost) lon
Return boolInLatBounds And boolInLonBounds
Else
'Throw New Exception(""boundbox is null."")
End If
Return False
End Function
End Class
", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class C
{
public bool IsPointWithinBoundaryBox(double dblLat, double dblLon, object boundbox)
{
if (boundbox is not null)
{
bool boolInLatBounds = Conversions.ToBoolean(Operators.AndObject(Operators.ConditionalCompareObjectLessEqual(dblLat, ((dynamic)boundbox).north, false), Operators.ConditionalCompareObjectGreaterEqual(dblLat, ((dynamic)boundbox).south, false))); // Less then highest (northmost) lat, AND more than lowest (southmost) lat
bool boolInLonBounds = Conversions.ToBoolean(Operators.AndObject(Operators.ConditionalCompareObjectGreaterEqual(dblLon, ((dynamic)boundbox).west, false), Operators.ConditionalCompareObjectLessEqual(dblLon, ((dynamic)boundbox).east, false))); // More than lowest (westmost) lat, AND less than highest (eastmost) lon
return boolInLatBounds & boolInLonBounds;
}
else
{
// Throw New Exception(""boundbox is null."")
}
return false;
}
}");
}
[Fact]
public async Task DynamicAccessAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Option Strict Off ' Directive gets removed
Public Class TestDynamicUsage
Property Prop As Integer
Sub S()
Dim o As Object
o = New TestDynamicUsage
o.Prop = 1 'Must not cast to object here
End Sub
End Class", @"
public partial class TestDynamicUsage
{
public int Prop { get; set; }
public void S()
{
object o;
o = new TestDynamicUsage();
((dynamic)o).Prop = 1; // Must not cast to object here
}
}");
}
[Fact]
public async Task DynamicBoolAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class C
Public Function IsHybridApp() As Boolean
Return New Object().Session(""hybrid"") IsNot Nothing AndAlso New Object().Session(""hybrid"") = 1
End Function
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class C
{
public bool IsHybridApp()
{
return Conversions.ToBoolean(((dynamic)new object()).Session(""hybrid"") is not null && Operators.ConditionalCompareObjectEqual(((dynamic)new object()).Session(""hybrid""), 1, false));
}
}");
}
[Fact]
public async Task ConversionOfNotUsesParensIfNeededAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim rslt = Not 1 = 2
Dim rslt2 = Not True
Dim rslt3 = TypeOf New Object() IsNot Boolean
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
bool rslt = !(1 == 2);
bool rslt2 = !true;
bool rslt3 = !(new object() is bool);
}
}");
}
[Fact]
public async Task DateLiteralsAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(Optional ByVal pDate As Date = #1/1/1900#)
Dim rslt = #1/1/1900#
Dim rslt2 = #8/13/2002 12:14 PM#
End Sub
End Class", @"using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
internal partial class TestClass
{
private void TestMethod([Optional, DateTimeConstant(599266080000000000L/* #1/1/1900# */)] DateTime pDate)
{
var rslt = DateTime.Parse(""1900-01-01"");
var rslt2 = DateTime.Parse(""2002-08-13 12:14:00"");
}
}");
}
[Fact]
public async Task ImplicitCastToDoubleLiteralAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Class DoubleLiteral
Private Function Test(myDouble As Double) As Double
Return Test(2.37D) + Test(&HFFUL) 'VB: D means decimal, C#: D means double
End Function
End Class", @"
internal partial class DoubleLiteral
{
private double Test(double myDouble)
{
return Test(2.37d) + Test(255d); // VB: D means decimal, C#: D means double
}
}");
}
[Fact]
public async Task DateConstsAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue213
Const x As Date = #1990-1-1#
Private Sub Y(Optional ByVal opt As Date = x)
End Sub
End Class", @"using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
public partial class Issue213
{
private static DateTime x = DateTime.Parse(""1990-01-01"");
private void Y([Optional, DateTimeConstant(627667488000000000L/* Global.Issue213.x */)] DateTime opt)
{
}
}");
}
[Fact]
public async Task MethodCallWithImplicitConversionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo()
Bar(True)
Me.Bar(""4"")
Dim ss(1) As String
Dim y = ss(""0"")
End Sub
Sub Bar(x as Integer)
End Sub
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class Class1
{
public void Foo()
{
Bar(Conversions.ToInteger(true));
Bar(Conversions.ToInteger(""4""));
var ss = new string[2];
string y = ss[Conversions.ToInteger(""0"")];
}
public void Bar(int x)
{
}
}");
}
[Fact]
public async Task Issue580_EnumCastsAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class EnumToString
Enum Tes As Short
None = 0
TEST2 = 2
End Enum
Private Sub TEest2(aEnum As Tes)
Dim sxtr_Tmp As String = ""Use"" & CShort(aEnum).ToString
Dim si_Txt As Short = CShort(2 ^ Tes.TEST2)
End Sub
End Class",
@"using System;
public partial class EnumToString
{
public enum Tes : short
{
None = 0,
TEST2 = 2
}
private void TEest2(Tes aEnum)
{
string sxtr_Tmp = ""Use"" + ((short)aEnum).ToString();
short si_Txt = (short)Math.Round(Math.Pow(2d, (double)Tes.TEST2));
}
}");
}
[Fact]
public async Task IntToEnumArgAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo(ByVal arg As TriState)
End Sub
Sub Main()
Foo(0)
End Sub
End Class",
@"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic
public partial class Class1
{
public void Foo(TriState arg)
{
}
public void Main()
{
Foo(0);
}
}");
}
[Fact] // https://github.com/icsharpcode/CodeConverter/issues/636
public async Task CharacterizeCompilationErrorsWithLateBoundImplicitObjectNarrowingAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass
Public Sub Rounding()
Dim o as Object = 3.0f
Dim x = Math.Round(o, 2)
End Sub
End Class",
@"using System;
public partial class VisualBasicClass
{
public void Rounding()
{
object o = 3.0f;
var x = Math.Round(o, 2);
}
}
1 target compilation errors:
CS1503: Argument 1: cannot convert from 'object' to 'decimal'");
}
[Fact]
public async Task EnumToIntCastAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class MyTest
Public Enum TestEnum As Integer
Test1 = 0
Test2 = 1
End Enum
Sub Main()
Dim EnumVariable = TestEnum.Test1
Dim t1 As Integer = EnumVariable
End Sub
End Class",
@"
public partial class MyTest
{
public enum TestEnum : int
{
Test1 = 0,
Test2 = 1
}
public void Main()
{
var EnumVariable = TestEnum.Test1;
int t1 = (int)EnumVariable;
}
}
");
}
[Fact]
public async Task FlagsEnumAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"<Flags()> Public Enum FilePermissions As Integer
None = 0
Create = 1
Read = 2
Update = 4
Delete = 8
End Enum
Public Class MyTest
Public MyEnum As FilePermissions = FilePermissions.None + FilePermissions.Create
End Class",
@"using System;
[Flags()]
public enum FilePermissions : int
{
None = 0,
Create = 1,
Read = 2,
Update = 4,
Delete = 8
}
public partial class MyTest
{
public FilePermissions MyEnum = (FilePermissions)((int)FilePermissions.None + (int)FilePermissions.Create);
}");
}
[Fact]
public async Task EnumSwitchAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Enum E
A
End Enum
Sub Main()
Dim e1 = E.A
Dim e2 As Integer
Select Case e1
Case 0
End Select
Select Case e2
Case E.A
End Select
End Sub
End Class",
@"
public partial class Class1
{
public enum E
{
A
}
public void Main()
{
var e1 = E.A;
var e2 = default(int);
switch (e1)
{
case 0:
break;
}
switch (e2)
{
case (int)E.A:
break;
}
}
}");
}
[Fact]
public async Task DuplicateCaseDiscardedAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System
Friend Module Module1
Sub Main()
Select Case 1
Case 1
Console.WriteLine(""a"")
Case 1
Console.WriteLine(""b"")
End Select
End Sub
End Module",
@"using System;
internal static partial class Module1
{
public static void Main()
{
switch (1)
{
case 1:
Console.WriteLine(""a"");
break;
case var @case when @case == 1:
Console.WriteLine(""b"");
break;
}
}
}
1 target compilation errors:
CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code");
//BUG: Correct textual output, but requires var pattern syntax construct not available before CodeAnalysis 3
}
[Fact]
public async Task MethodCallWithoutParensAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo()
Dim w = Bar
Dim x = Me.Bar
Dim y = Baz()
Dim z = Me.Baz()
End Sub
Function Bar() As Integer
Return 1
End Function
Property Baz As Integer
End Class", @"
public partial class Class1
{
public void Foo()
{
int w = Bar();
int x = Bar();
int y = Baz;
int z = Baz;
}
public int Bar()
{
return 1;
}
public int Baz { get; set; }
}");
}
[Fact]
public async Task ConversionOfCTypeUsesParensIfNeededAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim rslt = Ctype(true, Object).ToString()
Dim rslt2 = Ctype(true, Object)
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string rslt = true.ToString();
object rslt2 = true;
}
}");
}
[Fact]
public async Task DateKeywordAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private DefaultDate as Date = Nothing
End Class", @"using System;
internal partial class TestClass
{
private DateTime DefaultDate = default;
}");
}
[Fact]
public async Task IfNothingAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System
Public Class VisualBasicClass
Dim SomeDate = """"
Dim SomeDateDateNothing As Date? = If(String.IsNullOrEmpty(SomeDate), Nothing, DateTime.Parse(SomeDate))
Dim isNotNothing = SomeDateDateNothing IsNot Nothing
Dim isSomething = SomeDateDateNothing = New Date()
End Class", @"using System;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class VisualBasicClass
{
private object SomeDate = """";
private DateTime? SomeDateDateNothing;
private object isNotNothing;
private object isSomething;
public VisualBasicClass()
{
SomeDateDateNothing = string.IsNullOrEmpty(Conversions.ToString(SomeDate)) ? (object)null : DateTime.Parse(SomeDate);
isNotNothing = SomeDateDateNothing is not null;
isSomething = new DateTime() is var arg1 && SomeDateDateNothing.HasValue ? SomeDateDateNothing.Value == arg1 : (bool?)null;
}
}
1 target compilation errors:
CS1503: Argument 1: cannot convert from 'object' to 'System.ReadOnlySpan<char>'");
}
[Fact]
public async Task CTypeNothingAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System
Public Class VisualBasicClass
Dim SomeDate As String = ""2022-01-01""
Dim SomeDateDateParsed As Date? = If(String.IsNullOrEmpty(SomeDate), CType(Nothing, Date?), DateTime.Parse(SomeDate))
End Class", @"using System;
public partial class VisualBasicClass
{
private string SomeDate = ""2022-01-01"";
private DateTime? SomeDateDateParsed;
public VisualBasicClass()
{
SomeDateDateParsed = string.IsNullOrEmpty(SomeDate) ? default(DateTime?) : DateTime.Parse(SomeDate);
}
}");
}
[Fact]
public async Task NullConditionalIndexer_Issue993Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass
Private Function TestMethod(testArray As Object()) As Boolean
Return Not String.IsNullOrWhiteSpace(testArray?(0)?.ToString())
End Function
End Class", @"
public partial class VisualBasicClass
{
private bool TestMethod(object[] testArray)
{
return !string.IsNullOrWhiteSpace(testArray?[0]?.ToString());
}
}");
}
[Fact]
public async Task GenericComparisonAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class GenericComparison
Public Sub m(Of T)(p As T)
If p Is Nothing Then Return
End Sub
End Class", @"
public partial class GenericComparison
{
public void m<T>(T p)
{
if (p is null)
return;
}
}");
}
[Fact]
public async Task AccessSharedThroughInstanceAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class A
Public Shared x As Integer = 2
Public Sub Test()
Dim tmp = Me
Dim y = Me.x
Dim z = tmp.x
End Sub
End Class", @"
public partial class A
{
public static int x = 2;
public void Test()
{
var tmp = this;
int y = x;
int z = x;
}
}");
}
[Fact]
public async Task EmptyArrayExpressionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class Issue495AndIssue713
Public Function Empty() As Integer()
Dim emptySingle As IEnumerable(Of Integer) = {}
Dim initializedSingle As IEnumerable(Of Integer) = {1}
Dim emptyNested As Integer()() = {}
Dim initializedNested(1)() As Integer
Dim empty2d As Integer(,) = {{}}
Dim initialized2d As Integer(,) = {{1}}
Return {}
End Function
End Class", @"using System;
using System.Collections.Generic;
public partial class Issue495AndIssue713
{
public int[] Empty()
{
IEnumerable<int> emptySingle = Array.Empty<int>();
IEnumerable<int> initializedSingle = new[] { 1 };
int[][] emptyNested = Array.Empty<int[]>();
var initializedNested = new int[2][];
int[,] empty2d = new int[,] { { } };
int[,] initialized2d = new[,] { { 1 } };
return Array.Empty<int>();
}
}");
}
[Fact]
public async Task InitializedArrayExpressionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class Issue713
Public Function Empty() As Integer()
Dim initializedSingle As IEnumerable(Of Integer) = {1}
Dim initialized2d As Integer(,) = {{1}}
Return {}
End Function
End Class", @"using System;
using System.Collections.Generic;
public partial class Issue713
{
public int[] Empty()
{
IEnumerable<int> initializedSingle = new[] { 1 };
int[,] initialized2d = new[,] { { 1 } };
return Array.Empty<int>();
}
}");
}
[Fact]
public async Task EmptyArrayParameterAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass
Public Sub s()
If Validate({}) Then
End If
End Sub
Private Function Validate(w As IEnumerable(Of Int16)) As Boolean
Return True
End Function
End Class", @"using System;
using System.Collections.Generic;
public partial class VisualBasicClass
{
public void s()
{
if (Validate(Array.Empty<short>()))
{
}
}
private bool Validate(IEnumerable<short> w)
{
return true;
}
}");
}
[Fact]
public async Task Empty2DArrayExpressionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class Empty2DArray
Dim data(,) As Double = {}
End Class", @"
public partial class Empty2DArray
{
private double[,] data = new double[,] { };
}");
}
[Fact]
public async Task ReducedTypeParametersInferrableAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Linq
Public Class Class1
Sub Foo()
Dim y = """".Split("",""c).Select(Of String)(Function(x) x)
End Sub
End Class", @"using System.Linq;
public partial class Class1
{
public void Foo()
{
var y = """".Split(',').Select(x => x);
}
}");
}
[Fact]
public async Task ReducedTypeParametersNonInferrableAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Linq
Public Class Class1
Sub Foo()
Dim y = """".Split("",""c).Select(Of Object)(Function(x) x)
End Sub
End Class", @"using System.Linq;
public partial class Class1
{
public void Foo()
{
var y = """".Split(',').Select<string, object>(x => x);
}
}");
}
[Fact]
public async Task EnumNullableConversionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Main()
Dim x = DayOfWeek.Monday
Foo(x)
End Sub
Sub Foo(x As DayOfWeek?)
End Sub
End Class", @"using System;
public partial class Class1
{
public void Main()
{
var x = DayOfWeek.Monday;
Foo(x);
}
public void Foo(DayOfWeek? x)
{
}
}");
}
[Fact]
public async Task UninitializedVariableAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub New()
Dim needsInitialization As Integer
Dim notUsed As Integer
Dim y = needsInitialization
End Sub
Sub Foo()
Dim needsInitialization As Integer
Dim notUsed As Integer
Dim y = needsInitialization
End Sub
Sub Bar()
Dim i As Integer, temp As String = String.Empty
i += 1
End Sub
Sub Bar2()
Dim i As Integer, temp As String = String.Empty
i = i + 1
End Sub
Sub Bar3()
Dim i As Integer, temp As String = String.Empty
Dim k As Integer = i + 1
End Sub
Sub Bar4()
Dim i As Integer, temp As String = String.Empty
Dim k As Integer = i + 1
i = 1
End Sub
Public ReadOnly Property State As Integer
Get
Dim needsInitialization As Integer
Dim notUsed As Integer
Dim y = needsInitialization
Return y
End Get
End Property
End Class", @"
public partial class Class1
{
public Class1()
{
var needsInitialization = default(int);
int notUsed;
int y = needsInitialization;
}
public void Foo()
{
var needsInitialization = default(int);
int notUsed;
int y = needsInitialization;
}
public void Bar()
{
var i = default(int);
string temp = string.Empty;
i += 1;
}
public void Bar2()
{
var i = default(int);
string temp = string.Empty;
i = i + 1;
}
public void Bar3()
{
var i = default(int);
string temp = string.Empty;
int k = i + 1;
}
public void Bar4()
{
var i = default(int);
string temp = string.Empty;
int k = i + 1;
i = 1;
}
public int State
{
get
{
var needsInitialization = default(int);
int notUsed;
int y = needsInitialization;
return y;
}
}
}");
}
[Fact]
public async Task FullyTypeInferredEnumerableCreationAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim strings = { ""1"", ""2"" }
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string[] strings = new[] { ""1"", ""2"" };
}
}");
}
[Fact]
public async Task GetTypeExpressionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim typ = GetType(String)
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
var typ = typeof(string);
}
}");
}
[Fact]
public async Task NullableIntegerAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Public Function Bar(value As String) As Integer?
Dim result As Integer
If Integer.TryParse(value, result) Then
Return result
Else
Return Nothing
End If
End Function
End Class", @"
internal partial class TestClass
{
public int? Bar(string value)
{
int result;
if (int.TryParse(value, out result))
{
return result;
}
else
{
return default;
}
}
}");
}
[Fact]
public async Task NothingInvokesDefaultForValueTypesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Public Sub Bar()
Dim number As Integer