Skip to content

Commit 83a3288

Browse files
Add and clean up ProjectKey and DocumentKey tests
- Move ProjectKeyTests to Microsoft.AspNetCore.Razor.ProjectEngineHost.Test project - Delete TestProjectKey. This is a pretty old helper and creates a pretty dubious ProjectKey. The few tests that still used this have been updated and fixed where necessary. - Add tests for comparing ProjectKeys. - Add tests for comparing DocumentKeys.
1 parent d176678 commit 83a3288

File tree

8 files changed

+138
-49
lines changed

8 files changed

+138
-49
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using Microsoft.AspNetCore.Razor.Telemetry;
1919
using Microsoft.AspNetCore.Razor.Test.Common;
2020
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
21-
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
2221
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
2322
using Microsoft.AspNetCore.Razor.Threading;
2423
using Microsoft.CodeAnalysis;
@@ -299,6 +298,7 @@ internal static IDocumentSnapshot CreateDocumentSnapshot(
299298
bool inGlobalNamespace,
300299
bool forceRuntimeCodeGeneration)
301300
{
301+
var projectKey = new ProjectKey(Path.Combine(path, "obj"));
302302
var snapshotMock = new StrictMock<IDocumentSnapshot>();
303303

304304
snapshotMock
@@ -309,7 +309,7 @@ internal static IDocumentSnapshot CreateDocumentSnapshot(
309309
.Returns(path);
310310
snapshotMock
311311
.Setup(d => d.Project.Key)
312-
.Returns(TestProjectKey.Create("/obj"));
312+
.Returns(projectKey);
313313
snapshotMock
314314
.Setup(d => d.TargetPath)
315315
.Returns(path);

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/SingleServerDelegatingEndpointTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Threading.Tasks;
88
using Microsoft.AspNetCore.Razor.Language;
9+
using Microsoft.AspNetCore.Razor.ProjectSystem;
910
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
10-
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
1111
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1212
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1313
using Microsoft.CodeAnalysis.Razor.Workspaces;
@@ -33,7 +33,7 @@ private protected async Task<TestLanguageServer> CreateLanguageServerAsync(
3333
bool multiTargetProject = true,
3434
Action<VSInternalClientCapabilities>? capabilitiesUpdater = null)
3535
{
36-
var projectKey = TestProjectKey.Create("");
36+
var projectKey = new ProjectKey("C:/path/to/obj");
3737
var csharpSourceText = codeDocument.GetCSharpSourceText();
3838
var csharpDocumentUri = new Uri(FilePathService.GetRazorCSharpFilePath(projectKey, razorFilePath));
3939

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.AspNetCore.Razor.ProjectSystem;
7+
8+
internal static class CompareKeysTestData
9+
{
10+
private static readonly ProjectKey s_forwardSlash1 = new("/path/to/project/obj");
11+
private static readonly ProjectKey s_backslash1 = new(@"\path\to\project\obj");
12+
private static readonly ProjectKey s_forwardSlash2 = new("/path/to/proj/obj");
13+
private static readonly ProjectKey s_backslash2 = new(@"\path\to\proj\obj");
14+
15+
private const string DocumentFilePath1 = @"\path\to\project\file1.razor";
16+
private const string DocumentFilePath2 = @"\path\to\project\file2.razor";
17+
18+
internal enum CompareResult { Equal, LessThan, GreaterThan }
19+
20+
public static TheoryData<ProjectKey, ProjectKey, CompareResult> ProjectKeys =>
21+
new()
22+
{
23+
{ ProjectKey.Unknown, ProjectKey.Unknown, CompareResult.Equal },
24+
{ ProjectKey.Unknown, s_forwardSlash1, CompareResult.GreaterThan },
25+
{ s_forwardSlash1, ProjectKey.Unknown, CompareResult.LessThan },
26+
{ s_forwardSlash1, s_forwardSlash1, CompareResult.Equal },
27+
{ s_forwardSlash1, s_backslash1, CompareResult.Equal },
28+
{ s_backslash1, s_forwardSlash1, CompareResult.Equal },
29+
{ s_forwardSlash2, s_forwardSlash1, CompareResult.LessThan },
30+
{ s_forwardSlash2, s_backslash1, CompareResult.LessThan },
31+
{ s_backslash2, s_forwardSlash1, CompareResult.LessThan },
32+
{ s_forwardSlash1, s_forwardSlash2, CompareResult.GreaterThan },
33+
{ s_forwardSlash1, s_backslash2, CompareResult.GreaterThan },
34+
{ s_backslash1, s_forwardSlash2, CompareResult.GreaterThan }
35+
};
36+
37+
public static TheoryData<DocumentKey, DocumentKey, CompareResult> DocumentKeys =>
38+
new()
39+
{
40+
{ new(ProjectKey.Unknown, DocumentFilePath1), new(ProjectKey.Unknown, DocumentFilePath1), CompareResult.Equal },
41+
{ new(ProjectKey.Unknown, DocumentFilePath1), new(s_forwardSlash1, DocumentFilePath1), CompareResult.GreaterThan },
42+
{ new(s_forwardSlash1, DocumentFilePath1), new(ProjectKey.Unknown, DocumentFilePath1), CompareResult.LessThan },
43+
{ new(s_forwardSlash1, DocumentFilePath1), new(s_forwardSlash1, DocumentFilePath1), CompareResult.Equal },
44+
{ new(s_forwardSlash1, DocumentFilePath1), new(s_forwardSlash1, DocumentFilePath2), CompareResult.LessThan },
45+
{ new(s_forwardSlash1, DocumentFilePath2), new(s_forwardSlash1, DocumentFilePath1), CompareResult.GreaterThan }
46+
};
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Razor.Test.Common;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
using static Microsoft.AspNetCore.Razor.ProjectSystem.CompareKeysTestData;
8+
9+
namespace Microsoft.AspNetCore.Razor.ProjectSystem;
10+
11+
public class DocumentKeyTests(ITestOutputHelper testOuput) : ToolingTestBase(testOuput)
12+
{
13+
[Theory]
14+
[MemberData(nameof(CompareDocumentKeysData))]
15+
internal void CompareDocumentKeys(DocumentKey key1, DocumentKey key2, CompareResult result)
16+
{
17+
switch (result)
18+
{
19+
case CompareResult.Equal:
20+
Assert.Equal(0, key1.CompareTo(key2));
21+
break;
22+
23+
case CompareResult.LessThan:
24+
Assert.True(key1.CompareTo(key2) < 0);
25+
break;
26+
27+
case CompareResult.GreaterThan:
28+
Assert.True(key1.CompareTo(key2) > 0);
29+
break;
30+
31+
default:
32+
Assumed.Unreachable();
33+
break;
34+
}
35+
}
36+
37+
public static TheoryData CompareDocumentKeysData => CompareKeysTestData.DocumentKeys;
38+
}

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/ProjectKeyTests.cs renamed to src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/ProjectSystem/ProjectKeyTests.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,26 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.IO;
5-
using Microsoft.AspNetCore.Razor;
6-
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
75
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
6+
using Microsoft.CodeAnalysis;
87
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
98
using Xunit;
109
using Xunit.Abstractions;
10+
using static Microsoft.AspNetCore.Razor.ProjectSystem.CompareKeysTestData;
1111

12-
namespace Microsoft.CodeAnalysis.Razor.Workspaces.Test.ProjectSystem;
12+
namespace Microsoft.AspNetCore.Razor.ProjectSystem;
1313

14-
public class ProjectKeyTests : WorkspaceTestBase
14+
public class ProjectKeyTests(ITestOutputHelper testOutput) : WorkspaceTestBase(testOutput)
1515
{
16-
public ProjectKeyTests(ITestOutputHelper testOutput)
17-
: base(testOutput)
18-
{
19-
}
20-
2116
[Theory]
2217
[InlineData("/path/to/dir", @"\path\to\dir")]
2318
[InlineData("/path%2Fto/dir", @"\path\to\dir")]
2419
[InlineData(@"\path\to\dir\", @"\path\to\dir")]
2520
[InlineData(@"\path%5Cto\dir\", @"\path\to\dir")]
2621
public void EqualityTests(string id1, string id2)
2722
{
28-
var key1 = TestProjectKey.Create(id1);
29-
var key2 = TestProjectKey.Create(id2);
23+
var key1 = new ProjectKey(id1);
24+
var key2 = new ProjectKey(id2);
3025

3126
// I'm covering all bases out of a complete lack of trust in compilers
3227
Assert.True(key1 == key2);
@@ -50,8 +45,8 @@ public void EqualityTests(string id1, string id2)
5045
[InlineData(@"\PATH\TO\DIR\", @"\path\to\dir")]
5146
public void EqualityTests_Windows(string id1, string id2)
5247
{
53-
var key1 = TestProjectKey.Create(id1);
54-
var key2 = TestProjectKey.Create(id2);
48+
var key1 = new ProjectKey(id1);
49+
var key2 = new ProjectKey(id2);
5550

5651
// I'm covering all bases out of a complete lack of trust in compilers
5752
Assert.True(key1 == key2);
@@ -74,8 +69,8 @@ public void EqualityTests_Windows(string id1, string id2)
7469
[InlineData(@"\PATH\TO\OTHER\DIR\", @"\path\to\dir")]
7570
public void InequalityTests(string id1, string id2)
7671
{
77-
var key1 = TestProjectKey.Create(id1);
78-
var key2 = TestProjectKey.Create(id2);
72+
var key1 = new ProjectKey(id1);
73+
var key2 = new ProjectKey(id2);
7974

8075
// I'm covering all bases out of a complete lack of trust in compilers
8176
Assert.False(key1 == key2);
@@ -95,12 +90,43 @@ public void RoslynProjectToRazorProject()
9590
var intermediateOutputPath = @"c:\project\obj";
9691
var assemblyPath = Path.Combine(intermediateOutputPath, "project.dll");
9792

98-
var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Default, "Project", "Assembly", "C#").WithCompilationOutputInfo(new CompilationOutputInfo().WithAssemblyPath(assemblyPath));
99-
var project = Workspace.CurrentSolution.AddProject(projectInfo).GetProject(projectInfo.Id).AssumeNotNull();
93+
var projectInfo = ProjectInfo
94+
.Create(ProjectId.CreateNewId(), VersionStamp.Default, "Project", "Assembly", "C#")
95+
.WithCompilationOutputInfo(new CompilationOutputInfo().WithAssemblyPath(assemblyPath));
96+
97+
var project = Workspace.CurrentSolution
98+
.AddProject(projectInfo)
99+
.GetRequiredProject(projectInfo.Id);
100100

101101
var roslynKey = project.ToProjectKey();
102-
var razorKey = TestProjectKey.Create(intermediateOutputPath);
102+
var razorKey = new ProjectKey(intermediateOutputPath);
103103

104104
Assert.Equal(roslynKey, razorKey);
105105
}
106+
107+
[Theory]
108+
[MemberData(nameof(CompareProjectKeysData))]
109+
internal void CompareProjectKeys(ProjectKey key1, ProjectKey key2, CompareResult result)
110+
{
111+
switch (result)
112+
{
113+
case CompareResult.Equal:
114+
Assert.Equal(0, key1.CompareTo(key2));
115+
break;
116+
117+
case CompareResult.LessThan:
118+
Assert.True(key1.CompareTo(key2) < 0);
119+
break;
120+
121+
case CompareResult.GreaterThan:
122+
Assert.True(key1.CompareTo(key2) > 0);
123+
break;
124+
125+
default:
126+
Assumed.Unreachable();
127+
break;
128+
}
129+
}
130+
131+
public static TheoryData CompareProjectKeysData => CompareKeysTestData.ProjectKeys;
106132
}

src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestProjectKey.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/FilePathServiceTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using Microsoft.AspNetCore.Razor.ProjectSystem;
6-
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
76
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
87
using Xunit;
98

@@ -28,12 +27,12 @@ public void GetRazorFilePath_ReturnsExpectedPath(string inputFilePath)
2827
}
2928

3029
[Theory]
31-
[InlineData(true, @"C:\path\to\file.razor.t3Gf1FBjln6S9T95.ide.g.cs")]
30+
[InlineData(true, @"C:\path\to\file.razor.21z2YGQgr-neX-Hd.ide.g.cs")]
3231
[InlineData(false, @"C:\path\to\file.razor.ide.g.cs")]
3332
public void GetRazorCSharpFilePath_ReturnsExpectedPath(bool includeProjectKey, string expected)
3433
{
3534
// Arrange
36-
var projectKey = TestProjectKey.Create("Hello");
35+
var projectKey = new ProjectKey(@"C:\path\to\obj");
3736
var filePathService = new TestFilePathService(new TestLanguageServerFeatureOptions(includeProjectKeyInGeneratedFilePath: includeProjectKey));
3837

3938
// Act

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
using System.Collections.Generic;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Razor.ProjectSystem;
1011
using Microsoft.AspNetCore.Razor.Telemetry;
1112
using Microsoft.AspNetCore.Razor.Test.Common;
1213
using Microsoft.AspNetCore.Razor.Test.Common.Editor;
13-
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
1414
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
1515
using Microsoft.CodeAnalysis.Razor.Protocol;
1616
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
@@ -134,8 +134,8 @@ public async Task UpdateCSharpBuffer_UpdatesDocument()
134134
public async Task UpdateCSharpBuffer_UpdatesCorrectDocument()
135135
{
136136
// Arrange
137-
var projectKey1 = TestProjectKey.Create("Project1");
138-
var projectKey2 = TestProjectKey.Create("Project2");
137+
var projectKey1 = new ProjectKey("C:/path/to/p1/obj");
138+
var projectKey2 = new ProjectKey("C:/path/to/p2/obj");
139139
var doc1 = new CSharpVirtualDocumentSnapshot(projectKey1, new Uri("C:/path/to/p1/file.razor.g.cs"), _textBuffer.CurrentSnapshot, 0);
140140
var doc2 = new CSharpVirtualDocumentSnapshot(projectKey2, new Uri("C:/path/to/p2/file.razor.g.cs"), _textBuffer.CurrentSnapshot, 0);
141141
var documents = new[] { doc1, doc2 };

0 commit comments

Comments
 (0)