Skip to content

Commit ae54aca

Browse files
authored
Small cleanup to completion tests (#12474)
1 parent c62d987 commit ae54aca

File tree

5 files changed

+99
-143
lines changed

5 files changed

+99
-143
lines changed

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Completion/DirectiveAttributeCompletionItemProviderBaseTest.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Razor.Language;
55
using Microsoft.AspNetCore.Razor.Language.IntegrationTests;
66
using Microsoft.AspNetCore.Razor.Language.Syntax;
7+
using Microsoft.AspNetCore.Razor.Test.Common;
78
using Microsoft.CodeAnalysis.Text;
89
using Xunit;
910
using Xunit.Abstractions;
@@ -20,7 +21,7 @@ public class DirectiveAttributeCompletionItemProviderBaseTest(ITestOutputHelper
2021
public void TryGetAttributeInfo_NonAttribute_ReturnsFalse()
2122
{
2223
// Arrange
23-
var node = GetNodeAt("@DateTime.Now", 4);
24+
var node = GetNode($"@Dat$$eTime.Now");
2425

2526
// Act
2627
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out _, out _, out _, out _, out _);
@@ -33,7 +34,7 @@ public void TryGetAttributeInfo_NonAttribute_ReturnsFalse()
3334
public void TryGetAttributeInfo_EmptyAttribute_ReturnsFalse()
3435
{
3536
// Arrange
36-
var node = GetNodeAt("<p >", 3);
37+
var node = GetNode("<p $$ >");
3738

3839
// Act
3940
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out _, out _, out _, out _, out _);
@@ -46,7 +47,7 @@ public void TryGetAttributeInfo_EmptyAttribute_ReturnsFalse()
4647
public void TryGetAttributeInfo_PartialAttribute_ReturnsTrue()
4748
{
4849
// Arrange
49-
var node = GetNodeAt("<p bin>", 4);
50+
var node = GetNode("<p b$$in>");
5051

5152
// Act
5253
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out _);
@@ -63,7 +64,7 @@ public void TryGetAttributeInfo_PartialAttribute_ReturnsTrue()
6364
public void TryGetAttributeInfo_PartialTransitionedAttribute_ReturnsTrue()
6465
{
6566
// Arrange
66-
var node = GetNodeAt("<p @>", 4);
67+
var node = GetNode("<p @$$>");
6768

6869
// Act
6970
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out _);
@@ -80,7 +81,7 @@ public void TryGetAttributeInfo_PartialTransitionedAttribute_ReturnsTrue()
8081
public void TryGetAttributeInfo_FullAttribute_ReturnsTrue()
8182
{
8283
// Arrange
83-
var node = GetNodeAt("<p foo=\"anything\">", 4);
84+
var node = GetNode("<p f$$oo=\"anything\">");
8485

8586
// Act
8687
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out _);
@@ -96,7 +97,7 @@ public void TryGetAttributeInfo_FullAttribute_ReturnsTrue()
9697
[Fact]
9798
public void TryGetAttributeInfo_PartialDirectiveAttribute_ReturnsTrue()
9899
{
99-
var node = GetNodeAt("<input type=\"text\" @bind />", 22);
100+
var node = GetNode($"<input type=\"text\" @bi$$nd />");
100101

101102
// Act
102103
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out _);
@@ -112,10 +113,10 @@ public void TryGetAttributeInfo_PartialDirectiveAttribute_ReturnsTrue()
112113
[Fact]
113114
public void TryGetAttributeInfo_DirectiveAttribute_ReturnsTrue()
114115
{
115-
var node = GetNodeAt(@"<input type=""text"" @bind=""@CurrentDate"" />
116+
var node = GetNode(@"<input type=""text"" @bi$$nd=""@CurrentDate"" />
116117
@code {
117118
public DateTime CurrentDate { get; set; } = DateTime.Now;
118-
}", 22);
119+
}");
119120

120121
// Act
121122
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out _);
@@ -131,10 +132,10 @@ public void TryGetAttributeInfo_DirectiveAttribute_ReturnsTrue()
131132
[Fact]
132133
public void TryGetAttributeInfo_DirectiveAttributeWithParameter_ReturnsTrue()
133134
{
134-
var node = GetNodeAt(@"<input type=""text"" @bind:format=""MM/dd/yyyy"" @bind=""@CurrentDate"" />
135+
var node = GetNode(@"<input type=""text"" @bi$$nd:format=""MM/dd/yyyy"" @bind=""@CurrentDate"" />
135136
@code {
136137
public DateTime CurrentDate { get; set; } = DateTime.Now;
137-
}", 22);
138+
}");
138139

139140
// Act
140141
var result = DirectiveAttributeCompletionItemProviderBase.TryGetAttributeInfo(node, out var prefixLocation, out var name, out var nameLocation, out var parameterName, out var parameterNameLocation);
@@ -152,7 +153,7 @@ public void TryGetAttributeInfo_DirectiveAttributeWithParameter_ReturnsTrue()
152153
public void TryGetElementInfo_MarkupTagParent()
153154
{
154155
// Arrange
155-
var node = GetNodeAt("<p class='hello @DateTime.Now'>", 2);
156+
var node = GetNode("<p$$ class='hello @DateTime.Now'>");
156157

157158
// Act
158159
var result = DirectiveAttributeCompletionItemProviderBase.TryGetElementInfo(node, out var tagName, out var attributes);
@@ -168,10 +169,10 @@ public void TryGetElementInfo_MarkupTagParent()
168169
public void TryGetElementInfo_TagHelperParent()
169170
{
170171
// Arrange
171-
var node = GetNodeAt(@"<input type=""text"" @bind=""@CurrentDate"" />
172+
var node = GetNode(@"<i$$nput type=""text"" @bind=""@CurrentDate"" />
172173
@code {
173174
public DateTime CurrentDate { get; set; } = DateTime.Now;
174-
}", 2);
175+
}");
175176

176177
// Act
177178
var result = DirectiveAttributeCompletionItemProviderBase.TryGetElementInfo(node, out var tagName, out var attributes);
@@ -186,7 +187,7 @@ public void TryGetElementInfo_TagHelperParent()
186187
public void TryGetElementInfo_NoAttributes_ReturnsEmptyAttributeCollection()
187188
{
188189
// Arrange
189-
var node = GetNodeAt("<p>", 2);
190+
var node = GetNode("<p$$>");
190191

191192
// Act
192193
var result = DirectiveAttributeCompletionItemProviderBase.TryGetElementInfo(node, out _, out var attributes);
@@ -200,7 +201,7 @@ public void TryGetElementInfo_NoAttributes_ReturnsEmptyAttributeCollection()
200201
public void TryGetElementInfo_SingleAttribute_ReturnsAttributeName()
201202
{
202203
// Arrange
203-
var node = GetNodeAt("<p class='hello @DateTime.Now'>", 2);
204+
var node = GetNode("<p$$ class='hello @DateTime.Now'>");
204205

205206
// Act
206207
var result = DirectiveAttributeCompletionItemProviderBase.TryGetElementInfo(node, out _, out var attributes);
@@ -215,10 +216,10 @@ public void TryGetElementInfo_SingleAttribute_ReturnsAttributeName()
215216
public void TryGetElementInfo_MixedAttributes_ReturnsStringifiedAttributesResult()
216217
{
217218
// Arrange
218-
var node = GetNodeAt(@"<input type=""text"" @bind:format=""MM/dd/yyyy"" something @bind=""@CurrentDate"" />
219+
var node = GetNode(@"<i$$nput type=""text"" @bind:format=""MM/dd/yyyy"" something @bind=""@CurrentDate"" />
219220
@code {
220221
public DateTime CurrentDate { get; set; } = DateTime.Now;
221-
}", 2);
222+
}");
222223

223224
// Act
224225
var result = DirectiveAttributeCompletionItemProviderBase.TryGetElementInfo(node, out _, out var attributes);
@@ -228,9 +229,10 @@ public void TryGetElementInfo_MixedAttributes_ReturnsStringifiedAttributesResult
228229
Assert.Equal<string>(["type", "@bind:format", "something", "@bind"], attributes);
229230
}
230231

231-
private RazorSyntaxNode GetNodeAt(string content, int index)
232+
private RazorSyntaxNode GetNode(TestCode testCode)
232233
{
233-
var result = CompileToCSharp(content, throwOnFailure: false);
234+
var index = testCode.Position;
235+
var result = CompileToCSharp(testCode.Text, throwOnFailure: false);
234236
var root = result.CodeDocument.GetRequiredSyntaxRoot();
235237
var owner = root.FindInnermostNode(index, includeWhitespace: true, walkMarkersBack: true);
236238
owner = AbstractRazorCompletionFactsService.AdjustSyntaxNodeForWordBoundary(owner, index);

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Completion/DirectiveAttributeTransitionCompletionItemProviderTest.cs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111

1212
namespace Microsoft.CodeAnalysis.Razor.Completion;
1313

14-
public class DirectiveAttributeTransitionCompletionItemProviderTest : ToolingTestBase
14+
public class DirectiveAttributeTransitionCompletionItemProviderTest(ITestOutputHelper testOutput) : ToolingTestBase(testOutput)
1515
{
16-
private readonly TagHelperDocumentContext _tagHelperDocumentContext;
17-
private readonly DirectiveAttributeTransitionCompletionItemProvider _provider;
18-
19-
public DirectiveAttributeTransitionCompletionItemProviderTest(ITestOutputHelper testOutput)
20-
: base(testOutput)
21-
{
22-
_tagHelperDocumentContext = TagHelperDocumentContext.Create(prefix: string.Empty, tagHelpers: []);
23-
_provider = new DirectiveAttributeTransitionCompletionItemProvider(TestLanguageServerFeatureOptions.Instance);
24-
}
16+
private readonly TagHelperDocumentContext _tagHelperDocumentContext = TagHelperDocumentContext.Create(prefix: string.Empty, tagHelpers: []);
17+
private readonly DirectiveAttributeTransitionCompletionItemProvider _provider = new DirectiveAttributeTransitionCompletionItemProvider(TestLanguageServerFeatureOptions.Instance);
2518

2619
[Fact]
2720
public void IsValidCompletionPoint_AtPrefixLeadingEdge_ReturnsFalse()
@@ -129,7 +122,7 @@ public void IsValidCompletionPoint_OutsideOfNameAndPrefix_ReturnsFalse()
129122
public void GetCompletionItems_AttributeAreaInNonComponentFile_ReturnsEmptyList()
130123
{
131124
// Arrange
132-
var context = CreateContext(absoluteIndex: 7, "<input />", RazorFileKind.Legacy);
125+
var context = CreateContext("<input $$ />", RazorFileKind.Legacy);
133126

134127
// Act
135128
var result = _provider.GetCompletionItems(context);
@@ -142,7 +135,7 @@ public void GetCompletionItems_AttributeAreaInNonComponentFile_ReturnsEmptyList(
142135
public void GetCompletionItems_NonAttribute_ReturnsEmptyList()
143136
{
144137
// Arrange
145-
var context = CreateContext(absoluteIndex: 2, "<input />");
138+
var context = CreateContext("<i$$nput />");
146139

147140
// Act
148141
var result = _provider.GetCompletionItems(context);
@@ -155,7 +148,7 @@ public void GetCompletionItems_NonAttribute_ReturnsEmptyList()
155148
public void GetCompletionItems_ExistingAttribute_ReturnsEmptyList()
156149
{
157150
// Arrange
158-
var context = CreateContext(absoluteIndex: 8, "<input @ />");
151+
var context = CreateContext("<input @$$ />");
159152

160153
// Act
161154
var result = _provider.GetCompletionItems(context);
@@ -168,8 +161,8 @@ public void GetCompletionItems_ExistingAttribute_ReturnsEmptyList()
168161
public void GetCompletionItems_InbetweenSelfClosingEnd_ReturnsEmptyList()
169162
{
170163
// Arrange
171-
var context = CreateContext(absoluteIndex: 8, """
172-
<input /
164+
var context = CreateContext("""
165+
<input /$$
173166
174167
""");
175168

@@ -184,7 +177,7 @@ public void GetCompletionItems_InbetweenSelfClosingEnd_ReturnsEmptyList()
184177
public void GetCompletionItems_AttributeAreaInComponentFile_ReturnsTransitionCompletionItem()
185178
{
186179
// Arrange
187-
var context = CreateContext(absoluteIndex: 7, "<input />");
180+
var context = CreateContext("<input $$ />");
188181

189182
// Act
190183
var result = _provider.GetCompletionItems(context);
@@ -198,7 +191,7 @@ public void GetCompletionItems_AttributeAreaInComponentFile_ReturnsTransitionCom
198191
public void GetCompletionItems_AttributeAreaEndOfSelfClosingTag_ReturnsTransitionCompletionItem()
199192
{
200193
// Arrange
201-
var context = CreateContext(absoluteIndex: 7, "<input />");
194+
var context = CreateContext("<input $$/>");
202195

203196
// Act
204197
var result = _provider.GetCompletionItems(context);
@@ -212,7 +205,7 @@ public void GetCompletionItems_AttributeAreaEndOfSelfClosingTag_ReturnsTransitio
212205
public void GetCompletionItems_AttributeAreaEndOfOpeningTag_ReturnsTransitionCompletionItem()
213206
{
214207
// Arrange
215-
var context = CreateContext(absoluteIndex: 7, "<input ></input>");
208+
var context = CreateContext("<input $$></input>");
216209

217210
// Act
218211
var result = _provider.GetCompletionItems(context);
@@ -226,7 +219,7 @@ public void GetCompletionItems_AttributeAreaEndOfOpeningTag_ReturnsTransitionCom
226219
public void GetCompletionItems_ExistingAttribute_LeadingEdge_ReturnsEmptyList()
227220
{
228221
// Arrange
229-
var context = CreateContext(absoluteIndex: 7, "<input src=\"xyz\" />");
222+
var context = CreateContext("<input $$src=\"xyz\" />");
230223

231224
// Act
232225
var result = _provider.GetCompletionItems(context);
@@ -239,7 +232,7 @@ public void GetCompletionItems_ExistingAttribute_LeadingEdge_ReturnsEmptyList()
239232
public void GetCompletionItems_ExistingAttribute_TrailingEdge_ReturnsEmptyList()
240233
{
241234
// Arrange
242-
var context = CreateContext(absoluteIndex: 16, "<input src=\"xyz\" />");
235+
var context = CreateContext("<input src=\"xyz\"$$ />");
243236

244237
// Act
245238
var result = _provider.GetCompletionItems(context);
@@ -252,7 +245,7 @@ public void GetCompletionItems_ExistingAttribute_TrailingEdge_ReturnsEmptyList()
252245
public void GetCompletionItems_ExistingAttribute_TrailingEdgeOnSpace_ReturnsEmptyList()
253246
{
254247
// Arrange
255-
var context = CreateContext(absoluteIndex: 16, "<input src=\"xyz\" />");
248+
var context = CreateContext("<input src=\"xyz\"$$ />");
256249

257250
// Act
258251
var result = _provider.GetCompletionItems(context);
@@ -265,7 +258,7 @@ public void GetCompletionItems_ExistingAttribute_TrailingEdgeOnSpace_ReturnsEmpt
265258
public void GetCompletionItems_ExistingAttribute_Partial_ReturnsEmptyList()
266259
{
267260
// Arrange
268-
var context = CreateContext(absoluteIndex: 9, "<svg xml: ></svg>");
261+
var context = CreateContext("<svg xml:$$ ></svg>");
269262

270263
// Act
271264
var result = _provider.GetCompletionItems(context);
@@ -278,7 +271,7 @@ public void GetCompletionItems_ExistingAttribute_Partial_ReturnsEmptyList()
278271
public void GetCompletionItems_AttributeAreaInIncompleteAttributeTransition_ReturnsTransitionCompletionItem()
279272
{
280273
// Arrange
281-
var context = CreateContext(absoluteIndex: 7, "<input @{");
274+
var context = CreateContext("<input $$ @{");
282275

283276
// Act
284277
var result = _provider.GetCompletionItems(context);
@@ -292,7 +285,7 @@ public void GetCompletionItems_AttributeAreaInIncompleteAttributeTransition_Retu
292285
public void GetCompletionItems_AttributeAreaInIncompleteComponent_ReturnsTransitionCompletionItem()
293286
{
294287
// Arrange
295-
var context = CreateContext(absoluteIndex: 5, "<svg xml:base=\"d\"></svg>");
288+
var context = CreateContext("<svg $$ xml:base=\"d\"></svg>");
296289

297290
// Act
298291
var result = _provider.GetCompletionItems(context);
@@ -308,7 +301,7 @@ public void GetCompletionItems_AttributeAreaInIncompleteComponent_ReturnsTransit
308301
public void GetCompletionItems_WithAvoidExplicitCommitOption_ReturnsAppropriateCommitCharacters(bool supportsSoftSelection)
309302
{
310303
// Arrange
311-
var context = CreateContext(absoluteIndex: 7, "<input />");
304+
var context = CreateContext("<input $$ />");
312305
var provider = new DirectiveAttributeTransitionCompletionItemProvider(new TestLanguageServerFeatureOptions(supportsSoftSelectionInCompletion: supportsSoftSelection));
313306

314307
// Act
@@ -327,11 +320,11 @@ public void GetCompletionItems_WithAvoidExplicitCommitOption_ReturnsAppropriateC
327320
}
328321
}
329322

330-
private static RazorCodeDocument GetCodeDocument(string text, RazorFileKind? fileKind = null)
323+
private static RazorCodeDocument GetCodeDocument(TestCode text, RazorFileKind? fileKind = null)
331324
{
332325
var fileKindValue = fileKind ?? RazorFileKind.Component;
333326

334-
var sourceDocument = TestRazorSourceDocument.Create(text);
327+
var sourceDocument = TestRazorSourceDocument.Create(text.Text);
335328
var projectEngine = RazorProjectEngine.Create(builder =>
336329
{
337330
builder.ConfigureParserOptions(builder =>
@@ -343,9 +336,10 @@ private static RazorCodeDocument GetCodeDocument(string text, RazorFileKind? fil
343336
return projectEngine.Process(sourceDocument, fileKindValue, importSources: default, tagHelpers: []);
344337
}
345338

346-
private RazorCompletionContext CreateContext(int absoluteIndex, string documentContent, RazorFileKind? fileKind = null)
339+
private RazorCompletionContext CreateContext(TestCode text, RazorFileKind? fileKind = null)
347340
{
348-
var codeDocument = GetCodeDocument(documentContent, fileKind);
341+
var absoluteIndex = text.Position;
342+
var codeDocument = GetCodeDocument(text.Text, fileKind);
349343
var syntaxTree = codeDocument.GetRequiredSyntaxTree();
350344
var owner = syntaxTree.Root.FindInnermostNode(absoluteIndex, includeWhitespace: true, walkMarkersBack: true);
351345
owner = AbstractRazorCompletionFactsService.AdjustSyntaxNodeForWordBoundary(owner, absoluteIndex);

0 commit comments

Comments
 (0)