Skip to content

Commit bb00268

Browse files
authored
Share hover and diagnostics tests withe VS Code (#12399)
Part of #11984 This just leaves formatting as the only tests that are still VS only, and those are harder because we call WebTools to get Html formatting edits.
2 parents 562875e + 3e18623 commit bb00268

File tree

7 files changed

+228
-112
lines changed

7 files changed

+228
-112
lines changed

src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Endpoints/DocumentPullDiagnosticsEndpoint.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,13 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
7777
ResultId = Guid.NewGuid().ToString()
7878
};
7979
}
80+
81+
internal TestAccessor GetTestAccessor() => new(this);
82+
83+
internal readonly struct TestAccessor(DocumentPullDiagnosticsEndpoint instance)
84+
{
85+
public Task<LspDiagnostic[]?> HandleRequestAsync(TextDocument razorDocument, CancellationToken cancellationToken)
86+
=> instance.GetDiagnosticsAsync(razorDocument, cancellationToken);
87+
}
8088
}
8189

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

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Collections.Generic;
54
using System.Linq;
65
using System.Threading.Tasks;
76
using Microsoft.AspNetCore.Razor.LanguageServer.Test;
@@ -12,68 +11,11 @@
1211
using Microsoft.VisualStudio.Razor.Settings;
1312
using Roslyn.Test.Utilities;
1413
using Xunit;
15-
using Xunit.Abstractions;
1614

1715
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
1816

19-
public class CohostDocumentPullDiagnosticsTest(ITestOutputHelper testOutputHelper) : CohostEndpointTestBase(testOutputHelper)
17+
public partial class CohostDocumentPullDiagnosticsTest
2018
{
21-
[Fact]
22-
public Task NoDiagnostics()
23-
=> VerifyDiagnosticsAsync("""
24-
<div></div>
25-
26-
@code
27-
{
28-
public void IJustMetYou()
29-
{
30-
}
31-
}
32-
""");
33-
34-
[Fact]
35-
public Task CSharp()
36-
=> VerifyDiagnosticsAsync("""
37-
<div></div>
38-
39-
@code
40-
{
41-
public void IJustMetYou()
42-
{
43-
{|CS0103:CallMeMaybe|}();
44-
}
45-
}
46-
""");
47-
48-
[Fact]
49-
public Task Razor()
50-
=> VerifyDiagnosticsAsync("""
51-
<div>
52-
53-
{|RZ10012:<NonExistentComponent />|}
54-
55-
</div>
56-
""");
57-
58-
[Fact]
59-
public Task CSharpAndRazor_MiscellaneousFile()
60-
=> VerifyDiagnosticsAsync("""
61-
<div>
62-
63-
{|RZ10012:<NonExistentComponent />|}
64-
65-
</div>
66-
67-
@code
68-
{
69-
public void IJustMetYou()
70-
{
71-
{|CS0103:CallMeMaybe|}();
72-
}
73-
}
74-
""",
75-
miscellaneousFile: true);
76-
7719
[Fact]
7820
public Task Html()
7921
{
@@ -449,34 +391,6 @@ Task Send() =>
449391
}]);
450392
}
451393

