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
|
'
' LateBindingTests4.vb
'
' Author:
' Boris Kirzner (borisk@mainsoft.com)
'
'
' Copyright (C) 2002-2006 Mainsoft Corporation.
' Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
'
' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the
' "Software"), to deal in the Software without restriction, including
' without limitation the rights to use, copy, modify, merge, publish,
' distribute, sublicense, and/or sell copies of the Software, and to
' permit persons to whom the Software is furnished to do so, subject to
' the following conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
' LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Imports System
Imports System.Reflection
Imports NUnit.Framework
<TestFixture()> _
Public Class LateBindingTests4
Private Class C1
Public Function F(Optional ByVal i As Integer = -2, Optional ByVal l As Long = -1) As String
If i = -2 Then
If l = -1 Then
Return "Integer(optional value),Long(optional value)"
Else
Return "Integer(optional value),Long"
End If
Else
If l = -1 Then
Return "Integer,Long(optional value)"
Else
Return "Integer,Long"
End If
End If
End Function
Public Function G(ByVal i As Integer, Optional ByVal l As Long = -1) As String
If l = -1 Then
Return "Integer,Long(optional value)"
Else
Return "Integer,Long"
End If
End Function
End Class
<Test()> _
Public Sub LateBind_OptionalValue_1()
Dim o As Object = New C1
Assert.AreEqual("Integer(optional value),Long(optional value)", o.F())
Assert.AreEqual("Integer,Long(optional value)", o.F(2))
Assert.AreEqual("Integer,Long", o.F(2, 3))
End Sub
<Test()> _
Public Sub LateBind_OptionalValue_2()
Dim o As Object = New C1
Assert.AreEqual("Integer,Long(optional value)", o.G(2))
Assert.AreEqual("Integer,Long", o.G(2, 3))
End Sub
<Test(), ExpectedException(GetType(System.MissingMemberException))> _
Public Sub LateBind_OptionalValue_3()
Dim o As Object = New C1
o.G()
End Sub
Private Class C2
Public Function F(ByRef j As Integer)
j = 8
Return "ByRef Integer"
End Function
Public Function F(ByVal i As Integer, ByRef j As Integer)
j = i
Return "Integer,ByRef Integer"
End Function
Public Function F(ByVal i As Integer, ByRef j As Integer, ByVal ParamArray arg() As Integer)
j = arg(0) + arg.Length
Return "Integer,ByRef Integer,ParmaArray Integer()"
End Function
End Class
<Test()> _
Public Sub LateBind_ByRef_1()
Dim o As Object = New C2
Dim j As Integer = 15
Dim i As Integer = 23
Assert.AreEqual("ByRef Integer", o.F(j))
Assert.AreEqual(8, j)
Assert.AreEqual("Integer,ByRef Integer", o.F(i, j))
Assert.AreEqual(i, j)
Assert.AreEqual("Integer,ByRef Integer,ParmaArray Integer()", o.F(i, j, 7, 8, 9, 10))
Assert.AreEqual(7 + 4, j)
End Sub
Private Class C3
Public Function F(ByVal i As Integer, ByRef j() As Integer)
j(2) = i
Return "Integer,ByRef Integer()"
End Function
Public Function F(ByVal i() As Integer, ByRef j() As Integer)
For k As Integer = 0 To j.Length - 1
j(k) = i(k)
Next
Return "Integer(),ByRef Integer()"
End Function
Public Function F(ByVal i As Integer, ByRef j() As Integer, ByVal ParamArray arg() As Integer)
For k As Integer = 0 To j.Length - 1
j(k) = arg(k)
Next
Return "Integer,ByRef Integer(),ParmaArray Integer()"
End Function
End Class
<Test()> _
Public Sub LateBind_ByRef_2()
Dim o As Object = New C3
Dim j() As Integer = {-1, -2, -3, -4, -5}
Dim i() As Integer = {10, 20, 30, 40, 50, 60}
Dim arg() As Integer = {1, 2, 3, 4, 5, 6, 7}
Assert.AreEqual("Integer,ByRef Integer()", o.F(9, j))
Assert.AreEqual(9, j(2))
Assert.AreEqual("Integer(),ByRef Integer()", o.F(i, j))
For k As Integer = 0 To j.Length - 1
Assert.AreEqual(i(k), j(k))
Next
Assert.AreEqual("Integer,ByRef Integer(),ParmaArray Integer()", o.F(5, j, arg(0), arg(1), arg(2), arg(3), arg(4), arg(5), arg(6)))
For k As Integer = 0 To j.Length - 1
Assert.AreEqual(arg(k), j(k))
Next
End Sub
Private Class C4
Public Sub F(ByRef A() As Long)
Dim J As Integer
For J = 0 To 3
A(J) = A(J) + 1
Next J
End Sub
Public Sub G(ByRef A() As Long)
Dim J As Integer
Dim K() As Long = {100, 200, 300, 400}
A = K
For J = 0 To 3
A(J) = A(J) + 1
Next J
End Sub
End Class
<Test()> _
Public Sub LateBind_ByRef_3()
Dim o As Object = New C4
Dim N() As Long = {10, 20, 30, 40}
Dim N1() As Long = {11, 21, 31, 41}
Dim N2() As Long = {101, 201, 301, 401}
Dim i As Integer
o.F(N)
For i = 0 To N.Length - 1
Assert.AreEqual(N1(i), N(i))
Next i
o.G(N)
For i = 0 To N.Length - 1
Assert.AreEqual(N2(i), N(i))
Next i
End Sub
Class C5
Public Function F(ByVal i As Integer, Optional ByVal a1 As Char = "c", Optional ByVal j As Integer = 30) As Integer
If a1 = "c" And i = 2 And j = 40 Then
Return 10
End If
Return 11
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_1()
Dim o As Object = New C5
Dim a As Integer = o.F(j:=40, i:=2)
Assert.AreEqual(10, a)
End Sub
Class C6
Public Function F(ByRef i As Integer, Optional ByRef a1 As Char = "c", Optional ByRef j As Integer = 30) As Integer
If a1 = "a" And i = 2 And j = 30 Then
Return 10
End If
Return 11
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_2()
Dim o As Object = New C6
Dim a As Integer = o.F(a1:="a", i:=2)
Assert.AreEqual(10, a)
End Sub
Class C7
Public Function F(ByVal i As Integer, Optional ByVal a1 As Char = "d", Optional ByVal j As Integer = 30) As Integer
If a1 = "c" And i = 2 And j = 30 Then
Return 10
End If
Return 11
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_3()
'Dim i As Integer
Dim o As Object = New C7
Dim a As Integer = o.F(a1:="caa", i:=2.321)
Assert.AreEqual(10, a)
'Assert.AreEqual(2, i)
End Sub
Class C8
Public Function F(ByRef i As Integer, ByRef j As Integer) As Integer
i = 9
j = 10
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_4()
Dim o As Object = New C8
Dim a As Integer = 1
Dim err As String = ""
o.F(a, a)
Assert.AreEqual(10, a)
Assert.AreEqual(10, a)
o.F(j:=a, i:=a)
Assert.AreEqual(9, a)
End Sub
Class C9
Public Function F(ByVal i As Integer, ByVal ParamArray arr() As Integer) As String
Return "Integer,ParamArray Integer()"
End Function
Public Function F(ByVal i As Integer, ByVal arr As Integer) As String
Return "Integer,Integer"
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_5()
Dim o As Object = New C9
Assert.AreEqual("Integer,Integer", o.F(40, arr:=2))
End Sub
Class C10
Public Function F(ByVal i As Integer, ByVal ParamArray arr() As Integer) As String
Return "Integer,ParamArray Integer()"
End Function
End Class
'TargetJvmNotWorking - ArgumentException was thrown when InvalidCastExceptions should be thrown
<Category("TargetJvmNotWorking")> _
<Test(), ExpectedException(GetType(InvalidCastException))> _
Public Sub LateBind_NamedParam_6()
Dim o As Object = New C10
o.F(40, arr:=2)
End Sub
Class C11
Public Function F(ByVal i As Integer, ByVal ParamArray arr() As Integer) As String
Return "Integer,ParamArray Integer()"
End Function
Public Function F(ByVal i As Long, ByVal arr As Long) As String
Return "Long,Long"
End Function
Public Function F(ByVal i As Integer, ByVal j As Integer) As String
Return "Integer,Integer"
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_7()
Dim o As Object = New C11
Assert.AreEqual("Long,Long", o.F(40, arr:=2))
End Sub
Class C12
Public Function F(ByVal i As Long, ByVal arr As Long) As String
Return "Long,Long"
End Function
Public Function F(ByVal i As Integer, ByVal arr As Integer) As String
Return "Integer,Integer"
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_8()
Dim o As Object = New C12
Assert.AreEqual("Integer,Integer", o.F(40, arr:=2))
End Sub
Class C13
Public Function F(ByVal i As Integer, ByVal j As Long) As String
Return "Integer,Long"
End Function
Public Function F(ByVal j As Integer, ByVal i As Integer) As String
Return "Integer,Integer"
End Function
End Class
<Test()> _
Public Sub LateBind_NamedParam_9()
Dim o As Object = New C13
Assert.AreEqual("Integer,Integer", o.F(40, i:=2))
End Sub
Class C14
Public Function F(ByVal ParamArray arr() As Integer) As String
Return "ParamArray Integer()"
End Function
Public Function F(ByVal arr As String) As String
Return "String"
End Function
End Class
<Test(), ExpectedException(GetType(AmbiguousMatchException))> _
Public Sub LateBind_NamedParam_10()
Dim o As Object = New C14
Dim iarr() As Integer = {5, 6, 7}
o.F(arr:=iarr)
End Sub
'TargetJvmNotWorking - MissingMemberException was thrown when InvalidCastExceptions should be thrown
<Category("TargetJvmNotWorking")> _
<Test(), ExpectedException(GetType(InvalidCastException))> _
Public Sub LateBind_NamedParam_11()
Dim o As Object = New C14
Dim iarr() As Integer = {5, 6, 7}
o.F(40, iarr)
End Sub
<Test()> _
Public Sub LateBind_NamedParam_16()
Dim o As Object = New C14
Dim iarr() As Integer = {5, 6, 7}
Assert.AreEqual("ParamArray Integer()", o.F(iarr))
End Sub
Class C15
Public Function F(ByVal i As Integer, ByVal j As Integer, ByVal ParamArray arr() As Integer) As String
Return "Integer,Integer,ParamArray Integer()"
End Function
Public Function F(ByVal i As Integer, ByVal j As Integer, ByVal arr As String) As String
Return "Integer,Integer,String"
End Function
End Class
#If TARGET_JVM Then
<Test(), ExpectedException(GetType(InvalidCastException)),Category("TargetJvmNotWorking")> Public Sub LateBind_NamedParam_12()
#Else
<Test(), ExpectedException(GetType(InvalidCastException))> Public Sub LateBind_NamedParam_12()
#End If
Dim o As Object = New C15
Assert.AreEqual("Integer,Integer,ParamArray Integer()", o.F(i:=5, j:=6))
End Sub
'TargetJvmNotWorking - InvalidCastExceptions should be thrown
<Category("TargetJvmNotWorking")> _
<Test(), ExpectedException(GetType(InvalidCastException))> _
Public Sub LateBind_NamedParam_15()
Dim o As Object = New C15
Assert.AreEqual("Integer,Integer,ParamArray Integer()", o.F(i:=5, j:=6))
End Sub
<Test()> _
Public Sub LateBind_NamedParam_13()
Dim o As Object = New C15
Assert.AreEqual("Integer,Integer,String", o.F(i:=5, j:=6, arr:=Nothing))
End Sub
<Test(), ExpectedException(GetType(AmbiguousMatchException))> _
Public Sub LateBind_NamedParam_14()
Dim o As Object = New C15
Dim iarr() As Integer = {5, 6, 7}
Assert.AreEqual("Integer,Integer,String", o.F(i:=5, j:=6, arr:=iarr))
End Sub
End Class
|