Пример #1
0
        public async Task ClassWithMembers()
        {
            var source =
                @"public class Far {
                    private bool _b;
                    private bool B { get; set; }
                    public void M() { }
                    public event Action E;
                }";

            var workspace = await TestHelpers.CreateSimpleWorkspace(source, "d.cs");

            var nodes = await StructureComputer.Compute(workspace.GetDocuments("d.cs"));

            Assert.Equal(1, nodes.Count());
            Assert.Equal("Far", nodes.First().Location.Text);
            Assert.Equal(SyntaxKind.ClassDeclaration.ToString(), nodes.First().Kind);

            // children
            var children = nodes.First().ChildNodes;

            Assert.Equal(4, children.Count());
            Assert.Equal("_b", children.ElementAt(0).Location.Text);
            Assert.Equal("B", children.ElementAt(1).Location.Text);
            Assert.Equal("M", children.ElementAt(2).Location.Text);
            Assert.Equal("E", children.ElementAt(3).Location.Text);
        }
Пример #2
0
        public async Task ReturnsSource_ForGenericType()
        {
            var source1 = @"using System;

class Foo {
}";
            var source2 = @"class Bar {
    private Foo foo;
}";

            var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string> {
                { "foo.cs", source1 }, { "bar.cs", source2 }
            });
            var controller = new OmnisharpController(workspace, null);
            var response   = await controller.Metadata(new MetadataRequest
            {
                AssemblyName = "mscorlib",
                TypeName     = "System.Collections.Generic.List`1",
                Timeout      = 60000
            });

            Assert.NotNull(response.Source);

            response = await controller.Metadata(new MetadataRequest
            {
                AssemblyName = "mscorlib",
                TypeName     = "System.Collections.Generic.Dictionary`2"
            });

            Assert.NotNull(response.Source);
        }
Пример #3
0
        public async Task ReturnsEmptyResultWhenDefinitionIsNotFound()
        {
            var source1 = @"using System;

class Foo {
}";
            var source2 = @"class Bar {
    private Baz foo;
}";

            var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string> {
                { "foo.cs", source1 }, { "bar.cs", source2 }
            });
            var controller         = new OmnisharpController(workspace, null);
            var definitionResponse = await controller.GotoDefinition(new GotoDefinitionRequest
            {
                FileName = "bar.cs",
                Line     = 2,
                Column   = 14,
                Timeout  = 60000
            });

            Assert.Null(definitionResponse.FileName);
            Assert.Equal(0, definitionResponse.Line);
            Assert.Equal(0, definitionResponse.Column);
        }
        public async Task UpdateBuffer_AddsNewDocumentsIfNeeded()
        {
            var workspace = await TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string>
            {
                { "test.cs", "class C {}" }
            });

            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
                FileName = "test2.cs", Buffer = "interface I {}"
            });

            Assert.Equal(2, workspace.CurrentSolution.GetDocumentIdsWithFilePath("test2.cs").Length);
            var docId = workspace.CurrentSolution.GetDocumentIdsWithFilePath("test2.cs").FirstOrDefault();

            Assert.NotNull(docId);
            var sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("interface I {}", sourceText.ToString());

            docId = workspace.CurrentSolution.GetDocumentIdsWithFilePath("test.cs").FirstOrDefault();
            Assert.NotNull(docId);
            sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("class C {}", sourceText.ToString());
        }
        public async Task UpdateBufferIgnoresVoidRequests()
        {
            var workspace = TestHelpers.CreateSimpleWorkspace("class C {}", "test.cs");

            Assert.Equal(2, workspace.CurrentSolution.Projects.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(0).Documents.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(1).Documents.Count());

            await workspace.BufferManager.UpdateBuffer(new Request()
            {
            });

            Assert.Equal(2, workspace.CurrentSolution.Projects.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(0).Documents.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(1).Documents.Count());

            await workspace.BufferManager.UpdateBuffer(new Request()
            {
                FileName = "", Buffer = "enum E {}"
            });

            Assert.Equal(2, workspace.CurrentSolution.Projects.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(0).Documents.Count());
            Assert.Equal(1, workspace.CurrentSolution.Projects.ElementAt(1).Documents.Count());
        }
