Exemplo n.º 1
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);

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 2
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);

            //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";

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);
                refactoring.Refactor(qualifiedSelection);

                var rewriter = state.GetRewriter(component);
                Assert.AreEqual(expectedCode, rewriter.GetText());
            }
        }
Exemplo n.º 3
0
        public void ImplementInterface_PublicObject(string inputCode1)
        {
            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Property Get Class1_Foo() As Object
    Err.Raise 5 'TODO implement interface member
End Property

Private Property Set Class1_Foo(ByVal rhs As Object)
    Err.Raise 5 'TODO implement interface member
End Property
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);

                var refactoring = new ImplementInterfaceRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 4
0
                public void shouldReturnTrue()
                {
                    var inputCode = @"
Option Explicit
Private Sub Foo()
    Dim x As Integer
    x = 1 + 2
End Sub


Private Sub NewMethod
    dim a as string
    Debug.Print a
End Sub


Private Sub NewMethod4
    dim a as string

    Debug.Print a
End Sub";

                    QualifiedModuleName qualifiedModuleName;

                    using (var state = MockParser.ParseString(inputCode, out qualifiedModuleName))
                    {
                        var declarations = state.AllDeclarations;
                        var selection    = new Selection(4, 4, 5, 14);
                        QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                        var SUT = new ExtractMethodSelectionValidation(declarations);

                        var actual = SUT.withinSingleProcedure(qSelection.Value);

                        var expected = true;
                        Assert.AreEqual(expected, actual);
                    }
                }
Exemplo n.º 5
0
        public void ReloadComponent(string filePath)
        {
            HandleVbeSinkEvents = false;

            var codePane = Project.VBE.ActiveCodePane;

            if (codePane != null)
            {
                var codePaneWrapper = _wrapperFactory.Create(codePane);
                var selection       = new QualifiedSelection(new QualifiedModuleName(codePaneWrapper.CodeModule.Parent),
                                                             codePaneWrapper.Selection);
                string name = null;
                if (selection.QualifiedName.Component != null)
                {
                    name = selection.QualifiedName.Component.Name;
                }

                var component = Project.VBComponents.OfType <VBComponent>().FirstOrDefault(f => f.Name == filePath.Split('.')[0]);
                Project.VBComponents.RemoveSafely(component);

                var directory = CurrentRepository.LocalLocation;
                directory += directory.EndsWith("\\") ? string.Empty : "\\";
                Project.VBComponents.Import(directory + filePath);

                Project.VBE.SetSelection(selection.QualifiedName.Project, selection.Selection, name, _wrapperFactory);
            }
            else
            {
                var component = Project.VBComponents.OfType <VBComponent>().FirstOrDefault(f => f.Name == filePath.Split('.')[0]);
                Project.VBComponents.RemoveSafely(component);

                var directory = CurrentRepository.LocalLocation;
                directory += directory.EndsWith("\\") ? string.Empty : "\\";
                Project.VBComponents.Import(directory + filePath);
            }

            HandleVbeSinkEvents = true;
        }
Exemplo n.º 6
0
        public void DoesNotImplementInterface_SelectionNotOnImplementsStatement()
        {
            //Input
            const string inputCode1 =
                @"Public Sub Foo()
End Sub";

            const string inputCode2 =
                @"Implements Class1
   
";

            //Expectation
            const string expectedCode =
                @"Implements Class1
   
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), new Selection(2, 2));

                var refactoring = TestRefactoring(vbe.Object, rewritingManager, state);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 7
