Skip to content

Commit

Permalink
Merge pull request #65675 from dibarbet/remove_bogus_assert
Browse files Browse the repository at this point in the history
[LSP] Find refs - Remove bogus assert and add test
  • Loading branch information
dibarbet authored Dec 6, 2022
2 parents ff42dbf + 5aaa4fb commit 0683256
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/EditorFeatures/Test/Options/GlobalOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static void Recurse(Type type, object options, object defaultOptions, string lan

private static TestWorkspace CreateWorkspace(out TestGlobalOptions globalOptions)
{
var composition = EditorTestCompositions.LanguageServerProtocol.
var composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures.
AddExcludedPartTypes(typeof(GlobalOptionService)).
AddParts(typeof(TestGlobalOptions));

Expand Down
6 changes: 5 additions & 1 deletion src/EditorFeatures/TestUtilities/EditorTestCompositions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public static class EditorTestCompositions
.AddParts(
typeof(TestInteractiveWindowEditorFactoryService));

public static readonly TestComposition LanguageServerProtocol = EditorFeatures;
public static readonly TestComposition LanguageServerProtocol = FeaturesTestCompositions.Features
.AddAssemblies(typeof(LanguageServerResources).Assembly);

public static readonly TestComposition LanguageServerProtocolEditorFeatures = EditorFeatures
.AddAssemblies(typeof(LanguageServerResources).Assembly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected AbstractLanguageServerProtocolTests(ITestOutputHelper? testOutputHelpe
TestOutputLspLogger = testOutputHelper != null ? new TestOutputLspLogger(testOutputHelper) : NoOpLspLogger.Instance;
}

private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocol
private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures
.AddParts(typeof(TestDocumentTrackingService))
.AddParts(typeof(TestWorkspaceRegistrationService));

Expand Down
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 @@ -4,6 +4,7 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServer.UnitTests.References;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Xunit;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.DocumentChanges
Expand Down Expand Up @@ -36,15 +37,15 @@ void M2()

await DidOpen(testLspServer, locationTyped.Uri);

var findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped);
var findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync<VSInternalReferenceItem>(testLspServer, locationTyped);
Assert.Single(findResults);

Assert.Equal("A", findResults[0].ContainingType);

// Declare a local inside A.M()
await DidChange(testLspServer, locationTyped.Uri, (5, 0, "var i = someInt + 1;\r\n"));

findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped);
findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync<VSInternalReferenceItem>(testLspServer, locationTyped);
Assert.Equal(2, findResults.Length);

Assert.Equal("A", findResults[0].ContainingType);
Expand All @@ -53,7 +54,7 @@ void M2()
// Declare a field in B
await DidChange(testLspServer, locationTyped.Uri, (10, 0, "int someInt = A.someInt + 1;\r\n"));

findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped);
findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync<VSInternalReferenceItem>(testLspServer, locationTyped);
Assert.Equal(3, findResults.Length);

Assert.Equal("A", findResults[0].ContainingType);
Expand All @@ -63,7 +64,7 @@ void M2()
// Declare a local inside B.M2()
await DidChange(testLspServer, locationTyped.Uri, (13, 0, "var j = someInt + A.someInt;\r\n"));

findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped);
findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync<VSInternalReferenceItem>(testLspServer, locationTyped);
Assert.Equal(4, findResults.Length);

Assert.Equal("A", findResults[0].ContainingType);
Expand All @@ -78,7 +79,7 @@ void M2()
// the updated document, so if we regress and get lucky, we still know about it.
await DidClose(testLspServer, locationTyped.Uri);

findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped);
findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync<VSInternalReferenceItem>(testLspServer, locationTyped);
Assert.Single(findResults);

Assert.Equal("A", findResults[0].ContainingType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Test;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using Xunit.Abstractions;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.References;
public class FindAllReferencesHandlerFeaturesTests : AbstractLanguageServerProtocolTests
{
public FindAllReferencesHandlerFeaturesTests(ITestOutputHelper? testOutputHelper) : base(testOutputHelper)
{
}

protected override TestComposition Composition => EditorTestCompositions.LanguageServerProtocol
.AddParts(typeof(TestDocumentTrackingService))
.AddParts(typeof(TestWorkspaceRegistrationService));

[Fact]
public async Task TestFindAllReferencesAsync_DoesNotUseVSTypes()
{
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 FindAllReferencesHandlerTests.RunFindAllReferencesAsync<LSP.Location>(testLspServer, testLspServer.GetLocations("caret").First());
AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result));
}
}
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,19 +273,19 @@ 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;
Expand All @@ -301,11 +301,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,
var results = await testLspServer.ExecuteRequestAsync<LSP.ReferenceParams, T[]>(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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ValueTask DisposeAsync()
=> _disposable.DisposeAsync();
}

private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocol
private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures
.AddParts(typeof(TestDocumentTrackingService))
.AddParts(typeof(TestWorkspaceRegistrationService))
.RemoveParts(typeof(MockWorkspaceEventListenerProvider));
Expand Down

0 comments on commit 0683256

Please sign in to comment.