Пример #6
0
        public async Task Rename_UpdatesMultipleDocumentsIfNecessaryAndProducesTextChangesIfAsked()
        {
            const string file1 = "public class F$oo {}";
            const string file2 = @"public class Bar {
                                    public Foo Property {get; set;}
                                }";

            var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string> {
                { "test1.cs", file1 }, { "test2.cs", file2 }
            });
            var result = await SendRequest(workspace, "xxx", "test1.cs", file1, true);

            Assert.Equal(2, result.Changes.Count());
            Assert.Equal(1, result.Changes.ElementAt(0).Changes.Count());

            Assert.Null(result.Changes.ElementAt(0).Buffer);
            Assert.Equal("xxx", result.Changes.ElementAt(0).Changes.First().NewText);
            Assert.Equal(1, result.Changes.ElementAt(0).Changes.First().StartLine);
            Assert.Equal(14, result.Changes.ElementAt(0).Changes.First().StartColumn);
            Assert.Equal(1, result.Changes.ElementAt(0).Changes.First().EndLine);
            Assert.Equal(17, result.Changes.ElementAt(0).Changes.First().EndColumn);

            Assert.Null(result.Changes.ElementAt(1).Buffer);
            Assert.Equal("xxx", result.Changes.ElementAt(1).Changes.First().NewText);
            Assert.Equal(2, result.Changes.ElementAt(1).Changes.First().StartLine);
            Assert.Equal(44, result.Changes.ElementAt(1).Changes.First().StartColumn);
            Assert.Equal(2, result.Changes.ElementAt(1).Changes.First().EndLine);
            Assert.Equal(47, result.Changes.ElementAt(1).Changes.First().EndColumn);
        }
Пример #7
0
        public async Task Rename_UpdatesWorkspaceAndDocumentText()
        {
            const string fileContent = @"using System;

                        namespace OmniSharp.Models
                        {
                            public class CodeFormat$Response
                            {
                                public string Buffer { get; set; }
                            }
                        }";

            var workspace = TestHelpers.CreateSimpleWorkspace(fileContent, "test.cs");
            var result    = await SendRequest(workspace, "foo", "test.cs", fileContent);

            var docId      = workspace.CurrentSolution.GetDocumentIdsWithFilePath("test.cs").First();
            var sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            //compare workspace change with response
            Assert.Equal(result.Changes.First().Buffer, sourceText.ToString());

            //check that response refers to correct modified file
            Assert.Equal(result.Changes.First().FileName, "test.cs");

            //check response for change
            Assert.Equal(@"using System;

                        namespace OmniSharp.Models
                        {
                            public class foo
                            {
                                public string Buffer { get; set; }
                            }
                        }", result.Changes.First().Buffer);
        }
Пример #8
0
        private async Task <string> GetTestCommandArgumentsAsync(string source, TestCommandType testType = TestCommandType.Single)
        {
            var workspace      = TestHelpers.CreateSimpleWorkspace(source);
            var context        = new DnxContext();
            var projectName    = "project.json";
            var projectCounter = 1;

            context.ProjectContextMapping.Add(projectName, projectCounter);
            context.Projects.Add(projectCounter, new Project
            {
                Path     = "project.json",
                Commands = { { "test", "Xunit.KRunner" } }
            });

            var testCommandProviders = new[] { new DnxTestCommandProvider(context, new FakeEnvironment(), new FakeLoggerFactory(), new NullEventEmitter(), new FakeOmniSharpOptions()) };
            var controller           = new TestCommandController(workspace, testCommandProviders);
            var lineColumn           = TestHelpers.GetLineAndColumnFromDollar(source);

            var request = new TestCommandRequest
            {
                Line     = lineColumn.Line,
                Column   = lineColumn.Column,
                FileName = "dummy.cs",
                Buffer   = source.Replace("$", ""),
                Type     = testType
            };

            var bufferFilter = new UpdateBufferFilter(workspace);

            bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
            var testCommand = await controller.GetTestCommand(request);

            return(testCommand.TestCommand);
        }
Пример #9
0
        public async Task HighlightStringInterpolation()
        {
            var code = @"
                class C1
                {
                    string s = $""{5}"";
                }
            ";

            var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string>
            {
                { "a.cs", code }
            });

            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var regions    = await controller.Highlight(new HighlightRequest()
            {
                FileName = "a.cs"
            });

            AssertSyntax(regions.Highlights, code, 0,
                         Token("class", "keyword"),
                         Token("C1", "class name"),
                         Token("{", "punctuation"),
                         Token("string", "keyword"),
                         Token("s", "identifier"),
                         Token("=", "operator"),
                         Token("$\"", "string"),
                         Token("{", "punctuation"),
                         Token("5", "number"),
                         Token("}", "punctuation"),
                         Token("\"", "string"),
                         Token(";", "punctuation"),
                         Token("}", "punctuation"));
        }
