Esempio n. 1
0
        public async Task ReturnsResultsForSourceGenerators()
        {
            const string Source            = @"
public class {|generatedClassName:Generated|}
{
    public int {|propertyName:Property|} { get; set; }
}
";
            const string FileName          = "real.cs";
            TestFile     generatedTestFile = new("GeneratedFile.cs", Source);
            var          testFile          = new TestFile(FileName, @"
class C
{
    public void M(Generated g)
    {
        _ = g.P$$roperty;
    }
}
");

            TestHelpers.AddProjectToWorkspace(SharedOmniSharpTestHost.Workspace,
                                              "project.csproj",
                                              new[] { "netcoreapp3.1" },
                                              new[] { testFile },
                                              analyzerRefs: ImmutableArray.Create <AnalyzerReference>(new TestGeneratorReference(
                                                                                                          context => context.AddSource("GeneratedFile", generatedTestFile.Content.Code))));

            var point = testFile.Content.GetPointFromPosition();

            var gotoDefRequest = CreateRequest(FileName, point.Line, point.Offset, wantMetadata: true);
            var gotoDefHandler = GetRequestHandler(SharedOmniSharpTestHost);
            var response       = await gotoDefHandler.Handle(gotoDefRequest);

            var info = GetInfo(response).Single();

            Assert.NotNull(info.SourceGeneratorInfo);

            var expectedSpan  = generatedTestFile.Content.GetSpans("propertyName").Single();
            var expectedRange = generatedTestFile.Content.GetRangeFromSpan(expectedSpan);

            Assert.Equal(expectedRange.Start.Line, info.Line);
            Assert.Equal(expectedRange.Start.Offset, info.Column);

            var sourceGeneratedFileHandler = SharedOmniSharpTestHost.GetRequestHandler <SourceGeneratedFileService>(OmniSharpEndpoints.SourceGeneratedFile);
            var sourceGeneratedRequest     = new SourceGeneratedFileRequest
            {
                DocumentGuid = info.SourceGeneratorInfo.DocumentGuid,
                ProjectGuid  = info.SourceGeneratorInfo.ProjectGuid
            };

            var sourceGeneratedFileResponse = await sourceGeneratedFileHandler.Handle(sourceGeneratedRequest);

            Assert.NotNull(sourceGeneratedFileResponse);
            Assert.Equal(generatedTestFile.Content.Code, sourceGeneratedFileResponse.Source);
            Assert.Equal(@"OmniSharp.Roslyn.CSharp.Tests\OmniSharp.Roslyn.CSharp.Tests.TestSourceGenerator\GeneratedFile.cs", sourceGeneratedFileResponse.SourceName.Replace("/", @"\"));
        }
Esempio n. 2
0
        public IEnumerable <ProjectId> AddFilesToWorkspace(string folderPath, params TestFile[] testFiles)
        {
            folderPath = folderPath ?? Directory.GetCurrentDirectory();
            var projects = TestHelpers.AddProjectToWorkspace(
                OmniSharpTestHost.Workspace,
                Path.Combine(folderPath, "project.csproj"),
                new[] { "net472" },
                testFiles.Where(f => f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)).ToArray());

            return(projects);
        }
        private IEnumerable <ProjectId> AddProjectWitFile(OmniSharpTestHost host, TestFile testFile, TestAnalyzerReference testAnalyzerRef = null)
        {
            var analyzerReferences = testAnalyzerRef == null ? default :
                                     new AnalyzerReference[] { testAnalyzerRef }.ToImmutableArray();

                                     return(TestHelpers.AddProjectToWorkspace(
                                                host.Workspace,
                                                "project.csproj",
                                                new[] { "netcoreapp2.1" },
                                                new[] { testFile },
                                                analyzerRefs: analyzerReferences));
        }
Esempio n. 4
0
        public async Task UpdateReturnsChanges()
        {
            const string Code      = @"
_ = GeneratedCode.$$S;
_ = ""Hello world!""";
            const string Path      = @"Test.cs";
            var          reference = new TestGeneratorReference(context =>
            {
                // NOTE: Don't actually do this in a real generator. This is just for test
                // code. Do not use this as an example of what to do in production.

                var syntax = context.Compilation.SyntaxTrees.Single().GetRoot().DescendantNodes().OfType <LiteralExpressionSyntax>().SingleOrDefault();
                if (syntax != null)
                {
                    context.AddSource("GeneratedSource", @"
class GeneratedCode
{
    public static string S = " + syntax.ToString() + @";
}");
                }
            });

            TestFile testFile = new TestFile(Path, Code);

            TestHelpers.AddProjectToWorkspace(SharedOmniSharpTestHost.Workspace,
                                              "project.csproj",
                                              new[] { "netcoreapp3.1" },
                                              new[] { testFile },
                                              analyzerConfigFiles: null,
                                              otherFiles: null,
                                              ImmutableArray.Create <AnalyzerReference>(reference));

            var point = testFile.Content.GetPointFromPosition();

            var gotoDefRequest = new GotoDefinitionRequest {
                FileName = Path, Line = point.Line, Column = point.Offset
            };
            var gotoDefResponse = (await SharedOmniSharpTestHost
                                   .GetRequestHandler <GotoDefinitionServiceV2>(OmniSharpEndpoints.V2.GotoDefinition)
                                   .Handle(gotoDefRequest)).Definitions.Single();

            var initialContent = await SharedOmniSharpTestHost
                                 .GetRequestHandler <SourceGeneratedFileService>(OmniSharpEndpoints.SourceGeneratedFile)
                                 .Handle(new SourceGeneratedFileRequest()
            {
                ProjectGuid  = gotoDefResponse.SourceGeneratedFileInfo.ProjectGuid,
                DocumentGuid = gotoDefResponse.SourceGeneratedFileInfo.DocumentGuid
            });

            Assert.Contains("Hello world!", initialContent.Source);

            var updateRequest = new UpdateSourceGeneratedFileRequest
            {
                DocumentGuid = gotoDefResponse.SourceGeneratedFileInfo.DocumentGuid,
                ProjectGuid  = gotoDefResponse.SourceGeneratedFileInfo.ProjectGuid
            };
            var updateHandler   = SharedOmniSharpTestHost.GetRequestHandler <SourceGeneratedFileService>(OmniSharpEndpoints.UpdateSourceGeneratedFile);
            var updatedResponse = await updateHandler.Handle(updateRequest);

            Assert.Null(updatedResponse.Source);
            Assert.Equal(UpdateType.Unchanged, updatedResponse.UpdateType);

            var updateBufferHandler = SharedOmniSharpTestHost.GetRequestHandler <UpdateBufferService>(OmniSharpEndpoints.UpdateBuffer);

            _ = await updateBufferHandler.Handle(new()
            {
                FileName = Path,
                Buffer   = Code.Replace("Hello world!", "Goodbye!")
            });

            updatedResponse = await updateHandler.Handle(updateRequest);

            Assert.Equal(UpdateType.Modified, updatedResponse.UpdateType);
            Assert.Contains("Goodbye!", updatedResponse.Source);
            Assert.DoesNotContain("Hello world!", updatedResponse.Source);

            _ = await updateBufferHandler.Handle(new()
            {
                FileName = Path,
                Buffer   = @"_ = GeneratedCode.S;"
            });

            updatedResponse = await updateHandler.Handle(updateRequest);

            Assert.Equal(UpdateType.Deleted, updatedResponse.UpdateType);
            Assert.Null(updatedResponse.Source);
        }