public void ExtractMethod_PrivateFunction()
        {
            const string inputCode = @"
Private Sub Foo()
    Dim x As Integer
    x = 1 + 2
End Sub";

            const string expectedCode = @"
Private Sub Foo()
    x = Bar()
End Sub

Private Function Bar() As Integer
    Dim x As Integer
    x = 1 + 2
    Bar = x
End Function

";

            var codePaneFactory = new CodePaneWrapperFactory();

            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe    = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         module = component.CodeModule;

            var mockHost = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var editor = new ActiveCodePaneEditor(module.VBE, codePaneFactory);
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(module.Parent), new Selection(4, 1, 4, 20));
            var model = new ExtractMethodModel(editor, parser.State.AllDeclarations, qualifiedSelection);

            model.Method.Accessibility = Accessibility.Private;
            model.Method.MethodName    = "Bar";
            model.Method.ReturnValue   = new ExtractedParameter("Integer", ExtractedParameter.PassedBy.ByVal, "x");
            model.Method.Parameters    = new List <ExtractedParameter>();

            var factory = SetupFactory(model);

            //act
            var refactoring = new ExtractMethodRefactoring(factory.Object, editor);

            refactoring.Refactor(qualifiedSelection);

            //assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
Пример #2
0
        public void IntroduceParameterRefactoring_ImplementsInterface_Reject()
        {
            //Input
            const string inputCode1 =
                @"Sub fizz(ByVal boo As Boolean)
End Sub";

            const string inputCode2 =
                @"Implements IClass1

Sub IClass1_fizz(ByVal boo As Boolean)
    Dim fizz As Date
End Sub";
            var selection = new Selection(4, 10, 4, 14);

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents.Item(1);

            vbe.Setup(v => v.ActiveCodePane).Returns(component.CodeModule.CodePane);

            var codePaneFactory = new CodePaneWrapperFactory();
            var mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            var module1 = project.Object.VBComponents.Item(0).CodeModule;
            var module2 = project.Object.VBComponents.Item(1).CodeModule;

            var messageBox = new Mock <IMessageBox>();

            messageBox.Setup(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(), It.IsAny <MessageBoxIcon>()))
            .Returns(DialogResult.No);

            //Act
            var refactoring = new IntroduceParameterRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), messageBox.Object);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(inputCode1, module1.Lines());
            Assert.AreEqual(inputCode2, module2.Lines());
        }
Пример #3
0
        public void MoveCloserToUsageRefactoring_ReferenceIsNotBeginningOfStatement_PassAsParam_ReferenceIsNotFirstLine()
        {
            //Input
            const string inputCode =
                @"Private bar As Boolean
Private Sub Foo()
    Baz True, _
        True, _
        bar
End Sub
Sub Baz(ByVal bat As Boolean, ByVal bas As Boolean, ByVal bac As Boolean)
End Sub";

            const string expectedCode =
                @"Private Sub Foo()

    Dim bar As Boolean
    Baz True, _
        True, _
        bar
End Sub
Sub Baz(ByVal bat As Boolean, ByVal bas As Boolean, ByVal bac As Boolean)
End Sub";
            var selection = new Selection(1, 1, 1, 1);

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            //Act
            var refactoring = new MoveCloserToUsageRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), null);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
Пример #4
0
        public void MoveCloserToUsageRefactoring_MultipleVariablesOneStatement_MoveLast()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim bar As Integer, _
        bat As Boolean, _
        bay As Date

    bar = 4
    bay = #1/13/2004#
End Sub";
            var selection = new Selection(4, 16, 4, 16);

            //Expectation
            const string expectedCode =
                @"Private Sub Foo()
    Dim bar As Integer,        bat As Boolean

    bar = 4

    Dim bay As Date
    bay = #1/13/2004#
End Sub";   // note: VBE will remove extra spaces

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            //Act
            var refactoring = new MoveCloserToUsageRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), null);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void IntroduceFieldRefactoring_PassInTarget_Nonvariable()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim bar As Boolean
