Exemplo n.º 1
0
        public void ExtractInterfaceRefactoring_NullModel_NoChanges()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 23, 1, 27);

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

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

                //Specify Params to remove
                var model = new ExtractInterfaceModel(state, qualifiedSelection);

                var presenter = new Mock <IExtractInterfacePresenter>();
                presenter.Setup(p => p.Show()).Returns(value: null);

                //SetupFactory
                var factory = SetupFactory(model);
                factory.Setup(f => f.Create()).Returns(presenter.Object);

                var refactoring = new ExtractInterfaceRefactoring(vbe.Object, null, factory.Object);
                refactoring.Refactor();

                Assert.AreEqual(1, vbe.Object.ActiveVBProject.VBComponents.Count());
                Assert.AreEqual(inputCode, component.CodeModule.Content());
            }
        }
Exemplo n.º 2
0
        public void ExtractInterfaceRefactoring_ImplementProc()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 23, 1, 27);

            //Expectation
            const string expectedCode =
                @"Implements ITestModule1

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub
";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

";

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

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

                //Specify Params to remove
                var model = new ExtractInterfaceModel(state, qualifiedSelection);
                foreach (var member in model.Members)
                {
                    member.IsSelected = true;
                }

                //SetupFactory
                var factory = SetupFactory(model);

                var refactoring = new ExtractInterfaceRefactoring(vbe.Object, null, factory.Object);
                refactoring.Refactor(qualifiedSelection);

                Assert.AreEqual(expectedInterfaceCode, component.Collection[1].CodeModule.Content());
                Assert.AreEqual(expectedCode, component.CodeModule.Content());
            }
        }
 protected override void OnExecute(object parameter)
 {
     try
     {
         _refactoring.Refactor(((CodeExplorerItemViewModel)parameter).Declaration);
     }
     catch (RefactoringAbortedException)
     { }
     catch (RefactoringException exception)
     {
         _failureNotifier.Notify(exception);
     }
 }
Exemplo n.º 4
0
        protected override void OnExecute(object parameter)
        {
            var activeSelection = SelectionService.ActiveSelection();

            if (!activeSelection.HasValue)
            {
                return;
            }

            var refactoring = new ExtractInterfaceRefactoring(_state, _state, _messageBox, _factory, RewritingManager, SelectionService);

            refactoring.Refactor(activeSelection.Value);
        }
Exemplo n.º 5
0
        public void ExtractInterfaceRefactoring_PassTargetIn()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 23, 1, 27);

            //Expectation
            const string expectedCode =
                @"Implements ITestModule1

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub
";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

";

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

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

                //Specify Params to remove
                var model = new ExtractInterfaceModel(state, qualifiedSelection);
                model.Members = new[] { model.Members.ElementAt(0) }.ToList();

                //SetupFactory
                var factory = SetupFactory(model);

                var refactoring = new ExtractInterfaceRefactoring(vbe.Object, null, factory.Object, rewritingManager);
                refactoring.Refactor(state.AllUserDeclarations.Single(s => s.DeclarationType == DeclarationType.ClassModule));

                Assert.AreEqual(expectedInterfaceCode, component.Collection[1].CodeModule.Content());
                Assert.AreEqual(expectedCode, component.CodeModule.Content());
            }
        }
        protected override void ExecuteImpl(object parameter)
        {
            if (Vbe.ActiveCodePane == null)
            {
                return;
            }

            using (var view = new ExtractInterfaceDialog())
            {
                var factory     = new ExtractInterfacePresenterFactory(Vbe, _state, view);
                var refactoring = new ExtractInterfaceRefactoring(Vbe, _state, _messageBox, factory);
                refactoring.Refactor();
            }
        }
Exemplo n.º 7
0
        public void ExtractInterfaceRefactoring_NullModel_NoChanges()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 23, 1, 27);

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_ClassModule, out component, selection);
            var         project  = vbe.Object.VBProjects.Item(0);
            var         mockHost = new Mock <IHostApplication>();

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

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

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

            //Specify Params to remove
            var model = new ExtractInterfaceModel(parser.State, qualifiedSelection);

            var presenter = new Mock <IExtractInterfacePresenter>();

            presenter.Setup(p => p.Show()).Returns(value: null);

            //SetupFactory
            var factory = SetupFactory(model);

            factory.Setup(f => f.Create()).Returns(presenter.Object);

            //Act
            var refactoring = new ExtractInterfaceRefactoring(vbe.Object, parser.State, null, factory.Object);

            refactoring.Refactor();

            //Assert
            Assert.AreEqual(1, project.VBComponents.Cast <VBComponent>().Count());   // somehow, the VBComponents Count mock isn't working
            Assert.AreEqual(inputCode, project.VBComponents.Item(0).CodeModule.Lines());
        }
        protected override void OnExecute(object parameter)
        {
            using (var activePane = Vbe.ActiveCodePane)
            {
                if (activePane == null || activePane.IsWrappingNullReference)
                {
                    return;
                }
            }

            using (var view = new ExtractInterfaceDialog(new ExtractInterfaceViewModel()))
            {
                var factory     = new ExtractInterfacePresenterFactory(Vbe, _state, view);
                var refactoring = new ExtractInterfaceRefactoring(Vbe, _messageBox, factory, _rewritingManager);
                refactoring.Refactor();
            }
        }
