Пример #1
0
        private async Task AssertTextChanges(string source, string expected)
        {
            var testFile = new TestFile("dummy.cs", source);

            SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
            var span  = testFile.Content.GetSpans().Single();
            var range = testFile.Content.GetRangeFromSpan(span);

            var request = new FormatRangeRequest()
            {
                Buffer    = testFile.Content.Code,
                FileName  = testFile.FileName,
                Line      = range.Start.Line,
                Column    = range.Start.Offset,
                EndLine   = range.End.Line,
                EndColumn = range.End.Offset
            };

            var requestHandler = SharedOmniSharpTestHost.GetRequestHandler <FormatRangeService>(OmniSharpEndpoints.FormatRange);

            var response = await requestHandler.Handle(request);

            var actual = response.Changes.ToArray();

            var oldText     = testFile.Content.Text;
            var textChanges = GetTextChanges(oldText, response.Changes);
            var actualText  = oldText.WithChanges(textChanges).ToString();

            Assert.Equal(expected.Replace("\r\n", "\n"), actualText.Replace("\r\n", "\n"));
        }
Пример #2
0
        private async Task <QuickFix[]> GetBuildCommandAsync(string source)
        {
            var testFile = new TestFile("dummy.cs", source);

            SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
            var rh = SharedOmniSharpTestHost.GetRequestHandler <BuildCommandService>(OmniSharpEndpoints.BuildCommand);
            var cc = rh._config.MSBuildPath = new BuildPath()
            {
                Path = "path\\to\\msbuild"
            };


            var point          = testFile.Content.GetPointFromPosition();
            var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
            var request        = new BuildCommandRequest
            {
                Type     = BuildType.Build,
                Line     = point.Line,
                Column   = point.Offset,
                FileName = testFile.FileName,
                Buffer   = testFile.Content.Code
            };

            var response = await requestHandler.Handle(request);

            return(response.QuickFixes.ToArray());
        }
Пример #3
0
        private async Task AssertTextChanges(string source, params LinePositionSpanTextChange[] expected)
        {
            var testFile = new TestFile("dummy.cs", source);

            SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
            var span  = testFile.Content.GetSpans().Single();
            var range = testFile.Content.GetRangeFromSpan(span);

            var request = new FormatRangeRequest()
            {
                Buffer    = testFile.Content.Code,
                FileName  = testFile.FileName,
                Line      = range.Start.Line,
                Column    = range.Start.Offset,
                EndLine   = range.End.Line,
                EndColumn = range.End.Offset
            };

            var requestHandler = SharedOmniSharpTestHost.GetRequestHandler <FormatRangeService>(OmniSharpEndpoints.FormatRange);

            var response = await requestHandler.Handle(request);

            var actual = response.Changes.ToArray();

            Assert.Equal(expected.Length, actual.Length);

            for (var i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i].NewText, actual[i].NewText);
                Assert.Equal(expected[i].StartLine, actual[i].StartLine);
                Assert.Equal(expected[i].StartColumn, actual[i].StartColumn);
                Assert.Equal(expected[i].EndLine, actual[i].EndLine);
                Assert.Equal(expected[i].EndColumn, actual[i].EndColumn);
            }
        }
Пример #4
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("/", @"\"));
        }
        private async Task <(FormatRangeResponse, TestFile)> GetResponse(string text, string character, string fileName)
        {
            var file = new TestFile(fileName, text);

            SharedOmniSharpTestHost.AddFilesToWorkspace(file);
            var point = file.Content.GetPointFromPosition();

            var request = new FormatAfterKeystrokeRequest
            {
                Line      = point.Line,
                Column    = point.Offset,
                FileName  = fileName,
                Character = character,
            };

            var requestHandler = SharedOmniSharpTestHost.GetRequestHandler <FormatAfterKeystrokeService>(OmniSharpEndpoints.FormatAfterKeystroke);

            return(await requestHandler.Handle(request), file);
        }
Пример #6
0
        private async Task <string> GetTestCommandAsync(string source, TestCommandType testType)
        {
            var testFile = new TestFile("dummy.cs", source);

            SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);

            var rh = SharedOmniSharpTestHost.GetRequestHandler <TestCommandService>(OmniSharpEndpoints.TestCommand);
            OmniSharpConfiguration config = new OmniSharpConfiguration()
            {
                MSBuildPath = new BuildPath()
                {
                    Path = "path\\to\\source"
                },
                TestCommands = new TestCommands()
                {
                    All     = "nunit3-console.exe --noresult --noh {{AssemblyPath}}",
                    Single  = "nunit3-console.exe --noresult --noh {{AssemblyPath}} --test={{TypeName}}",
                    Fixture = "nunit3-console.exe --noresult --noh {{AssemblyPath}} --test={{TypeName}}"
                }
            };

            rh._config = config;
            var cc = rh._config;

            rh._testCommandProviders.First().testCommands = TestCommandType.All;

            var point          = testFile.Content.GetPointFromPosition();
            var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
            var request        = new TestCommandRequest
            {
                Type     = testType,
                Line     = point.Line,
                FileName = testFile.FileName,
                Buffer   = testFile.Content.Code
            };
            var response = await requestHandler.Handle(request);

            return(response.TestCommand);
        }
        private async Task VerifyChange(string fileName, string typedCharacter, string originalMarkup, string expected)
        {
            var(response, testFile) = await GetResponse(originalMarkup, typedCharacter, fileName);

            Assert.NotNull(response);

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

            _ = await fileChangedService.Handle(new UpdateBufferRequest()
            {
                FileName             = testFile.FileName,
                Changes              = response.Changes,
                ApplyChangesTogether = true,
            });

            var actualDoc = SharedOmniSharpTestHost.Workspace.GetDocument(testFile.FileName);

            Assert.NotNull(actualDoc);
            var actualText = (await actualDoc.GetTextAsync()).ToString();

            AssertUtils.Equal(expected, actualText);
        }
        private async Task <(FormatRangeResponse, TestFile)> GetResponse(string text, string character, string fileName)
        {
            // Ensure system newlines are used
            var options = SharedOmniSharpTestHost.Workspace.Options.WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, System.Environment.NewLine);

            SharedOmniSharpTestHost.Workspace.TryApplyChanges(SharedOmniSharpTestHost.Workspace.CurrentSolution.WithOptions(options));

            var file = new TestFile(fileName, text);

            SharedOmniSharpTestHost.AddFilesToWorkspace(file);
            var point = file.Content.GetPointFromPosition();

            var request = new FormatAfterKeystrokeRequest
            {
                Line      = point.Line,
                Column    = point.Offset,
                FileName  = fileName,
                Character = character,
            };

            var requestHandler = SharedOmniSharpTestHost.GetRequestHandler <FormatAfterKeystrokeService>(OmniSharpEndpoints.FormatAfterKeystroke);

            return(await requestHandler.Handle(request), file);
        }
Пример #9
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);
        }