0
        public void ImplementInterface_Procedure()
        {
            //Input
            const string inputCode1 =
                @"Public Sub Foo()
End Sub";

            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Sub Class1_Foo()
    Err.Raise 5 'TODO implement interface member
End Sub
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);

                var refactoring = new ImplementInterfaceRefactoring(vbe.Object, state, null);
                refactoring.Refactor(qualifiedSelection);

                var rewriter = state.GetRewriter(component);
                Assert.AreEqual(expectedCode, rewriter.GetText());
            }
        }
        public void shouldClassifyDeclarations()
        {
            QualifiedModuleName   qualifiedModuleName;
            RubberduckParserState state;

            MockParser.ParseString(internalVariable, out qualifiedModuleName, out state);
            var declarations = state.AllDeclarations;

            var selection = new Selection(8, 1, 12, 24);
            QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

            var extractedMethod     = new Mock <IExtractedMethod>();
            var extractedMethodProc = new Mock <IExtractMethodProc>();
            var paramClassify       = new Mock <IExtractMethodParameterClassification>();

            var SUT = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

            SUT.extract(declarations, qSelection.Value, selectedCode);

            paramClassify.Verify(
                pc => pc.classifyDeclarations(qSelection.Value, It.IsAny <Declaration>()),
                Times.Exactly(3));
        }
            public void shouldProvideThePositionForTheMethodCall()
            {
                QualifiedModuleName   qualifiedModuleName;
                RubberduckParserState state;

                MockParser.ParseString(inputCode, out qualifiedModuleName, out state);
                var declarations = state.AllDeclarations;

                var selection = new Selection(10, 1, 12, 17);
                QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                var emr             = new Mock <IExtractMethodRule>();
                var extractedMethod = new Mock <IExtractedMethod>();
                var paramClassify   = new Mock <IExtractMethodParameterClassification>();
                var SUT             = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

                SUT.extract(declarations, qSelection.Value, selectedCode);

                var expected = new Selection(10, 1, 10, 1);
                var actual   = SUT.PositionForMethodCall;

                Assert.AreEqual(expected, actual, "Call should have been at row " + expected + " but is at " + actual);
            }
Exemplo n.º 10
0
                public void shouldThrowAnException()
                {
                    QualifiedModuleName qualifiedModuleName;

                    using (var state = MockParser.ParseString(inputCode, out qualifiedModuleName))
                    {
                        var declarations = state.AllDeclarations;

                        var selection = new Selection(21, 1, 22, 17);
                        QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                        var emr             = new Mock <IExtractMethodRule>();
                        var extractedMethod = new Mock <IExtractedMethod>();
                        var paramClassify   = new Mock <IExtractMethodParameterClassification>();
                        var SUT             = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

                        //Act
                        SUT.extract(declarations, qSelection.Value, selectedCode);

                        //Assert
                        // ExpectedException
                    }
                }
Exemplo n.º 11
0
        public void Presenter_NullTarget_ReturnsNull()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 15, 1, 15);

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleModule(inputCode, ComponentType.ClassModule, out component, selection);
            var          state = MockParser.CreateAndParse(vbe.Object);

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

            var model = new ExtractInterfaceModel(state, qualifiedSelection);

            var view = new Mock <IRefactoringDialog <ExtractInterfaceViewModel> >();

            view.SetupGet(v => v.ViewModel).Returns(new ExtractInterfaceViewModel());
            var presenter = new ExtractInterfacePresenter(view.Object, model);

            Assert.AreEqual(null, presenter.Show());
        }
Exemplo n.º 12
0
            public void shouldProvideAListOfDimsNoLongerNeededInTheContainingMethod()
            {
                QualifiedModuleName qualifiedModuleName;

                using (var state = MockParser.ParseString(inputCode, out qualifiedModuleName))
                {
                    var declarations = state.AllDeclarations;

                    var selection = new Selection(10, 1, 12, 17);
                    QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);
                    var extractDecl = declarations.Where(x => x.IdentifierName.Equals("y"));

                    var emr             = new Mock <IExtractMethodRule>();
                    var extractedMethod = new Mock <IExtractedMethod>();
                    var paramClassify   = new Mock <IExtractMethodParameterClassification>();
                    paramClassify.Setup(pc => pc.DeclarationsToMove).Returns(extractDecl);
                    var SUT = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);
                    SUT.extract(declarations, qSelection.Value, selectedCode);

                    Assert.AreEqual(1, SUT.DeclarationsToMove.Count());
                    Assert.IsTrue(SUT.DeclarationsToMove.Contains(extractDecl.First()), "The selectionToRemove should contain the Declaration being moved");
                }
            }
