Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnAutoInsert Cohosting Tests #10829

Merged
merged 9 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ private static ImmutableArray<string> CalculateTriggerChars(IEnumerable<IOnAutoI
protected override RazorTextDocumentIdentifier? GetRazorTextDocumentIdentifier(VSInternalDocumentOnAutoInsertParams request)
=> request.TextDocument.ToRazorTextDocumentIdentifier();

protected override async Task<VSInternalDocumentOnAutoInsertResponseItem?> HandleRequestAsync(VSInternalDocumentOnAutoInsertParams request, RazorCohostRequestContext context, CancellationToken cancellationToken)
{
var razorDocument = context.TextDocument.AssumeNotNull();
protected override Task<VSInternalDocumentOnAutoInsertResponseItem?> HandleRequestAsync(VSInternalDocumentOnAutoInsertParams request, RazorCohostRequestContext context, CancellationToken cancellationToken)
=> HandleRequestAsync(request, context.TextDocument.AssumeNotNull(), cancellationToken);

private async Task<VSInternalDocumentOnAutoInsertResponseItem?> HandleRequestAsync(VSInternalDocumentOnAutoInsertParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
_logger.LogDebug($"Resolving auto-insertion for {razorDocument.FilePath}");

var clientSettings = _clientSettingsManager.GetClientSettings();
Expand Down Expand Up @@ -173,4 +174,15 @@ private static ImmutableArray<string> CalculateTriggerChars(IEnumerable<IOnAutoI

return result.Response;
}

internal TestAccessor GetTestAccessor() => new(this);

internal readonly struct TestAccessor(CohostOnAutoInsertEndpoint instance)
{
public Task<VSInternalDocumentOnAutoInsertResponseItem?> HandleRequestAsync(
VSInternalDocumentOnAutoInsertParams request,
TextDocument razorDocument,
CancellationToken cancellationToken)
=> instance.HandleRequestAsync(request, razorDocument, cancellationToken);
alexgav marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodBeAnalysis.Remote.Razor.AutoInsert;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Razor.AutoInsert;
using Microsoft.CodeAnalysis.Razor.Settings;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.LanguageServices.Razor.LanguageClient.Cohost;
using Microsoft.VisualStudio.Razor.Settings;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

public class CohostOnAutoInsertEndpointTest(ITestOutputHelper testOutputHelper) : CohostEndpointTestBase(testOutputHelper)
{
[Theory]
[InlineData("PageTitle", "$0</PageTitle>", ">")]
[InlineData("div", "$0</div>", ">")]
[InlineData("text", "$0</text>", ">")]
alexgav marked this conversation as resolved.
Show resolved Hide resolved

alexgav marked this conversation as resolved.
Show resolved Hide resolved
public async Task Component_AutoInsertEndTag(string startTag, string endTag, string triggerCharacter)
{
var input = $"""
This is a Razor document.

<{startTag}$$

The end.
""";

await VerifyOnAutoInsertAsync(input, endTag, triggerCharacter);
}

[Theory]
[InlineData("div style", "\"\"", "=")]
alexgav marked this conversation as resolved.
Show resolved Hide resolved
public async Task Component_AutoInsertAttributeQuotes(string startTag, string insertedText, string triggerCharacter)
{
var input = $"""
This is a Razor document.

<{startTag}$$

The end.
""";

await VerifyOnAutoInsertAsync(input, insertedText, triggerCharacter, createDelegatedResponse: true);
}

[Theory]
[InlineData("""
@code {
//$$
void TestMethod() {}
}
""",
"/// <summary>", "/")]
alexgav marked this conversation as resolved.
Show resolved Hide resolved
public async Task Component_AutoInsertCSharp(string input, string insertedText, string triggerCharacter)
{
await VerifyOnAutoInsertAsync(input, insertedText, triggerCharacter);
}

private async Task VerifyOnAutoInsertAsync(
string input,
string insertedText,
string triggerCharacter,
bool createDelegatedResponse = false)
{
TestFileMarkupParser.GetPosition(input, out input, out var cursorPosition);
alexgav marked this conversation as resolved.
Show resolved Hide resolved
var document = CreateProjectAndRazorDocument(input);
var sourceText = await document.GetTextAsync(DisposalToken);

var clientSettingsManager = new ClientSettingsManager([], null, null);
clientSettingsManager.Update(ClientAdvancedSettings.Default with { FormatOnType = true, AutoClosingTags = true });
alexgav marked this conversation as resolved.
Show resolved Hide resolved

IOnAutoInsertTriggerCharacterProvider[] onAutoInsertTriggerCharacterProviders = [
new RemoteAutoClosingTagOnAutoInsertProvider(),
new RemoteCloseTextTagOnAutoInsertProvider()];

VSInternalDocumentOnAutoInsertResponseItem? response = null;
if (createDelegatedResponse)
{
var start = sourceText.GetPosition(cursorPosition + triggerCharacter.Length);
var end = start;
response = new VSInternalDocumentOnAutoInsertResponseItem()
{
TextEdit = new TextEdit() { NewText = insertedText, Range = new() { Start = start, End = end } },
TextEditFormat = InsertTextFormat.Snippet
};
}

var requestInvoker = new TestLSPRequestInvoker([(VSInternalMethods.OnAutoInsertName, response)]);

var endpoint = new CohostOnAutoInsertEndpoint(
RemoteServiceInvoker,
clientSettingsManager,
onAutoInsertTriggerCharacterProviders,
TestHtmlDocumentSynchronizer.Instance,
requestInvoker,
LoggerFactory);

var formattingOptions = new FormattingOptions()
{
InsertSpaces = true,
TabSize = 4
};

var request = new VSInternalDocumentOnAutoInsertParams()
{
TextDocument = new TextDocumentIdentifier()
{
Uri = document.CreateUri()
},
Position = sourceText.GetPosition(cursorPosition),
Character = triggerCharacter,
Options = formattingOptions
};

var result = await endpoint.GetTestAccessor().HandleRequestAsync(request, document, DisposalToken);

Assert.NotNull(result);

if (createDelegatedResponse)
{
Assert.Equal(response, result);
}

Assert.Equal(insertedText, result.TextEdit.NewText);
alexgav marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading