Skip to content

Commit

Permalink
Remove bogus assert and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Nov 30, 2022
1 parent 1568387 commit 9016317
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public FindAllReferencesHandler(
RequestContext context,
CancellationToken cancellationToken)
{
var clientCapabilities = context.GetRequiredClientCapabilities();
Debug.Assert(clientCapabilities.HasVisualStudioLspCapability());

var document = context.Document;
var workspace = context.Workspace;
Contract.ThrowIfNull(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FindAllReferencesHandlerTests(ITestOutputHelper testOutputHelper) : base(
{
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync()
{
var markup =
Expand All @@ -49,7 +49,7 @@ void M2()
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location));

// Results are returned in a non-deterministic order, so we order them by location
Expand All @@ -63,7 +63,7 @@ void M2()
AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync_Streaming()
{
var markup =
Expand All @@ -87,7 +87,7 @@ void M2()

using var progress = BufferedProgress.Create<object>(null);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First(), progress);
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First(), progress);

Assert.Null(results);

Expand All @@ -113,7 +113,7 @@ void M2()
AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync_Class()
{
var markup =
Expand All @@ -135,7 +135,7 @@ void M2()
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location));

var textElement = results[0].Text as ClassifiedTextElement;
Expand All @@ -154,7 +154,7 @@ void M2()
AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 2);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync_MultipleDocuments()
{
var markups = new string[] {
Expand All @@ -178,7 +178,7 @@ void M2()

await using var testLspServer = await CreateTestLspServerAsync(markups, new InitializationOptions { ClientCapabilities = CapabilitiesWithVSExtensions });

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location));

// Results are returned in a non-deterministic order, so we order them by location
Expand All @@ -192,7 +192,7 @@ void M2()
AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync_InvalidLocation()
{
var markup =
Expand All @@ -202,11 +202,11 @@ public async Task TestFindAllReferencesAsync_InvalidLocation()
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
Assert.Empty(results);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesMetadataDefinitionAsync()
{
var markup =
Expand All @@ -221,12 +221,12 @@ void M()
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
Assert.NotNull(results[0].Location.Uri);
AssertHighlightCount(results, expectedDefinitionCount: 0, expectedWrittenReferenceCount: 0, expectedReferenceCount: 1);
}

[WpfFact, WorkItem(1240061, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1240061/")]
[Fact, WorkItem(1240061, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1240061/")]
public async Task TestFindAllReferencesAsync_Namespace()
{
var markup =
Expand All @@ -243,7 +243,7 @@ void M()
";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());

// Namespace definitions should not have a location
Assert.True(results.Any(r => r.DefinitionText != null && r.Location == null));
Expand All @@ -255,7 +255,7 @@ void M()
AssertHighlightCount(results, expectedDefinitionCount: 0, expectedWrittenReferenceCount: 0, expectedReferenceCount: 2);
}

[WpfFact, WorkItem(1245616, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1245616/")]
[Fact, WorkItem(1245616, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1245616/")]
public async Task TestFindAllReferencesAsync_Highlights()
{
var markup =
Expand All @@ -273,25 +273,51 @@ void M()
";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());
AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 1, expectedReferenceCount: 1);
}

[WpfFact]
[Fact]
public async Task TestFindAllReferencesAsync_StaticClassification()
{
var markup =
@"static class {|caret:|}{|reference:C|} { }
";
await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions);

var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First());
var results = await RunFindAllReferencesAsync<LSP.VSInternalReferenceItem>(testLspServer, testLspServer.GetLocations("caret").First());

// Ensure static definitions and references are only classified once
var textRuns = ((ClassifiedTextElement)results.First().Text).Runs;
Assert.Equal(9, textRuns.Count());
}

[Fact]
public async Task TestFindAllReferencesAsync_OnlyLocations()
{
var markup =
@"class A
{
public int {|reference:someInt|} = 1;
void M()
{
var i = {|reference:someInt|} + 1;
}
}
class B
{
int someInt = A.{|reference:someInt|} + 1;
void M2()
{
var j = someInt + A.{|caret:|}{|reference:someInt|};
}
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, new LSP.ClientCapabilities());

var results = await RunFindAllReferencesAsync<LSP.Location>(testLspServer, testLspServer.GetLocations("caret").First());
AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result));
}

private static LSP.ReferenceParams CreateReferenceParams(LSP.Location caret, IProgress<object> progress)
=> new LSP.ReferenceParams()
{
Expand All @@ -301,11 +327,11 @@ private static LSP.ReferenceParams CreateReferenceParams(LSP.Location caret, IPr
PartialResultToken = progress
};

internal static async Task<LSP.VSInternalReferenceItem[]> RunFindAllReferencesAsync(TestLspServer testLspServer, LSP.Location caret, IProgress<object> progress = null)
internal static async Task<T[]> RunFindAllReferencesAsync<T>(TestLspServer testLspServer, LSP.Location caret, IProgress<object> progress = null)
{
var results = await testLspServer.ExecuteRequestAsync<LSP.ReferenceParams, LSP.VSInternalReferenceItem[]>(LSP.Methods.TextDocumentReferencesName,
CreateReferenceParams(caret, progress), CancellationToken.None);
return results?.Cast<LSP.VSInternalReferenceItem>()?.ToArray();
return results?.Cast<T>()?.ToArray();
}

private static void AssertValidDefinitionProperties(LSP.VSInternalReferenceItem[] referenceItems, int definitionIndex, Glyph definitionGlyph)
Expand Down

0 comments on commit 9016317

Please sign in to comment.