Exemplo n.º 13
0
        public void MoveCloserToUsageRefactoring_WorksWithNamedParametersAndStatementSeparaters()
        {
            //Input
            const string inputCode =
                @"Private foo As Long

Public Sub Test(): SomeSub someParam:=foo: End Sub

Public Sub SomeSub(ByVal someParam As Long)
    Debug.Print someParam
End Sub";

            var          selection    = new Selection(1, 1, 1, 1);
            const string expectedCode =
                @"
Public Sub Test()
    Dim foo As Long

SomeSub someParam:=foo
End Sub

Public Sub SomeSub(ByVal someParam As Long)
    Debug.Print someParam
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);
            var          state = MockParser.CreateAndParse(vbe.Object);

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

            var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);

            refactoring.Refactor(qualifiedSelection);

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
Exemplo n.º 14
0
        public void MoveCloserToUsageRefactoring_MultipleFieldsOneStatement_MoveLast()
        {
            //Input
            const string inputCode =
                @"Private bar As Integer, _
          bat As Boolean, _
          bay As Date

Private Sub Foo()
    bay = #1/13/2004#
End Sub";
            var selection = new Selection(6, 6);

            //Expectation
            const string expectedCode =
                @"Private bar As Integer, _
          bat As Boolean

Private Sub Foo()
    Dim bay As Date
    bay = #1/13/2004#
End Sub";

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
            public void shouldCallApplyOnExtraction()
            {
                QualifiedModuleName   qualifiedModuleName;
                RubberduckParserState state;

                MockParser.ParseString(inputCode, out qualifiedModuleName, out state);

                var declarations = state.AllDeclarations;
                var selection    = new Selection(4, 4, 4, 14);
                QualifiedSelection?qualifiedSelection = new QualifiedSelection(qualifiedModuleName, selection);
                var             codeModule            = new Mock <ICodeModuleWrapper>();
                var             extractedMethod       = new Mock <IExtractedMethod>();
                Action <object> onParseRequest        = (obj) => { };

                extractedMethod.Setup(em => em.MethodName).Returns("Bar");

                var paramClassify = new Mock <IExtractMethodParameterClassification>();
                var model         = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

                model.extract(declarations, qualifiedSelection.Value, extractCode);
                var insertCode = "Bar x";

                Func <QualifiedSelection?, string, IExtractMethodModel> createMethodModel = (q, s) => { return(model); };

                codeModule.SetupGet(cm => cm.QualifiedSelection).Returns(qualifiedSelection);
                codeModule.Setup(cm => cm.GetLines(selection)).Returns(extractCode);
                codeModule.Setup(cm => cm.DeleteLines(It.IsAny <Selection>()));
                codeModule.Setup(cm => cm.InsertLines(It.IsAny <int>(), It.IsAny <String>()));

                var extraction = new Mock <IExtractMethodExtraction>();

                var SUT = new ExtractMethodRefactoring(codeModule.Object, onParseRequest, createMethodModel, extraction.Object);

                SUT.Refactor();

                extraction.Verify(ext => ext.apply(It.IsAny <ICodeModuleWrapper>(), It.IsAny <IExtractMethodModel>(), It.IsAny <Selection>()));
            }
Exemplo n.º 16
0
        public void AcquireTarget_MethodRenamingAtSameComponent_CorrectTargetChosen()
        {
            // arange
            var symbolSelection    = new Selection(8, 1, 8, 16);
            var selectedComponent  = new QualifiedModuleName("TestProject", "TestModule");
            var qualifiedSelection = new QualifiedSelection(selectedComponent, symbolSelection);

            // just for passing null reference exception
            var context = new Mock <ParserRuleContext>();

            context.SetupGet(c => c.Start.Line).Returns(1);
            context.SetupGet(c => c.Stop.Line).Returns(2);
            context.SetupGet(c => c.Stop.Text).Returns("Four");

            // simulate all the components and symbols
            var          member         = new QualifiedMemberName(selectedComponent, "fakeModule");
            const string identifierName = "Foo";

            AddDeclarationItem(context, symbolSelection, member, DeclarationType.Procedure, identifierName);
            AddDeclarationItem(context, new Selection(1, 1, 1, 16), member, DeclarationType.Procedure);
            AddDeclarationItem(context, new Selection(1, 1, 1, 1), member, DeclarationType.Module);

            // allow Moq to set the Target property
            _view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
            _view.SetupProperty(view => view.Target);

            //act
            Assert.Inconclusive("This test is broken");
            //var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
            //presenter.Show();

            //assert
            var retVal = _view.Object.Target;

            Assert.AreEqual(symbolSelection, retVal.Selection, "Returns only the declaration on the desired selection");
            Assert.AreEqual(identifierName, retVal.IdentifierName);
        }
Exemplo n.º 17
0
        public void MoveCloserToUsageRefactoring_MultipleVariablesOneStatement_MoveSecond()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim bar As Integer, _
        bat As Boolean, _
        bay As Date

    bar = 1
    bat = True
End Sub";
            var selection = new Selection(3, 16, 3, 16);

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

    bar = 1

    Dim bat As Boolean
    bat = True
End Sub";   // note: VBE will remove extra spaces

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);
            var          state = MockParser.CreateAndParse(vbe.Object);

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

            var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);

            refactoring.Refactor(qualifiedSelection);

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
Exemplo n.º 18
0
        public void MoveCloserToUsageRefactoring_MultipleFields_MoveSecond()
        {
            //Input
            const string inputCode =
                @"Private bar As Integer
Private bat As Boolean
Private bay As Date

Private Sub Foo()
    bat = True
End Sub";
            var selection = new Selection(2, 1);

            //Expectation
            const string expectedCode =
                @"Private bar As Integer
Private bay As Date

Private Sub Foo()
    Dim bat As Boolean
bat = True
End Sub";

            IVBComponent component;
            var          vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);
                refactoring.Refactor(qualifiedSelection);

                var rewriter = state.GetRewriter(component);
                Assert.AreEqual(expectedCode, rewriter.GetText());
            }
        }
