Example #1
0
        public async Task WhenIssueThatCannotBeAutomaticallyFixedIsAvailable_ThenDontTryToFixIt()
        {
            using (var host = GetHost(true))
            {
                var originalText =
                    @"
                    invalidSyntaxThatCannotBeFixedHere
                ";

                var expectedText =
                    @"
                    invalidSyntaxThatCannotBeFixedHere
                ";

                var testFilePath = CreateTestProjectWithDocument(host, originalText);

                var handler = host.GetRequestHandler <RunFixAllCodeActionService>(OmniSharpEndpoints.RunFixAll);

                await handler.Handle(new RunFixAllRequest
                {
                    Scope                        = FixAllScope.Document,
                    FileName                     = testFilePath,
                    WantsTextChanges             = true,
                    WantsAllCodeActionOperations = true
                });

                string textAfterFix = await GetContentOfDocumentFromWorkspace(host, testFilePath);

                AssertUtils.AssertIgnoringIndent(textAfterFix, expectedText);
            }
        }
Example #2
0
        public async Task Can_extract_method(bool roslynAnalyzersEnabled)
        {
            const string code =
                @"public class Class1
                {
                    public void Whatever()
                    {
                        [|Console.Write(""should be using System;"");|]
                    }
                }";
            const string expected =
                @"public class Class1
                {
                    public void Whatever()
                    {
                        NewMethod();
                    }

                    private static void NewMethod()
                    {
                        Console.Write(""should be using System;"");
                    }
                }";
            var response =
                (await RunRefactoringAsync(code, "Extract Method", isAnalyzersEnabled: roslynAnalyzersEnabled))
                .Single();
            var updatedText = await OmniSharpTestHost.Workspace.GetDocument(response.FileName).GetTextAsync(CancellationToken);

            AssertUtils.AssertIgnoringIndent(expected, updatedText.ToString());
        }
Example #3
0
        // This is specifically tested because has custom mapping logic in it.
        public async Task WhenTextContainsUnusedImports_ThenTheyCanBeAutomaticallyFixed()
        {
            using (var host = GetHost(true))
            {
                var originalText =
                    @"
                    using System.IO;
                ";

                var expectedText = @"";

                var testFilePath = CreateTestProjectWithDocument(host, originalText);

                var handler = host.GetRequestHandler <RunFixAllCodeActionService>(OmniSharpEndpoints.RunFixAll);

                await handler.Handle(new RunFixAllRequest
                {
                    Scope                        = FixAllScope.Document,
                    FileName                     = testFilePath,
                    WantsTextChanges             = true,
                    WantsAllCodeActionOperations = true
                });

                string textAfterFix = await GetContentOfDocumentFromWorkspace(host, testFilePath);

                AssertUtils.AssertIgnoringIndent(expectedText, textAfterFix);
            }
        }
