public void ImplicitActiveSheetReference_Ignored_DoesNotReportRange()
        {
            const string inputCode =
                @"Sub foo()
    Dim arr1() As Variant

    '@Ignore ImplicitActiveSheetReference
    arr1 = Range(""A1:B2"")
End Sub
";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", "TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
                          .AddReference("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", true)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();

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

            GetExcelRangeDeclarations().ForEach(d => parser.State.AddDeclaration(d));

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

            var inspection        = new ImplicitActiveSheetReferenceInspection(vbe.Object, parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
Esempio n. 2
0
        public void MultipleDeclarations_QuickFixWorks_StaticVariables()
        {
            const string inputCode =
                @"Public Sub Foo()
    Static var1 As Integer, var2 As String
End Sub";

            const string expectedCode =
                @"Public Sub Foo()
Static var1 As Integer
Static var2 As String

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         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 inspection        = new MultipleDeclarationsInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            inspectionResults.First().QuickFixes.First().Fix();

            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void FindAllImplementations_StateNotReady_Aborts()
        {
            const string inputCode =
                @"Public Sub Foo()
End Sub

Private Sub Bar()
    Foo: Foo
    Foo
    Foo
End Sub";

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

            vbe.Setup(s => s.ActiveCodePane).Returns(value: null);
            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");
            }

            parser.State.SetStatusAndFireStateChanged(ParserState.ResolvedDeclarations);

            var vm      = new SearchResultsWindowViewModel();
            var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null);

            command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo"));

            Assert.IsFalse(vm.Tabs.Any());
        }
        public void ImplementInterface_PropertySet_WithParam()
        {
            //Input
            const string inputCode1 =
                @"Public Property Set Foo(a)
End Property";

            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Property Set Class1_Foo(ByRef a As Variant)
    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];

            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 ObjectVariableNotSet_ForFunctionAssignment_ReturnsResult()
        {
            var expectedResultCount = 2;
            var input =
                @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";
            var expectedCode =
                @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        Set CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        Set CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";

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

            var inspection        = new ObjectVariableNotSetInspection(state);
            var inspectionResults = inspection.GetInspectionResults().ToList();

            Assert.AreEqual(expectedResultCount, inspectionResults.Count);
            var fix = new UseSetKeywordForObjectAssignmentQuickFix(state);

            foreach (var result in inspectionResults)
            {
                fix.Fix(result);
            }

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
        public void RedundantByRefModifier_ReturnsResult_MultipleInterfaceImplementation()
        {
            const string inputCode1 =
                @"Sub Foo(ByRef arg1 As Integer)
End Sub";

            const string inputCode2 =
                @"Implements IClass1

Sub IClass1_Foo(ByRef arg1 As Integer)
End Sub";

            const string inputCode3 =
                @"Implements IClass1

Sub IClass1_Foo(ByRef arg1 As Integer)
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new RedundantByRefModifierInspection(state)
                {
                    Severity = CodeInspectionSeverity.Hint
                };
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
        public void ObsoleteCallStatement_ReturnsResults_SomeObsoleteCallStatements()
        {
            const string inputCode =
                @"Sub Foo()
    Call Goo(1, ""test"")
End Sub

Sub Goo(arg1 As Integer, arg1 As String)
    Foo
End Sub";

            //Arrange
            var settings = new Mock <IGeneralConfigService>();
            var config   = GetTestConfig();

            settings.Setup(x => x.LoadConfiguration()).Returns(config);

            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleStandardModule(inputCode, out component);
            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 inspection = new ObsoleteCallStatementInspection(parser.State);
            var inspector  = new Inspector(settings.Object, new IInspection[] { inspection });

            var inspectionResults = inspector.FindIssuesAsync(parser.State, CancellationToken.None).Result;

            Assert.AreEqual(1, inspectionResults.Count());
        }
Esempio n. 8
0
        public void ImplicitPublicMember_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(ByVal arg1 as Integer)
'Just an inoffensive little comment

End Sub";

            const string expectedCode =
                @"Public Sub Foo(ByVal arg1 as Integer)
'Just an inoffensive little comment

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         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 inspection        = new ImplicitPublicMemberInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            inspectionResults.First().QuickFixes.First().Fix();

            Assert.AreEqual(expectedCode, module.Lines());
        }
Esempio n. 9
0
        public void EmptyIfBlock_QuickFixRemovesElseIf_HasComment()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
        Dim d
        d = 0
    ElseIf False Then
        ' Im a comment
    Else
        Dim b
        b = 0
    End If
End Sub";
            const string expectedCode =
                @"Sub Foo()
    If True Then
        Dim d
        d = 0
    Else
        Dim b
        b = 0
    End If
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new EmptyIfBlockInspection(state);
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                new RemoveEmptyIfBlockQuickFix(state).Fix(inspectionResults.First());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Esempio n. 10
0
        public void IntroduceFieldRefactoring_PassInTarget_Nonvariable()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
Dim bar As Boolean
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                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);

                var refactoring = new IntroduceFieldRefactoring(vbe.Object, state, messageBox.Object);

                try
                {
                    refactoring.Refactor(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("target", e.ParamName);
                    var actual = state.GetRewriter(component).GetText();
                    Assert.AreEqual(inputCode, actual);
                    return;
                }

                Assert.Fail();
            }
        }
