PageRenderTime 6394ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Workspaces/CoreTest/FindReferencesTests.cs

https://gitlab.com/sharadag/TestProject2
C# | 285 lines | 239 code | 38 blank | 8 comment | 1 complexity | aaf15916b777e20a1dc02ea3a7f6ea78 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. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CSharp;
  6. using Microsoft.CodeAnalysis.CSharp.Symbols;
  7. using Microsoft.CodeAnalysis.CSharp.Syntax;
  8. using Microsoft.CodeAnalysis.FindSymbols;
  9. using Microsoft.CodeAnalysis.Text;
  10. using Roslyn.Test.Utilities;
  11. using Xunit;
  12. namespace Microsoft.CodeAnalysis.UnitTests
  13. {
  14. public partial class FindReferencesTests : TestBase
  15. {
  16. private Solution CreateSolution()
  17. {
  18. return new AdhocWorkspace().CurrentSolution;
  19. }
  20. private Solution GetSingleDocumentSolution(string sourceText)
  21. {
  22. var pid = ProjectId.CreateNewId();
  23. var did = DocumentId.CreateNewId(pid);
  24. return CreateSolution()
  25. .AddProject(pid, "foo", "foo", LanguageNames.CSharp)
  26. .AddMetadataReference(pid, MscorlibRef)
  27. .AddDocument(did, "foo.cs", SourceText.From(sourceText));
  28. }
  29. [Fact]
  30. public void FindFieldReferencesInSingleDocumentProject()
  31. {
  32. var text = @"
  33. public class C {
  34. public int X;
  35. public int Y = X * X;
  36. public void M() {
  37. int x = 10;
  38. int y = x + X;
  39. }
  40. }
  41. ";
  42. var solution = GetSingleDocumentSolution(text);
  43. var project = solution.Projects.First();
  44. var symbol = project.GetCompilationAsync().Result.GetTypeByMetadataName("C").GetMembers("X").First();
  45. var result = SymbolFinder.FindReferencesAsync(symbol, solution).Result.ToList();
  46. Assert.Equal(1, result.Count); // 1 symbol found
  47. Assert.Equal(3, result[0].Locations.Count()); // 3 locations found
  48. }
  49. [Fact]
  50. public void FindTypeReference_DuplicateMetadataReferences()
  51. {
  52. var text = @"
  53. public class C {
  54. public string X;
  55. }
  56. ";
  57. var pid = ProjectId.CreateNewId();
  58. var did = DocumentId.CreateNewId(pid);
  59. var solution = CreateSolution()
  60. .AddProject(pid, "foo", "foo.dll", LanguageNames.CSharp)
  61. .AddMetadataReference(pid, MscorlibRef)
  62. .AddMetadataReference(pid, ((PortableExecutableReference)MscorlibRef).WithAliases(new[] { "X" }))
  63. .AddDocument(did, "foo.cs", SourceText.From(text));
  64. var project = solution.Projects.First();
  65. var symbol = (IFieldSymbol)project.GetCompilationAsync().Result.GetTypeByMetadataName("C").GetMembers("X").First();
  66. var result = SymbolFinder.FindReferencesAsync(symbol.Type, solution).Result.ToList();
  67. Assert.Equal(9, result.Count);
  68. var typeSymbol = result.Where(@ref => @ref.Definition.Kind == SymbolKind.NamedType).Single();
  69. Assert.Equal(1, typeSymbol.Locations.Count());
  70. }
  71. [Fact]
  72. public void PinvokeMethodReferences_VB()
  73. {
  74. var tree = Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(
  75. @"
  76. Module Module1
  77. Declare Function CreateDirectory Lib ""kernel32"" Alias ""CreateDirectoryA"" (ByVal lpPathName As String) As Integer
  78. Private prop As Integer
  79. Property Prop1 As Integer
  80. Get
  81. Return prop
  82. End Get
  83. Set(value As Integer)
  84. CreateDirectory(""T"") ' Method Call 1
  85. prop = value
  86. prop = Nothing
  87. End Set
  88. End Property
  89. Sub Main()
  90. CreateDirectory(""T"") 'Method Call 2
  91. NormalMethod() ' Method Call 1
  92. NormalMethod() ' Method Call 2
  93. End Sub
  94. Sub NormalMethod()
  95. End Sub
  96. End Module
  97. ");
  98. ProjectId prj1Id = ProjectId.CreateNewId();
  99. DocumentId docId = DocumentId.CreateNewId(prj1Id);
  100. Microsoft.CodeAnalysis.Solution sln = new AdhocWorkspace().CurrentSolution
  101. .AddProject(prj1Id, "testDeclareReferences", "testAssembly", LanguageNames.VisualBasic)
  102. .AddMetadataReference(prj1Id, MscorlibRef)
  103. .AddDocument(docId, "testFile", tree.GetText());
  104. Microsoft.CodeAnalysis.Project prj = sln.GetProject(prj1Id).WithCompilationOptions(new VisualBasic.VisualBasicCompilationOptions(OutputKind.ConsoleApplication, embedVbCoreRuntime: true));
  105. tree = (SyntaxTree)prj.GetDocument(docId).GetSyntaxTreeAsync().Result;
  106. Compilation comp = prj.GetCompilationAsync().Result;
  107. SemanticModel semanticModel = comp.GetSemanticModel(tree);
  108. SyntaxNode declareMethod = tree.GetRoot().DescendantNodes().OfType<Microsoft.CodeAnalysis.VisualBasic.Syntax.DeclareStatementSyntax>().FirstOrDefault();
  109. SyntaxNode normalMethod = tree.GetRoot().DescendantNodes().OfType<Microsoft.CodeAnalysis.VisualBasic.Syntax.MethodStatementSyntax>().ToList()[1];
  110. // declared method calls
  111. var symbol = semanticModel.GetDeclaredSymbol(declareMethod);
  112. var references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
  113. Assert.Equal(expected: 2, actual: references.ElementAt(0).Locations.Count());
  114. // normal method calls
  115. symbol = semanticModel.GetDeclaredSymbol(normalMethod);
  116. references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
  117. Assert.Equal(expected: 2, actual: references.ElementAt(0).Locations.Count());
  118. }
  119. [Fact]
  120. public void PinvokeMethodReferences_CS()
  121. {
  122. var tree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(
  123. @"
  124. using System;
  125. using System.Collections;
  126. using System.Collections.Generic;
  127. using System.Data;
  128. using System.Diagnostics;
  129. using System.Runtime.InteropServices;
  130. static class Module1
  131. {
  132. [DllImport(""kernel32"", EntryPoint = ""CreateDirectoryA"", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  133. public static extern int CreateDirectory(string lpPathName);
  134. private static int prop;
  135. public static int Prop1
  136. {
  137. get { return prop; }
  138. set
  139. {
  140. CreateDirectory(""T"");
  141. // Method Call 1
  142. prop = value;
  143. prop = null;
  144. }
  145. }
  146. public static void Main()
  147. {
  148. CreateDirectory(""T""); // Method Call 2
  149. NormalMethod(); // Method Call 1
  150. NormalMethod(); // Method Call 2
  151. }
  152. public static void NormalMethod()
  153. {
  154. }
  155. }
  156. ");
  157. ProjectId prj1Id = ProjectId.CreateNewId();
  158. DocumentId docId = DocumentId.CreateNewId(prj1Id);
  159. var sln = new AdhocWorkspace().CurrentSolution
  160. .AddProject(prj1Id, "testDeclareReferences", "testAssembly", LanguageNames.CSharp)
  161. .AddMetadataReference(prj1Id, MscorlibRef)
  162. .AddDocument(docId, "testFile", tree.GetText());
  163. Microsoft.CodeAnalysis.Project prj = sln.GetProject(prj1Id).WithCompilationOptions(new CSharp.CSharpCompilationOptions(OutputKind.ConsoleApplication));
  164. tree = (SyntaxTree)prj.GetDocument(docId).GetSyntaxTreeAsync().Result;
  165. Compilation comp = prj.GetCompilationAsync().Result;
  166. SemanticModel semanticModel = comp.GetSemanticModel(tree);
  167. List<Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax> methodlist = tree.GetRoot().DescendantNodes().OfType<Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax>().ToList();
  168. SyntaxNode declareMethod = methodlist.ElementAt(0);
  169. SyntaxNode normalMethod = methodlist.ElementAt(2);
  170. // pinvoke method calls
  171. var symbol = semanticModel.GetDeclaredSymbol(declareMethod);
  172. var references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
  173. Assert.Equal(2, references.ElementAt(0).Locations.Count());
  174. // normal method calls
  175. symbol = semanticModel.GetDeclaredSymbol(normalMethod);
  176. references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
  177. Assert.Equal(2, references.ElementAt(0).Locations.Count());
  178. }
  179. [Fact, WorkItem(537936, "DevDiv")]
  180. public void FindReferences_InterfaceMapping()
  181. {
  182. var text = @"
  183. abstract class C
  184. {
  185. public abstract void Boo(); // Line 3
  186. }
  187. interface A
  188. {
  189. void Boo(); // Line 7
  190. }
  191. class B : C, A
  192. {
  193. void A.Boo() { } // Line 12
  194. public override void Boo() { } // Line 13
  195. public void Bar() { Boo(); } // Line 14
  196. }
  197. ";
  198. var solution = GetSingleDocumentSolution(text);
  199. var project = solution.Projects.First();
  200. var comp = project.GetCompilationAsync().Result;
  201. // Find references on definition B.Boo()
  202. var typeB = comp.GetTypeByMetadataName("B");
  203. var boo = typeB.GetMembers("Boo").First();
  204. var result = SymbolFinder.FindReferencesAsync(boo, solution).Result.ToList();
  205. Assert.Equal(2, result.Count); // 2 symbols found
  206. HashSet<int> expectedMatchedLines = new HashSet<int> { 3, 13, 14 };
  207. result.ForEach((reference) => Verify(reference, expectedMatchedLines));
  208. Assert.Empty(expectedMatchedLines);
  209. // Find references on definition C.Boo()
  210. var typeC = comp.GetTypeByMetadataName("C");
  211. boo = typeC.GetMembers("Boo").First();
  212. result = SymbolFinder.FindReferencesAsync(boo, solution).Result.ToList();
  213. Assert.Equal(2, result.Count); // 2 symbols found
  214. expectedMatchedLines = new HashSet<int> { 3, 13, 14 };
  215. result.ForEach((reference) => Verify(reference, expectedMatchedLines));
  216. Assert.Empty(expectedMatchedLines);
  217. // Find references on definition A.Boo()
  218. var typeA = comp.GetTypeByMetadataName("A");
  219. boo = typeA.GetMembers("Boo").First();
  220. result = SymbolFinder.FindReferencesAsync(boo, solution).Result.ToList();
  221. Assert.Equal(2, result.Count); // 2 symbols found
  222. expectedMatchedLines = new HashSet<int> { 7, 12 };
  223. result.ForEach((reference) => Verify(reference, expectedMatchedLines));
  224. Assert.Empty(expectedMatchedLines);
  225. }
  226. private static void Verify(ReferencedSymbol reference, HashSet<int> expectedMatchedLines)
  227. {
  228. System.Action<Location> verifier = (location) => Assert.True(expectedMatchedLines.Remove(location.GetLineSpan().StartLinePosition.Line));
  229. foreach (var location in reference.Locations)
  230. {
  231. verifier(location.Location);
  232. }
  233. foreach (var location in reference.Definition.Locations)
  234. {
  235. verifier(location);
  236. }
  237. }
  238. }
  239. }