|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using ICSharpCode.CodeConverter.Tests.TestRunners; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests |
| 10 | +{ |
| 11 | + public class ByRefTests : ConverterTestBase |
| 12 | + { |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public async Task OptionalRefDateConstsWithOmittedArgListAsync() |
| 16 | + { |
| 17 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue213 |
| 18 | + Const x As Date = #1990-1-1# |
| 19 | +
|
| 20 | + Private Sub Y(Optional ByRef opt As Date = x) |
| 21 | + End Sub |
| 22 | +
|
| 23 | + Private Sub CallsY() |
| 24 | + Y |
| 25 | + End Sub |
| 26 | +End Class", @"using System; |
| 27 | +using System.Runtime.CompilerServices; |
| 28 | +using System.Runtime.InteropServices; |
| 29 | +
|
| 30 | +public partial class Issue213 |
| 31 | +{ |
| 32 | + private static DateTime x = DateTime.Parse(""1990-01-01""); |
| 33 | +
|
| 34 | + private void Y([Optional, DateTimeConstant(627667488000000000/* Global.Issue213.x */)] ref DateTime opt) |
| 35 | + { |
| 36 | + } |
| 37 | +
|
| 38 | + private void CallsY() |
| 39 | + { |
| 40 | + DateTime argopt = DateTime.Parse(""1990-01-01""); |
| 41 | + Y(opt: ref argopt); |
| 42 | + } |
| 43 | +}"); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task NullInlineRefArgumentAsync() |
| 48 | + { |
| 49 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass |
| 50 | + Public Sub UseStuff() |
| 51 | + Stuff(Nothing) |
| 52 | + End Sub |
| 53 | +
|
| 54 | + Public Sub Stuff(ByRef strs As String()) |
| 55 | + End Sub |
| 56 | +End Class", @" |
| 57 | +public partial class VisualBasicClass |
| 58 | +{ |
| 59 | + public void UseStuff() |
| 60 | + { |
| 61 | + string[] argstrs = null; |
| 62 | + Stuff(ref argstrs); |
| 63 | + } |
| 64 | +
|
| 65 | + public void Stuff(ref string[] strs) |
| 66 | + { |
| 67 | + } |
| 68 | +}"); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task RefArgumentRValueAsync() |
| 73 | + { |
| 74 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1 |
| 75 | + Private Property C1 As Class1 |
| 76 | + Private _c2 As Class1 |
| 77 | + Private _o1 As Object |
| 78 | +
|
| 79 | + Sub Foo() |
| 80 | + Bar(New Class1) |
| 81 | + Bar(C1) |
| 82 | + Bar(Me.C1) |
| 83 | + Bar(_c2) |
| 84 | + Bar(Me._c2) |
| 85 | + Bar(_o1) |
| 86 | + Bar(Me._o1) |
| 87 | + End Sub |
| 88 | +
|
| 89 | + Sub Bar(ByRef class1) |
| 90 | + End Sub |
| 91 | +End Class", @" |
| 92 | +public partial class Class1 |
| 93 | +{ |
| 94 | + private Class1 C1 { get; set; } |
| 95 | +
|
| 96 | + private Class1 _c2; |
| 97 | + private object _o1; |
| 98 | +
|
| 99 | + public void Foo() |
| 100 | + { |
| 101 | + object argclass1 = new Class1(); |
| 102 | + Bar(ref argclass1); |
| 103 | + object argclass11 = C1; |
| 104 | + Bar(ref argclass11); |
| 105 | + object argclass12 = C1; |
| 106 | + Bar(ref argclass12); |
| 107 | + object argclass13 = _c2; |
| 108 | + Bar(ref argclass13); |
| 109 | + object argclass14 = _c2; |
| 110 | + Bar(ref argclass14); |
| 111 | + Bar(ref _o1); |
| 112 | + Bar(ref _o1); |
| 113 | + } |
| 114 | +
|
| 115 | + public void Bar(ref object class1) |
| 116 | + { |
| 117 | + } |
| 118 | +}"); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task RefArgumentRValue2Async() |
| 123 | + { |
| 124 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1 |
| 125 | + Sub Foo() |
| 126 | + Dim x = True |
| 127 | + Bar(x = True) |
| 128 | + End Sub |
| 129 | +
|
| 130 | + Function Foo2() |
| 131 | + Return Bar(True = False) |
| 132 | + End Function |
| 133 | +
|
| 134 | + Sub Foo3() |
| 135 | + If Bar(True = False) Then Bar(True = False) |
| 136 | + End Sub |
| 137 | +
|
| 138 | + Sub Foo4() |
| 139 | + If Bar(True = False) Then |
| 140 | + Bar(True = False) |
| 141 | + ElseIf Bar(True = False) Then |
| 142 | + Bar(True = False) |
| 143 | + Else |
| 144 | + Bar(True = False) |
| 145 | + End If |
| 146 | + End Sub |
| 147 | +
|
| 148 | + Sub Foo5() |
| 149 | + Bar(Nothing) |
| 150 | + End Sub |
| 151 | +
|
| 152 | + Function Bar(ByRef b As Boolean) As Boolean |
| 153 | + Return True |
| 154 | + End Function |
| 155 | +
|
| 156 | + Function Bar2(ByRef c1 As Class1) As Integer |
| 157 | + If c1 IsNot Nothing AndAlso Len(Bar3(Me)) <> 0 Then |
| 158 | + Return 1 |
| 159 | + End If |
| 160 | + Return 0 |
| 161 | + End Function |
| 162 | +
|
| 163 | + Function Bar3(ByRef c1 As Class1) As String |
| 164 | + Return """" |
| 165 | + End Function |
| 166 | +
|
| 167 | +End Class", @"using Microsoft.VisualBasic; |
| 168 | +
|
| 169 | +public partial class Class1 |
| 170 | +{ |
| 171 | + public void Foo() |
| 172 | + { |
| 173 | + bool x = true; |
| 174 | + bool argb = x == true; |
| 175 | + Bar(ref argb); |
| 176 | + } |
| 177 | +
|
| 178 | + public object Foo2() |
| 179 | + { |
| 180 | + bool argb = true == false; |
| 181 | + return Bar(ref argb); |
| 182 | + } |
| 183 | +
|
| 184 | + public void Foo3() |
| 185 | + { |
| 186 | + bool argb1 = true == false; |
| 187 | + if (Bar(ref argb1)) |
| 188 | + { |
| 189 | + bool argb = true == false; |
| 190 | + Bar(ref argb); |
| 191 | + } |
| 192 | + } |
| 193 | +
|
| 194 | + public void Foo4() |
| 195 | + { |
| 196 | + bool argb3 = true == false; |
| 197 | + bool argb4 = true == false; |
| 198 | + if (Bar(ref argb3)) |
| 199 | + { |
| 200 | + bool argb = true == false; |
| 201 | + Bar(ref argb); |
| 202 | + } |
| 203 | + else if (Bar(ref argb4)) |
| 204 | + { |
| 205 | + bool argb2 = true == false; |
| 206 | + Bar(ref argb2); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + bool argb1 = true == false; |
| 211 | + Bar(ref argb1); |
| 212 | + } |
| 213 | + } |
| 214 | +
|
| 215 | + public void Foo5() |
| 216 | + { |
| 217 | + bool argb = default; |
| 218 | + Bar(ref argb); |
| 219 | + } |
| 220 | +
|
| 221 | + public bool Bar(ref bool b) |
| 222 | + { |
| 223 | + return true; |
| 224 | + } |
| 225 | +
|
| 226 | + public int Bar2(ref Class1 c1) |
| 227 | + { |
| 228 | + var argc1 = this; |
| 229 | + if (c1 is object && Strings.Len(Bar3(ref argc1)) != 0) |
| 230 | + { |
| 231 | + return 1; |
| 232 | + } |
| 233 | +
|
| 234 | + return 0; |
| 235 | + } |
| 236 | +
|
| 237 | + public string Bar3(ref Class1 c1) |
| 238 | + { |
| 239 | + return """"; |
| 240 | + } |
| 241 | +}"); |
| 242 | + } |
| 243 | + |
| 244 | + [Fact] |
| 245 | + public async Task RefArgumentUsingAsync() |
| 246 | + { |
| 247 | + await TestConversionVisualBasicToCSharpAsync(@"Imports System.Data.SqlClient |
| 248 | +
|
| 249 | +Public Class Class1 |
| 250 | + Sub Foo() |
| 251 | + Using x = New SqlConnection |
| 252 | + Bar(x) |
| 253 | + End Using |
| 254 | + End Sub |
| 255 | + Sub Bar(ByRef x As SqlConnection) |
| 256 | +
|
| 257 | + End Sub |
| 258 | +End Class", @"using System.Data.SqlClient; |
| 259 | +
|
| 260 | +public partial class Class1 |
| 261 | +{ |
| 262 | + public void Foo() |
| 263 | + { |
| 264 | + using (var x = new SqlConnection()) |
| 265 | + { |
| 266 | + var argx = x; |
| 267 | + Bar(ref argx); |
| 268 | + } |
| 269 | + } |
| 270 | +
|
| 271 | + public void Bar(ref SqlConnection x) |
| 272 | + { |
| 273 | + } |
| 274 | +}"); |
| 275 | + } |
| 276 | + |
| 277 | + [Fact] |
| 278 | + public async Task RefOptionalArgumentAsync() |
| 279 | + { |
| 280 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class OptionalRefIssue91 |
| 281 | + Public Shared Function TestSub(Optional ByRef IsDefault As Boolean = False) As Boolean |
| 282 | + End Function |
| 283 | +
|
| 284 | + Public Shared Function CallingFunc() As Boolean |
| 285 | + Return TestSub() AndAlso TestSub(True) |
| 286 | + End Function |
| 287 | +End Class", @"using System.Runtime.InteropServices; |
| 288 | +
|
| 289 | +public partial class OptionalRefIssue91 |
| 290 | +{ |
| 291 | + public static bool TestSub([Optional, DefaultParameterValue(false)] ref bool IsDefault) |
| 292 | + { |
| 293 | + return default; |
| 294 | + } |
| 295 | +
|
| 296 | + public static bool CallingFunc() |
| 297 | + { |
| 298 | + bool argIsDefault = false; |
| 299 | + bool argIsDefault1 = true; |
| 300 | + return TestSub(IsDefault: ref argIsDefault) && TestSub(ref argIsDefault1); |
| 301 | + } |
| 302 | +}"); |
| 303 | + } |
| 304 | + |
| 305 | + [Fact] |
| 306 | + public async Task RefArgumentPropertyInitializerAsync() |
| 307 | + { |
| 308 | + await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1 |
| 309 | + Private _p1 As Class1 = Foo(New Class1) |
| 310 | + Public Shared Function Foo(ByRef c1 As Class1) As Class1 |
| 311 | + Return c1 |
| 312 | + End Function |
| 313 | +End Class", @" |
| 314 | +public partial class Class1 |
| 315 | +{ |
| 316 | + static Class1 Foo__p1() |
| 317 | + { |
| 318 | + var argc1 = new Class1(); |
| 319 | + return Foo(ref argc1); |
| 320 | + } |
| 321 | +
|
| 322 | + private Class1 _p1 = Foo__p1(); |
| 323 | +
|
| 324 | + public static Class1 Foo(ref Class1 c1) |
| 325 | + { |
| 326 | + return c1; |
| 327 | + } |
| 328 | +}"); |
| 329 | + } |
| 330 | + } |
| 331 | +} |
0 commit comments