End Sub";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var messageBox = new Mock <IMessageBox>();

            messageBox.Setup(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(), It.IsAny <MessageBoxIcon>()))
            .Returns(DialogResult.OK);

            //Act
            var refactoring = new IntroduceFieldRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), messageBox.Object);

            //Assert
            try
            {
                refactoring.Refactor(parser.State.AllUserDeclarations.First(d => d.DeclarationType != DeclarationType.Variable));
            }
            catch (ArgumentException e)
            {
                messageBox.Verify(m =>
                                  m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(),
                                         It.IsAny <MessageBoxIcon>()), Times.Once);

                Assert.AreEqual("Invalid declaration type\r\nParameter name: target", e.Message);
                Assert.AreEqual(inputCode, module.Lines());
                return;
            }

            Assert.Fail();
        }
Пример #6
0
        public void IntroduceParameterRefactoring_Properties_GetAndSet()
        {
            //Input
            const string inputCode =
                @"Property Get Foo(ByVal fizz As Boolean) As Variant
    Dim bar As Integer
    Foo = fizz
End Property

Property Set Foo(ByVal fizz As Boolean, ByVal buzz As Variant)
End Property";
            var selection = new Selection(2, 10, 2, 13);

            //Expectation
            const string expectedCode =
                @"Property Get Foo(ByVal fizz As Boolean, ByVal bar As Integer) As Variant
    
    Foo = fizz
End Property

Property Set Foo(ByVal fizz As Boolean, ByVal bar As Integer, ByVal buzz As Variant)
End Property";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            //Act
            var refactoring = new IntroduceParameterRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), null);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void IntroduceFieldRefactoring_MultipleVariablesInStatement_MoveLast()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo(ByVal buz As Integer, _
                  ByRef baz As Date)
    Dim bar As Boolean, _
        bat As Date, _
        bap As Integer
End Sub";
            var selection = new Selection(5, 10, 5, 13);

            //Expectation
            const string expectedCode =
                @"Private bap As Integer
Private Sub Foo(ByVal buz As Integer, _
                  ByRef baz As Date)
    Dim bar As Boolean, _
        bat As Date
        
End Sub";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            //Act
            var refactoring = new IntroduceFieldRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), null);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
Пример #8
0
        public void MoveCloserToUsageRefactoring_ReferencedInMultipleProcedures()
        {
            //Input
            const string inputCode =
                @"Private bar As Boolean
Private Sub Foo()
    bar = True
End Sub
Private Sub Bar()
    bar = True
End Sub";
            var selection = new Selection(1, 1, 1, 1);

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            var messageBox = new Mock <IMessageBox>();

            messageBox.Setup(m =>
                             m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(),
                                    It.IsAny <MessageBoxIcon>())).Returns(DialogResult.OK);

            //Act
            var refactoring = new MoveCloserToUsageRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), messageBox.Object);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            messageBox.Verify(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(),
                                          It.IsAny <MessageBoxIcon>()), Times.Once);
            Assert.AreEqual(inputCode, module.Lines());
        }
        public void IntroduceFieldRefactoring_NoFieldsInList_Function()
        {
            //Input
            const string inputCode =
                @"Private Function Foo() As Boolean
    Dim bar As Boolean
    Foo = True
End Function";
            var selection = new Selection(2, 10, 2, 13);

            //Expectation
            const string expectedCode =
                @"Private bar As Boolean
Private Function Foo() As Boolean
    
    Foo = True
End Function";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe             = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         project         = vbe.Object.VBProjects.Item(0);
            var         module          = project.VBComponents.Item(0).CodeModule;
            var         codePaneFactory = new CodePaneWrapperFactory();
            var         mockHost        = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

            //Act
            var refactoring = new IntroduceFieldRefactoring(parser.State, new ActiveCodePaneEditor(vbe.Object, codePaneFactory), null);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedCode, module.Lines());
        }