Exemplo n.º 19
0
        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";

            IVBComponent component;
            var          vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new IntroduceFieldRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 20
0
            public void ShouldProvideThePositionForTheNewMethod()
            {
                using (var state = MockParser.ParseString(inputCode, out QualifiedModuleName qualifiedModuleName))
                {
                    var declarations = state.AllDeclarations;

                    var selection = new Selection(10, 1, 12, 17);
                    QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                    var emr                 = new Mock <IExtractMethodRule>();
                    var extractedMethod     = new Mock <IExtractedMethod>();
                    var extractedMethodProc = new Mock <IExtractMethodProc>();
                    var paramClassify       = new Mock <IExtractMethodParameterClassification>();
                    var SUT                 = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);
                    //Act
                    SUT.extract(declarations, qSelection.Value, selectedCode);

                    //Assert
                    var expected = new Selection(18, 1, 18, 1);
                    var actual   = SUT.PositionForNewMethod;

                    Assert.AreEqual(expected, actual, "Call should have been at row " + expected + " but is at " + actual);
                }
            }
Exemplo n.º 21
0
            public void shouldProvideTheSelectionOfLinesOfToRemove()
            {
                QualifiedModuleName   qualifiedModuleName;
                RubberduckParserState state;

                MockParser.ParseString(inputCode, out qualifiedModuleName, out state);
                var declarations = state.AllDeclarations;

                var selection = new Selection(10, 2, 12, 17);
                QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                var emr             = new Mock <IExtractMethodRule>();
                var extractedMethod = new Mock <IExtractedMethod>();
                var paramClassify   = new Mock <IExtractMethodParameterClassification>();
                var extractDecl     = declarations.Where(x => x.IdentifierName.Equals("y"));

                paramClassify.Setup(pc => pc.DeclarationsToMove).Returns(extractDecl);
                var extractedMethodModel = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

                //Act
                extractedMethodModel.extract(declarations, qSelection.Value, selectedCode);

                //Assert
                var actual        = extractedMethodModel.RowsToRemove;
                var yDimSelection = new Selection(5, 9, 5, 10);
                var expected      = new [] { selection, yDimSelection }
                .Select(x => new Selection(x.StartLine, 1, x.EndLine, 1));
                var missing = expected.Except(actual);
                var extra   = actual.Except(expected);

                missing.ToList().ForEach(x => Trace.WriteLine(string.Format("missing item {0}", x)));
                extra.ToList().ForEach(x => Trace.WriteLine(string.Format("extra item {0}", x)));

                Assert.AreEqual(expected.Count(), actual.Count(), "Selection To Remove doesn't have the right number of members");
                expected.ToList().ForEach(s => Assert.IsTrue(actual.Contains(s), string.Format("selection {0} missing from actual SelectionToRemove", s)));
            }
