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

More hardening of the completion test #10904

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,8 +2,10 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Extensibility.Testing;
using Roslyn.Test.Utilities;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -17,55 +19,270 @@ public class CompletionIntegrationTests(ITestOutputHelper testOutputHelper) : Ab
[IdeFact]
public async Task SnippetCompletion_Html()
{
await TestServices.SolutionExplorer.AddFileAsync(
RazorProjectConstants.BlazorProjectName,
"Test.razor",
"""
@page "Test"
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>
<PageTitle>Test</PageTitle>

<h1>Test</h1>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<h1>Test</h1>
<PageTitle>Test</PageTitle>

@code {
private int currentCount = 0;
<h1>Test</h1>
<dl>
<dt></dt>
<dd></dd>
</dl>

private void IncrementCount()
@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<h1>Test</h1>",
stringsToType: ["{ENTER}", "d", "d"]);
}

[IdeFact, WorkItem("https://github.com/dotnet/razor/issues/10787")]
public async Task CompletionCommit_HtmlAttributeWithoutValue()
{
currentCount++;
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

<button></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<button disabled></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<button",
stringsToType: [" ", "d", "i", "s"]);
}
}
""",
open: true,
ControlledHangMitigatingCancellationToken);

await TestServices.Editor.WaitForComponentClassificationAsync(ControlledHangMitigatingCancellationToken);
[IdeFact]
public async Task CompletionCommit_HtmlAttributeWithValue()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

await TestServices.Editor.PlaceCaretAsync("<h1>Test</h1>", charsOffset: 1, ControlledHangMitigatingCancellationToken);
TestServices.Input.Send("{ENTER}");
TestServices.Input.Send("d");
TestServices.Input.Send("d");
<PageTitle>Test</PageTitle>

await CommitCompletionAndVerifyAsync("""
@page "Test"
<button></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>
<PageTitle>Test</PageTitle>

<h1>Test</h1>
<dl>
<dt></dt>
<dd></dd>
</dl>
<button style=""></button>

@code {
private int currentCount = 0;
@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<button",
stringsToType: [" ", "s", "t", "y"]);
}

private void IncrementCount()
[IdeFact]
public async Task CompletionCommit_HtmlTag()
{
currentCount++;
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<span

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "</PageTitle>",
stringsToType: ["{ENTER}", "{ENTER}", "<", "s", "p", "a"],
commitChar: null,
expectedSelectedItemLabel: "span");
}
}
""");

[IdeFact]
public async Task CompletionCommit_WithAngleBracket_HtmlTag()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<span></span>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "</PageTitle>",
stringsToType: ["{ENTER}", "{ENTER}", "<", "s", "p", "a"],
commitChar: '>',
"span");
}

[IdeFact]
public async Task CompletionCommit_CSharp()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int myCurrentCount = 0;

private void IncrementCount()
{
myCurrentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int myCurrentCount = 0;

private void IncrementCount()
{
myCurrentCount++;

myCurrentCount
}
}
""",
search: "myCurrentCount++;",
stringsToType: ["{ENTER}", "{ENTER}", "m", "y", "C", "u", "r"]);
}

private async Task VerifyTypeAndCommitCompletionAsync(string input, string output, string search, string[] stringsToType, char? commitChar = null, string? expectedSelectedItemLabel = null)
{
await TestServices.SolutionExplorer.AddFileAsync(
RazorProjectConstants.BlazorProjectName,
"Test.razor",
input,
open: true,
ControlledHangMitigatingCancellationToken);

await TestServices.Editor.WaitForComponentClassificationAsync(ControlledHangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync(search, charsOffset: 1, ControlledHangMitigatingCancellationToken);
foreach (var stringToType in stringsToType)
{
TestServices.Input.Send(stringToType);
}

if (expectedSelectedItemLabel is not null)
{
await CommitCompletionAndVerifyAsync(output, expectedSelectedItemLabel, commitChar);
}
else
{
await CommitCompletionAndVerifyAsync(output, commitChar);
}
}

[IdeFact]
Expand Down Expand Up @@ -225,12 +442,25 @@ public enum MyEnum
""");
}

private async Task CommitCompletionAndVerifyAsync(string expected)
private async Task CommitCompletionAndVerifyAsync(string expected, char? commitChar = null)
{
var session = await TestServices.Editor.WaitForCompletionSessionAsync(HangMitigatingCancellationToken);

Assert.NotNull(session);
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
if (commitChar.HasValue)
{
// Commit using the specified commit character
session.Commit(commitChar.Value, HangMitigatingCancellationToken);

// session.Commit call above commits as if the commit character was typed,
// but doesn't actually insert the character into the buffer.
// So we still need to insert the character into the buffer ourselves.
TestServices.Input.Send(commitChar.Value.ToString());
}
else
{
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
}

var textView = await TestServices.Editor.GetActiveTextViewAsync(HangMitigatingCancellationToken);
var text = textView.TextBuffer.CurrentSnapshot.GetText();
Expand All @@ -239,4 +469,41 @@ private async Task CommitCompletionAndVerifyAsync(string expected)
// tests allow for it as long as the content is correct
AssertEx.AssertEqualToleratingWhitespaceDifferences(expected, text);
}

private async Task CommitCompletionAndVerifyAsync(string expected, string expectedSelectedItemLabel, char? commitChar = null)
{
// Actually open completion UI and wait for it have selected item we are interested in
var session = await TestServices.Editor.OpenCompletionSessionAndWaitForItemAsync(TimeSpan.FromSeconds(10), expectedSelectedItemLabel, HangMitigatingCancellationToken);

Assert.NotNull(session);
if (commitChar is char commitCharValue)
{
// Commit using the specified commit character
session.Commit(commitCharValue, HangMitigatingCancellationToken);

// session.Commit call above commits as if the commit character was typed,
// but doesn't actually insert the character into the buffer.
// So we still need to insert the character into the buffer ourselves.
TestServices.Input.Send(commitCharValue.ToString());
}
else
{
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
}

var textView = await TestServices.Editor.GetActiveTextViewAsync(HangMitigatingCancellationToken);

var stopwatch = new Stopwatch();
string text;
while ((text = textView.TextBuffer.CurrentSnapshot.GetText()) != expected && stopwatch.ElapsedMilliseconds < EditorInProcess.DefaultCompletionWaitTimeMilliseconds)
{
// Text might get updated *after* completion by something like auto-insert, so wait for the desired text
await Task.Delay(100, HangMitigatingCancellationToken);
}

// Snippets may have slight whitespace differences due to line endings. These
// tests allow for it as long as the content is correct
Assert.Equal(expected, text);
}

}
Loading
Loading