Skip to content

Commit c740450

Browse files
committed
Tests
1 parent a273d11 commit c740450

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Runtime.CompilerServices;
6+
using System.Runtime.Serialization;
7+
using System.Text.Json;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Razor;
10+
using Microsoft.AspNetCore.Razor.Language;
11+
using Microsoft.AspNetCore.Razor.Test.Common;
12+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
13+
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
14+
using Microsoft.CodeAnalysis.Razor.Formatting;
15+
using Microsoft.CodeAnalysis.Razor.Protocol;
16+
using Microsoft.CodeAnalysis.Remote.Razor.DocumentMapping;
17+
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
18+
using Microsoft.CodeAnalysis.Text;
19+
using Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
20+
using Microsoft.VisualStudio.Razor.LanguageClient.Cohost.Formatting;
21+
using Xunit;
22+
using Xunit.Abstractions;
23+
24+
namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost.Formatting;
25+
26+
/// <summary>
27+
/// Not tests of the formatting log, but tests that use formatting logs sent in
28+
/// by users reporting issues.
29+
/// </summary>
30+
[Collection(HtmlFormattingCollection.Name)]
31+
public class HtmlFormattingPassTest(FormattingTestContext context, HtmlFormattingFixture fixture, ITestOutputHelper testOutput)
32+
: FormattingTestBase(context, fixture.Service, testOutput), IClassFixture<FormattingTestContext>
33+
{
34+
[Theory]
35+
[WorkItem("https://github.com/dotnet/razor/issues/11846")]
36+
[InlineData("", "")]
37+
[InlineData("$", "")]
38+
[InlineData("", "u8")]
39+
[InlineData("$", "u8")]
40+
[InlineData("@", "")]
41+
[InlineData("@$", "")]
42+
[InlineData(@"""""""", @"""""""")]
43+
[InlineData(@"$""""""", @"""""""")]
44+
[InlineData(@"""""""\r\n", @"\r\n""""""")]
45+
[InlineData(@"$""""""\r\n", @"\r\n""""""")]
46+
[InlineData(@"""""""", @"""""""u8")]
47+
[InlineData(@"$""""""", @"""""""u8")]
48+
[InlineData(@"""""""\r\n", @"\r\n""""""u8")]
49+
[InlineData(@"$""""""\r\n", @"\r\n""""""u8")]
50+
public async Task RemoveEditThatSplitsStringLiteral(string prefix, string suffix)
51+
{
52+
var document = CreateProjectAndRazorDocument($"""
53+
@({prefix}"this is a line that is 46 characters long"{suffix})
54+
""");
55+
var change = new TextChange(new TextSpan(24, 0), "\r\n");
56+
var edits = await GetHtmlFormattingEditsAsync(document, change);
57+
Assert.Empty(edits);
58+
}
59+
60+
private async Task<System.Collections.Immutable.ImmutableArray<TextChange>> GetHtmlFormattingEditsAsync(CodeAnalysis.TextDocument document, TextChange change)
61+
{
62+
var documentMappingService = OOPExportProvider.GetExportedValue<IDocumentMappingService>();
63+
var pass = new HtmlFormattingPass(documentMappingService);
64+
65+
var snapshotManager = OOPExportProvider.GetExportedValue<RemoteSnapshotManager>();
66+
var snapshot = snapshotManager.GetSnapshot(document);
67+
68+
var loggerFactory = new TestFormattingLoggerFactory(TestOutputHelper);
69+
var logger = loggerFactory.CreateLogger(document.FilePath.AssumeNotNull(), "Html");
70+
var codeDocument = await snapshot.GetGeneratedOutputAsync(DisposalToken);
71+
var context = FormattingContext.Create(snapshot,
72+
codeDocument,
73+
new RazorFormattingOptions(),
74+
logger);
75+
76+
var edits = await pass.GetTestAccessor().FilterIncomingChangesAsync(context, [change], DisposalToken);
77+
return edits;
78+
}
79+
}

0 commit comments

Comments
 (0)