Skip to content

Commit a28edaf

Browse files
Add cohosting Hover test for component attribute
Even though component attributes look like HTML they map to generated C#. In this case, Hover prefers C# over HTML and requests hover data from Roslyn. This adds a test for that scenario.
1 parent 6e796fe commit a28edaf

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostHoverEndpointTest.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ await VerifyHoverAsync(code, async (hover, document) =>
4040
Container(
4141
Container(
4242
Image,
43-
ClassifiedText(
43+
ClassifiedText( // Microsoft.AspNetCore.Components.Web.PageTitle
4444
Text("Microsoft"),
4545
Punctuation("."),
4646
Text("AspNetCore"),
@@ -67,6 +67,7 @@ public async Task Html()
6767
}
6868
""";
6969

70+
// This simply verifies that Hover will call into HTML.
7071
var htmlResponse = new VSInternalHover();
7172

7273
await VerifyHoverAsync(code, htmlResponse, h => Assert.Same(htmlResponse, h));
@@ -94,7 +95,7 @@ await VerifyHoverAsync(code, async (hover, document) =>
9495
Container(
9596
Container(
9697
Image,
97-
ClassifiedText(
98+
ClassifiedText( // (local variable) string myVariable
9899
Punctuation("("),
99100
Text("local variable"),
100101
Punctuation(")"),
@@ -105,6 +106,45 @@ await VerifyHoverAsync(code, async (hover, document) =>
105106
});
106107
}
107108

109+
[Fact]
110+
public async Task ComponentAttribute()
111+
{
112+
// Component attributes are within HTML but actually map to C#.
113+
// In this situation, Hover prefers treating the position as C# and calls
114+
// Roslyn rather than HTML.
115+
116+
TestCode code = """
117+
<EditForm [|Form$$Name|]="Hello" />
118+
""";
119+
120+
await VerifyHoverAsync(code, async (hover, document) =>
121+
{
122+
await hover.VerifyRangeAsync(code.Span, document);
123+
124+
hover.VerifyRawContent(
125+
Container(
126+
Container(
127+
Image,
128+
ClassifiedText( // string? EditForm.FormName { get; set; }
129+
Keyword("string"),
130+
Punctuation("?"),
131+
WhiteSpace(" "),
132+
ClassName("EditForm"),
133+
Punctuation("."),
134+
PropertyName("FormName"),
135+
WhiteSpace(" "),
136+
Punctuation("{"),
137+
WhiteSpace(" "),
138+
Keyword("get"),
139+
Punctuation(";"),
140+
WhiteSpace(" "),
141+
Keyword("set"),
142+
Punctuation(";"),
143+
WhiteSpace(" "),
144+
Punctuation("}")))));
145+
});
146+
}
147+
108148
private async Task VerifyHoverAsync(TestCode input, Func<RoslynHover, TextDocument, Task> verifyHover)
109149
{
110150
var document = await CreateProjectAndRazorDocumentAsync(input.Text);

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/HoverAssertions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ public static Action<ClassifiedTextRun> Run(string text, string? classificationT
7979
Assert.Equal(text, run.Text);
8080
};
8181

82+
public static Action<ClassifiedTextRun> ClassName (string text)
83+
=> Run(text, ClassificationTypeNames.ClassName);
84+
8285
public static Action<ClassifiedTextRun> Keyword(string text)
8386
=> Run(text, ClassificationTypeNames.Keyword);
8487

8588
public static Action<ClassifiedTextRun> LocalName(string text)
8689
=> Run(text, ClassificationTypeNames.LocalName);
8790

91+
public static Action<ClassifiedTextRun> PropertyName(string text)
92+
=> Run(text, ClassificationTypeNames.PropertyName);
93+
8894
public static Action<ClassifiedTextRun> Punctuation(string text)
8995
=> Run(text, ClassificationTypeNames.Punctuation);
9096

0 commit comments

Comments
 (0)