Skip to content

Commit

Permalink
Force tests to express all of their expected inputs, and validate tha…
Browse files Browse the repository at this point in the history
…t no false positives exist from delegated items or snippets
  • Loading branch information
davidwengier committed Jan 2, 2025
1 parent 1fb550c commit 5305512
Showing 1 changed file with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ The end.
TriggerCharacter = "<",
TriggerKind = RoslynCompletionTriggerKind.TriggerCharacter
},
expectedItemLabels: ["text", "EditForm", "InputDate"]);
expectedItemLabels: ["text", "EditForm", "InputDate", "div"],
delegatedItemLabels: ["div"]);
}

[Fact]
Expand Down Expand Up @@ -245,7 +246,8 @@ The end.
TriggerCharacter = "<",
TriggerKind = RoslynCompletionTriggerKind.TriggerCharacter
},
expectedItemLabels: ["LayoutView", "EditForm", "ValidationMessage"]);
expectedItemLabels: ["LayoutView", "EditForm", "ValidationMessage", "div"],
delegatedItemLabels: ["div"]);
}

[Fact]
Expand Down Expand Up @@ -330,6 +332,7 @@ The end.
TriggerKind = RoslynCompletionTriggerKind.Invoked
},
expectedItemLabels: ["snippet1", "snippet2"],
delegatedItemLabels: [],
snippetLabels: ["snippet1", "snippet2"]);
}

Expand Down Expand Up @@ -479,7 +482,8 @@ The end.
TriggerCharacter = " ",
TriggerKind = RoslynCompletionTriggerKind.TriggerCharacter
},
expectedItemLabels: ["FormName", "OnValidSubmit", "@..."],
expectedItemLabels: ["FormName", "OnValidSubmit", "@...", "style"],
delegatedItemLabels: ["style"],
autoInsertAttributeQuotes: false);
}

Expand All @@ -490,6 +494,7 @@ private async Task VerifyCompletionListAsync(
string[]? unexpectedItemLabels = null,
string[]? delegatedItemLabels = null,
string[]? delegatedItemCommitCharacters = null,
string[]? snippetLabels = null,
bool autoInsertAttributeQuotes = true,
bool commitElementsWithSpace = true,
bool fuse = false)
Expand All @@ -502,31 +507,30 @@ private async Task VerifyCompletionListAsync(
var clientSettingsManager = new ClientSettingsManager([], null, null);
clientSettingsManager.Update(ClientAdvancedSettings.Default with { AutoInsertAttributeQuotes = autoInsertAttributeQuotes, CommitElementsWithSpace = commitElementsWithSpace });

VSInternalCompletionList? response = null;
if (delegatedItemLabels is not null)
const string InvalidLabel = "_INVALID_";

// If delegatedItemLabels wasn't supplied, supply our own to ensure delegation isn't happening and causing a false positive result
delegatedItemLabels ??= [InvalidLabel];
var response = new VSInternalCompletionList()
{
response = new VSInternalCompletionList()
Items = delegatedItemLabels.Select((label) => new VSInternalCompletionItem()
{
Items = delegatedItemLabels.Select((label) => new VSInternalCompletionItem()
{
Label = label,
CommitCharacters = delegatedItemCommitCharacters,
// If test specifies not to commit with space, set kind to element since we remove space
// commit from elements only. Otherwise test doesn't care, so set to None
Kind = !commitElementsWithSpace ? CompletionItemKind.Element : CompletionItemKind.None,
}).ToArray(),
IsIncomplete = true
};
}
Label = label,
CommitCharacters = delegatedItemCommitCharacters,
// If test specifies not to commit with space, set kind to element since we remove space
// commit from elements only. Otherwise test doesn't care, so set to None
Kind = !commitElementsWithSpace ? CompletionItemKind.Element : CompletionItemKind.None,
}).ToArray(),
IsIncomplete = true
};

var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentCompletionName, response)]);

var snippetCompletionItemProvider = new SnippetCompletionItemProvider(new SnippetCache());
if (snippetLabels is not null)
{
var snippetInfos = snippetLabels.Select(label => new SnippetInfo(label, label, label, string.Empty, SnippetLanguage.Html)).ToImmutableArray();
snippetCompletionItemProvider.SnippetCache.Update(SnippetLanguage.Html, snippetInfos);
}
// If snippetLabels wasn't supplied, supply our own to ensure snippets aren't being requested and causing a false positive result
snippetLabels ??= [InvalidLabel];
var snippetInfos = snippetLabels.Select(label => new SnippetInfo(label, label, label, string.Empty, SnippetLanguage.Html)).ToImmutableArray();
snippetCompletionItemProvider.SnippetCache.Update(SnippetLanguage.Html, snippetInfos);

var endpoint = new CohostDocumentCompletionEndpoint(
RemoteServiceInvoker,
Expand All @@ -552,6 +556,9 @@ private async Task VerifyCompletionListAsync(

using var _ = HashSetPool<string>.GetPooledObject(out var labelSet);
labelSet.AddRange(result.Items.Select((item) => item.Label));

Assert.DoesNotContain(InvalidLabel, labelSet);

foreach (var expectedItemLabel in expectedItemLabels)
{
Assert.Contains(expectedItemLabel, labelSet);
Expand Down

0 comments on commit 5305512

Please sign in to comment.