Пример #10
0
        public async Task HighlightEntireFile()
        {
            var code = @"
                namespace N1
                {
                    class C1 { int n = true; }
                }
            ";

            var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string>
            {
                { "a.cs", code }
            });

            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var regions    = await controller.Highlight(new HighlightRequest()
            {
                FileName = "a.cs"
            });

            AssertSyntax(regions.Highlights, code, 0,
                         Token("namespace", "keyword"),
                         Token("N1", "identifier"),
                         Token("{", "punctuation"),
                         Token("class", "keyword"),
                         Token("C1", "class name"),
                         Token("{", "punctuation"),
                         Token("int", "keyword"),
                         Token("n", "identifier"),
                         Token("=", "operator"),
                         Token("true", "keyword"),
                         Token(";", "punctuation"),
                         Token("}", "punctuation"),
                         Token("}", "punctuation"));
        }
        private async Task <QuickFixResponse> FindSymbols(string source)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(source);
            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());

            return(await controller.FindSymbols());
        }
Пример #12
0
        OmnisharpWorkspace CreateTestWorkspace()
        {
            var source1 = @"using System;

class Foo {
}";
            var source2 = @"using System;
using System.Collections.Generic;
using System.Linq;

class Bar {
    public void Baz() {
        Console.WriteLine(""Stuff"");

        var foo = new List<string>();
        var str = String.Emtpy;
        foo.ToList();
        var dict = new Dictionary<string, string>();
    }
}";

            return(TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string> {
                { "foo.cs", source1 }, { "bar.cs", source2 }
            }));
        }
Пример #13
0
        private static async Task <IEnumerable <LinePositionSpanTextChange> > FormattingChangesForRange(FormatRangeRequest req)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(req.Buffer, req.FileName);
            var controller = new OmnisharpController(workspace, null);

            return((await controller.FormatRange(req)).Changes);
        }
Пример #14
0
        private async Task <QuickFixResponse> FindUsages(string source)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(source);
            var controller = new OmnisharpController(workspace, null);
            var request    = CreateRequest(source);

            return(await controller.FindUsages(request));
        }
Пример #15
0
        public void ReturnsEmptyResponseForEmptyWorskpace()
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string>());
            var controller = new OmnisharpController(workspace, null);
            var response   = controller.GoToFile(new Request());

            Assert.Equal(0, response.QuickFixes.Count());
        }
        private async Task <IEnumerable <ISymbol> > FindImplementations(string source)
        {
            var workspace       = TestHelpers.CreateSimpleWorkspace(source);
            var controller      = new OmnisharpController(workspace, null);
            var request         = CreateRequest(source);
            var implementations = await controller.FindImplementations(request);

            return(await TestHelpers.SymbolsFromQuickFixes(workspace, implementations.QuickFixes));
        }