Exemplo n.º 22
0
        public void ImplementsInterfaceInUserFormModule()
        {
            const string interfaceCode = @"Option Explicit
Public Sub DoSomething()
End Sub
";
            const string initialCode   = @"Implements IInterface";
            const string expectedCode  = @"Implements IInterface

Private Sub IInterface_DoSomething()
    Err.Raise 5 'TODO implement interface member
End Sub
";

            var vbe = new MockVbeBuilder()
                      .ProjectBuilder("TestProject", ProjectProtection.Unprotected)
                      .AddComponent("IInterface", ComponentType.ClassModule, interfaceCode)
                      .AddComponent("Form1", ComponentType.UserForm, initialCode, Selection.Home)
                      .AddProjectToVbeBuilder()
                      .Build();

            var project   = vbe.Object.VBProjects[0];
            var component = project.VBComponents["Form1"];

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);

                var refactoring = new ImplementInterfaceRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 23
0
 internal IdentifierReference(
     QualifiedModuleName qualifiedName,
     Declaration parentScopingDeclaration,
     Declaration parentNonScopingDeclaration,
     string identifierName,
     Selection selection,
     ParserRuleContext context,
     Declaration declaration,
     bool isAssignmentTarget      = false,
     bool hasExplicitLetStatement = false,
     IEnumerable <IParseTreeAnnotation> annotations = null,
     bool isSetAssigned = false,
     bool isIndexedDefaultMemberAccess    = false,
     bool isNonIndexedDefaultMemberAccess = false,
     int defaultMemberRecursionDepth      = 0,
     bool isArrayAccess       = false,
     bool isProcedureCoercion = false,
     bool isInnerRecursiveDefaultMemberAccess = false)
 {
     ParentScoping                       = parentScopingDeclaration;
     ParentNonScoping                    = parentNonScopingDeclaration;
     QualifiedSelection                  = new QualifiedSelection(qualifiedName, selection);
     IdentifierName                      = identifierName;
     Context                             = context;
     Declaration                         = declaration;
     HasExplicitLetStatement             = hasExplicitLetStatement;
     IsAssignment                        = isAssignmentTarget;
     IsSetAssignment                     = isSetAssigned;
     IsIndexedDefaultMemberAccess        = isIndexedDefaultMemberAccess;
     IsNonIndexedDefaultMemberAccess     = isNonIndexedDefaultMemberAccess;
     DefaultMemberRecursionDepth         = defaultMemberRecursionDepth;
     IsArrayAccess                       = isArrayAccess;
     IsProcedureCoercion                 = isProcedureCoercion;
     Annotations                         = annotations ?? new List <IParseTreeAnnotation>();
     IsInnerRecursiveDefaultMemberAccess = isInnerRecursiveDefaultMemberAccess;
 }
Exemplo n.º 24
0
        private List <RegexSearchResult> SearchCurrentBlock(string searchPattern)
        {
            var declarationTypes = new[]
            {
                DeclarationType.Event,
                DeclarationType.Function,
                DeclarationType.Procedure,
                DeclarationType.PropertyGet,
                DeclarationType.PropertyLet,
                DeclarationType.PropertySet
            };

            var state  = _parser.State;
            var pane   = _vbe.ActiveCodePane;
            var module = pane.CodeModule;
            {
                var results = GetResultsFromModule(module, searchPattern);

                var     qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(module.Parent), pane.Selection);
                dynamic block     = state.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context.Parent;
                var     selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line, block.Stop.Column);
                return(results.Where(r => selection.Contains(r.Selection)).ToList());
            }
        }
