Exemplo n.º 1
0
        public async Task Should_register_MoveDocument_when_class_has_no_typeidentifier_and_namespace_is_assembly_based(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite.Test {
    public class {
}
";
            var          created   = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { created.Add(act); });

            await Task.WhenAll(new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("estSuit", StringComparison.Ordinal), 4),
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ))
                               .Select(sut.ComputeRefactoringsAsync));

            var moveActions = created.OfType <MoveDocumentCodeAction>().ToArray();

            Assert.Equal(1, moveActions.Length);

            Assert.Equal("Test", moveActions[0].FixParameters.Name);
            moveActions[0].FixParameters.Folders.ShouldBeEquivalentTo(new[] { "Test" });
        }
Exemplo n.º 2
0
        public async Task Should_not_register_when_namespace_declaration_is_nested(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite.Test {
    namespace Inner {
        public class {
        }
    }
}
";

            var cases = new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("Inner", StringComparison.Ordinal), 4),
                         projectName: "TestSuite",
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ));

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 3
0
        public async Task Should_not_register_MoveDocument_when_namespace_is_right_for_each_class(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite.Folder {
    public class Foo {}
    public class Test {}
    public class Other {}
}
";
            var          created   = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { created.Add(act); });

            await Task.WhenAll(new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("estSuit", StringComparison.Ordinal), 4),
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ))
                               .Select(sut.ComputeRefactoringsAsync));

            var moveActions = created.OfType <MoveDocumentCodeAction>().ToArray();

            Assert.Equal(0, moveActions.Length);
        }
Exemplo n.º 4
0
        public async Task Should_not_register_with_a_single_class_when_namespace_is_right_and_class_is_equal_to_document(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite.Folder {
    public class Test {}
}
";
            var          cases     = new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("estSuit", StringComparison.Ordinal), 4),
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ));

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 5
0
        public async Task _Should_not_register_when_context_is_not_namespace_identifier(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase = @"
using System;
namespace Test {
    public class Foo {}
}
";
            await Task.WhenAll(new[]
            {
                "namespace",
                "using",
                "System",
                "public",
                "class",
                "Foo"
            }.Select(c => GetContext(
                         TestCase,
                         new TextSpan(TestCase.IndexOf(c, StringComparison.Ordinal), c.Length),
                         interceptRegister: interceptor.Register
                         )).Select(sut.ComputeRefactoringsAsync));

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 6
0
        public async Task Should_register_ChangeNamespace_with_a_single_class_when_namespace_is_assembly_based_and_class_is_equal_to_document(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite.Folder {
    public class Test {}
}
";
            var          created   = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { created.Add(act); });

            await Task.WhenAll(new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("estSuit", StringComparison.Ordinal), 4),
                         folders: new[] { "Inner", "Nested" },
                         interceptRegister: interceptor.Register
                         ))
                               .Select(sut.ComputeRefactoringsAsync));

            var moveActions = created.OfType <ChangeNamespaceCodeAction>().ToArray();

            Assert.Equal(1, moveActions.Length);
            Assert.Equal("TestSuite.Folder", moveActions[0].FixParameters.NamespaceToFix);
            Assert.Equal("TestSuite.Inner.Nested", moveActions[0].FixParameters.NewNamespace);
            Assert.Equal($"Change Namespace to '{moveActions[0].FixParameters.NewNamespace}'", moveActions[0].Title);
        }