Пример #17
0
        private async Task <IEnumerable <string> > FindRefactoringsAsync(string source)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(source);
            var controller = new CodeActionController(workspace, new[] { new NRefactoryCodeActionProvider() });
            var request    = CreateRequest(source);
            var response   = await controller.GetCodeActions(request);

            return(response.CodeActions);
        }
        private async Task <QuickFixResponse> FindSymbolsWithFilter(string source, string filter)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(source);
            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var request    = new FindSymbolsRequest {
                Filter = filter
            };

            return(await controller.FindSymbols(request));
        }
        private async Task <IEnumerable <OmniSharpCodeAction> > FindRefactoringsAsync(string source)
        {
            var request = CreateGetCodeActionsRequest(source);

            _workspace = _workspace ?? TestHelpers.CreateSimpleWorkspace(request.Buffer, bufferPath);
            var controller = new Api.V2.CodeActionController(_workspace, new ICodeActionProvider[] { new RoslynCodeActionProvider(), new NRefactoryCodeActionProvider() }, new FakeLoggerFactory());
            var response   = await controller.GetCodeActions(request);

            return(response.CodeActions);
        }
        private async Task <RunCodeActionResponse> RunRefactoringsAsync(string source, string identifier, bool wantsChanges = false)
        {
            var request = CreateRunCodeActionRequest(source, identifier, wantsChanges);

            _workspace = _workspace ?? TestHelpers.CreateSimpleWorkspace(request.Buffer, bufferPath);
            var controller = new Api.V2.CodeActionController(_workspace, new ICodeActionProvider[] { new RoslynCodeActionProvider(), new NRefactoryCodeActionProvider() }, new FakeLoggerFactory());
            var response   = await controller.RunCodeAction(request);

            return(response);
        }
Пример #21
0
        public async Task Rename_DoesTheRightThingWhenDocumentIsNotFound()
        {
            const string fileContent = "class f$oo{}";
            var          workspace   = TestHelpers.CreateSimpleWorkspace(fileContent);

            var result = await SendRequest(workspace, "xxx", "test.cs", fileContent);

            Assert.Equal(1, result.Changes.Count());
            Assert.Equal("test.cs", result.Changes.ElementAt(0).FileName);
        }
Пример #22
0
        private async Task <QuickFixResponse> FindRegions(string source)
        {
            var workspace    = TestHelpers.CreateSimpleWorkspace(source);
            var controller   = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var request      = CreateRequest(source);
            var bufferFilter = new UpdateBufferFilter(workspace);

            bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
            return(await controller.GoToRegion(request));
        }
        public async Task UpdateBuffer_HandlesVoidRequest()
        {
            var workspace = await TestHelpers.CreateSimpleWorkspace(new Dictionary <string, string>
            {
                { "test.cs", "class C {}" }
            });

            var docId = workspace.CurrentSolution.GetDocumentIdsWithFilePath("test.cs").First();

            // ignore void buffers
            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
            });

            var sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("class C {}", sourceText.ToString());

            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
                FileName = "test.cs"
            });

            sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("class C {}", sourceText.ToString());

            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
                Buffer = "// c", FileName = "some_other_file.cs"
            });

            sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("class C {}", sourceText.ToString());

            // valid updates
            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
                FileName = "test.cs", Buffer = "interface I {}"
            });

            sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("interface I {}", sourceText.ToString());

            await workspace.BufferManager.UpdateBuffer(new OmniSharp.Models.Request()
            {
                FileName = "test.cs", Buffer = ""
            });

            sourceText = await workspace.CurrentSolution.GetDocument(docId).GetTextAsync();

            Assert.Equal("", sourceText.ToString());
        }