Exemplo n.º 25
0
        public void RenameRefactoring_InterfaceRenamed_RejectPrompt()
        {
            //Input
            const string inputCode1 =
                @"Implements IClass1

Private Sub IClass1_DoSomething(ByVal a As Integer, ByVal b As String)
End Sub";
            const string inputCode2 =
                @"Public Sub DoSomething(ByVal a As Integer, ByVal b As String)
End Sub";

            var selection = new Selection(3, 23, 3, 27);

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("IClass1", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var state = MockParser.CreateAndParse(vbe.Object);

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(project.Object.VBComponents[0]), 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.No);

            var vbeWrapper = vbe.Object;
            var model      = new RenameModel(vbeWrapper, state, qualifiedSelection, messageBox.Object);

            Assert.AreEqual(null, model.Target);
        }
Exemplo n.º 26
0
        public void MoveCloserToUsageRefactoring_Variable_MultipleLines()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim _
    bar _
    As _
    Boolean
    Dim bat As Integer
    bar = True
End Sub";
            var selection = new Selection(4, 6, 4, 8);

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

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
        public void EncapsulateField_PresenterIsNull()
        {
            var inputCode =
                @"Private fizz As Variant";

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);
                var selectionService   = MockedSelectionService();
                var factory            = new Mock <IRefactoringPresenterFactory>();
                factory.Setup(f => f.Create <IEncapsulateFieldPresenter, EncapsulateFieldModel>(It.IsAny <EncapsulateFieldModel>()))
                .Returns(() => null);     // resolves ambiguous method overload

                var refactoring = TestRefactoring(rewritingManager, state, factory.Object, selectionService);

                Assert.Throws <InvalidRefactoringPresenterException>(() => refactoring.Refactor(qualifiedSelection));

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(inputCode, actualCode);
            }
        }
Exemplo n.º 28
0
        public void IntroduceFieldRefactoring_MultipleFieldsOnMultipleLines()
        {
            //Input
            const string inputCode =
                @"Public fizz As Integer
Public buzz As Integer
Private Sub Foo(ByVal buz As Integer, _
ByRef baz As Date)
Dim bar As Boolean
End Sub";
            var selection = new Selection(5, 8, 5, 20);

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

            IVBComponent component;
            var          vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = TestRefactoring(rewritingManager, state);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Exemplo n.º 29
0
        public void MoveCloserToUsageRefactoring_Variable_MultipleLines()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim _
    bar _
    As _
    Boolean
    Dim bat As Integer
    bar = True
End Sub";
            var selection = new Selection(4, 6, 4, 8);

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

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component, selection);
            var          state = MockParser.CreateAndParse(vbe.Object);

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

            var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);

            refactoring.Refactor(qualifiedSelection);

            var rewriter = state.GetRewriter(component);

            Assert.AreEqual(expectedCode, rewriter.GetText());
        }
Exemplo n.º 30
0
        public void ImplementsInterfaceInDocumentModule()
        {
            const string interfaceCode = @"Option Explicit
Public Sub DoSomething()
End Sub
";
            const string initialCode   = @"Implements IInterface";
            const string expectedCode  = @"Implements IInterface

Private Sub IInterface_DoSomething()
    Err.Raise 5 'TODO implement interface member
End Sub
";

            var vbe = new MockVbeBuilder()
                      .ProjectBuilder("TestProject", ProjectProtection.Unprotected)
                      .AddComponent("IInterface", ComponentType.ClassModule, interfaceCode)
                      .AddComponent("Sheet1", ComponentType.Document, initialCode, Selection.Home)
                      .MockVbeBuilder()
                      .Build();

            var project   = vbe.Object.VBProjects[0];
            var component = project.VBComponents["Sheet1"];

            var state = MockParser.CreateAndParse(vbe.Object);

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);

            var refactoring = new ImplementInterfaceRefactoring(vbe.Object, state, null);

            refactoring.Refactor(qualifiedSelection);

            var rewriter = state.GetRewriter(component);

            Assert.AreEqual(expectedCode, rewriter.GetText());
        }