Esempio n. 11
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());
            }
        }
Esempio n. 12
0
        public void RemovesMiddleConstantInDeclarationListWithLineContinuations()
        {
            const string content  = @"
Private Const foo _
          As String = ""Something"", _
        bar As Long _
          = 42, _
        buzz As Integer = 12
";
            const string expected = @"
Private Const foo _
          As String = ""Something"", _
        buzz As Integer = 12
";
            IVBComponent component;
            var          vbe    = MockVbeBuilder.BuildFromSingleStandardModule(content, out component).Object;
            var          parser = MockParser.Create(vbe);

            parser.Parse(new CancellationTokenSource());
            if (parser.State.Status != ParserState.Ready)
            {
                Assert.Inconclusive("Parser isn't ready. Test cannot proceed.");
            }

            var declarations = parser.State.AllUserDeclarations;
            var target       = declarations.SingleOrDefault(d => d.IdentifierName == "bar" && d.DeclarationType == DeclarationType.Constant);

            if (target == null)
            {
                Assert.Inconclusive("Target constant was found in test code.");
            }

            var rewriter = parser.State.GetRewriter(target);

            rewriter.Remove(target);

            Assert.AreEqual(expected, rewriter.GetText());
        }
        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 = TestRefactoring(rewritingManager, state);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
        public void ObsoleteCallStatement_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Call Goo(1, ""test"")
End Sub

Sub Goo(arg1 As Integer, arg1 As String)
    Call Foo
End Sub";

            const string expectedCode =
                @"Sub Foo()
'@Ignore ObsoleteCallStatement
    Call Goo(1, ""test"")
End Sub

Sub Goo(arg1 As Integer, arg1 As String)
'@Ignore ObsoleteCallStatement
    Call Foo
End Sub";

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

            var inspection        = new ObsoleteCallStatementInspection(state);
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            var fix = new IgnoreOnceQuickFix(state, new[] { inspection });

            foreach (var result in inspectionResults)
            {
                fix.Fix(result);
            }

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
        public void WriteOnlyProperty_AddPropertyGetQuickFixWorks_MultipleParams()
        {
            const string inputCode =
                @"Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
End Property";

            const string expectedCode =
                @"Public Property Get Foo(ByRef value1 As Variant, ByVal value2 As Integer, ByRef value3 As Long, ByRef value4 As Date, ByVal value5 As Variant) As String
End Property
Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
End Property";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
                          .Build();
            var module = project.Object.VBComponents.Item(0).CodeModule;
            var vbe    = builder.AddProject(project).Build();

            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 inspection        = new WriteOnlyPropertyInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            inspectionResults.First().QuickFixes.Single(s => s is WriteOnlyPropertyQuickFix).Fix();

            Assert.AreEqual(expectedCode, module.Lines());
        }
