-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8607 from davidwengier/RoslynTests
- Loading branch information
Showing
6 changed files
with
221 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...oslynWorkspace.Test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(DefaultNetCoreTargetFrameworks)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> | ||
<Compile Include="..\OSSkipConditionFactAttribute.cs" LinkBase="Shared" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace\Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.csproj" /> | ||
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
141 changes: 141 additions & 0 deletions
141
...rosoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/RazorWorkspaceListenerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test; | ||
|
||
public class RazorWorkspaceListenerTest | ||
{ | ||
[Fact] | ||
public async Task ProjectAdded_SchedulesTask() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
Assert.Equal(1, listener.SerializeCalls[project.Id]); | ||
} | ||
|
||
[Fact] | ||
public async Task TwoProjectsAdded_SchedulesTwoTasks() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project1 = workspace.AddProject("TestProject1", LanguageNames.CSharp); | ||
var project2 = workspace.AddProject("TestProject2", LanguageNames.CSharp); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
// These are different projects, so should cause two calls | ||
Assert.Equal(1, listener.SerializeCalls[project1.Id]); | ||
Assert.Equal(1, listener.SerializeCalls[project2.Id]); | ||
} | ||
|
||
[Fact] | ||
public async Task ProjectAddedAndRemoved_NoTasks() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
var newSolution = project.Solution.RemoveProject(project.Id); | ||
Assert.True(workspace.TryApplyChanges(newSolution)); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
Assert.Empty(listener.SerializeCalls); | ||
} | ||
|
||
[Fact] | ||
public async Task TwoProjectChanges_SchedulesOneTasks() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
var newSolution = project.Solution.WithProjectDefaultNamespace(project.Id, "NewDefaultNamespace"); | ||
Assert.True(workspace.TryApplyChanges(newSolution)); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
Assert.Equal(1, listener.SerializeCalls[project.Id]); | ||
} | ||
|
||
[Fact] | ||
public async Task DocumentAdded_SchedulesTask() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
|
||
workspace.AddDocument(project.Id, "Document", SourceText.From("Hi there")); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
Assert.Equal(1, listener.SerializeCalls[project.Id]); | ||
} | ||
|
||
[Fact] | ||
public async Task DocumentAdded_WithDelay_SchedulesTwoTasks() | ||
{ | ||
using var workspace = new AdhocWorkspace(CodeAnalysis.Host.Mef.MefHostServices.DefaultHost); | ||
|
||
using var listener = new TestRazorWorkspaceListener(); | ||
listener.EnsureInitialized(workspace, "temp.json"); | ||
|
||
var project = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
workspace.AddDocument(project.Id, "Document", SourceText.From("Hi there")); | ||
|
||
// Wait for debounce | ||
await Task.Delay(500); | ||
|
||
Assert.Equal(2, listener.SerializeCalls[project.Id]); | ||
} | ||
|
||
private class TestRazorWorkspaceListener : RazorWorkspaceListener | ||
{ | ||
private Dictionary<ProjectId, int> _serializeCalls = new(); | ||
|
||
public Dictionary<ProjectId, int> SerializeCalls => _serializeCalls; | ||
|
||
protected override Task SerializeProjectAsync(Project project, CancellationToken ct) | ||
{ | ||
if (!_serializeCalls.ContainsKey(project.Id)) | ||
{ | ||
_serializeCalls.Add(project.Id, 0); | ||
} | ||
|
||
_serializeCalls[project.Id]++; | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...zor/test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/xunit.runner.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"methodDisplay": "method", | ||
"shadowCopy": false, | ||
"parallelizeTestCollections": false | ||
} |