Skip to content

Commit df013a0

Browse files
Fix Windows line endings in SemanticTokensTest
1 parent 7631d6b commit df013a0

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Linq;
88
using System.Reflection;
99
using System.Runtime.CompilerServices;
10+
using System.Runtime.InteropServices;
1011
using System.Text;
12+
using System.Text.RegularExpressions;
1113
using System.Threading;
1214
using System.Threading.Tasks;
1315
using Microsoft.AspNetCore.Razor.Language;
@@ -22,6 +24,7 @@
2224
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
2325
using Microsoft.CodeAnalysis.Razor.Workspaces;
2426
using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions;
27+
using Microsoft.CodeAnalysis.Testing;
2528
using Microsoft.CodeAnalysis.Text;
2629
using Microsoft.Extensions.Options;
2730
using Microsoft.VisualStudio.LanguageServer.Protocol;
@@ -32,7 +35,7 @@
3235
namespace Microsoft.AspNetCore.Razor.LanguageServer.Semantic;
3336

3437
[UseExportProvider]
35-
public class SemanticTokensTest(ITestOutputHelper testOutput) : TagHelperServiceTestBase(testOutput)
38+
public partial class SemanticTokensTest(ITestOutputHelper testOutput) : TagHelperServiceTestBase(testOutput)
3639
{
3740
private readonly Mock<ClientNotifierServiceBase> _languageServer = new(MockBehavior.Strict);
3841
private static readonly string s_projectPath = TestProject.GetProjectDirectory(typeof(TagHelperServiceTestBase), layer: TestProject.Layer.Tooling);
@@ -46,6 +49,15 @@ public class SemanticTokensTest(ITestOutputHelper testOutput) : TagHelperService
4649
}
4750
};
4851

52+
private static Regex s_matchNewLines = MyRegex();
53+
54+
#if NET
55+
[GeneratedRegex("\r\n")]
56+
private static partial Regex MyRegex();
57+
#else
58+
private static Regex MyRegex() => new Regex("\r\n|\r|\n");
59+
#endif
60+
4961
#if GENERATE_BASELINES
5062
private bool GenerateBaselines { get; set; } = true;
5163
#else
@@ -851,31 +863,34 @@ public async Task GetSemanticTokens_CSharp_ExplicitStatement_WithBackground(bool
851863
public void GetMappedCSharpRanges_MinimalRangeVsSmallDisjointRanges_DisjointRangesAreSmaller(bool precise)
852864
{
853865
var documentText = """
854-
@using System
855-
@functions {
856-
Action<object> abc = @<span></span>;
857-
}
866+
@[|using System|]
867+
@functions {[|
868+
Action<object> abc = |]@<span></span>[|;
869+
|]}
858870
""";
859871

872+
TestFileMarkupParser.GetSpans(documentText, out documentText,
873+
out ImmutableArray<TextSpan> spans);
874+
860875
var codeDocument = CreateCodeDocument(documentText, isRazorFile: true, DefaultTagHelpers);
861876
var csharpSourceText = codeDocument.GetCSharpSourceText();
862877
var razorRange = GetRange(documentText);
863878

864879
if (precise)
865880
{
866-
var expectedCsharpRangeLengths = new int[] { 12, 27, 3 };
867881
Assert.True(RazorSemanticTokensInfoService.TryGetSortedCSharpRanges(codeDocument, razorRange, out var csharpRanges));
868-
Assert.Equal(3, csharpRanges.Length);
882+
Assert.Equal(spans.Length, csharpRanges.Length);
869883
for (var i = 0; i < csharpRanges.Length; i++)
870884
{
871885
var csharpRange = csharpRanges[i];
872886
var textSpan = csharpRange.ToTextSpan(csharpSourceText);
873-
Assert.Equal(expectedCsharpRangeLengths[i], textSpan.Length);
887+
Assert.Equal(spans[i].Length, textSpan.Length);
874888
}
875889
}
876890
else
877891
{
878-
var expectedCsharpRangeLength = 970;
892+
// Note that the expected lengths are different on Windows vs. Unix.
893+
var expectedCsharpRangeLength = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 970 : 938;
879894
Assert.True(RazorSemanticTokensInfoService.TryGetMinimalCSharpRange(codeDocument, razorRange, out var csharpRange));
880895
var textSpan = csharpRange.ToTextSpan(csharpSourceText);
881896
Assert.Equal(expectedCsharpRangeLength, textSpan.Length);
@@ -1096,7 +1111,14 @@ private string GetBaselineFileContents(string baselineFileName)
10961111
return string.Empty;
10971112
}
10981113

1099-
return semanticFile.ReadAllText();
1114+
var baselineContents = semanticFile.ReadAllText();
1115+
1116+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1117+
{
1118+
baselineContents = s_matchNewLines.Replace(baselineContents, "\n");
1119+
}
1120+
1121+
return baselineContents;
11001122
}
11011123

11021124
private Range[]? GetMappedCSharpRanges(RazorCodeDocument codeDocument, Range razorRange, bool precise)

0 commit comments

Comments
 (0)