Пример #24
0
        private async Task <IEnumerable <string> > FindCompletionsAsync(string source)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(source);
            var controller = new OmnisharpController(workspace, null);
            var request    = CreateRequest(source);
            var response   = await controller.AutoComplete(request);

            var completions = response as IEnumerable <AutoCompleteResponse>;

            return(completions.Select(completion => BuildCompletion(completion)));
        }
        private async Task AssertPosition(string fileContent, NavigateDirection navigateDirection)
        {
            var fileContentNoPercentMarker = TestHelpers.RemovePercentMarker(fileContent);
            var workspace = TestHelpers.CreateSimpleWorkspace(fileContentNoPercentMarker, "test.cs");
            var response  = await SendRequest(workspace, "test.cs", fileContentNoPercentMarker, navigateDirection);

            var finalCursorLineColumn = TestHelpers.GetLineAndColumnFromPercent(TestHelpers.RemoveDollarMarker(fileContent));

            Assert.Equal(finalCursorLineColumn.Line, response.Line);
            Assert.Equal(finalCursorLineColumn.Column, response.Column);
        }
Пример #26
0
        private async Task <QuickFixResponse> FindUsages(Dictionary <string, string> sources, string currentFile, bool onlyThisFile, bool excludeDefinition = false)
        {
            var workspace  = TestHelpers.CreateSimpleWorkspace(sources);
            var controller = new OmnisharpController(workspace, null);
            var request    = CreateRequest(sources[currentFile], currentFile, excludeDefinition);

            request.OnlyThisFile = onlyThisFile;
            var bufferFilter = new UpdateBufferFilter(workspace);

            bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
            return(await controller.FindUsages(request));
        }
Пример #27
0
        private async Task <IEnumerable <OmniSharpCodeAction> > FindRefactoringsAsync(string source)
        {
            var request = CreateGetCodeActionsRequest(source);

            _host      = _host ?? TestHelpers.CreatePluginHost(new[] { typeof(RoslynCodeActionProvider).GetTypeInfo().Assembly, typeof(NRefactoryCodeActionProvider).GetTypeInfo().Assembly, typeof(GetCodeActionsService).GetTypeInfo().Assembly });
            _workspace = _workspace ?? await TestHelpers.CreateSimpleWorkspace(_host, request.Buffer, bufferPath);

            var controller = new GetCodeActionsService(_workspace, new ICodeActionProvider[] { new RoslynCodeActionProvider(), new NRefactoryCodeActionProvider() }, new FakeLoggerFactory());
            var response   = await controller.Handle(request);

            return(response.CodeActions);
        }
Пример #28
0
        private async Task <RunCodeActionResponse> RunRefactoringsAsync(string source, string identifier, bool wantsChanges = false)
        {
            var request = CreateRunCodeActionRequest(source, identifier, wantsChanges);

            _host      = _host ?? TestHelpers.CreatePluginHost(new[] { typeof(RoslynCodeActionProvider).GetTypeInfo().Assembly, typeof(NRefactoryCodeActionProvider).GetTypeInfo().Assembly, typeof(GetCodeActionsService).GetTypeInfo().Assembly });
            _workspace = _workspace ?? await TestHelpers.CreateSimpleWorkspace(_host, request.Buffer, bufferPath);

            var controller = new RunCodeActionService(_workspace, new ICodeActionProvider[] { new RoslynCodeActionProvider(), new NRefactoryCodeActionProvider() }, new FakeLoggerFactory());
            var response   = await controller.Handle(request);

            return(response);
        }
Пример #29
0
        private async Task <IEnumerable <ISymbol> > FindImplementations(string source)
        {
            var workspace    = TestHelpers.CreateSimpleWorkspace(source);
            var controller   = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var request      = CreateRequest(source);
            var bufferFilter = new UpdateBufferFilter(workspace);

            bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
            var implementations = await controller.FindImplementations(request);

            return(await TestHelpers.SymbolsFromQuickFixes(workspace, implementations.QuickFixes));
        }
Пример #30
0
        public async Task IncludesNamespaceForRegularCSharpSyntax()
        {
            var source1 = @"namespace Bar {
            class Foo {}
            }";

            var workspace = TestHelpers.CreateSimpleWorkspace(source1);

            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var response   = await controller.TypeLookup(new TypeLookupRequest { FileName = "dummy.cs", Line = 2, Column = 20 });

            Assert.Equal("Bar.Foo", response.Type);
        }