Exemplo n.º 7
0
        public async Task Should_not_register_when_context_is_not_a_constructor_parameter_or_parameter_name_is_missing(
            ICodeRefactoringContextSubscriber interceptor,
            InitializeFieldsFromConstructorCodeRefactoringProvider sut
            )
        {
            const string TestCase = @"
namespace Test {
    public class Foo {
        private class Inner { }
        public Foo() {}
        public Foo(int intParam, MissingParameterName) {
            var innerDecl = new DateTime(2016, 3, 21, 0, 0, 0);
        }
        public void MyMethod(MyType boolParam) { }
    }
}
";
            var          cases    = new[]
            {
                "MissingParameterName",
                "namespace",
                "Test",
                "public",
                "Foo",
                "class I",
                "Inner",
                "Foo(",
                "innerDecl",
                "DateTime",
                "MyMethod",
                "MyType",
                "boolParam",
                "()"
            }.Select(c => GetContext(
                         TestCase,
                         new TextSpan(TestCase.IndexOf(c, StringComparison.Ordinal) + 1, 0),
                         projectName: "TestSuite",
                         interceptRegister: interceptor.Register
                         ));

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 8
0
        public async Task Should_not_register_when_context_is_not_a_basetype(
            ICodeRefactoringContextSubscriber interceptor,
            MoveClassCodeRefactoringProvider sut
            )
        {
            const string CaseTest = @"
using System;
namespace Test {
    public class Foo {
        private class Inner { }
        public Foo() {
            var innerDecl = new DateTime(2016, 3, 21, 0, 0, 0);
        }
    }
    public class Other {
        public class InnerOther { }
    }
    private class Error { }
}
";
            var          cases    = new[]
            {
                "namespace",
                "using",
                "System",
                "Test",
                "Inner",
                "InnerOther",
                "Error",
                "innerDecl",
                "DateTime",
                "new"
            }.Select(c => GetContext(
                         CaseTest,
                         new TextSpan(CaseTest.IndexOf(c, StringComparison.Ordinal), c.Length),
                         projectName: "TestSuite",
                         interceptRegister: interceptor.Register
                         ));

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 9
0
        public async Task Should_register_with_correct_parameters(
            ICodeRefactoringContextSubscriber interceptor,
            InitializeFieldsFromConstructorCodeRefactoringProvider sut
            )
        {
            const string TestCase = @"
namespace Test {
    public class Foo {
        public Foo(int intParam) {
            field = otherField;
            intParam = 12;
        }
    }
}
";
            var          cases    = new[]
            {
                "intParam"
            }.Select(c => GetContext(
                         TestCase,
                         new TextSpan(TestCase.IndexOf(c, StringComparison.Ordinal) + 1, 0),
                         projectName: "TestSuite",
                         interceptRegister: interceptor.Register
                         )).ToArray();

            var actions = new List <InitializeFieldsFromConstructorCodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { actions.Add(act as InitializeFieldsFromConstructorCodeAction); });

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);

            Assert.Equal("Initialize field '_intParam'", actions[0].Title);

            Assert.Equal(cases[0].Document.Id, actions[0].FixParameters.DocumentId);
            Assert.Equal(cases[0].Document.Project.Solution.Id, actions[0].FixParameters.Solution.Id);
            Assert.Equal("intParam", actions[0].FixParameters.ParameterName);
            Assert.Equal(cases[0].Span, actions[0].FixParameters.Span);
        }
Exemplo n.º 10
0
        public async Task Should_register_MoveDocument_when_namespace_is_assembly_based_for_each_class(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace TestSuite {
    public class Foo {}
    public class Test {}
    public class Other {}
}
";
            var          created   = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { created.Add(act); });

            await Task.WhenAll(new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("estSuit", StringComparison.Ordinal), 4),
                         folders: new[] { "Folder", "Inner" },
                         interceptRegister: interceptor.Register
                         ))
                               .Select(sut.ComputeRefactoringsAsync));

            var moveActions = created.OfType <MoveDocumentCodeAction>().ToArray();

            Assert.Equal(3, moveActions.Length);

            Assert.Equal("Foo", moveActions[0].FixParameters.Name);
            moveActions[0].FixParameters.Folders.ShouldBeEquivalentTo(new string[] { });
            Assert.Equal($@"Move File to '{moveActions[0].FixParameters.Name}.cs'", moveActions[0].Title);

            Assert.Equal("Test", moveActions[1].FixParameters.Name);
            moveActions[1].FixParameters.Folders.ShouldBeEquivalentTo(new string[] { });
            Assert.Equal($@"Move File to '{moveActions[1].FixParameters.Name}.cs'", moveActions[1].Title);

            Assert.Equal("Other", moveActions[2].FixParameters.Name);
            moveActions[2].FixParameters.Folders.ShouldBeEquivalentTo(new string[] { });
            Assert.Equal($@"Move File to '{moveActions[2].FixParameters.Name}.cs'", moveActions[2].Title);
        }
Exemplo n.º 11
0
        public async Task Should_register_RenameDocument_when_namespace_is_not_assembly_name_based(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase1 = @"
using System;
namespace Tests {
    public class Foo {}
    public class Test {}
    public class Other {}
}
";
            var          created   = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { created.Add(act); });

            await Task.WhenAll(new[]
            {
                TestCase1
            }.Select(c => GetContext(
                         c,
                         new TextSpan(c.IndexOf("est", StringComparison.Ordinal), 3),
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ))
                               .Select(sut.ComputeRefactoringsAsync));

            var moveActions = created.OfType <RenameDocumentCodeAction>().ToArray();

            Assert.Equal(2, moveActions.Length);

            var action = moveActions[0];

            Assert.NotNull(action);
            Assert.Equal("Foo", action.FixParameters.Name);

            action = moveActions[1];
            Assert.NotNull(action);
            Assert.Equal("Other", action.FixParameters.Name);
        }