Esempio n. 16
0
        public void Discovery_IgnoresNonAnnotatedTestMethods()
        {
            var testMethods = @"Public Sub TestMethod1()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("TestModule1", vbext_ComponentType.vbext_ct_StdModule, GetTestModuleInput + testMethods);

            var vbe      = builder.AddProject(project.Build()).Build();
            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");
            }

            Assert.IsFalse(UnitTestUtils.GetAllTests(vbe.Object, parser.State).Any());
        }
Esempio n. 17
0
        public void EmptyElseBlock_HasWhitespace()
        {
            const string inputcode =
                @"Sub Foo()
    If True Then
        Dim a as Long
        a = 0
    Else
    
    
    End If
End Sub";

            var vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputcode, out _);
            var state = MockParser.CreateAndParse(vbe.Object);

            var       inspection    = new EmptyConditionBlockInspection(state);
            var       inspector     = InspectionsHelper.GetInspector(inspection);
            var       actualResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;
            const int expectedCount = 1;

            Assert.AreEqual(expectedCount, actualResults.Count());
        }
Esempio n. 18
0
        public void AddsExpectedErrorTest_NullActiveCodePane()
        {
            var input = @"
Option Explicit
Option Private Module

'@TestModule
Private Assert As Object
";

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

            vbe.Setup(s => s.ActiveCodePane).Returns((ICodePane)null);

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

            var addTestMethodCommand = new AddTestMethodExpectedErrorCommand(vbe.Object, state);

            addTestMethodCommand.Execute(null);

            Assert.AreEqual(input, component.CodeModule.Content());
        }
Esempio n. 19
0
        public void UnassignedVariable_MultipleVariablesOnSingleLine_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
Dim var1 As Integer, var2 As Boolean
End Sub";

            const string expectedCode =
                @"Sub Foo()
Dim var1 As Integer
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new VariableNotAssignedInspection(state);
                new RemoveUnassignedIdentifierQuickFix(state).Fix(
                    inspection.GetInspectionResults(CancellationToken.None).Single(s => s.Target.IdentifierName == "var2"));

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Esempio n. 20
0
        public void EnclosingProcedureComesBeforeEnclosingModule()
        {
            string testCode = string.Format(@"
Public Sub Test()
    Dim {0} As Long
    Dim a As String * {0}
End Sub", BindingTargetName);

            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent(TestClassName, ComponentType.ClassModule, testCode);
            var enclosingProject = enclosingProjectBuilder.Build();

            builder.AddProject(enclosingProject);
            var vbe = builder.Build();

            using (var state = Parse(vbe))
            {
                var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Variable && d.IdentifierName == BindingTargetName);
                Assert.AreEqual(1, declaration.References.Count());
            }
        }
        public void VariableNotUsed_ReturnsResult_MultipleVariables_SomeAssigned()
        {
            const string inputCode =
                @"Sub Foo()
    Dim var1 as Integer
    var1 = 8

    Dim var2 as String

    Goo var1
End Sub

Sub Goo(ByVal arg1 As Integer)
End Sub";

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

            var inspection        = new VariableNotUsedInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
        public void WriteOnlyProperty_ReturnsResult_LetAndSet()
        {
            const string inputCode =
                @"Property Let Foo(value)
End Property

Property Set Foo(value)
End Property";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("MyClass", ComponentType.ClassModule, inputCode)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new WriteOnlyPropertyInspection(state);
                var inspectionResults = inspection.GetInspectionResults();

                Assert.AreEqual(2, inspectionResults.Count());
            }
        }
        public void ObsoleteTypeHint_QuickFixWorks_Field_SingleTypeHint()
        {
            const string inputCode =
                @"Public Foo!";

            const string expectedCode =
                @"Public Foo As Single";

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

            var inspection        = new ObsoleteTypeHintInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            var fix = new RemoveTypeHintsQuickFix(state);

            foreach (var result in inspectionResults)
            {
                fix.Fix(result);
            }

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
        public void MultilineParameter_ReturnsMultipleResults()
        {
            const string inputCode =
                @"Public Sub Foo( _
    ByVal _
    Var1 _
    As _
    Integer, _
    ByVal _
    Var2 _
    As _
    Date)
End Sub";

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

            var inspection        = new MultilineParameterInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(2, inspectionResults.Count());
        }