Example #4
0
        public async Task Can_generate_overrides()
        {
            const string code =
                @"public class Class1[||]
                {
                }";

            const string expected =
                @"public class Class1
                {
                    public override bool Equals(object obj)
                    {
                        return base.Equals(obj);
                    }

                    public override int GetHashCode()
                    {
                        return base.GetHashCode();
                    }

                    public override string ToString()
                    {
                        return base.ToString();
                    }
                }";

            var response = await RunRefactoringAsync(code, "Generate overrides...");

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
        public async Task WhenFixAllItemsAreDefinedByFilter_ThenFixOnlyFilteredItems()
        {
            using (var host = GetHost(true))
            {
                var originalText =
                    @"
                    class C{}
                ";

                // If filtering isn't set, this should also add 'internal' etc which
                // should not appear now as result.
                var expectedText =
                    @"
                    class C { }
                ";

                var testFilePath = CreateTestProjectWithDocument(host, originalText);

                var handler = host.GetRequestHandler <RunFixAllCodeActionService>(OmniSharpEndpoints.RunFixAll);

                await handler.Handle(new RunFixAllRequest
                {
                    Scope                        = FixAllScope.Document,
                    FileName                     = testFilePath,
                    FixAllFilter                 = new[] { new FixAllItem("IDE0055", "Fix formatting") },
                    WantsTextChanges             = true,
                    WantsAllCodeActionOperations = true
                });

                string textAfterFix = await GetContentOfDocumentFromWorkspace(host, testFilePath);

                AssertUtils.AssertIgnoringIndent(expectedText, textAfterFix);
            }
        }
        public async Task Can_extract_method(bool roslynAnalyzersEnabled)
        {
            const string code =
                @"public class Class1
                {
                    public void Whatever()
                    {
                        [|Console.Write(""should be using System;"");|]
                    }
                }";
            const string expected =
                @"public class Class1
                {
                    public void Whatever()
                    {
                        NewMethod();
                    }

                    private static void NewMethod()
                    {
                        Console.Write(""should be using System;"");
                    }
                }";
            var response = await RunRefactoringAsync(code, "Extract Method", isAnalyzersEnabled : roslynAnalyzersEnabled);

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
        public async Task RespectFolderName_InExecutedCodeActions_AfterLiveChange(string expectedNamespace, params string[] relativePath)
        {
            var expected = "namespace " + expectedNamespace + " { }";
            var testFile = new TestFile(Path.Combine(TestAssets.Instance.TestFilesFolder, Path.Combine(relativePath)), @"namespace Xx$$x { }");

            using (var host = CreateOmniSharpHost(new[] { testFile }, null, TestAssets.Instance.TestFilesFolder))
            {
                var point             = testFile.Content.GetPointFromPosition();
                var runRequestHandler = host.GetRequestHandler <RunCodeActionService>(OmniSharpEndpoints.V2.RunCodeAction);

                var changedSolution = host.Workspace.CurrentSolution.WithProjectDefaultNamespace(host.Workspace.CurrentSolution.Projects.ElementAt(0).Id, "LiveChanged");
                host.Workspace.TryApplyChanges(changedSolution);

                var runRequest = new RunCodeActionRequest
                {
                    Line                         = point.Line,
                    Column                       = point.Offset,
                    FileName                     = testFile.FileName,
                    Identifier                   = $"Change namespace to '{expectedNamespace}'",
                    WantsTextChanges             = false,
                    WantsAllCodeActionOperations = true,
                    Buffer                       = testFile.Content.Code
                };
                var runResponse = await runRequestHandler.Handle(runRequest);

                AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)runResponse.Changes.First()).Buffer);
            }
        }
        public async Task Can_extract_base_class()
        {
            const string code =
                @"public class Class1[||]
                {
                    public string Property { get; set; }

                    public void Method() { }
                }";
            const string expected =
                @"public class NewBaseType
                {
                    public string Property { get; set; }

                    public void Method() { }
                }

                public class Class1 : NewBaseType
                {
                }";

            var response = await RunRefactoringAsync(code, "Extract base class...");

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
        public async Task RespectNamingConventions_InNamingStyleCodeFixProvider(string filename)
        {
            const string code =
                @"public class Foo
                {
                    private readonly System.String som$$ething;

                    public Foo()
                    {
                    }
                }";
            const string expected =
                @"public class Foo
                {
                    private readonly System.String xxx_something;

                    public Foo()
                    {
                    }
                }";

            var testFile = new TestFile(Path.Combine(TestAssets.Instance.TestFilesFolder, filename), code);

            using (var host = CreateOmniSharpHost(new[] { testFile }, new Dictionary <string, string>
            {
                ["FormattingOptions:EnableEditorConfigSupport"] = "true",
                ["RoslynExtensionsOptions:EnableAnalyzersSupport"] = "true"
            }, TestAssets.Instance.TestFilesFolder))
            {
                var point             = testFile.Content.GetPointFromPosition();
                var runRequestHandler = host.GetRequestHandler <RunCodeActionService>(OmniSharpEndpoints.V2.RunCodeAction);
                var runRequest        = new RunCodeActionRequest
                {
                    Line                         = point.Line,
                    Column                       = point.Offset,
                    FileName                     = testFile.FileName,
                    Identifier                   = "NamingStyleCodeFixProvider",
                    WantsTextChanges             = false,
                    WantsAllCodeActionOperations = true,
                    Buffer                       = testFile.Content.Code
                };
                var runResponse = await runRequestHandler.Handle(runRequest);

                AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)runResponse.Changes.First()).Buffer);
            }
        }
        public async Task Can_remove_unnecessary_usings(bool roslynAnalyzersEnabled)
        {
            const string code =
                @"using MyNamespace3;
                using MyNamespace4;
                using MyNamespace2;
                using System;
                u[||]sing MyNamespace1;

                public class c {public c() {Guid.NewGuid();}}";

            const string expected =
                @"using System;

                public class c {public c() {Guid.NewGuid();}}";

            var response = await RunRefactoringAsync(code, "Remove Unnecessary Usings", isAnalyzersEnabled : roslynAnalyzersEnabled);

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
Example #11
0
        public async Task WhenFileContainsFixableIssuesWithAnalyzersEnabled_ThenFixThemAll()
        {
            using (var host = GetHost(true))
            {
                var originalText = @"class C {}";

                var expectedText = @"internal class C { }";

                var testFilePath = CreateTestProjectWithDocument(host, originalText);

                string textBeforeFix = await GetContentOfDocumentFromWorkspace(host, testFilePath);

                var handler = host.GetRequestHandler <RunFixAllCodeActionService>(OmniSharpEndpoints.RunFixAll);

                var response = await handler.Handle(new RunFixAllRequest
                {
                    Scope                        = FixAllScope.Document,
                    FileName                     = testFilePath,
                    WantsTextChanges             = true,
                    WantsAllCodeActionOperations = true
                });

                string textAfterFix = await GetContentOfDocumentFromWorkspace(host, testFilePath);

                AssertUtils.AssertIgnoringIndent(expectedText, textAfterFix);

                var internalClassChange = response.Changes.OfType <ModifiedFileResponse>().Single().Changes.Single(x => x.NewText == "internal ");

                Assert.Equal(0, internalClassChange.StartLine);
                Assert.Equal(0, internalClassChange.StartColumn);
                Assert.Equal(0, internalClassChange.EndLine);
                Assert.Equal(0, internalClassChange.EndColumn);

                var formatFix = response.Changes.OfType <ModifiedFileResponse>().Single().Changes.Single(x => x.NewText == " ");

                Assert.Equal(0, formatFix.StartLine);
                Assert.Equal(9, formatFix.StartColumn);
                Assert.Equal(0, formatFix.EndLine);
                Assert.Equal(9, formatFix.EndColumn);
            }
        }
Example #12
0
        public async Task Can_generate_constructor_with_default_arguments()
        {
            const string code =
                @"public class Class1[||]
                {
                    public string PropertyHere { get; set; }
                }";
            const string expected =
                @"public class Class1
                {
                    public Class1(string propertyHere)
                    {
                        PropertyHere = propertyHere;
                    }

                    public string PropertyHere { get; set; }
                }";
            var response = await RunRefactoringAsync(code, "Generate constructor...");

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
Example #13
0
        public async Task Can_remove_unnecessary_usings(bool roslynAnalyzersEnabled)
        {
            const string code =
                @"using MyNamespace3;
                using MyNamespace4;
                using MyNamespace2;
                using System;
                u[||]sing MyNamespace1;

                public class c {public c() {Guid.NewGuid();}}";

            const string expected =
                @"using System;

                public class c {public c() {Guid.NewGuid();}}";

            var response =
                (await RunRefactoringAsync(code, "Remove Unnecessary Usings",
                                           isAnalyzersEnabled: roslynAnalyzersEnabled)).Single();
            var updatedText = await OmniSharpTestHost.Workspace.GetDocument(response.FileName).GetTextAsync(CancellationToken);

            AssertUtils.AssertIgnoringIndent(expected, updatedText.ToString());
        }
Example #14
0
        public async Task Can_extract_interface()
        {
            const string code =
                @"public class Class1[||]
                {
                    public string PropertyHere { get; set; }
                }";

            const string expected =
                @"public interface IClass1
                {
                    string PropertyHere { get; set; }
                }

                public class Class1 : IClass1
                {
                    public string PropertyHere { get; set; }
                }";

            var response = await RunRefactoringAsync(code, "Extract interface...");

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }
Example #15
0
        public async Task WhenFixAllIsScopedToDocumentAndProject_ThenOnlyFixInScopeInsteadOfEverything_DifferentProjects(FixAllScope scope)
        {
            using (var host = GetHost(true))
            {
                var originalIde0055Text =
                    @"
                    internal class InvalidFormatIDE0055ExpectedHere{}
                ";

                var expectedIde0055TextWithFixedFormat =
                    @"
                    internal class InvalidFormatIDE0055ExpectedHere { }
                ";

                var fileInScope = CreateTestProjectWithDocument(host, originalIde0055Text);

                var fileNotInScope = CreateTestProjectWithDocument(host, originalIde0055Text);

                var handler = host.GetRequestHandler <RunFixAllCodeActionService>(OmniSharpEndpoints.RunFixAll);

                await handler.Handle(new RunFixAllRequest
                {
                    Scope                        = scope,
                    FileName                     = fileInScope,
                    FixAllFilter                 = new[] { new FixAllItem("IDE0055", "Fix formatting") },
                    WantsTextChanges             = true,
                    WantsAllCodeActionOperations = true
                });

                string textAfterFixInScope = await GetContentOfDocumentFromWorkspace(host, fileInScope);

                string textAfterNotInScope = await GetContentOfDocumentFromWorkspace(host, fileNotInScope);

                AssertUtils.AssertIgnoringIndent(expectedIde0055TextWithFixedFormat, textAfterFixInScope);
                AssertUtils.AssertIgnoringIndent(originalIde0055Text, textAfterNotInScope);
            }
        }
Example #16
0
        public async Task Can_generate_equals_for_object()
        {
            const string code =
                @"public class Class1[||]
                {
                    public string PropertyHere { get; set; }
                }";

            const string expected =
                @"public class Class1
                {
                    public string PropertyHere { get; set; }

                    public override bool Equals(object obj)
                    {
                        return obj is Class1 @class &&
                               PropertyHere == @class.PropertyHere;
                    }
                }";

            var response = await RunRefactoringAsync(code, "Generate Equals(object)...");

            AssertUtils.AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer);
        }