Add failing test, and add verification to existing tests

This commit is contained in:
David Wengier 2024-09-03 16:54:11 +10:00
Родитель 8bddfe9971
Коммит 1e82de71fe
1 изменённых файлов: 54 добавлений и 6 удалений

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -14,6 +15,7 @@ using Xunit.Abstractions;
using RoslynDocumentLink = Roslyn.LanguageServer.Protocol.DocumentLink;
using RoslynLocation = Roslyn.LanguageServer.Protocol.Location;
using RoslynLspExtensions = Roslyn.LanguageServer.Protocol.RoslynLspExtensions;
using TextDocument = Microsoft.CodeAnalysis.TextDocument;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
@ -157,7 +159,7 @@ public class CohostGoToDefinitionEndpointTest(ITestOutputHelper testOutputHelper
""";
var result = await GetGoToDefinitionResultAsync(input, FileKinds.Component,
(FileName("SurveyPrompt.razor"), surveyPrompt.Text),
(FileName("SurveyPrompt.razor"), surveyPrompt.Text),
(FileName("SurveyPrompt.razor.g.cs"), surveyPromptGeneratedCode.Text));
Assert.NotNull(result.Value.Second);
@ -283,7 +285,7 @@ public class CohostGoToDefinitionEndpointTest(ITestOutputHelper testOutputHelper
#endregion
var result = await GetGoToDefinitionResultAsync(input, FileKinds.Component,
(FileName("SurveyPrompt.razor"), surveyPrompt.Text),
(FileName("SurveyPrompt.razor"), surveyPrompt.Text),
(FileName("SurveyPrompt.razor.g.cs"), surveyPromptGeneratedCode.Text));
Assert.NotNull(result.Value.Second);
@ -295,24 +297,70 @@ public class CohostGoToDefinitionEndpointTest(ITestOutputHelper testOutputHelper
Assert.Equal(range, location.Range);
}
[Fact]
public async Task Html()
{
// This really just validates Uri remapping, the actual response is largely arbitrary
TestCode input = """
<div></div>
<script>
function [|foo|]() {
f$$oo();
}
</script>
""";
var document = CreateProjectAndRazorDocument(input.Text);
var inputText = await document.GetTextAsync(DisposalToken);
var htmlResponse = new SumType<Location, Location[], DocumentLink[]>?(new Location[]
{
new Location
{
Uri = new Uri(document.CreateUri(), document.Name + FeatureOptions.HtmlVirtualDocumentSuffix),
Range = inputText.GetRange(input.Span),
},
});
await VerifyGoToDefinitionAsync(input, htmlResponse: htmlResponse);
}
private static string FileName(string projectRelativeFileName)
=> Path.Combine(TestProjectData.SomeProjectPath, projectRelativeFileName);
private async Task VerifyGoToDefinitionAsync(TestCode input, string? fileKind = null, params (string fileName, string contents)[]? additionalFiles)
private async Task VerifyGoToDefinitionAsync(TestCode input, string? fileKind = null, SumType<Location, Location[], DocumentLink[]>? htmlResponse = null)
{
var result = await GetGoToDefinitionResultAsync(input, fileKind, additionalFiles);
var document = CreateProjectAndRazorDocument(input.Text, fileKind);
var result = await GetGoToDefinitionResultAsync(document, input, htmlResponse);
Assumes.NotNull(result);
Assert.NotNull(result.Value.Second);
var locations = result.Value.Second;
var location = Assert.Single(locations);
var text = SourceText.From(input.Text);
var range = RoslynLspExtensions.GetRange(text, input.Span);
Assert.Equal(range, location.Range);
Assert.Equal(document.CreateUri(), location.Uri);
}
private async Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
private Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
TestCode input, string? fileKind = null, params (string fileName, string contents)[]? additionalFiles)
{
var document = CreateProjectAndRazorDocument(input.Text, fileKind, additionalFiles);
return GetGoToDefinitionResultAsync(document, input, htmlResponse: null);
}
private async Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
TextDocument document, TestCode input, SumType<Location, Location[], DocumentLink[]>? htmlResponse)
{
var inputText = await document.GetTextAsync(DisposalToken);
var position = inputText.GetPosition(input.Position);
var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentDefinitionName, null)]);
var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentDefinitionName, htmlResponse)]);
var endpoint = new CohostGoToDefinitionEndpoint(RemoteServiceInvoker, TestHtmlDocumentSynchronizer.Instance, requestInvoker);