Esempio n. 25
0
        public void EmptyElseBlock_HasExecutableStatement()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
    Else
        Dim d
        d = 0
    End If
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var       inspection    = new EmptyElseBlockInspection(state);
                var       inspector     = InspectionsHelper.GetInspector(inspection);
                var       actualResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;
                const int expectedCount = 0;

                Assert.AreEqual(expectedCount, actualResults.Count());
            }
        }
Esempio n. 26
0
        public void MultipleDeclarations_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Public Sub Foo()
    Dim var1 As Integer, var2 As String
End Sub";

            const string expectedCode =
                @"Public Sub Foo()
'@Ignore MultipleDeclarations
    Dim var1 As Integer, var2 As String
End Sub";

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

            var inspection        = new MultipleDeclarationsInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            inspectionResults.First().QuickFixes.Single(s => s is IgnoreOnceQuickFix).Fix();
            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void LabelNotUsed_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
label1:
End Sub";

            const string expectedCode =
                @"Sub Foo()

End Sub";

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

            var inspection = new LineLabelNotUsedInspection(state);

            new RemoveUnusedDeclarationQuickFix(state).Fix(inspection.GetInspectionResults().First());

            var rewriter = state.GetRewriter(component);

            Assert.AreEqual(expectedCode, rewriter.GetText());
        }
Esempio n. 28
0
        public void ExtractInterface_CanExecute_ClassWithMembers_SameNameAsClassWithMembers()
        {
            var input =
                @"Sub foo()
End Sub";

            var builder = new MockVbeBuilder();
            var proj1   = builder.ProjectBuilder("TestProj1", ProjectProtection.Unprotected).AddComponent("Comp1", ComponentType.ClassModule, input, Selection.Home).Build();
            var proj2   = builder.ProjectBuilder("TestProj2", ProjectProtection.Unprotected).AddComponent("Comp1", ComponentType.ClassModule, string.Empty).Build();

            var vbe = builder
                      .AddProject(proj1)
                      .AddProject(proj2)
                      .Build();

            vbe.Setup(s => s.ActiveCodePane).Returns(proj1.Object.VBComponents[0].CodeModule.CodePane);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var extractInterfaceCommand = new RefactorExtractInterfaceCommand(vbe.Object, state, null);
                Assert.IsTrue(extractInterfaceCommand.CanExecute(null));
            }
        }
Esempio n. 29
0
        public void LabelNotUsed_ReturnsResult_WithUsedLabelThatDoesNotReturnResult()
        {
            const string inputCode =
                @"Sub Foo()
    GoTo Label1:

label2:

label1:
End Sub

Sub Goo(ByVal arg1 As Integer)
End Sub";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new LineLabelNotUsedInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
        public void UnassignedVariable_WithCommentOnSameLine_DoesNotRemoveComment()
        {
            const string inputCode =
                @"Sub Foo()
Dim var1 As String ' Comment
End Sub";

            const string expectedCode =
                @"Sub Foo()
' Comment
End Sub";

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

            var inspection = new VariableNotUsedInspection(state);

            new RemoveUnusedDeclarationQuickFix(state).Fix(inspection.GetInspectionResults().First());

            var rewriter = state.GetRewriter(component);

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