Skip to content

Commit 6d8018b

Browse files
committed
Tests
1 parent b275917 commit 6d8018b

File tree

4 files changed

+159
-14
lines changed

4 files changed

+159
-14
lines changed

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Runtime.CompilerServices;
76
using System.Threading;
87
using System.Threading.Tasks;
@@ -350,17 +349,4 @@ private class RemoteServiceInvoker(TextDocument document, Func<Task>? generateTa
350349
return (TResult)(object)(await solution.GetAdditionalDocument(_documentId).AssumeNotNull().GetTextAsync(cancellationToken)).ToString();
351350
}
352351
}
353-
354-
private class TestHtmlDocumentPublisher : IHtmlDocumentPublisher
355-
{
356-
private readonly List<(TextDocument, string, ChecksumWrapper)> _publishes = [];
357-
358-
public List<(TextDocument Document, string Text, ChecksumWrapper Checksum)> Publishes => _publishes;
359-
360-
public Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken)
361-
{
362-
_publishes.Add((document, htmlText, synchronizationResult.Checksum));
363-
return Task.CompletedTask;
364-
}
365-
}
366352
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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;
5+
using System.Runtime.CompilerServices;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Razor;
9+
using Microsoft.AspNetCore.Razor.Test.Common.Editor;
10+
using Microsoft.AspNetCore.Razor.Test.Common.VisualStudio;
11+
using Microsoft.CodeAnalysis;
12+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
13+
using Microsoft.CodeAnalysis.Razor;
14+
using Microsoft.CodeAnalysis.Razor.Remote;
15+
using Microsoft.CodeAnalysis.Razor.Telemetry;
16+
using Microsoft.CodeAnalysis.Text;
17+
using Xunit;
18+
using Xunit.Abstractions;
19+
20+
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
21+
22+
public class HtmlRequestInvokerTest(ITestOutputHelper testOutput) : VisualStudioWorkspaceTestBase(testOutput)
23+
{
24+
private DocumentId? _documentId;
25+
26+
protected override void ConfigureWorkspace(AdhocWorkspace workspace)
27+
{
28+
var project = workspace.CurrentSolution.AddProject("Project", "Project.dll", LanguageNames.CSharp);
29+
var document = project.AddAdditionalDocument("File.razor", SourceText.From("<div></div>"), filePath: "file://File.razor");
30+
_documentId = document.Id;
31+
32+
Assert.True(workspace.TryApplyChanges(document.Project.Solution));
33+
}
34+
35+
[Fact]
36+
public async Task DiagnosticsRequest_UpdatesUri()
37+
{
38+
var document = Workspace.CurrentSolution.GetAdditionalDocument(_documentId).AssumeNotNull();
39+
40+
var htmlDocumentUri = new Uri("file://File.razor.html", UriKind.Absolute);
41+
42+
var htmlTextSnapshot = new StringTextSnapshot("");
43+
var htmlTextBuffer = new TestTextBuffer(htmlTextSnapshot);
44+
var checksum = await document.GetChecksumAsync(DisposalToken);
45+
var requestInvoker = new TestLSPRequestInvoker((VSInternalMethods.DocumentPullDiagnosticName, null));
46+
var lspDocumentManager = new TestDocumentManager();
47+
var htmlVirtualDocument = new HtmlVirtualDocumentSnapshot(htmlDocumentUri, htmlTextBuffer.CurrentSnapshot, hostDocumentSyncVersion: 1, state: checksum);
48+
var documentSnapshot = new TestLSPDocumentSnapshot(document.CreateUri(), version: (int)(htmlVirtualDocument.HostDocumentSyncVersion!.Value + 1), htmlVirtualDocument);
49+
lspDocumentManager.AddDocument(documentSnapshot.Uri, documentSnapshot);
50+
51+
var publisher = new TestHtmlDocumentPublisher();
52+
var remoteServiceInvoker = new RemoteServiceInvoker();
53+
var htmlDocumentSynchronizer = new HtmlDocumentSynchronizer(remoteServiceInvoker, publisher, LoggerFactory);
54+
var invoker = new HtmlRequestInvoker(requestInvoker, lspDocumentManager, htmlDocumentSynchronizer, NoOpTelemetryReporter.Instance, LoggerFactory);
55+
56+
var validated = false;
57+
requestInvoker.RequestAction = request =>
58+
{
59+
validated = true;
60+
var diagnosticParams = Assert.IsType<VSInternalDiagnosticParams>(request);
61+
Assert.Equal(htmlDocumentUri, diagnosticParams.TextDocument!.DocumentUri.GetRequiredParsedUri());
62+
};
63+
64+
var diagnosticRequest = new VSInternalDiagnosticParams
65+
{
66+
TextDocument = new TextDocumentIdentifier { DocumentUri = document.CreateDocumentUri() }
67+
};
68+
69+
_ = await invoker.MakeHtmlLspRequestAsync<VSInternalDiagnosticParams, object>(
70+
document,
71+
VSInternalMethods.DocumentPullDiagnosticName,
72+
diagnosticRequest,
73+
DisposalToken);
74+
75+
Assert.Equal(document.CreateDocumentUri(), diagnosticRequest.TextDocument!.DocumentUri);
76+
77+
Assert.True(validated);
78+
}
79+
80+
[Fact]
81+
public async Task ITextDocumentParamsRequest_UpdatesUri()
82+
{
83+
var document = Workspace.CurrentSolution.GetAdditionalDocument(_documentId).AssumeNotNull();
84+
85+
var htmlDocumentUri = new Uri("file://File.razor.html", UriKind.Absolute);
86+
87+
var htmlTextSnapshot = new StringTextSnapshot("");
88+
var htmlTextBuffer = new TestTextBuffer(htmlTextSnapshot);
89+
var checksum = await document.GetChecksumAsync(DisposalToken);
90+
var requestInvoker = new TestLSPRequestInvoker((Methods.TextDocumentHoverName, null));
91+
var lspDocumentManager = new TestDocumentManager();
92+
var htmlVirtualDocument = new HtmlVirtualDocumentSnapshot(htmlDocumentUri, htmlTextBuffer.CurrentSnapshot, hostDocumentSyncVersion: 1, state: checksum);
93+
var documentSnapshot = new TestLSPDocumentSnapshot(document.CreateUri(), version: (int)(htmlVirtualDocument.HostDocumentSyncVersion!.Value + 1), htmlVirtualDocument);
94+
lspDocumentManager.AddDocument(documentSnapshot.Uri, documentSnapshot);
95+
96+
var publisher = new TestHtmlDocumentPublisher();
97+
var remoteServiceInvoker = new RemoteServiceInvoker();
98+
var htmlDocumentSynchronizer = new HtmlDocumentSynchronizer(remoteServiceInvoker, publisher, LoggerFactory);
99+
var invoker = new HtmlRequestInvoker(requestInvoker, lspDocumentManager, htmlDocumentSynchronizer, NoOpTelemetryReporter.Instance, LoggerFactory);
100+
101+
var validated = false;
102+
requestInvoker.RequestAction = request =>
103+
{
104+
validated = true;
105+
var diagnosticParams = Assert.IsAssignableFrom<ITextDocumentParams>(request);
106+
Assert.Equal(htmlDocumentUri, diagnosticParams.TextDocument!.DocumentUri.GetRequiredParsedUri());
107+
};
108+
109+
var diagnosticRequest = new HoverParams
110+
{
111+
TextDocument = new TextDocumentIdentifier { DocumentUri = document.CreateDocumentUri() }
112+
};
113+
114+
_ = await invoker.MakeHtmlLspRequestAsync<HoverParams, object>(
115+
document,
116+
Methods.TextDocumentHoverName,
117+
diagnosticRequest,
118+
DisposalToken);
119+
120+
Assert.Equal(document.CreateDocumentUri(), diagnosticRequest.TextDocument!.DocumentUri);
121+
122+
Assert.True(validated);
123+
}
124+
125+
private class RemoteServiceInvoker : IRemoteServiceInvoker
126+
{
127+
public ValueTask<TResult?> TryInvokeAsync<TService, TResult>(Solution solution, Func<TService, RazorPinnedSolutionInfoWrapper, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken, [CallerFilePath] string? callerFilePath = null, [CallerMemberName] string? callerMemberName = null) where TService : class
128+
{
129+
return new((TResult?)(object)"");
130+
}
131+
}
132+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
9+
10+
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
11+
12+
internal sealed class TestHtmlDocumentPublisher : IHtmlDocumentPublisher
13+
{
14+
private readonly List<(TextDocument, string, ChecksumWrapper)> _publishes = [];
15+
16+
public List<(TextDocument Document, string Text, ChecksumWrapper Checksum)> Publishes => _publishes;
17+
18+
public Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken)
19+
{
20+
_publishes.Add((document, htmlText, synchronizationResult.Checksum));
21+
return Task.CompletedTask;
22+
}
23+
}

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/TestLSPRequestInvoker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal sealed class TestLSPRequestInvoker(params IEnumerable<(string method, o
1818
{
1919
private readonly Dictionary<string, object?> _responses = responses.ToDictionary(kvp => kvp.method, kvp => kvp.response);
2020

21+
public Action<object>? RequestAction { get; set; }
22+
2123
[Obsolete]
2224
public override Task<IEnumerable<ReinvokeResponse<TOut>>> ReinvokeRequestOnMultipleServersAsync<TIn, TOut>(
2325
string method,
@@ -86,6 +88,8 @@ public override Task<ReinvokeResponse<TOut>> ReinvokeRequestOnServerAsync<TIn, T
8688
TIn parameters,
8789
CancellationToken cancellationToken)
8890
{
91+
RequestAction?.Invoke(parameters);
92+
8993
Assert.True(_responses.TryGetValue(method, out var response), $"'{method}' was not defined with a response.");
9094

9195
return Task.FromResult(new ReinvocationResponse<TOut>(languageClientName: "html", (TOut?)response)).AsNullable();

0 commit comments

Comments
 (0)