提交 21316855 编写于 作者: M Manish Vasani

Add FindReferences tests for Kind column/SymbolUsageInfo for references

上级 4994a53a
......@@ -411,6 +411,41 @@ class Definition:Program
]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestField_ValueUsageInfo(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
int {|Definition:$$i|};
void Goo()
{
Console.WriteLine({|ValueUsageInfo.Read:[|i|]|});
{|ValueUsageInfo.Write:[|i|]|} = 0;
{|ValueUsageInfo.ReadWrite:[|i|]|}++;
Goo2(in {|ValueUsageInfo.ReadableReference:[|i|]|}, ref {|ValueUsageInfo.ReadableWritableReference:[|i|]|});
Goo3(out {|ValueUsageInfo.WritableReference:[|i|]|});
Console.WriteLine(nameof({|ValueUsageInfo.Name:[|i|]|}));
}
void Goo2(in int j, ref int k)
{
}
void Goo3(out int i)
{
i = 0;
}
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
......
......@@ -541,5 +541,38 @@ End Module
Await TestAPIAndFeature(input, kind, host)
End Function
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestLocal_ValueUsageInfo(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
void Goo()
{
int {|Definition:$$i|} = 0;
Console.WriteLine({|ValueUsageInfo.Read:[|i|]|});
{|ValueUsageInfo.Write:[|i|]|} = 0;
{|ValueUsageInfo.ReadWrite:[|i|]|}++;
Goo2(in {|ValueUsageInfo.ReadableReference:[|i|]|}, ref {|ValueUsageInfo.ReadableWritableReference:[|i|]|});
Goo3(out {|ValueUsageInfo.WritableReference:[|i|]|});
Console.WriteLine(nameof({|ValueUsageInfo.Name:[|i|]|}));
}
void Goo2(in int j, ref int k)
{
}
void Goo3(out int i)
{
i = 0;
}
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
End Class
End Namespace
......@@ -2189,5 +2189,48 @@ public class D { }
End Function
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestNamedType_TypeOrNamespaceUsageInfo(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
namespace N1
{
using Alias1 = N2.{|TypeOrNamespaceUsageInfo.Import:[|Class1|]|};
}
namespace N2
{
public interface I<T> { }
public class {|Definition:$$Class1|}
{
public static int Field;
public class Nested { }
}
public class Class2 : {|TypeOrNamespaceUsageInfo.Base:[|Class1|]|}, I<{|TypeOrNamespaceUsageInfo.TypeArgument:[|Class1|]|}>
{
public static int M() => {|TypeOrNamespaceUsageInfo.Qualified:[|Class1|]|}.Field;
}
}
namespace N2.N3
{
using Alias2 = N2.{|TypeOrNamespaceUsageInfo.Qualified,Import:[|Class1|]|}.Nested;
public class Class3: {|TypeOrNamespaceUsageInfo.Qualified,Base:[|Class1|]|}.Nested, I<{|TypeOrNamespaceUsageInfo.Qualified,TypeArgument:[|Class1|]|}.Nested>
{
public static [|Class1|] M2() => new {|TypeOrNamespaceUsageInfo.ObjectCreation:[|Class1|]|}();
}
}]]>
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
End Class
End Namespace
......@@ -372,5 +372,35 @@ namespace var { }
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestNamespace_TypeOrNamespaceUsageInfo(host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
using {|TypeOrNamespaceUsageInfo.Import:[|N1|]|};
using {|TypeOrNamespaceUsageInfo.Qualified,Import:[|N1|]|}.N2;
namespace {|Definition:{|TypeOrNamespaceUsageInfo.NamespaceDeclaration:[|$$N1|]|}|}
{
public class Class1
{
public static int Field;
}
}
namespace {|TypeOrNamespaceUsageInfo.Qualified,NamespaceDeclaration:[|N1|]|}.N2
{
public class Class2
{
public static int M() => {|TypeOrNamespaceUsageInfo.Qualified:[|N1|]|}.Class1.Field;
}
}
</Document>
</Project>
</Workspace>
Await TestAPI(input, host)
End Function
End Class
End Namespace
......@@ -635,6 +635,39 @@ end class
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestParameter_ValueUsageInfo(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
void Goo(int {|Definition:$$i|})
{
Console.WriteLine({|ValueUsageInfo.Read:[|i|]|});
{|ValueUsageInfo.Write:[|i|]|} = 0;
{|ValueUsageInfo.ReadWrite:[|i|]|}++;
Goo2(in {|ValueUsageInfo.ReadableReference:[|i|]|}, ref {|ValueUsageInfo.ReadableWritableReference:[|i|]|});
Goo3(out {|ValueUsageInfo.WritableReference:[|i|]|});
Console.WriteLine(nameof({|ValueUsageInfo.Name:[|i|]|}));
}
void Goo2(in int j, ref int k)
{
}
void Goo3(out int i)
{
i = 0;
}
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
......
......@@ -912,5 +912,41 @@ interface IC
</Workspace>
Await TestStreamingFeature(input, host)
End Function
<WorkItem(538886, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538886")>
<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestProperty_ValueUsageInfo(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
using System;
namespace ConsoleApplication22
{
class Program
{
static public int {|Definition:G$$oo|}
{
get
{
return 1;
}
set
{
}
}
static void Main(string[] args)
{
Console.WriteLine(Program.{|ValueUsageInfo.Read:[|Goo|]|});
Program.{|ValueUsageInfo.Write:[|Goo|]|} = 0;
Program.{|ValueUsageInfo.ReadWrite:[|Goo|]|} += 1;
}
}
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function
End Class
End Namespace
......@@ -19,6 +19,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
<[UseExportProvider]>
Partial Public Class FindReferencesTests
Private Const DefinitionKey As String = "Definition"
Private Const ValueUsageInfoKey As String = "ValueUsageInfo."
Private Const TypeOrNamespaceUsageInfoKey As String = "TypeOrNamespaceUsageInfo."
Private ReadOnly _outputHelper As ITestOutputHelper
......@@ -96,6 +98,37 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
context.References.Select(Function(r) r.SourceSpan))
Assert.Equal(expectedReferences, actualReferences)
Dim valueUsageInfoKeys = workspace.Documents.SelectMany(Function(d) d.AnnotatedSpans.Keys.Where(Function(key) key.StartsWith(ValueUsageInfoKey)))
For Each key In valueUsageInfoKeys
Dim expected =
workspace.Documents.Where(Function(d) d.AnnotatedSpans.ContainsKey(key) AndAlso d.AnnotatedSpans(key).Any()).
OrderBy(Function(d) d.Name).
Select(Function(d) New FileNameAndSpans(
d.Name, d.AnnotatedSpans(key).ToList())).ToList()
Dim valueUsageInfoField = key.Substring(ValueUsageInfoKey.Length)
Dim actual = GetFileNamesAndSpans(
context.References.Where(Function(r) r.SymbolUsageInfo.ValueUsageInfoOpt?.ToString() = valueUsageInfoField).Select(Function(r) r.SourceSpan))
Assert.Equal(expected, actual)
Next
Dim typeOrNamespaceUsageInfoKeys = workspace.Documents.SelectMany(Function(d) d.AnnotatedSpans.Keys.Where(Function(key) key.StartsWith(TypeOrNamespaceUsageInfoKey)))
For Each key In typeOrNamespaceUsageInfoKeys
Dim expected =
workspace.Documents.Where(Function(d) d.AnnotatedSpans.ContainsKey(key) AndAlso d.AnnotatedSpans(key).Any()).
OrderBy(Function(d) d.Name).
Select(Function(d) New FileNameAndSpans(
d.Name, d.AnnotatedSpans(key).ToList())).ToList()
Dim typeOrNamespaceUsageInfoFieldNames = key.Substring(TypeOrNamespaceUsageInfoKey.Length).Split(","c).Select(Function(s) s.Trim)
Dim actual = GetFileNamesAndSpans(
context.References.Where(Function(r)
Return r.SymbolUsageInfo.TypeOrNamespaceUsageInfoOpt IsNot Nothing AndAlso
r.SymbolUsageInfo.TypeOrNamespaceUsageInfoOpt.ToString().Split(","c).Select(Function(s) s.Trim).SetEquals(typeOrNamespaceUsageInfoFieldNames)
End Function).Select(Function(r) r.SourceSpan))
Assert.Equal(expected, actual)
Next
Next
End Using
End Function
......@@ -226,7 +259,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
Assert.Equal(Of String)(documentsWithAnnotatedSpans.Select(Function(d) GetFilePathAndProjectLabel(workspace, d)).Order(), actualDefinitions.Keys.Order())
For Each doc In documentsWithAnnotatedSpans
Dim expected = doc.AnnotatedSpans(DefinitionKey).Order()
Dim expected = If(doc.AnnotatedSpans.ContainsKey(DefinitionKey), doc.AnnotatedSpans(DefinitionKey), ImmutableArray(Of TextSpan).Empty).Order()
Dim actual = actualDefinitions(GetFilePathAndProjectLabel(workspace, doc)).Order()
If Not TextSpansMatch(expected, actual) Then
......@@ -234,15 +267,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
End If
Next
Dim actualReferences =
result.FilterToItemsToShow(options).
SelectMany(Function(r) r.Locations.Select(Function(loc) loc.Location)).
Where(Function(loc) IsInSource(workspace, loc, uiVisibleOnly)).
Distinct().
GroupBy(Function(loc) loc.SourceTree).
ToDictionary(
Function(g) GetFilePathAndProjectLabel(document.Project.Solution, g.Key),
Function(g) g.Select(Function(loc) loc.SourceSpan).Distinct().ToList())
Dim actualReferences = GetActualReferences(result, uiVisibleOnly, options, document, workspace)
Dim expectedDocuments = workspace.Documents.Where(Function(d) d.SelectedSpans.Any())
Assert.Equal(expectedDocuments.Select(Function(d) GetFilePathAndProjectLabel(workspace, d)).Order(), actualReferences.Keys.Order())
......@@ -254,10 +279,66 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
AssertEx.Equal(expectedSpans, actualSpans,
message:=PrintSpans(expectedSpans, actualSpans, workspace.CurrentSolution.GetDocument(doc.Id), "[|", "|]", messageOnly:=True))
Next
Dim valueUsageInfoKeys = workspace.Documents.SelectMany(Function(d) d.AnnotatedSpans.Keys.Where(Function(key) key.StartsWith(ValueUsageInfoKey)))
For Each key In valueUsageInfoKeys
For Each doc In documentsWithAnnotatedSpans.Where(Function(d) d.AnnotatedSpans.ContainsKey(key))
Dim expectedSpans = doc.AnnotatedSpans(key).Order()
Dim valueUsageInfoField = key.Substring(ValueUsageInfoKey.Length)
actualReferences = GetActualReferences(result, uiVisibleOnly, options, document, workspace, Function(r) r.SymbolUsageInfo.ValueUsageInfoOpt?.ToString() = valueUsageInfoField)
Dim actualSpans = actualReferences(GetFilePathAndProjectLabel(workspace, doc)).Order()
If Not TextSpansMatch(expectedSpans, actualSpans) Then
Assert.True(False, PrintSpans(expectedSpans, actualSpans, workspace.CurrentSolution.GetDocument(doc.Id), $"{{|{key}:", "|}"))
End If
Next
Next
Dim typeOrNamespaceUsageInfoKeys = workspace.Documents.SelectMany(Function(d) d.AnnotatedSpans.Keys.Where(Function(key) key.StartsWith(TypeOrNamespaceUsageInfoKey)))
For Each key In typeOrNamespaceUsageInfoKeys
For Each doc In documentsWithAnnotatedSpans.Where(Function(d) d.AnnotatedSpans.ContainsKey(key))
Dim expectedSpans = doc.AnnotatedSpans(key).Order()
Dim typeOrNamespaceUsageInfoFieldNames = key.Substring(TypeOrNamespaceUsageInfoKey.Length).Split(","c).Select(Function(s) s.Trim)
actualReferences = GetActualReferences(result, uiVisibleOnly, options, document, workspace, Function(r)
Return r.SymbolUsageInfo.TypeOrNamespaceUsageInfoOpt IsNot Nothing AndAlso
r.SymbolUsageInfo.TypeOrNamespaceUsageInfoOpt.ToString().Split(","c).Select(Function(s) s.Trim).SetEquals(typeOrNamespaceUsageInfoFieldNames)
End Function)
Dim actualSpans = actualReferences(GetFilePathAndProjectLabel(workspace, doc)).Order()
If Not TextSpansMatch(expectedSpans, actualSpans) Then
Assert.True(False, PrintSpans(expectedSpans, actualSpans, workspace.CurrentSolution.GetDocument(doc.Id), $"{{|{key}:", "|}"))
End If
Next
Next
Next
End Using
End Function
Private Shared Function GetActualReferences(result As IEnumerable(Of ReferencedSymbol),
uiVisibleOnly As Boolean,
options As FindReferencesSearchOptions,
document As Document,
workspace As TestWorkspace,
Optional locationFilterOpt As Func(Of ReferenceLocation, Boolean) = Nothing) As Dictionary(Of String, List(Of TextSpan))
Dim referenceLocations = result.FilterToItemsToShow(options).SelectMany(Function(r) r.Locations)
If locationFilterOpt IsNot Nothing Then
referenceLocations = referenceLocations.Where(locationFilterOpt)
End If
Return referenceLocations.
Select(Function(loc) loc.Location).
Where(Function(loc) IsInSource(workspace, loc, uiVisibleOnly)).
Distinct().
GroupBy(Function(loc) loc.SourceTree).
ToDictionary(
Function(g) GetFilePathAndProjectLabel(document.Project.Solution, g.Key),
Function(g) g.Select(Function(loc) loc.SourceSpan).Distinct().ToList())
End Function
Private Shared Function PrintSpans(expected As IOrderedEnumerable(Of TextSpan), actual As IOrderedEnumerable(Of TextSpan), doc As Document, prefix As String, suffix As String, Optional messageOnly As Boolean = False) As String
Debug.Assert(expected IsNot Nothing)
Debug.Assert(actual IsNot Nothing)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册