/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingScopeTests.vb

https://gitlab.com/sharadag/TestProject2 · Visual Basic · 300 lines · 208 code · 48 blank · 44 comment · 0 complexity · cd04d083e73db0460004fa14daeceea7 MD5 · raw file

  1. ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. Imports System.Xml.Linq
  3. Imports Roslyn.Test.Utilities
  4. Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
  5. ' Test that binding APIs that take character positions determine the edges of language
  6. ' constructs as we expect. We just use LookupNames as the test API, since all these APIs use the
  7. ' same underlying helpers to find the Binder object used to answer the questions.
  8. Public Class BindingScopeTests
  9. Private Sub CheckScopeOfSymbol(comp As VisualBasicCompilation,
  10. treeName As String,
  11. symbolName As String,
  12. expectedScope As XElement)
  13. Dim tree As SyntaxTree = CompilationUtils.GetTree(comp, treeName)
  14. Dim treeText As String = tree.GetText().ToString()
  15. Dim expectedScopeText As String = expectedScope.Value.Replace(vbLf, vbCrLf)
  16. Dim expectedStart As Integer = treeText.IndexOf(expectedScopeText, StringComparison.Ordinal)
  17. Assert.True(expectedStart >= 0, "did not found expectedScope")
  18. Dim expectedEnd As Integer = expectedStart + expectedScopeText.Length
  19. Dim semanticModel = comp.GetSemanticModel(tree)
  20. For position As Integer = 0 To treeText.Length - 1
  21. Dim names = semanticModel.LookupNames(position)
  22. Dim found = names.Contains(symbolName)
  23. Dim expectedToFind = (position >= expectedStart AndAlso position < expectedEnd)
  24. If found <> expectedToFind Then
  25. Dim locationText = If(treeText.Length > position + 50, treeText.Substring(position, 50), treeText.Substring(position))
  26. If expectedToFind Then
  27. Assert.True(found, String.Format("Should have been in scope at position {0} text '{1}...'", position, locationText))
  28. Else
  29. Assert.False(found, String.Format("Should have been out of scope at position {0} text '{1}...'", position, locationText))
  30. End If
  31. End If
  32. Next
  33. End Sub
  34. <Fact(), WorkItem(546396, "DevDiv")>
  35. Public Sub TestScopes1()
  36. Dim comp As VisualBasicCompilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
  37. <compilation name="Compilation">
  38. <file name="a.vb">
  39. Option Strict On
  40. Imports System
  41. Imports System.Collections
  42. Namespace Foo.Bar
  43. ' ClassA
  44. Public Class Apple(Of TBravo)
  45. Public alpha As Integer 'alpha
  46. End Class 'hello
  47. ' in between
  48. Namespace Baz
  49. 'Delta
  50. Delegate Sub Delta(Of TCharlie _
  51. )(x As Integer)
  52. ' after delta
  53. End Namespace
  54. 'about to end Bar
  55. End Namespace
  56. ' outside namespaces
  57. </file>
  58. </compilation>)
  59. CheckScopeOfSymbol(comp, "a.vb", "alpha",
  60. <expected>Public Class Apple(Of TBravo)
  61. Public alpha As Integer 'alpha
  62. End Class 'hello</expected>)
  63. CheckScopeOfSymbol(comp, "a.vb", "TBravo",
  64. <expected>Public Class Apple(Of TBravo)
  65. Public alpha As Integer 'alpha
  66. End Class 'hello</expected>)
  67. CheckScopeOfSymbol(comp, "a.vb", "Apple",
  68. <expected>
  69. ' ClassA
  70. Public Class Apple(Of TBravo)
  71. Public alpha As Integer 'alpha
  72. End Class 'hello
  73. ' in between
  74. Namespace Baz
  75. 'Delta
  76. Delegate Sub Delta(Of TCharlie _
  77. )(x As Integer)
  78. ' after delta
  79. End Namespace
  80. 'about to end Bar
  81. End Namespace</expected>)
  82. CheckScopeOfSymbol(comp, "a.vb", "Bar",
  83. <expected>
  84. ' ClassA
  85. Public Class Apple(Of TBravo)
  86. Public alpha As Integer 'alpha
  87. End Class 'hello
  88. ' in between
  89. Namespace Baz
  90. 'Delta
  91. Delegate Sub Delta(Of TCharlie _
  92. )(x As Integer)
  93. ' after delta
  94. End Namespace
  95. 'about to end Bar
  96. End Namespace</expected>)
  97. CheckScopeOfSymbol(comp, "a.vb", "Delta",
  98. <expected>
  99. 'Delta
  100. Delegate Sub Delta(Of TCharlie _
  101. )(x As Integer)
  102. ' after delta
  103. End Namespace</expected>)
  104. CheckScopeOfSymbol(comp, "a.vb", "TCharlie",
  105. <expected>Delegate Sub Delta(Of TCharlie _
  106. )(x As Integer)</expected>)
  107. End Sub
  108. <Fact(), WorkItem(546396, "DevDiv")>
  109. Public Sub TestScopes2()
  110. Dim comp As VisualBasicCompilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
  111. <compilation name="Compilation">
  112. <file name="a.vb">
  113. Option Strict On
  114. Imports System
  115. Imports System.Collections
  116. Namespace Foo.Bar
  117. ' ClassA
  118. Public Class Apple(Of TBravo)
  119. ' this is before mango
  120. Public Sub Mango(Of TRind)(yellow as Integer) 'zzz
  121. Dim red As String 'yyy
  122. 'before end sub
  123. End Sub ' xxx
  124. ' this is after mango
  125. End Class 'hello
  126. 'about to end Bar
  127. End Namespace
  128. ' outside namespaces
  129. </file>
  130. </compilation>)
  131. CheckScopeOfSymbol(comp, "a.vb", "TRind",
  132. <expected>Public Sub Mango(Of TRind)(yellow as Integer) 'zzz
  133. Dim red As String 'yyy
  134. 'before end sub
  135. End Sub ' xxx</expected>)
  136. CheckScopeOfSymbol(comp, "a.vb", "yellow",
  137. <expected>
  138. Dim red As String 'yyy
  139. 'before end sub
  140. End Sub ' xxx</expected>)
  141. CheckScopeOfSymbol(comp, "a.vb", "red",
  142. <expected>
  143. Dim red As String 'yyy
  144. 'before end sub
  145. End Sub ' xxx</expected>)
  146. End Sub
  147. <Fact(), WorkItem(546396, "DevDiv")>
  148. Public Sub TestScopes3()
  149. Dim comp As VisualBasicCompilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
  150. <compilation name="Compilation">
  151. <file name="a.vb">
  152. Public Class Apple(Of TBravo)
  153. Public Sub Mango(Of TRind)(yellow as Integer)
  154. Dim red As String
  155. ' before For
  156. For color = 1 to 10 'yyy
  157. ' inside for 1
  158. Dim cheetos as String 'xxx
  159. ' inside for 2
  160. Next color 'zzz
  161. 'after for
  162. If True = False Then Dim freetos As Integer : Dim chips As String Else Dim water As Integer : Dim fire as String 'if statement
  163. End Sub
  164. End Class 'hello
  165. </file>
  166. </compilation>)
  167. CheckScopeOfSymbol(comp, "a.vb", "cheetos",
  168. <expected>
  169. ' inside for 1
  170. Dim cheetos as String 'xxx
  171. ' inside for 2
  172. Next color 'zzz</expected>)
  173. CheckScopeOfSymbol(comp, "a.vb", "freetos",
  174. <expected>Then Dim freetos As Integer : Dim chips As String </expected>)
  175. CheckScopeOfSymbol(comp, "a.vb", "chips",
  176. <expected>Then Dim freetos As Integer : Dim chips As String </expected>)
  177. CheckScopeOfSymbol(comp, "a.vb", "water",
  178. <expected>Else Dim water As Integer : Dim fire as String 'if statement</expected>)
  179. CheckScopeOfSymbol(comp, "a.vb", "fire",
  180. <expected>Else Dim water As Integer : Dim fire as String 'if statement</expected>)
  181. End Sub
  182. <Fact(), WorkItem(546396, "DevDiv")>
  183. Public Sub TestScopes4()
  184. Dim comp As VisualBasicCompilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
  185. <compilation name="Compilation">
  186. <file name="a.vb">
  187. Public Class Apple(Of TBravo)
  188. Public Sub Mango(Of TRind)(yellow as Integer)
  189. Dim red As String
  190. While True 'yyy
  191. Dim cheetos as String 'xxx
  192. 'hello there
  193. End Sub
  194. End Class 'hello
  195. </file>
  196. </compilation>)
  197. CheckScopeOfSymbol(comp, "a.vb", "cheetos",
  198. <expected>
  199. Dim cheetos as String 'xxx
  200. 'hello there
  201. </expected>)
  202. End Sub
  203. <Fact(), WorkItem(546396, "DevDiv")>
  204. Public Sub TestPropertyScopes()
  205. Dim comp As VisualBasicCompilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
  206. <compilation name="Compilation">
  207. <file name="c.vb">
  208. Class C
  209. Private _p
  210. Private _q
  211. Private _r
  212. Property P
  213. Get
  214. Return _p
  215. End Get
  216. Set(val)
  217. _p = val
  218. End Set
  219. End Property
  220. ReadOnly Property Q(i As Integer)
  221. Get
  222. Return _q
  223. End Get
  224. End Property
  225. WriteOnly Property R(x, y)
  226. Set
  227. _r = Value
  228. End Set
  229. End Property
  230. End Class
  231. </file>
  232. </compilation>)
  233. ' Set value argument should be in scope for the body of the accessor.
  234. CheckScopeOfSymbol(comp, "c.vb", "val",
  235. <expected>
  236. _p = val
  237. End Set</expected>)
  238. ' Property parameters should be in scope for individual accessors.
  239. ' Note: The parameter is represented as separate symbols in the property and
  240. ' the accessors so that the ContainingSymbol property on the parameter symbol
  241. ' always refers to the immediately enclosing container (property or accessor).
  242. CheckScopeOfSymbol(comp, "c.vb", "i",
  243. <expected>
  244. Return _q
  245. End Get</expected>)
  246. CheckScopeOfSymbol(comp, "c.vb", "y",
  247. <expected>
  248. _r = Value
  249. End Set</expected>)
  250. End Sub
  251. End Class
  252. End Namespace