Skip to content

Commit 3e18623

Browse files
committed
Share C# and Razor diagnostic tests
We don't support Html diagnostics in VS Code
1 parent 31ad5b1 commit 3e18623

File tree

4 files changed

+148
-87
lines changed

4 files changed

+148
-87
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("""
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+
}

0 commit comments

Comments
 (0)