From 90163174f6f256050855a17d44f885c2eca001be Mon Sep 17 00:00:00 2001 From: David Barbet Date: Tue, 29 Nov 2022 18:38:57 -0800 Subject: [PATCH] Remove bogus assert and add test --- .../References/FindAllReferencesHandler.cs | 3 - .../FindAllReferencesHandlerTests.cs | 66 +++++++++++++------ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs b/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs index c2b954e599941..c2c4372be3f54 100644 --- a/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs @@ -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); diff --git a/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs index 45112ce1e8be5..d73df1d615301 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs @@ -27,7 +27,7 @@ public FindAllReferencesHandlerTests(ITestOutputHelper testOutputHelper) : base( { } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync() { var markup = @@ -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(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 @@ -63,7 +63,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_Streaming() { var markup = @@ -87,7 +87,7 @@ void M2() using var progress = BufferedProgress.Create(null); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First(), progress); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First(), progress); Assert.Null(results); @@ -113,7 +113,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_Class() { var markup = @@ -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(testLspServer, testLspServer.GetLocations("caret").First()); AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location)); var textElement = results[0].Text as ClassifiedTextElement; @@ -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[] { @@ -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(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 @@ -192,7 +192,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_InvalidLocation() { var markup = @@ -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(testLspServer, testLspServer.GetLocations("caret").First()); Assert.Empty(results); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesMetadataDefinitionAsync() { var markup = @@ -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(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 = @@ -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(testLspServer, testLspServer.GetLocations("caret").First()); // Namespace definitions should not have a location Assert.True(results.Any(r => r.DefinitionText != null && r.Location == null)); @@ -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 = @@ -273,11 +273,11 @@ void M() "; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 1, expectedReferenceCount: 1); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_StaticClassification() { var markup = @@ -285,13 +285,39 @@ public async Task TestFindAllReferencesAsync_StaticClassification() "; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(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(testLspServer, testLspServer.GetLocations("caret").First()); + AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result)); + } + private static LSP.ReferenceParams CreateReferenceParams(LSP.Location caret, IProgress progress) => new LSP.ReferenceParams() { @@ -301,11 +327,11 @@ private static LSP.ReferenceParams CreateReferenceParams(LSP.Location caret, IPr PartialResultToken = progress }; - internal static async Task RunFindAllReferencesAsync(TestLspServer testLspServer, LSP.Location caret, IProgress progress = null) + internal static async Task RunFindAllReferencesAsync(TestLspServer testLspServer, LSP.Location caret, IProgress progress = null) { var results = await testLspServer.ExecuteRequestAsync(LSP.Methods.TextDocumentReferencesName, CreateReferenceParams(caret, progress), CancellationToken.None); - return results?.Cast()?.ToArray(); + return results?.Cast()?.ToArray(); } private static void AssertValidDefinitionProperties(LSP.VSInternalReferenceItem[] referenceItems, int definitionIndex, Glyph definitionGlyph)