Exemplo n.º 9
0
        public void ExtractInterfaceRefactoring_ImplementProcAndFuncAndPropGetSetLet()
        {
            //Input
            const string inputCode = @"
Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property";

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

            //Expectation
            const string expectedCode = @"
Implements ITestModule1

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property

Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub

Private Function ITestModule1_Fizz(ByRef b As Variant) As Variant
    Err.Raise 5 'TODO implement interface member
End Function

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

Private Property Let ITestModule1_Buzz(ByRef value As Variant)
    Err.Raise 5 'TODO implement interface member
End Property

Private Property Set ITestModule1_Buzz(ByRef value As Variant)
    Err.Raise 5 'TODO implement interface member
End Property
";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(ByRef b As Variant) As Variant
End Function

Public Property Get Buzz() As Variant
End Property

Public Property Let Buzz(ByRef value As Variant)
End Property

Public Property Set Buzz(ByRef value As Variant)
End Property

";

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

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

                //Specify Params to remove
                var model = new ExtractInterfaceModel(state, qualifiedSelection);

                //SetupFactory
                var factory = SetupFactory(model);

                var refactoring = new ExtractInterfaceRefactoring(vbe.Object, null, factory.Object, rewritingManager);
                refactoring.Refactor(qualifiedSelection);

                Assert.AreEqual(expectedInterfaceCode, component.Collection[1].CodeModule.Content());
                Assert.AreEqual(expectedCode, component.CodeModule.Content());
            }
        }
Exemplo n.º 10
0
        public void ExtractInterfaceRefactoring_ImplementProcAndFuncAndPropGetSetLet()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property";

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

            //Expectation
            const string expectedCode =
                @"Implements ITestModule1


Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub

Private Function ITestModule1_Fizz(ByRef b As Variant) As Variant
    Err.Raise 5 'TODO implement interface member
End Function

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

Private Property Let ITestModule1_Buzz(ByRef value As Variant)
    Err.Raise 5 'TODO implement interface member
End Property

Private Property Set ITestModule1_Buzz(ByRef value As Variant)
    Err.Raise 5 'TODO implement interface member
End Property

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(ByRef b As Variant) As Variant
End Function

Public Property Get Buzz() As Variant
End Property

Public Property Let Buzz(ByRef value As Variant)
End Property

Public Property Set Buzz(ByRef value As Variant)
End Property

";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_ClassModule, out component, selection);
            var         project  = vbe.Object.VBProjects.Item(0);
            var         mockHost = new Mock <IHostApplication>();

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

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

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

            //Specify Params to remove
            var model = new ExtractInterfaceModel(parser.State, qualifiedSelection);

            foreach (var member in model.Members)
            {
                member.IsSelected = true;
            }

            //SetupFactory
            var factory = SetupFactory(model);

            //Act
            var refactoring = new ExtractInterfaceRefactoring(vbe.Object, parser.State, null, factory.Object);

            refactoring.Refactor(qualifiedSelection);

            //Assert
            Assert.AreEqual(expectedInterfaceCode, project.VBComponents.Item(1).CodeModule.Lines());
            Assert.AreEqual(expectedCode, project.VBComponents.Item(0).CodeModule.Lines());
        }
Exemplo n.º 11
0
        public void ExtractInterfaceRefactoring_PassTargetIn()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";
            var selection = new Selection(1, 23, 1, 27);

            //Expectation
            const string expectedCode =
                @"Implements ITestModule1


Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_ClassModule, out component, selection);
            var         project  = vbe.Object.VBProjects.Item(0);
            var         mockHost = new Mock <IHostApplication>();

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

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

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

            //Specify Params to remove
            var model = new ExtractInterfaceModel(parser.State, qualifiedSelection);

            model.Members.ElementAt(0).IsSelected = true;

            //SetupFactory
            var factory = SetupFactory(model);

            //Act
            var refactoring = new ExtractInterfaceRefactoring(vbe.Object, parser.State, null, factory.Object);

            refactoring.Refactor(parser.State.AllUserDeclarations.Single(s => s.DeclarationType == DeclarationType.ClassModule));

            //Assert
            Assert.AreEqual(expectedInterfaceCode, project.VBComponents.Item(1).CodeModule.Lines());
            Assert.AreEqual(expectedCode, project.VBComponents.Item(0).CodeModule.Lines());
        }
Exemplo n.º 12
0
        public void ExtractInterfaceRefactoring_ImplementProcAndFunc_IgnoreProperties()
        {
            //Input
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property";

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

            //Expectation
            const string expectedCode =
                @"Implements ITestModule1

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(b) As Variant
End Function

Public Property Get Buzz()
End Property

Public Property Let Buzz(value)
End Property

Public Property Set Buzz(value)
End Property

Private Sub ITestModule1_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
    Err.Raise 5 'TODO implement interface member
End Sub

Private Function ITestModule1_Fizz(ByRef b As Variant) As Variant
    Err.Raise 5 'TODO implement interface member
End Function
";

            const string expectedInterfaceCode =
                @"Option Explicit

Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
End Sub

Public Function Fizz(ByRef b As Variant) As Variant
End Function

";

            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);

            //Specify Params to remove
            var model = new ExtractInterfaceModel(state, qualifiedSelection);

            foreach (var member in model.Members)
            {
                if (!member.FullMemberSignature.Contains("Property"))
                {
                    member.IsSelected = true;
                }
            }

            //SetupFactory
            var factory = SetupFactory(model);

            var refactoring = new ExtractInterfaceRefactoring(vbe.Object, null, factory.Object);

            refactoring.Refactor(qualifiedSelection);

            Assert.AreEqual(expectedInterfaceCode, component.Collection[1].CodeModule.Content());
            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }