Skip to content

Commit

Permalink
Add tests for cohosting linked editing range that use real components (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Jul 15, 2024
2 parents 6d563c3 + ed5ddf0 commit d59d623
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,30 @@
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

public class CohostLinkedEditingRangeTest(ITestOutputHelper testOutputHelper) : CohostTestBase(testOutputHelper)
public class CohostLinkedEditingRangeEndpointTest(ITestOutputHelper testOutputHelper) : CohostTestBase(testOutputHelper)
{
[Theory]
[InlineData("$$PageTitle", "PageTitle")]
[InlineData("Page$$Title", "PageTitle")]
[InlineData("PageTitle$$", "PageTitle")]
[InlineData("PageTitle", "$$PageTitle")]
[InlineData("PageTitle", "Page$$Title")]
[InlineData("PageTitle", "PageTitle$$")]
public async Task Component_StartAndEndTag(string startTag, string endTag)
{
var input = $"""
This is a Razor document.

<[|{startTag}|]>This is the title</[|{endTag}|]>

The end.
""";

await VerifyLinkedEditingRangeAsync(input);
}

[Theory]
[InlineData("$$div")]
[InlineData("di$$v")]
Expand Down Expand Up @@ -141,7 +161,7 @@ The end.
private async Task VerifyLinkedEditingRangeAsync(string input)
{
TestFileMarkupParser.GetPositionAndSpans(input, out input, out int cursorPosition, out ImmutableArray<TextSpan> spans);
var document = CreateRazorDocument(input);
var document = CreateProjectAndRazorDocument(input);
var sourceText = await document.GetTextAsync(DisposalToken);
sourceText.GetLineAndOffset(cursorPosition, out var lineIndex, out var characterIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Basic.Reference.Assemblies;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
Expand All @@ -12,7 +14,7 @@
using Microsoft.CodeAnalysis.Text;
using Xunit.Abstractions;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

public abstract class CohostTestBase(ITestOutputHelper testOutputHelper) : WorkspaceTestBase(testOutputHelper)
{
Expand All @@ -28,27 +30,42 @@ protected override async Task InitializeAsync()
_remoteServiceProvider = AddDisposable(new TestRemoteServiceProvider(exportProvider));
}

protected TextDocument CreateRazorDocument(string contents)
protected TextDocument CreateProjectAndRazorDocument(string contents)
{
var projectFilePath = TestProjectData.SomeProject.FilePath;
var documentFilePath = TestProjectData.SomeProjectComponentFile1.FilePath;
var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
var projectId = ProjectId.CreateNewId(debugName: projectName);
var documentId = DocumentId.CreateNewId(projectId, debugName: documentFilePath);

var solution = Workspace.CurrentSolution.AddProject(ProjectInfo.Create(
projectId,
VersionStamp.Create(),
name: projectName,
assemblyName: projectName,
LanguageNames.CSharp,
documentFilePath));

solution = solution.AddAdditionalDocument(
documentId,
documentFilePath,
SourceText.From(contents),
filePath: documentFilePath);
var projectInfo = ProjectInfo
.Create(
projectId,
VersionStamp.Create(),
name: projectName,
assemblyName: projectName,
LanguageNames.CSharp,
documentFilePath)
.WithMetadataReferences(AspNet80.ReferenceInfos.All.Select(r => r.Reference));

var solution = Workspace.CurrentSolution.AddProject(projectInfo);

solution = solution
.AddAdditionalDocument(
documentId,
documentFilePath,
SourceText.From(contents),
filePath: documentFilePath)
.AddAdditionalDocument(
DocumentId.CreateNewId(projectId),
name: "_Imports.razor",
text: SourceText.From("""
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
"""),
filePath: TestProjectData.SomeProjectComponentImportFile1.FilePath);

return solution.GetAdditionalDocument(documentId).AssumeNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System;
using Xunit;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

internal static class ServiceFactoryMap
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.VisualStudio.Composition;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

/// <summary>
/// An implementation of IRemoteServiceProvider that doesn't actually do anything remote, but rather directly calls service methods
/// </summary>
internal class TestRemoteServiceProvider(ExportProvider exportProvider) : IRemoteServiceProvider, IDisposable
internal sealed class TestRemoteServiceProvider(ExportProvider exportProvider) : IRemoteServiceProvider, IDisposable
{
private readonly TestServiceBroker _testServiceBroker = new TestServiceBroker();
private readonly Dictionary<Type, IDisposable> _services = new Dictionary<Type, IDisposable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Remote.Razor;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

internal class TestServiceBroker : IRazorServiceBroker
internal sealed class TestServiceBroker : IRazorServiceBroker
{
private Solution? _solution;

Expand Down

0 comments on commit d59d623

Please sign in to comment.