Exemplo n.º 12
0
        public async Task Should_not_register_if_parameter_is_already_assigned(
            ICodeRefactoringContextSubscriber interceptor,
            InitializeFieldsFromConstructorCodeRefactoringProvider sut
            )
        {
            const string TestCase = @"
namespace Test {
    public class Foo {
        public Foo(int intParam) {
            _tmp = intParam;
        }
    }
    public class Foo2 {
        public Foo(bool boolParam) {
            this.tmp = boolParam;
        }
    }
}
";
            var          cases    = new[]
            {
                "intParam",
                "boolParam"
            }.Select(c => GetContext(
                         TestCase,
                         new TextSpan(TestCase.IndexOf(c, StringComparison.Ordinal) + 1, 0),
                         projectName: "TestSuite",
                         interceptRegister: interceptor.Register
                         ));

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 13
0
        public async Task Should_not_register_with_a_single_class_when_namespace_is_equal_to_assembly_and_class_is_equal_to_document(
            ICodeRefactoringContextSubscriber interceptor,
            FixNamespaceCodeRefactoringProvider sut
            )
        {
            const string TestCase = @"
using System;
namespace TestSuite {
    public class Test {}
}
";
            var          context  = GetContext(
                TestCase,
                new TextSpan(TestCase.IndexOf("Suite", StringComparison.Ordinal), 4),
                interceptRegister: interceptor.Register
                );

            await sut.ComputeRefactoringsAsync(context);

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 14
0
        public async Task Should_register_rename_and_move_document(
            ICodeRefactoringContextSubscriber interceptor,
            MoveClassCodeRefactoringProvider sut
            )
        {
            const string CaseTest = @"
using System;
namespace TestSuite.Inner {
    public class Foo {
    }
}
";
            var          context  = GetContext(
                CaseTest,
                new TextSpan(CaseTest.IndexOf("Foo", StringComparison.Ordinal), "Foo".Length),
                projectName: "TestSuite",
                documentName: "Other",
                folders: new[] { "Folder" },
                interceptRegister: interceptor.Register
                );

            var actions = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { actions.Add(act); });

            await sut.ComputeRefactoringsAsync(context);

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustHaveHappened(Repeated.Exactly.Twice);

            Assert.Equal("Rename File to 'Foo.cs'", actions[0].Title);
            Assert.Equal("Move File to '\\Inner\\Foo.cs'", actions[1].Title);
        }
Exemplo n.º 15
0
        public async Task Should_register_move_class_when_more_than_one_declaration_exist_and_context_is_a_basetype_and_target_file_by_convention_does_not_exist(
            ICodeRefactoringContextSubscriber interceptor,
            MoveClassCodeRefactoringProvider sut
            )
        {
            const string CaseTest = @"
using System;
namespace TestSuite {
    namespace Inner {
        public class Foo {
            private class Inner { }
        }
    }
    namespace Folder {
        public class OtherClass {
            public class InnerOther { }
        }
        public struct OtherStruct {
        }
        public enum OtherEnum {
        }
    }
}
namespace Test {
    public class Alone {
        private class Inner { }
    }
}
";
            var          cases    = new[]
            {
                "Foo",         //class Foo can only be extracted on folder Inner not on itself
                "OtherClass",  //class OtherClass has right namespace, so can only be extracted in current folder
                "OtherStruct", //same as before
                "OtherEnum",   //same as before
                "Alone"        //class Alone is from a namespace not based on assembly so can be only extracted in current folder
            }.Select(c => GetContext(
                         CaseTest,
                         new TextSpan(CaseTest.IndexOf(c, StringComparison.Ordinal), c.Length),
                         projectName: "TestSuite",
                         documentName: "Foo",
                         folders: new[] { "Folder" },
                         interceptRegister: interceptor.Register
                         ));

            var actions = new List <CodeAction>();

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .Invokes((CodeAction act) => { actions.Add(act); });

            foreach (var context in cases)
            {
                await sut.ComputeRefactoringsAsync(context);
            }

            A
            .CallTo(() => interceptor.Register(A <CodeAction> .Ignored))
            .MustHaveHappened(Repeated.Exactly.Times(cases.Count()));

            Assert.Equal("Move class into '\\Inner\\Foo.cs'", actions[0].Title);
            Assert.Equal("Move class into '\\Folder\\OtherClass.cs'", actions[1].Title);
            Assert.Equal("Move class into '\\Folder\\OtherStruct.cs'", actions[2].Title);
            Assert.Equal("Move class into '\\Folder\\OtherEnum.cs'", actions[3].Title);
            Assert.Equal("Move class into '\\Folder\\Alone.cs'", actions[4].Title);
        }