Skip to content

Commit

Permalink
Add test for misc Workspace scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Sep 10, 2021
1 parent dcbef8d commit d79627d
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using OmniSharp.Models;
using OmniSharp.Models.FindUsages;
using OmniSharp.Roslyn.CSharp.Services.Navigation;
Expand Down Expand Up @@ -419,7 +420,7 @@ public class F$$oo {
}

[Fact]
public async Task MappedLocationFileNameProperlyRooted()
public async Task MappedLocationFileNameProperlyRootedInAdditionalDocuments()
{
var folderPath = Directory.GetCurrentDirectory();
var relativeFile = ".\\Index.cshtml";
Expand Down Expand Up @@ -463,6 +464,45 @@ public void OnGet()
Assert.Empty(quickFix.Projects);
}

[Fact]
public async Task MappedLocationFileNameProperlyRootedInMiscellaneousWorkspace()
{
var folderPath = Directory.GetCurrentDirectory();
var relativeFile = ".\\Index.cshtml.cs";
var mappedFilePath = Path.GetFullPath(Path.Combine(folderPath, relativeFile));

var testFiles = new[]
{
new TestFile("Constants.cs", @"
public static class Constants
{
public const string My$$Text = ""Hello World"";
}"),
new TestFile("Index.cshtml_virtual.cs", $@"
#line 1 ""{relativeFile}""
Constants.MyText
#line default
#line hidden")
};

var miscFile = new TestFile(mappedFilePath, "// Constants.MyText;");

SharedOmniSharpTestHost.AddFilesToWorkspace(folderPath, testFiles);
SharedOmniSharpTestHost.Workspace.TryAddMiscellaneousDocument(
miscFile.FileName,
TextLoader.From(TextAndVersion.Create(miscFile.Content.Text, VersionStamp.Create())),
LanguageNames.CSharp);

var testFile = testFiles.Single(tf => tf.Content.HasPosition);
var usages = await FindUsagesAsync(testFile, onlyThisFile: false);

Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.EndsWith("Index.cshtml_virtual.cs"));
Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.Equals(relativeFile));

var quickFix = Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath));
Assert.Empty(quickFix.Projects);
}

[Fact]
public async Task DontFindDefinitionInAnotherFile()
{
Expand Down Expand Up @@ -510,20 +550,25 @@ private Task<QuickFixResponse> FindUsagesAsync(string code, bool excludeDefiniti
return FindUsagesAsync(new[] { new TestFile("dummy.cs", code) }, false, excludeDefinition);
}

private async Task<QuickFixResponse> FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false, string folderPath = null)
private Task<QuickFixResponse> FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false, string folderPath = null)
{
SharedOmniSharpTestHost.AddFilesToWorkspace(folderPath, testFiles);
var file = testFiles.Single(tf => tf.Content.HasPosition);
var point = file.Content.GetPointFromPosition();
var testFile = testFiles.Single(tf => tf.Content.HasPosition);
return FindUsagesAsync(testFile, onlyThisFile, excludeDefinition);
}

private async Task<QuickFixResponse> FindUsagesAsync(TestFile testFile, bool onlyThisFile, bool excludeDefinition = false)
{
var point = testFile.Content.GetPointFromPosition();

var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

var request = new FindUsagesRequest
{
Line = point.Line,
Column = point.Offset,
FileName = file.FileName,
Buffer = file.Content.Code,
FileName = testFile.FileName,
Buffer = testFile.Content.Code,
OnlyThisFile = onlyThisFile,
ExcludeDefinition = excludeDefinition
};
Expand Down

0 comments on commit d79627d

Please sign in to comment.