452-
[Fact]
453-
public Task CombinedAndNestedDiagnostics()
454-
=> VerifyDiagnosticsAsync("""
455-
@using System.Threading.Tasks;
456-
457-
<div>
458-
459-
{|RZ10012:<NonExistentComponent />|}
460-
461-
@code
462-
{
463-
public void IJustMetYou()
464-
{
465-
{|CS0103:CallMeMaybe|}();
466-
}
467-
}
468-
469-
<div>
470-
@{
471-
{|CS4033:await Task.{|CS1501:Delay|}()|};
472-
}
473-
474-
{|RZ9980:<p>|}
475-
</div>
476-
477-
</div>
478-
""");
479-
480394
[Fact]
481395
public Task TODOComments()
482396
=> VerifyDiagnosticsAsync("""

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,16 @@
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
77
using System.Linq;
8-
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis;
108
using Microsoft.CodeAnalysis.Classification;
119
using Microsoft.CodeAnalysis.Razor.Tooltip;
12-
using Microsoft.CodeAnalysis.Text;
1310
using Roslyn.Text.Adornments;
1411
using Xunit;
1512

1613
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
1714

1815
internal static class HoverAssertions
1916
{
20-
public static async Task VerifyRangeAsync(this Hover hover, TextSpan expected, TextDocument document)
21-
{
22-
var text = await document.GetTextAsync();
23-
Assert.NotNull(hover.Range);
24-
Assert.Equal(text.GetLinePositionSpan(expected), hover.Range.ToLinePositionSpan());
25-
}
26-
27-
public static void VerifyRawContent(this Hover hover, Action<object?> verifier)
17+
public static void VerifyContents(this LspHover hover, Action<object?> verifier)
2818
{
2919
var vsHover = Assert.IsType<VSInternalHover>(hover);
3020
verifier(vsHover.RawContent);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Razor.Test.Common;
7+
using Microsoft.CodeAnalysis.Razor.Telemetry;
8+
using Microsoft.CodeAnalysis.Text;
9+
using Roslyn.Test.Utilities;
10+
using Xunit;
11+
12+
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
13+
14+
public partial class CohostDocumentPullDiagnosticsTest
15+
{
16+
private async Task VerifyDiagnosticsAsync(TestCode input, VSInternalDiagnosticReport[]? htmlResponse = null, bool miscellaneousFile = false)
17+
{
18+
var document = CreateProjectAndRazorDocument(input.Text, miscellaneousFile: miscellaneousFile);
19+
var inputText = await document.GetTextAsync(DisposalToken);
20+
21+
var requestInvoker = new TestHtmlRequestInvoker([(VSInternalMethods.DocumentPullDiagnosticName, htmlResponse)]);
22+
23+
var endpoint = new DocumentPullDiagnosticsEndpoint(IncompatibleProjectService, RemoteServiceInvoker, requestInvoker, ClientCapabilitiesService, NoOpTelemetryReporter.Instance, LoggerFactory);
24+
25+
var result = await endpoint.GetTestAccessor().HandleRequestAsync(document, DisposalToken);
26+
27+
Assert.NotNull(result);
28+
29+
var markers = result.SelectMany(d =>
30+
new[] {
31+
(index: inputText.GetTextSpan(d.Range).Start, text: $"{{|{d.Code!.Value.Second}:"),
32+
(index: inputText.GetTextSpan(d.Range).End, text:"|}")
33+
});
34+
35+
var testOutput = input.Text;
36+
// Ordering by text last means start tags get sorted before end tags, for zero width ranges
37+
foreach (var (index, text) in markers.OrderByDescending(i => i.index).ThenByDescending(i => i.text))
38+
{
39+
testOutput = testOutput.Insert(index, text);
40+
}
41+
42+
AssertEx.EqualOrDiff(input.OriginalInput, testOutput);
43+
}
44+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
9+
10+
public partial class CohostDocumentPullDiagnosticsTest(ITestOutputHelper testOutputHelper) : CohostEndpointTestBase(testOutputHelper)
11+
{
12+
[Fact]
13+
public Task NoDiagnostics()
14+
=> VerifyDiagnosticsAsync("""
15+
<div></div>
16+
17+
@code
18+
{
19+
public void IJustMetYou()
20+
{
21+
}
22+
}
23+
""");
24+
25+
[Fact]
26+
public Task CSharp()
27+
=> VerifyDiagnosticsAsync("""
28+
<div></div>
29+
30+
@code
31+
{
32+
public void IJustMetYou()
33+
{
34+
{|CS0103:CallMeMaybe|}();
35+
}
36+
}
37+
""");
38+
39+
[Fact]
40+
public Task Razor()
41+
=> VerifyDiagnosticsAsync("""
42+
<div>
43+
44+
{|RZ10012:<NonExistentComponent />|}
45+
46+
</div>
47+
""");
48+
49+
[Fact]
50+
public Task CSharpAndRazor_MiscellaneousFile()
51+
=> VerifyDiagnosticsAsync("""
52+
<div>
53+
54+
{|RZ10012:<NonExistentComponent />|}
55+
56+
</div>
57+
58+
@code
59+
{
60+
public void IJustMetYou()
61+
{
62+
{|CS0103:CallMeMaybe|}();
63+
}
64+
}
65+
""",
66+
miscellaneousFile: true);
67+
68+
[Fact]
69+
public Task CombinedAndNestedDiagnostics()
70+
=> VerifyDiagnosticsAsync("""
71+
@using System.Threading.Tasks;
72+
73+
<div>
74+
75+
{|RZ10012:<NonExistentComponent />|}
76+
77+
@code
78+
{
79+
public void IJustMetYou()
80+
{
81+
{|CS0103:CallMeMaybe|}();
82+
}
83+
}
84+
85+
<div>
86+
@{
87+
{|CS4033:await Task.{|CS1501:Delay|}()|};
88+
}
89+
90+
{|RZ9980:<p>|}
91+
</div>
92+
93+
</div>
94+
""");
95+
}
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public async Task Razor()
3232

3333
await VerifyHoverAsync(code, async (hover, document) =>
3434
{
35-
await hover.VerifyRangeAsync(code.Span, document);
35+
await VerifyRangeAsync(hover, code.Span, document);
3636

37-
hover.VerifyRawContent(
37+
hover.VerifyContents(
3838
Container(
3939
Container(
4040
Image,
@@ -109,9 +109,9 @@ public async Task CSharp()
109109

110110
await VerifyHoverAsync(code, async (hover, document) =>
111111
{
112-
await hover.VerifyRangeAsync(code.Span, document);
112+
await VerifyRangeAsync(hover, code.Span, document);
113113

114-
hover.VerifyRawContent(
114+
hover.VerifyContents(
115115
Container(
116116
Container(
117117
Image,
@@ -139,9 +139,9 @@ public async Task ComponentAttribute()
139139

140140
await VerifyHoverAsync(code, async (hover, document) =>
141141
{
142-
await hover.VerifyRangeAsync(code.Span, document);
142+
await VerifyRangeAsync(hover, code.Span, document);
143143

144-
hover.VerifyRawContent(
144+
hover.VerifyContents(
145145
Container(
146146
Container(
147147
Image,
@@ -184,9 +184,9 @@ private void Foo()
184184

185185
await VerifyHoverAsync(code, async (hover, document) =>
186186
{
187-
await hover.VerifyRangeAsync(code.Span, document);
187+
await VerifyRangeAsync(hover, code.Span, document);
188188

189-
hover.VerifyRawContent(
189+
hover.VerifyContents(
190190
Container(
191191
Container(
192192
Image,
@@ -221,9 +221,9 @@ public async Task ComponentEndTag()
221221

222222
await VerifyHoverAsync(code, async (hover, document) =>
223223
{
224-
await hover.VerifyRangeAsync(code.Span, document);
224+
await VerifyRangeAsync(hover, code.Span, document);
225225

226-
hover.VerifyRawContent(
226+
hover.VerifyContents(
227227
Container(
228228
Container(
229229
Image,
@@ -258,9 +258,9 @@ public async Task ComponentEndTag_FullyQualified()
258258

259259
await VerifyHoverAsync(code, async (hover, document) =>
260260
{
261-
await hover.VerifyRangeAsync(code.Span, document);
261+
await VerifyRangeAsync(hover, code.Span, document);
262262

263-
hover.VerifyRawContent(
263+
hover.VerifyContents(
264264
Container(
265265
Container(
266266
Image,
@@ -295,9 +295,9 @@ public async Task ComponentEndTag_FullyQualified_Namespace()
295295

296296
await VerifyHoverAsync(code, async (hover, document) =>
297297
{
298-
await hover.VerifyRangeAsync(code.Span, document);
298+
await VerifyRangeAsync(hover, code.Span, document);
299299

300-
hover.VerifyRawContent(
300+
hover.VerifyContents(
301301
Container(
302302
Container(
303303
Image,
@@ -344,4 +344,11 @@ private async Task VerifyHoverAsync(TestCode input, Hover htmlResponse, Action<H
344344

345345
return await endpoint.GetTestAccessor().HandleRequestAsync(textDocumentPositionParams, document, DisposalToken);
346346
}
347+
348+
private static async Task VerifyRangeAsync(Hover hover, TextSpan expected, TextDocument document)
349+
{
350+
var text = await document.GetTextAsync();
351+
Assert.NotNull(hover.Range);
352+
Assert.Equal(text.GetLinePositionSpan(expected), hover.Range.ToLinePositionSpan());
353+
}
347354
}

0 commit comments

Comments
 (0)