-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathStringExpressionTests.cs
More file actions
567 lines (515 loc) · 16.1 KB
/
StringExpressionTests.cs
File metadata and controls
567 lines (515 loc) · 16.1 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
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.Tests.TestRunners;
using Xunit;
namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests;
public class StringExpressionTests : ConverterTestBase
{
[Fact]
public async Task MultilineStringAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim x = ""Hello\ All strings in VB are verbatim """" < that's just a single escaped quote
World!""
Dim y = $""Hello\ All strings in VB are verbatim """" < that's just a single escaped quote
World!""
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string x = @""Hello\ All strings in VB are verbatim """" < that's just a single escaped quote
World!"";
string y = $@""Hello\ All strings in VB are verbatim """" < that's just a single escaped quote
World!"";
}
}");
}
[Fact]
public async Task QuoteCharacterAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class C
Public Sub s
Dim x As String = Chr(34)
x = Chr(92)
End Sub
End Class", @"
public partial class C
{
public void s()
{
string x = ""\"""";
x = @""\"";
}
}");
}
[Fact]
public async Task QuotesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Shared Function GetTextFeedInput(pStream As String, pTitle As String, pText As String) As String
Return ""{"" & AccessKey() & "",""""streamName"""": """""" & pStream & """""",""""point"""": ["" & GetTitleTextPair(pTitle, pText) & ""]}""
End Function
Shared Function AccessKey() As String
Return """"""accessKey"""": """"8iaiHNZpNbBkYHHGbMNiHhAp4uPPyQke""""""
End Function
Shared Function GetNameValuePair(pName As String, pValue As Integer) As String
Return (""{""""name"""": """""" & pName & """""", """"value"""": """""" & pValue & """"""}"")
End Function
Shared Function GetNameValuePair(pName As String, pValue As String) As String
Return (""{""""name"""": """""" & pName & """""", """"value"""": """""" & pValue & """"""}"")
End Function
Shared Function GetTitleTextPair(pName As String, pValue As String) As String
Return (""{""""title"""": """""" & pName & """""", """"msg"""": """""" & pValue & """"""}"")
End Function
Shared Function GetDeltaPoint(pDelta As Integer) As String
Return (""{""""delta"""": """""" & pDelta & """"""}"")
End Function
End Class", @"
internal partial class TestClass
{
public static string GetTextFeedInput(string pStream, string pTitle, string pText)
{
return ""{"" + AccessKey() + "",\""streamName\"": \"""" + pStream + ""\"",\""point\"": ["" + GetTitleTextPair(pTitle, pText) + ""]}"";
}
public static string AccessKey()
{
return ""\""accessKey\"": \""8iaiHNZpNbBkYHHGbMNiHhAp4uPPyQke\"""";
}
public static string GetNameValuePair(string pName, int pValue)
{
return ""{\""name\"": \"""" + pName + ""\"", \""value\"": \"""" + pValue + ""\""}"";
}
public static string GetNameValuePair(string pName, string pValue)
{
return ""{\""name\"": \"""" + pName + ""\"", \""value\"": \"""" + pValue + ""\""}"";
}
public static string GetTitleTextPair(string pName, string pValue)
{
return ""{\""title\"": \"""" + pName + ""\"", \""msg\"": \"""" + pValue + ""\""}"";
}
public static string GetDeltaPoint(int pDelta)
{
return ""{\""delta\"": \"""" + pDelta + ""\""}"";
}
}");
}
[Fact]
public async Task StringCompareAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo()
Dim s1 As String = Nothing
Dim s2 As String = """"
If s1 <> s2 Then
Throw New Exception()
End If
If s1 = ""something"" Then
Throw New Exception()
End If
If ""something"" = s1 Then
Throw New Exception()
End If
If s1 = Nothing Then
'
End If
If s1 = """" Then
'
End If
End Sub
End Class", @"using System;
public partial class Class1
{
public void Foo()
{
string s1 = null;
string s2 = """";
if ((s1 ?? """") != (s2 ?? """"))
{
throw new Exception();
}
if (s1 == ""something"")
{
throw new Exception();
}
if (""something"" == s1)
{
throw new Exception();
}
if (string.IsNullOrEmpty(s1))
{
//
}
if (string.IsNullOrEmpty(s1))
{
//
}
}
}");
}
[Fact]
public async Task StringCompareTextAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Option Compare Text
Public Class Class1
Sub Foo()
Dim s1 As String = Nothing
Dim s2 As String = """"
If s1 <> s2 Then
Throw New Exception()
End If
If s1 = ""something"" Then
Throw New Exception()
End If
If ""something"" = s1 Then
Throw New Exception()
End If
If s1 = Nothing Then
'
End If
If s1 = """" Then
'
End If
End Sub
End Class", @"using System;
using System.Globalization;
public partial class Class1
{
public void Foo()
{
string s1 = null;
string s2 = """";
if (CultureInfo.CurrentCulture.CompareInfo.Compare(s1 ?? """", s2 ?? """", CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) != 0)
{
throw new Exception();
}
if (CultureInfo.CurrentCulture.CompareInfo.Compare(s1, ""something"", CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) == 0)
{
throw new Exception();
}
if (CultureInfo.CurrentCulture.CompareInfo.Compare(""something"", s1, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) == 0)
{
throw new Exception();
}
if (CultureInfo.CurrentCulture.CompareInfo.Compare(s1 ?? """", """", CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) == 0)
{
//
}
if (CultureInfo.CurrentCulture.CompareInfo.Compare(s1 ?? """", """", CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) == 0)
{
//
}
}
}");
}
[Fact]
public async Task StringCompareDefaultInstrAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports Microsoft.VisualBasic
Class Issue655
Dim s1 = InStr(1, ""obj"", ""object '"")
Dim s2 = InStrRev(1, ""obj"", ""object '"")
Dim s3 = Replace(1, ""obj"", ""object '"")
Dim s4 = Split(1, ""obj"", ""object '"")
Dim s5 = Filter(New String() { 1, 2}, ""obj"")
Dim s6 = StrComp(1, ""obj"")
Dim s7 = OtherFunction()
Function OtherFunction(Optional c As CompareMethod = CompareMethod.Binary) As Boolean
Return c = CompareMethod.Binary
End Function
End Class",
@"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
internal partial class Issue655
{
private object s1 = Strings.InStr(1, ""obj"", ""object '"");
private object s2 = Strings.InStrRev(1.ToString(), ""obj"", Conversions.ToInteger(""object '""));
private object s3 = Strings.Replace(1.ToString(), ""obj"", ""object '"");
private object s4 = Strings.Split(1.ToString(), ""obj"", Conversions.ToInteger(""object '""));
private object s5 = Strings.Filter(new string[] { 1.ToString(), 2.ToString() }, ""obj"");
private object s6 = Strings.StrComp(1.ToString(), ""obj"");
private object s7;
public Issue655()
{
s7 = OtherFunction();
}
public bool OtherFunction(CompareMethod c = CompareMethod.Binary)
{
return c == CompareMethod.Binary;
}
}");
}
[Fact]
public async Task StringCompareTextInstrAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Option Compare Text ' Comment omitted since line has no conversion
Imports Microsoft.VisualBasic
Class Issue655
Dim s1 = InStr(1, ""obj"", ""object '"")
Dim s2 = InStrRev(1, ""obj"", ""object '"")
Dim s3 = Replace(1, ""obj"", ""object '"")
Dim s4 = Split(1, ""obj"", ""object '"")
Dim s5 = Filter(New String() { 1, 2}, ""obj"")
Dim s6 = StrComp(1, ""obj"")
Dim s7 = OtherFunction()
Function OtherFunction(Optional c As CompareMethod = CompareMethod.Binary) As Boolean
Return c = CompareMethod.Binary
End Function
End Class",
@"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
internal partial class Issue655
{
private object s1 = Strings.InStr(1, ""obj"", ""object '"", Compare: CompareMethod.Text);
private object s2 = Strings.InStrRev(1.ToString(), ""obj"", Conversions.ToInteger(""object '""), Compare: CompareMethod.Text);
private object s3 = Strings.Replace(1.ToString(), ""obj"", ""object '"", Compare: CompareMethod.Text);
private object s4 = Strings.Split(1.ToString(), ""obj"", Conversions.ToInteger(""object '""), Compare: CompareMethod.Text);
private object s5 = Strings.Filter(new string[] { 1.ToString(), 2.ToString() }, ""obj"");
private object s6 = Strings.StrComp(1.ToString(), ""obj"", Compare: CompareMethod.Text);
private object s7;
public Issue655()
{
s7 = OtherFunction();
}
public bool OtherFunction(CompareMethod c = CompareMethod.Binary)
{
return c == CompareMethod.Binary;
}
}");
}
[Fact]
public async Task StringConcatPrecedenceAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo()
Dim x = ""x "" & 5 - 4 & "" y""
End Sub
End Class", @"
public partial class Class1
{
public void Foo()
{
string x = ""x "" + (5 - 4) + "" y"";
}
}");
}
[Fact]
public async Task StringConcatenationAssignmentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim str = ""Hello, ""
str &= ""World""
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
string str = ""Hello, "";
str += ""World"";
}
}");
}
[Fact]
public async Task StringInterpolationWithConditionalOperatorAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Function GetString(yourBoolean as Boolean) As String
Return $""You {if (yourBoolean, ""do"", ""do not"")} have a true value""
End Function",
@"public string GetString(bool yourBoolean)
{
return $""You {(yourBoolean ? ""do"" : ""do not"")} have a true value"";
}");
}
[Fact]
public async Task StringInterpolationWithDoubleQuotesAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Imports System
Namespace Global.InnerNamespace
Public Class Test
Public Function StringInter(t As String, dt As DateTime) As String
Dim a = $""pre{t} t""
Dim b = $""pre{t} """" t""
Dim c = $""pre{t} """"\ t""
Dim d = $""pre{t & """"""""} """" t""
Dim e = $""pre{t & """"""""} """"\ t""
Dim f = $""pre{{escapedBraces}}{dt,4:hh}""
Return a & b & c & d & e & f
End Function
End Class
End Namespace",
@"using System;
namespace InnerNamespace
{
public partial class Test
{
public string StringInter(string t, DateTime dt)
{
string a = $""pre{t} t"";
string b = $""pre{t} \"" t"";
string c = $@""pre{t} """"\ t"";
string d = $""pre{t + ""\""""} \"" t"";
string e = $@""pre{t + ""\""""} """"\ t"";
string f = $""pre{{escapedBraces}}{dt,4:hh}"";
return a + b + c + d + e + f;
}
}
}");
}
[Fact]
public async Task StringInterpolationWithDateFormatAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Imports System
Namespace Global.InnerNamespace
Public Class Test
public function InterStringDateFormat(dt As DateTime) As String
Dim a As String = $""Soak: {dt: d\.h\:mm\:ss\.f}""
return a
End function
End Class
End Namespace",
@"using System;
namespace InnerNamespace
{
public partial class Test
{
public string InterStringDateFormat(DateTime dt)
{
string a = $""Soak: {dt: d\\.h\\:mm\\:ss\\.f}"";
return a;
}
}
}");
}
[Fact]
public async Task NoConversionRequiredWithinConcatenationAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Class Issue508
Sub Foo()
Dim x = ""x"" & 4 & ""y""
End Sub
End Class",
@"
public partial class Issue508
{
public void Foo()
{
string x = ""x"" + 4 + ""y"";
}
}");
}
[Fact]
public async Task EmptyStringCoalesceSkippedForLiteralComparisonAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Class VisualBasicClass
Sub Foo()
Dim x = """"
Dim y = x = ""something""
End Sub
End Class",
@"
public partial class VisualBasicClass
{
public void Foo()
{
string x = """";
bool y = x == ""something"";
}
}");
}
[Fact]
public async Task Issue396ComparisonOperatorForStringsAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Class Issue396ComparisonOperatorForStringsAsync
Private str = 1.ToString()
Private b = str > """"
End Class",
@"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class Issue396ComparisonOperatorForStringsAsync
{
private object str = 1.ToString();
private object b;
public Issue396ComparisonOperatorForStringsAsync()
{
b = Operators.ConditionalCompareObjectGreater(str, """", false);
}
}");
}
[Fact]
public async Task Issue590EnumConvertsToNumericStringAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Class EnumTests
Private Enum RankEnum As SByte
First = 1
Second = 2
End Enum
Public Sub TestEnumConcat()
Console.Write(RankEnum.First & RankEnum.Second)
End Sub
End Class",
@"using System;
using System.IO;
public partial class EnumTests
{
private enum RankEnum : sbyte
{
First = 1,
Second = 2
}
public void TestEnumConcat()
{
Console.Write(RankEnum.First + RankEnum.Second);
}
}
1 target compilation errors:
CS0019: Operator '+' cannot be applied to operands of type 'EnumTests.RankEnum' and 'EnumTests.RankEnum'");
}
[Fact]
public async Task Issue806DateTimeConvertsToStringWithinConcatenationAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Class Issue806
Sub Foo()
Dim x = #2022-01-01# & "" 15:00""
End Sub
End Class",
@"using System;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class Issue806
{
public void Foo()
{
string x = Conversions.ToString(DateTime.Parse(""2022-01-01"")) + "" 15:00"";
}
}");
}
[Fact]
public async Task CharEqualityEmptyStringAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim testChar As Char = Nothing
Dim testResult = testChar = """"
Dim testResult2 = """" = testChar
End Sub
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
char testChar = default;
bool testResult = testChar == char.MinValue;
bool testResult2 = char.MinValue == testChar;
}
}");
}
}